public void ReadData(object stateObject)
        {
            try
            {
                if (fPreviousPacket == null)
                {
                    fClient.fMyExtasysServer.OnDataReceive(fClient, fData);
                }
                else
                {
                    fPreviousPacket.fDone.WaitOne();
                    if (!fCancel && !fPreviousPacket.fCancel)
                    {
                        fClient.fMyExtasysServer.OnDataReceive(fClient, fData);
                    }
                    else
                    {
                        fCancel = true;
                    }
                }
            }
            catch (Exception ex)
            {
            }

            if (fPreviousPacket != null)
            {
                fPreviousPacket = null;
            }
            fDone.Set();
        }
        /// <summary>
        /// Constructs a new incoming packet for an existing TCP client connection.
        ///
        /// 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 client where this message belongs to.</param>
        /// <param name="data">DataFrame class.</param>
        /// <param name="previousPacket">The previous incoming message of the client.</param>
        public IncomingTCPClientConnectionPacket(TCPClientConnection client, DataFrame data, IncomingTCPClientConnectionPacket previousPacket)
        {
            try
            {
                fClient         = client;
                fData           = data;
                fPreviousPacket = previousPacket;
            }
            catch (Exception ex)
            {
                return;
            }

            ThreadPool.QueueUserWorkItem(new WaitCallback(ReadData));
        }
        /// <summary>
        /// Constructs a new incoming packet for an existing TCP client connection.
        /// 
        /// 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 client where this message belongs to.</param>
        /// <param name="data">DataFrame class.</param>
        /// <param name="previousPacket">The previous incoming message of the client.</param>
        public IncomingTCPClientConnectionPacket(TCPClientConnection client, DataFrame data, IncomingTCPClientConnectionPacket previousPacket)
        {
            try
            {
                fClient = client;
                fData = data;
                fPreviousPacket = previousPacket;
            }
            catch (Exception ex)
            {
                return;
            }

            ThreadPool.QueueUserWorkItem(new WaitCallback(ReadData));
        }
Пример #4
0
        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);
            }
        }
Пример #5
0
        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);
            }
        }
        public void ReadData(object stateObject)
        {
            try
            {
                if (fPreviousPacket == null)
                {
                    fClient.fMyExtasysServer.OnDataReceive(fClient, fData);
                }
                else
                {
                    fPreviousPacket.fDone.WaitOne();
                    if (!fCancel && !fPreviousPacket.fCancel)
                    {
                        fClient.fMyExtasysServer.OnDataReceive(fClient, fData);
                    }
                    else
                    {
                        fCancel = true;
                    }
                }
            }
            catch (Exception ex)
            {
            }

            if (fPreviousPacket != null)
            {
                fPreviousPacket = null;
            }
            fDone.Set();
        }