private void SendData(object stateObject) { try { if (fPreviousPacket == null) { try { fClient.fConnection.BeginSend(fBytes, fOffset, fLength, 0, new AsyncCallback(SendCallback), fClient.fConnection); fDataSendCompleted.WaitOne(); } catch (Exception exception) { fCancel = true; fClient.DisconnectMe(); } } else { fPreviousPacket.fDone.WaitOne(); if (!fCancel && !fPreviousPacket.fCancel) { try { fClient.fConnection.BeginSend(fBytes, fOffset, fLength, 0, new AsyncCallback(SendCallback), fClient.fConnection); fDataSendCompleted.WaitOne(); } catch (Exception exception) { fCancel = true; fClient.DisconnectMe(); } } else { fCancel = true; } } } catch (Exception ex) { } if (fPreviousPacket != null) { fPreviousPacket = null; } fDone.Set(); }
/// <summary> /// Constructs a new outgoing packet for an existing TCP client connection. /// /// Use this class to send data from the server to a connected client. /// This is an outgoing message that will remain in the servers thread pool /// as a job for the thread pool workers. /// </summary> /// <param name="client">The client where this message belongs to.</param> /// <param name="bytes">The byte array to be sent.</param> /// <param name="offset">The position in the data buffer at witch to begin sending.</param> /// <param name="length">The number of the bytes to be send.</param> /// <param name="previousPacket">The previous outgoing packet of the client.</param> public OutgoingTCPClientConnectionPacket(TCPClientConnection client, byte[] bytes, int offset, int length, OutgoingTCPClientConnectionPacket previousPacket) { try { fClient = client; fBytes = bytes; fOffset = offset; fLength = length; fPreviousPacket = previousPacket; ThreadPool.QueueUserWorkItem(new WaitCallback(SendData)); } catch (Exception ex) { throw new OutgoingPacketFailedException(this); } }
public OutgoingPacketFailedException(OutgoingTCPClientConnectionPacket packet) :base("Outgoing packet failed") { fPacket = packet; }
/// <summary> /// Send data to client. /// </summary> /// <param name="bytes">The byte array to be send.</param> /// <param name="offset">The position in the data buffer at witch to begin sending.</param> /// <param name="length">The number of the bytes to be send.</param> public void SendData(byte[] bytes, int offset, int length) { if (!this.fIsConnected) { throw new ClientIsDisconnectedException(this); } fBytesOut += length; fMyListener.fBytesOut += length; fLastOugoingPacket = new OutgoingTCPClientConnectionPacket(this, bytes, offset, length, fLastOugoingPacket); }
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); } }