Exemple #1
0
        /// <summary>
        /// Add a TCPOutPacket instance to the pool.
        /// </summary>
        /// <param name="item">SocketAsyncEventArgs instance to add to the pool.</param>
        internal void Push(TCPOutPacket item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("添加到TCPOutPacketPool 的item不能是空(null)");
            }

            lock (this.pool)
            {
                bool found = false;
                foreach (var oldItem in this.pool)
                {
                    if (oldItem == item)
                    {
                        found = true;
                        break;
                    }
                }

                //必须在这儿调用,保证分配的内存被还回缓冲区
                item.Reset();
                if (this.pool.Count < 5000) //限制最大1000个缓存
                {
                    this.pool.Push(item);
                }
                else
                {
                    item.Dispose(); //释放
                }
            }

            //必须在这儿调用,保证分配的内存被还回缓冲区
            //  item.Reset();
            //  item.Dispose(); //释放
        }
Exemple #2
0
        public static TCPOutPacket MakeTCPOutPacket(TCPOutPacketPool pool, byte[] data, int offset, int length, int cmd)
        {
            TCPOutPacket tcpOutPacket = pool.Pop();

            tcpOutPacket.PacketCmdID = (ushort)cmd;
            tcpOutPacket.FinalWriteData(data, offset, length);
            return(tcpOutPacket);
        }
Exemple #3
0
        /// <summary>
        /// 生成TCPOutPacket
        /// </summary>
        /// <param name="data"></param>
        /// <param name="cmd"></param>
        public static TCPOutPacket MakeTCPOutPacket(TCPOutPacketPool pool, byte[] data, int cmd)
        {
            TCPOutPacket tcpOutPacket = pool.Pop();

            tcpOutPacket.PacketCmdID = (UInt16)cmd;
            tcpOutPacket.FinalWriteData(data, 0, data.Length);
            return(tcpOutPacket);
        }
Exemple #4
0
        public static TCPOutPacket MakeTCPOutPacket(TCPOutPacketPool pool, string data, int cmd)
        {
            TCPOutPacket tcpOutPacket = pool.Pop();

            tcpOutPacket.PacketCmdID = (ushort)cmd;
            byte[] bytesCmd = new UTF8Encoding().GetBytes(data);
            tcpOutPacket.FinalWriteData(bytesCmd, 0, bytesCmd.Length);
            return(tcpOutPacket);
        }
Exemple #5
0
        /// 生成TCPOutPacket
        public static TCPOutPacket MakeTCPOutPacket(byte[] data, int offset, int length, int cmd)
        {
            //连接成功, 立即发送请求登陆的指令
            TCPOutPacket tcpOutPacket = new TCPOutPacket();

            tcpOutPacket.PacketCmdID = (Int16)cmd;
            tcpOutPacket.FinalWriteData(data, offset, length);
            return(tcpOutPacket);
        }
Exemple #6
0
 internal void Push(TCPOutPacket item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("添加到TCPOutPacketPool 的item不能是空(null)");
     }
     item.Reset();
     item.Dispose();
 }
Exemple #7
0
        /// 生成TCPOutPacket
        public static TCPOutPacket MakeTCPOutPacket(string data, int cmd)
        {
            //连接成功, 立即发送请求登陆的指令
            TCPOutPacket tcpOutPacket = new TCPOutPacket();

            tcpOutPacket.PacketCmdID = (Int16)cmd;
            byte[] bytesCmd = new UTF8Encoding().GetBytes(data);
            tcpOutPacket.FinalWriteData(bytesCmd, 0, bytesCmd.Length);
            return(tcpOutPacket);
        }
Exemple #8
0
 /// <summary>
 /// Add a TCPOutPacket instance to the pool.
 /// </summary>
 /// <param name="item">SocketAsyncEventArgs instance to add to the pool.</param>
 internal void Push(TCPOutPacket item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("添加到TCPOutPacketPool 的item不能是空(null)");
     }
     lock (this.pool)
     {
         item.Reset();
         this.pool.Push(item);
     }
 }
Exemple #9
0
        /// <summary>
        /// Removes a TCPOutPacket instance from the pool.
        /// </summary>
        /// <returns>TCPOutPacket removed from the pool.</returns>
        internal TCPOutPacket Pop()
        {
            lock (this.pool)
            {
                if (this.pool.Count <= 0)
                {
                    //临时分配
                    return(new TCPOutPacket());
                }

                TCPOutPacket item = this.pool.Pop();
                return(item);
            }

            // return new TCPOutPacket();
        }
        internal TCPOutPacket Pop()
        {
            TCPOutPacket result;

            lock (this.pool)
            {
                if (this.pool.Count <= 0)
                {
                    result = new TCPOutPacket();
                }
                else
                {
                    result = this.pool.Pop();
                }
            }
            return(result);
        }
Exemple #11
0
        /// <summary>
        /// 生成TCPOutPacket
        /// </summary>
        /// <param name="data"></param>
        /// <param name="cmd"></param>
        public static TCPOutPacket MakeTCPOutPacket(TCPOutPacketPool pool, string data, int cmd)
        {
            //if (pool.Count <= 0) //如果缓存为空,则打印日志,看是谁在分配
            //{
            //    //调试信息
            //    if (LogManager.LogTypeToWrite >= LogTypes.Info && LogManager.LogTypeToWrite <= LogTypes.Error)
            //    {
            //        try
            //        {
            //            System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
            //            string methodName1 = "", methodName2 = "";
            //            if (stackTrace.FrameCount > 1)
            //            {
            //                methodName2 = stackTrace.GetFrame(1).GetMethod().Name;
            //            }

            //            if (stackTrace.FrameCount > 2)
            //            {
            //                methodName1 = stackTrace.GetFrame(2).GetMethod().Name;
            //            }

            //            LogManager.WriteLog(LogTypes.Error, string.Format("{0}->{1}, 调用MakeTCPOutPacket", methodName1, methodName2));
            //        }
            //        catch (Exception)
            //        {
            //        }
            //    }
            //}

            //连接成功, 立即发送请求登陆的指令
            TCPOutPacket tcpOutPacket = pool.Pop();

            tcpOutPacket.PacketCmdID = (Int16)cmd;
            byte[] bytesCmd = new UTF8Encoding().GetBytes(data);
            tcpOutPacket.FinalWriteData(bytesCmd, 0, bytesCmd.Length);
            return(tcpOutPacket);
        }
Exemple #12
0
 public void Dispose()
 {
     TCPOutPacket.DecInstanceCount();
     this.Tag = null;
 }
Exemple #13
0
 public TCPOutPacket()
 {
     TCPOutPacket.IncInstanceCount();
 }