/// <summary>
        /// Gets called when a receive operation resolves.  If we were listening for data and the connection
        /// closed, that also counts as a receive operation resolving.
        /// </summary>
        /// <param name="args"></param>
        private void OnReceiveResolved(bool isUDP, byte[] buffer, int bytesReceived, SocketError status, SockState sockState)
        {
            SocketDebug(19);
            //// Log.LogMsg("==>++++ Async RECEIVE Op Completed - #" + ((SockState)args.UserToken).ID.ToString() + "#");
            try
            {
                if (!OwningConnection.IsAlive && bytesReceived > 0)
                {
                    SocketDebug(20);
                    return;
                }

                SocketDebug(21);
                // If there was a socket error, close the connection. This is NOT a normal
                // situation, if you get an error here.
                if (status != SocketError.Success)
                {
                    SocketDebug(22);
                    Log.LogMsg("Receive status = " + status.ToString());
                    if (!OwningConnection.ShuttingDown)
                    {
                        //// Log.LogMsg("Testy 111");
                        OwningConnection.KillConnection("Connection lost! Network receive error: " + status);
                    }
                    //Jump out of the ProcessReceive method.
                    SocketDebug(23);
                    return;
                }

                SocketDebug(24);
                m_TCPSockState.AsyncEventArgs.RemoteEndPoint = OwningConnection.MyTCPSocket.RemoteEndPoint;

                // If no data was received, close the connection. This is a NORMAL
                // situation that shows when the client has finished sending data.
                if (bytesReceived == 0)
                {
                    SocketDebug(25);
                    if (!OwningConnection.ShuttingDown)
                    {
                        //// Log.LogMsg("Testy 114");
                        OwningConnection.KillConnection("Connection closed by remote host.");
                    }
                    return;
                }

                SocketDebug(26);
                OwningConnection.ReceivedBytes(bytesReceived);

                // restart listening process
                SocketDebug(27);
                OwningConnection.AssembleInboundPacket(buffer, bytesReceived, sockState);
                SocketDebug(28);
            }
            catch (Exception ex)
            {
                Log.LogMsg("Error ProcessReceive. " + ex.Message);
                OwningConnection.KillConnection("Error receive. " + ex.Message);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets called when a receive operation resolves.  If we were listening for data and the connection
        /// closed, that also counts as a receive operation resolving.
        /// </summary>
        /// <param name="args"></param>
        private void OnReceiveResolved(SocketAsyncEventArgs args)
        {
            // Log.LogMsg("Testy 21");
            // Log.LogMsg("==>++++ Async RECEIVE Op Completed - #" + ((SockState)args.UserToken).ID.ToString() + "#");
            try
            {
                if (!OwningConnection.IsAlive && args.BytesTransferred > 0)
                {
                    // Log.LogMsg("Testy 22");
                    return;
                }

                SockState state = args.UserToken as SockState;
                // If there was a socket error, close the connection. This is NOT a normal
                // situation, if you get an error here.
                if (args.SocketError != SocketError.Success)
                {
                    // Log.LogMsg("Testy 222");
                    OwningConnection.KillConnection("Connection lost! Network receive error: " + args.SocketError);
                    //Jump out of the ProcessReceive method.
                    return;
                }

                // If no data was received, close the connection. This is a NORMAL
                // situation that shows when the client has finished sending data.
                if (args.BytesTransferred == 0)
                {
                    // Log.LogMsg("Testy 223");
                    OwningConnection.KillConnection("Connection closed by remote host.");
                    return;
                }

                //// Log.LogMsg("Testy 224");
                OwningConnection.ReceivedBytes(args.BytesTransferred);
                // restart listening process
                //// Log.LogMsg("Testy 225");
                OwningConnection.AssembleInboundPacket(args, state);
                //// Log.LogMsg("Testy 226");
                ListenForDataOnSocket();
            }
            catch (Exception ex)
            {
                //// Log.LogMsg("Testy 23");
                Log.LogMsg("Error ProcessReceive. " + ex.Message);
                OwningConnection.KillConnection("Error receive. " + ex.Message);
            }
        }