Ejemplo n.º 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);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void ProcessSend(SocketAsyncEventArgs e)
        {
            if (null != this.SocketSended)
            {
                this.SocketSended(this, e);
            }
            if (e.SocketError == SocketError.Success)
            {
                Interlocked.Add(ref this.totalBytesWrite, (long)e.BytesTransferred);
            }
            e.SetBuffer(null, 0, 0);
            TMSKSocket s = (e.UserToken as AsyncUserToken).CurrentSocket;

            (e.UserToken as AsyncUserToken).CurrentSocket = null;
            (e.UserToken as AsyncUserToken).Tag           = null;
            if (null != s)
            {
                s.PushWriteSocketAsyncEventArgs(e);
            }
        }
Ejemplo n.º 3
0
        public bool SendData(TMSKSocket s, byte[] buffer, int offset, int count, MemoryBlock item, SendBuffer sendBuffer)
        {
            this.GTotalSendCount++;
            SocketAsyncEventArgs writeEventArgs = s.PopWriteSocketAsyncEventArgs();

            if (null == writeEventArgs)
            {
                writeEventArgs            = new SocketAsyncEventArgs();
                writeEventArgs.Completed += this.OnIOCompleted;
                writeEventArgs.UserToken  = new AsyncUserToken
                {
                    CurrentSocket = null,
                    Tag           = null
                };
            }
            writeEventArgs.SetBuffer(buffer, offset, count);
            AsyncUserToken userToken = writeEventArgs.UserToken as AsyncUserToken;

            userToken.CurrentSocket = s;
            userToken.Tag           = item;
            userToken._SendBuffer   = sendBuffer;
            bool exception = false;

            if (!this._SendAsync(writeEventArgs, out exception))
            {
                this.ProcessSend(writeEventArgs);
            }
            if (exception)
            {
                if (null != this.SocketSended)
                {
                    this.SocketSended(this, writeEventArgs);
                }
                writeEventArgs.SetBuffer(null, 0, 0);
                userToken.CurrentSocket = null;
                userToken.Tag           = null;
                s.PushWriteSocketAsyncEventArgs(writeEventArgs);
            }
            return(!exception);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This method is invoked when an asynchronous send operation completes.
        /// The method issues another receive on the socket to read any additional
        /// data sent from the client.
        /// </summary>
        /// <param name="e">SocketAsyncEventArg associated with the completed send operation.</param>
        private void ProcessSend(SocketAsyncEventArgs e)
        {
            /// 发送数据通知函数
            if (null != SocketSended)
            {
                SocketSended(this, e);
            }

            if (e.SocketError == SocketError.Success)
            {
                Interlocked.Add(ref this.totalBytesWrite, e.BytesTransferred);
            }
            else
            {
                //this.CloseClientSocket(e);
            }

            //什么事情都不做, 收回使用的e和buffer
            // Free the SocketAsyncEventArg so they can be reused by another client.
            e.SetBuffer(null, 0, 0); //回收内存
            TMSKSocket s = (e.UserToken as AsyncUserToken).CurrentSocket;

            (e.UserToken as AsyncUserToken).CurrentSocket = null; //释放
            (e.UserToken as AsyncUserToken).Tag           = null; //释放
            if (GameManager.FlagOptimizeThreadPool3)
            {
                if (null != s)
                {
                    s.PushWriteSocketAsyncEventArgs(e);
                }
            }
            else
            {
                this.writePool.Push(e);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 向客户端发送数据
        /// </summary>
        /// <param name="data"></param>
        public bool SendData(TMSKSocket s, Byte[] buffer, int offset, int count, MemoryBlock item, SendBuffer sendBuffer)
        {
            GTotalSendCount++;
            SocketAsyncEventArgs writeEventArgs;

            if (GameManager.FlagOptimizeThreadPool3)
            {
                writeEventArgs = s.PopWriteSocketAsyncEventArgs(); //线程安全的操作
            }
            else
            {
                writeEventArgs = this.writePool.Pop(); //线程安全的操作
            }
            if (null == writeEventArgs)
            {
                writeEventArgs            = new SocketAsyncEventArgs();
                writeEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(OnIOCompleted);
                writeEventArgs.UserToken  = new AsyncUserToken()
                {
                    CurrentSocket = null, Tag = null
                };
            }

            writeEventArgs.SetBuffer(buffer, offset, count);
            AsyncUserToken userToken = (writeEventArgs.UserToken as AsyncUserToken);

            userToken.CurrentSocket = s;
            userToken.Tag           = item;
            userToken._SendBuffer   = sendBuffer;

            bool exception = false;

            if (GameManager.FlagSkipSocketSend && GameManager.CanSkipCmd(s))
            {
                writeEventArgs.SocketError = SocketError.Success;
                this.ProcessSend(writeEventArgs);
            }
            else
            {
                Boolean willRaiseEvent = _SendAsync(writeEventArgs, out exception);
                if (!willRaiseEvent)
                {
                    this.ProcessSend(writeEventArgs);
                }
            }

            if (exception) //此处不处理会导致内存泄露
            {
                /// 发送数据通知函数
                if (null != SocketSended)
                {
                    SocketSended(this, writeEventArgs);
                }

                //什么事情都不做, 收回使用的e和buffer
                // Free the SocketAsyncEventArg so they can be reused by another client.
                writeEventArgs.SetBuffer(null, 0, 0); //回收内存
                userToken.CurrentSocket = null;       //释放
                userToken.Tag           = null;       //释放
                if (GameManager.FlagOptimizeThreadPool3)
                {
                    s.PushWriteSocketAsyncEventArgs(writeEventArgs);
                }
                else
                {
                    this.writePool.Push(writeEventArgs);
                }
            }

            return(!exception);
        }
Ejemplo n.º 6
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);
                    }
                }
            }
        }