Exemple #1
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);
        }
        /// <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);
        }