Beispiel #1
0
        /// <summary>
        /// Constructs a new outgoing packet for an existing TCP Connector.
        /// Use this class to send data from the TCP Connector to a server.
        /// This is an outgoing message that will remain in the servers thread pool
        /// as a job for the thread pool workers.
        /// </summary>
        /// <param name="connector">TCPConnector 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 TCPConnector.</param>
        public OutgoingTCPClientPacket(TCPConnector connector, byte[] bytes, int offset, int length, OutgoingTCPClientPacket previousPacket)
        {
            fConnector      = connector;
            fBytes          = bytes;
            fOffset         = offset;
            fLength         = length;
            fPreviousPacket = previousPacket;

            ThreadPool.QueueUserWorkItem(new WaitCallback(SendData));
        }
        /// <summary>
        /// Constructs a new outgoing packet for an existing TCP Connector.
        /// Use this class to send data from the TCP Connector to a server.
        /// This is an outgoing message that will remain in the servers thread pool 
        /// as a job for the thread pool workers.
        /// </summary>
        /// <param name="connector">TCPConnector 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 TCPConnector.</param>
        public OutgoingTCPClientPacket(TCPConnector connector, byte[] bytes, int offset, int length, OutgoingTCPClientPacket previousPacket)
        {
            fConnector = connector;
            fBytes = bytes;
            fOffset = offset;
            fLength = length;
            fPreviousPacket = previousPacket;

            ThreadPool.QueueUserWorkItem(new WaitCallback(SendData));
        }
Beispiel #3
0
        private void SendData(object stateObject)
        {
            try
            {
                if (fPreviousPacket == null)
                {
                    try
                    {
                        fConnector.fConnection.GetStream().BeginWrite(fBytes, fOffset, fLength, new AsyncCallback(OnEndSend), fConnector.fConnection.GetStream());
                        fConnector.fBytesOut += fLength;
                        fDataSendCompleted.WaitOne();
                    }
                    catch (IOException ioException)
                    {
                        fCancel = true;
                        fConnector.Stop();
                    }
                }
                else
                {
                    fPreviousPacket.fDone.WaitOne();

                    if (!fCancel && !fPreviousPacket.fCancel)
                    {
                        try
                        {
                            fConnector.fConnection.GetStream().BeginWrite(fBytes, fOffset, fLength, new AsyncCallback(OnEndSend), fConnector.fConnection.GetStream());
                            fConnector.fBytesOut += fLength;
                            fDataSendCompleted.WaitOne();
                        }
                        catch (IOException ioException)
                        {
                            fCancel = true;
                            fConnector.Stop();
                        }
                    }
                    else
                    {
                        fCancel = true;
                    }
                }
            }
            catch (Exception ex)
            {
            }

            if (fPreviousPacket != null)
            {
                fPreviousPacket = null;
            }

            fDone.Set();
        }
 public ConnectorCannotSendPacketException(TCPConnector connector, OutgoingTCPClientPacket packet)
     : base("Connector is disconnected")
 {
     fConnector = connector;
     fPacket = packet;
 }
        private void SendData(object stateObject)
        {
            try
            {
                if (fPreviousPacket == null)
                {
                    try
                    {
                        fConnector.fConnection.GetStream().BeginWrite(fBytes, fOffset, fLength, new AsyncCallback(OnEndSend), fConnector.fConnection.GetStream());
                        fConnector.fBytesOut += fLength;
                        fDataSendCompleted.WaitOne();
                    }
                    catch (IOException ioException)
                    {
                        fCancel = true;
                        fConnector.Stop();
                    }
                }
                else
                {
                    fPreviousPacket.fDone.WaitOne();

                    if (!fCancel && !fPreviousPacket.fCancel)
                    {
                        try
                        {
                            fConnector.fConnection.GetStream().BeginWrite(fBytes, fOffset, fLength, new AsyncCallback(OnEndSend), fConnector.fConnection.GetStream());
                            fConnector.fBytesOut += fLength;
                            fDataSendCompleted.WaitOne();
                        }
                        catch (IOException ioException)
                        {
                            fCancel = true;
                            fConnector.Stop();
                        }
                    }
                    else
                    {
                        fCancel = true;
                    }
                }
            }
            catch (Exception ex)
            {
            }

            if (fPreviousPacket != null)
            {
                fPreviousPacket = null;
            }

            fDone.Set();
        }