private void ReadData(object stateObject) { try { if (fPreviousPacket == null) { fClient.fMyMessageCollector.AppendData(fData); } else { fPreviousPacket.fDone.WaitOne(); if (!fCancel && !fPreviousPacket.fCancel) { fClient.fMyMessageCollector.AppendData(fData); } else { fCancel = true; } } } catch (Exception ex) { } if (fPreviousPacket != null) { fPreviousPacket = null; } fDone.Set(); }
/// <summary> /// Constructs a new (incoming) message collector packet. /// /// Use this class to receive data from a client. /// This is an incoming message that will remain in the servers thread pool /// as a job for the thread pool workers. /// </summary> /// <param name="client">The packet's TCPClientConnection.</param> /// <param name="data">The string received.</param> /// <param name="previousPacket">The previous message collector packet of the TCPClientConnection.</param> public MessageCollectorTCPClientConnectionPacket(TCPClientConnection client, string data, MessageCollectorTCPClientConnectionPacket previousPacket) { fClient = client; fData = data; fPreviousPacket = previousPacket; ThreadPool.QueueUserWorkItem(new WaitCallback(ReadData)); }
/// <summary> /// Constructs a new (incoming) message collector packet. /// /// Use this class to receive data from a client. /// This is an incoming message that will remain in the servers thread pool /// as a job for the thread pool workers. /// </summary> /// <param name="client">The packet's TCPClientConnection.</param> /// <param name="data">The string received.</param> /// <param name="previousPacket">The previous message collector packet of the TCPClientConnection.</param> public MessageCollectorTCPClientConnectionPacket(TCPClientConnection client, string data, MessageCollectorTCPClientConnectionPacket previousPacket) { fClient = client; fData = data; fPreviousPacket = previousPacket; ThreadPool.QueueUserWorkItem(new WaitCallback(ReadData)); }
public void DisconnectMe() { if (fActive) { fActive = false; fIsConnected = false; try { fConnection.Close(); } catch (Exception ex) { } if (fUseMessageCollector && fMyMessageCollector != null) { fMyMessageCollector.Dispose(); } if (fLastIncomingPacket != null) { fLastIncomingPacket.Cancel(); } if (fLastOugoingPacket != null) { fLastOugoingPacket.Cancel(); } if (fLastMessageCollectorPacket != null) { fLastMessageCollectorPacket.Cancel(); } fConnection = null; fLastIncomingPacket = null; fLastOugoingPacket = null; fLastMessageCollectorPacket = null; fMyMessageCollector = null; fMyListener.RemoveClient(fIPAddress); fMyListener.MyExtasysTCPServer.OnClientDisconnect(this); } }
private void StreamReceiver(IAsyncResult ar) { int bytesRead; try { if (fIsConnected) { bytesRead = fConnection.EndReceive(ar); if (bytesRead > 0) { fBytesIn += bytesRead; fMyListener.fBytesIn += bytesRead; fLastDataAction = DateTime.Now.Ticks; switch (fUseMessageCollector) { case true: fLastMessageCollectorPacket = new MessageCollectorTCPClientConnectionPacket(this, Encoding.Default.GetString(fReadBuffer, 0, bytesRead), fLastMessageCollectorPacket); break; case false: fLastIncomingPacket = new IncomingTCPClientConnectionPacket(this, new DataFrame(fReadBuffer, 0, bytesRead), fLastIncomingPacket); break; } } else { DisconnectMe(); return; } //Start a new asynchronous read into readBuffer. fConnection.BeginReceive(fReadBuffer, 0, fReadBufferSize, SocketFlags.None, new AsyncCallback(StreamReceiver), null); } } catch (ArgumentNullException argumentNullException) //asyncResult is a null reference (Nothing in Visual Basic). { DisconnectMe(); } catch (ArgumentException argumentException) //asyncResult was not returned by a call to the BeginReceive method. { DisconnectMe(); } catch (SocketException socketException) //An error occurred when attempting to access the socket. See the Remarks section for more information { DisconnectMe(); } catch (ObjectDisposedException objectDisposedException) //Socket has been closed. { DisconnectMe(); } catch (Exception ex) { fConnection.BeginReceive(fReadBuffer, 0, fReadBufferSize, SocketFlags.None, new AsyncCallback(StreamReceiver), null); } }
private void ReadData(object stateObject) { try { if (fPreviousPacket == null) { fClient.fMyMessageCollector.AppendData(fData); } else { fPreviousPacket.fDone.WaitOne(); if (!fCancel && !fPreviousPacket.fCancel) { fClient.fMyMessageCollector.AppendData(fData); } else { fCancel = true; } } } catch (Exception ex) { } if (fPreviousPacket != null) { fPreviousPacket = null; } fDone.Set(); }