void m_Connection_ReceiveData(object sender, ConnectionReceiveDataEventArgs e) { //Log.WriteLog(Log.LogLevelType.Comm, "CommandInterface::ReceiveData(): " + // "[" + m_Connection.Address + ":" + m_Connection.Port + "] " + // "Received " + e.Length.ToString() + " bytes"); //Log.HexDump(Log.LogLevelType.Comm, e.Buffer); // Append incoming Data to Global Recv Buffer if (ReceiveBuffer == null) { ReceiveBuffer = e.Buffer; } else { int OldLength = ReceiveBuffer.Length; Array.Resize(ref ReceiveBuffer, ReceiveBuffer.Length + e.Buffer.Length); Array.Copy(e.Buffer, 0, ReceiveBuffer, OldLength, e.Buffer.Length); } byte[] WorkBuffer = null; while (GetMessage(ref WorkBuffer) == true) { ParseMessage(WorkBuffer); } }
protected virtual void OnReceiveData(ConnectionReceiveDataEventArgs e) { var handler = ReceiveData; if (handler != null) { handler(this, e); } }
private void ReceiveCallback(IAsyncResult ar) { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket client = state.WorkSocket; SocketError error; int bytesRead; //ensure current client connection is the same as //the one contained in the callback object if (client != ClientSocket) { return; } if (ClientSocket == null) { return; } if (Connected == false) { return; } System.Threading.Monitor.Enter(countLock); //Read data from the remote device. try { bytesRead = client.EndReceive(ar, out error); } catch (SocketException e) { System.Threading.Monitor.Exit(countLock); ConnectionErrorEventArgs args = new ConnectionErrorEventArgs(); args.ConnectionID = m_ID; args.ErrorCode = e.ErrorCode; args.ErrorMessage = e.Message; OnError(args); Disconnect(); return; } catch (Exception) { Disconnect(); return; } if ((bytesRead == 0) || (error != SocketError.Success)) { System.Threading.Monitor.Exit(countLock); if (error != SocketError.Success) { ConnectionErrorEventArgs args = new ConnectionErrorEventArgs(); args.ConnectionID = m_ID; args.ErrorCode = (int)error; args.ErrorMessage = "Disconnect: Bytes = " + bytesRead.ToString() + ", Error = " + error.ToString(); OnError(args); } Disconnect(); return; } Byte[] buffer = null; if (bytesRead > 0) { buffer = new Byte[bytesRead]; Array.Copy(state.buffer, buffer, bytesRead); } System.Threading.Monitor.Exit(countLock); if (bytesRead > 0) { ConnectionReceiveDataEventArgs RecvArgs = new ConnectionReceiveDataEventArgs(); RecvArgs.ConnectionID = m_ID; RecvArgs.Buffer = buffer; RecvArgs.Length = bytesRead; OnReceiveData(RecvArgs); } //invoke could trigger disconnect (or disconnect via send()), //which means our socket could be invalid if (client != ClientSocket) { return; } if (ClientSocket == null) { return; } if (Connected == false) { return; } //Get the rest of the data. try { client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } catch (SocketException e) { // ConnectionErrorEventArgs args = new ConnectionErrorEventArgs(); // args.ConnectionID = m_ID; // args.ErrorCode = e.ErrorCode; // args.ErrorMessage = e.Message; // OnError(args); Disconnect(); return; } catch (Exception) { Disconnect(); return; } }