private void OnDataReceived(IAsyncResult asyn) { int bytesRead = _socket.EndReceive(asyn); //------------Handle Multi-byte Charactor------------- _bufferList = new List <BufferWrapper>(); //---------------------------------------------------- do { bytesRead = _socket.Receive(_buffer); SocketLogMgt.SetLog(this, _logID + "Receive succeeded. " + bytesRead.ToString() + " bytes."); //------------Handle Multi-byte Charactor------------- _bufferList.Add(BufferWrapper.Create(_buffer, bytesRead)); //foreach (BufferWrapper w in _bufferList) SocketLogMgt.SetLog(">>>>>>>>>>>> " + w.Buffer.Length.ToString()); //---------------------------------------------------- if (bytesRead > 0) { //No matter how to cut the buffer, ASCII charactor can always be decoded properly, //therefore we can use this segment to detect whether an ending sign (for example "</XMLRequestMessage>") is received. //See class XmlTest.FormCoding for unit test. string str = _entity.Encoder.GetString(_buffer, 0, bytesRead); _sb.Append(str); if (SocketLogMgt.DumpData) { SocketLogMgt.SetLog(this, _logID + ": Data received."); SocketLogMgt.SetLog(this, "------------------------"); SocketLogMgt.SetLog(this, str); SocketLogMgt.SetLog(this, "------------------------"); } string receiveData = _sb.ToString(); if (SocketHelper.IsEOF(receiveData, _entity.Config.ReceiveEndSign)) { string sendData = null; //------------Handle Multi-byte Charactor------------- receiveData = BufferWrapper.GetString(_entity.Encoder, _bufferList); _bufferList = null; //---------------------------------------------------- _entity.NotifyRequest(receiveData, ref sendData); ResponseData(sendData); break; } } }while (bytesRead > 0); }
private void OnDataReceived(IAsyncResult asyn) { try { if (_isClosed) { SocketLogMgt.SetLog(SocketLogType.Warning, this, "Data received, but the socket is closed."); return; } int bytesRead = _socket.EndReceive(asyn); //------------Handle Multi-byte Charactor------------- //Buffer all the bytes received and then decode this bytes at one time. //in order to support multi byte charactor set (for example: utf-8) //However, ASCII or GB or BIG5 or Unicode are easy, //we can decode even bytes at each time. List <BufferWrapper> bufferList = new List <BufferWrapper>(); //---------------------------------------------------- do { bytesRead = _socket.Receive(_buffer); SocketLogMgt.SetLog(this, _strSocketID + "Receive succeeded. " + bytesRead.ToString() + " bytes."); UpdateActiveDT(); //------------Handle Multi-byte Charactor------------- bufferList.Add(BufferWrapper.Create(_buffer, bytesRead)); //foreach (BufferWrapper w in bufferList) SocketLogMgt.SetLog(">>>>>>>>>>>> " + w.Buffer.Length.ToString()); //---------------------------------------------------- if (bytesRead > 0) { //No matter how to cut the buffer, ASCII charactor can always be decoded properly, //therefore we can use this segment to detect whether an ASCII charactor ending sign (for example "</XMLRequestMessage>") is received. //See class XmlTest.FormCoding for unit test. string str = _server.Encoder.GetString(_buffer, 0, bytesRead); _sb.Append(str); if (SocketLogMgt.DumpData) { SocketLogMgt.SetLog(this, _strSocketID + ": Data received."); SocketLogMgt.SetLog(this, "------------------------"); SocketLogMgt.SetLog(this, str); SocketLogMgt.SetLog(this, "------------------------"); } string receiveData = _sb.ToString(); if (SocketHelper.FindBlockEnding(receiveData)) { string sendData = null; //------------Handle Multi-byte Charactor------------- receiveData = BufferWrapper.GetString(_server.Encoder, bufferList); bufferList.Clear(); //---------------------------------------------------- receiveData = SocketHelper.UnpackMessageBlock(receiveData); _server.NotifyRequest(receiveData, ref sendData); ResponseData(sendData); //// if allow multiple session per connection, then continue receiving till connection is closed by the client. //if (_server.Config.AllowMultipleSessionPerConnection) // continue; //else // break; continue; } } }while (bytesRead > 0); // if(bytesRead==0) which means the connection is closed by the client, we will close the worker socket. SocketLogMgt.SetLog(this, "Stop receiving data because the connection is closed by the client."); CloseWorker(); } catch (SocketException err) { //needToContinue = false; SocketLogMgt.SetLastError(err); // if client does not send data in some period of time and the connection will be expired // (controled by the lower levels, and this exception occurs when the client send message again after the connection expired) // or other network exception when receiving data, then close the server socket. CloseWorker(); } catch (Exception e) { SocketLogMgt.SetLastError(e); // if communication is ok, but process message failed, do not need to close connection. } }
private void OnDataReceived(IAsyncResult asyn) { try { int bytesRead = _socket.EndReceive(asyn); //------------Handle Multi-byte Charactor------------- //Buffer all the bytes received and then decode this bytes at one time. //in order to support multi byte charactor set (for example: utf-8) //However, ASCII or GB or BIG5 or Unicode are easy, //we can decode even bytes at each time. List <BufferWrapper> bufferList = new List <BufferWrapper>(); //---------------------------------------------------- do { bytesRead = _socket.Receive(_buffer); SocketLogMgt.SetLog(this, _strSocketID + "Receive succeeded. " + bytesRead.ToString() + " bytes."); UpdateActiveDT(); //------------Handle Multi-byte Charactor------------- bufferList.Add(BufferWrapper.Create(_buffer, bytesRead)); //foreach (BufferWrapper w in bufferList) SocketLogMgt.SetLog(">>>>>>>>>>>> " + w.Buffer.Length.ToString()); //---------------------------------------------------- if (bytesRead > 0) { //No matter how to cut the buffer, ASCII charactor can always be decoded properly, //therefore we can use this segment to detect whether an ASCII charactor ending sign (for example "</XMLRequestMessage>") is received. //See class XmlTest.FormCoding for unit test. string str = _server.Encoder.GetString(_buffer, 0, bytesRead); _sb.Append(str); if (SocketLogMgt.DumpData) { SocketLogMgt.SetLog(this, _strSocketID + ": Data received."); SocketLogMgt.SetLog(this, "------------------------"); SocketLogMgt.SetLog(this, str); SocketLogMgt.SetLog(this, "------------------------"); } string receiveData = _sb.ToString(); if (SocketHelper.FindBlockEnding(receiveData)) { string sendData = null; //------------Handle Multi-byte Charactor------------- receiveData = BufferWrapper.GetString(_server.Encoder, bufferList); bufferList.Clear(); //---------------------------------------------------- receiveData = SocketHelper.UnpackMessageBlock(receiveData); _server.NotifyRequest(receiveData, ref sendData); ResponseData(sendData); break; } } }while (bytesRead > 0); } catch (Exception e) { SocketLogMgt.SetLastError(e); // if communication is ok, but process message failed, do not need to close connection. } }