Example #1
0
        private void CloseClientSocket(SocketAsyncEventArgs e, string reason)
        {
            AsyncUserToken aut = e.UserToken as AsyncUserToken;
            TMSKSocket     s   = null;

            try
            {
                s = aut.CurrentSocket;
                string ip = "未知";
                try
                {
                    ip = string.Format("{0}", s.RemoteEndPoint);
                }
                catch (Exception)
                {
                }
                LogManager.WriteLog(LogTypes.Error, string.Format("远程连接关闭: {0}, 当前总共: {1}, 原因1:{2}, 原因2:{3}", new object[]
                {
                    ip,
                    this.ConnectedSocketsCount,
                    reason,
                    s.CloseReason
                }), null, true);
                this.CloseSocket(s, "");
            }
            finally
            {
                aut.CurrentSocket = null;
                aut.Tag           = null;
                if (e.LastOperation == SocketAsyncOperation.Send)
                {
                    e.SetBuffer(null, 0, 0);
                    if (null != s)
                    {
                        s.PushWriteSocketAsyncEventArgs(e);
                    }
                }
                else if (e.LastOperation == SocketAsyncOperation.Receive)
                {
                    if (null != s)
                    {
                        s.PushReadSocketAsyncEventArgs(e);
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Close the socket associated with the client.
        /// </summary>
        /// <param name="e">SocketAsyncEventArg associated with the completed send/receive operation.</param>
        private void CloseClientSocket(SocketAsyncEventArgs e)
        {
            AsyncUserToken aut = e.UserToken as AsyncUserToken;

            TMSKSocket s = null;

            try
            {
                s = aut.CurrentSocket;

                string ip = "未知";

                try
                {
                    ip = string.Format("{0}", s.RemoteEndPoint);
                }
                catch (System.Exception)
                {
                }

                LogManager.WriteLog(LogTypes.Error, string.Format("远程连接关闭: {0}, 当前总共: {1}", ip, ConnectedSocketsCount));

                CloseSocket(s);
            }
            finally
            {
                aut.CurrentSocket = null; //释放
                aut.Tag           = null; //释放

                // Free the SocketAsyncEventArg so they can be reused by another client.
                if (e.LastOperation == SocketAsyncOperation.Send)
                {
                    e.SetBuffer(null, 0, 0); //回收内存
                    if (GameManager.FlagOptimizeThreadPool3)
                    {
                        //TMSKThreadStaticClass.GetInstance().PushReadSocketAsyncEventArgs(e);
                        if (null != s)
                        {
                            s.PushWriteSocketAsyncEventArgs(e);
                        }
                    }
                    else
                    {
                        this.writePool.Push(e);
                    }
                }
                else if (e.LastOperation == SocketAsyncOperation.Receive)
                {
                    if (GameManager.FlagOptimizeThreadPool3)
                    {
                        //TMSKThreadStaticClass.GetInstance().PushReadSocketAsyncEventArgs(e);
                        if (null != s)
                        {
                            s.PushReadSocketAsyncEventArgs(e);
                        }
                    }
                    else
                    {
                        this.readPool.Push(e);
                    }
                }
            }
        }