Exemplo n.º 1
0
        public static TCPCmdWrapper Get(TCPManager tcpMgr,
                                        TMSKSocket socket,
                                        TCPClientPool tcpClientPool,
                                        TCPRandKey tcpRandKey,
                                        TCPOutPacketPool pool,
                                        int nID,
                                        byte[] data,
                                        int count)
        {
            TCPCmdWrapper wrapper = null;

            lock (CachedWrapperList)
            {
                if (CachedWrapperList.Count > 0)
                {
                    wrapper               = CachedWrapperList.Dequeue();
                    wrapper.tcpMgr        = tcpMgr;
                    wrapper.socket        = socket;
                    wrapper.tcpClientPool = tcpClientPool;
                    wrapper.tcpRandKey    = tcpRandKey;
                    wrapper.pool          = pool;
                    wrapper.nID           = nID;
                    wrapper.data          = data;
                    wrapper.count         = count;
                }
            }
            if (null == wrapper)
            {
                wrapper = new TCPCmdWrapper(tcpMgr, socket, tcpClientPool, tcpRandKey, pool, nID, data, count);
            }
            return(wrapper);
        }
Exemplo n.º 2
0
        public void initialize(int capacity)
        {
            MaxConnectedClientLimit = capacity;

            socketListener = new SocketListener(capacity, (int)TCPCmdPacketSize.RECV_MAX_SIZE);
            socketListener.SocketClosed    += SocketClosed;
            socketListener.SocketConnected += SocketConnected;
            socketListener.SocketReceived  += SocketReceived;
            socketListener.SocketSended    += SocketSended;

            _tcpClientPool = TCPClientPool.getInstance();
            _tcpClientPool.initialize(100);
            _tcpLogClientPool = TCPClientPool.getLogInstance();
            _tcpLogClientPool.initialize(100);

            tcpInPacketPool = new TCPInPacketPool(capacity);
            /*tcpOutPacketPool = new TCPOutPacketPool(capacity);*/
            TCPOutPacketPool.getInstance().initialize(capacity);
            tcpOutPacketPool = TCPOutPacketPool.getInstance();
            dictInPackets    = new Dictionary <TMSKSocket, TCPInPacket>(capacity);
            tcpSessions      = new Dictionary <TMSKSocket, TCPSession>();
            TCPCmdDispatcher.getInstance().initialize();
#if UseTimer
            taskExecutor = new ScheduleExecutor(0);
#else
            taskExecutor = new ScheduleExecutor(1);
#endif
            taskExecutor.start();
        }
Exemplo n.º 3
0
        /// <summary>
        /// 透传到DBServer处理
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="nID"></param>
        /// <param name="data"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public TCPProcessCmdResults transmission(TMSKSocket socket, int nID, byte[] data, int count)
        {
            string cmdData = null;

            try
            {
                cmdData = new UTF8Encoding().GetString(data, 0, count);
            }
            catch (Exception) //解析错误
            {
                LogManager.WriteLog(LogTypes.Error, string.Format("解析指令字符串错误, CMD={0}, Client={1}", (TCPGameServerCmds)nID, Global.GetSocketRemoteEndPoint(socket)));
                return(TCPProcessCmdResults.RESULT_FAILED);
            }

//             GameClient client = GameManager.ClientMgr.FindClient(socket);
//             if (null == client)
//             {
//                 LogManager.WriteLog(LogTypes.Error, string.Format("根据RoleID定位GameClient对象失败, CMD={0}, Client={1}", (TCPGameServerCmds)nID, Global.GetSocketRemoteEndPoint(socket)));
//                 return TCPProcessCmdResults.RESULT_FAILED;
//             }

            try
            {
                byte[] bytesData = Global.SendAndRecvData(TCPClientPool.getInstance(), TCPOutPacketPool.getInstance(), data, count, nID);
                if (null == bytesData)
                {
                    LogManager.WriteLog(LogTypes.Error, string.Format("与DBServer通讯失败, CMD={0}", (TCPGameServerCmds)nID));
                    return(TCPProcessCmdResults.RESULT_FAILED);
                }

                Int32 length = BitConverter.ToInt32(bytesData, 0);
                Int16 cmd    = BitConverter.ToInt16(bytesData, 4);

                TCPOutPacket tcpOutPacket = TCPOutPacketPool.getInstance().Pop();
                tcpOutPacket.PacketCmdID = (Int16)cmd;
                tcpOutPacket.FinalWriteData(bytesData, 6, length - 2);

                //client.sendCmd(tcpOutPacket);
                TCPManager.getInstance().MySocketListener.SendData(socket, tcpOutPacket);
                return(TCPProcessCmdResults.RESULT_OK);
            }
            catch (Exception ex)
            {
                // 格式化异常错误信息
                DataHelper.WriteFormatExceptionLog(ex, Global.GetDebugHelperInfo(socket), false);
            }


            return(TCPProcessCmdResults.RESULT_FAILED);
        }
Exemplo n.º 4
0
 public TCPCmdWrapper(TCPManager tcpMgr, TMSKSocket socket, TCPClientPool tcpClientPool, TCPRandKey tcpRandKey, TCPOutPacketPool pool, int nID, byte[] data, int count)
 {
     this.tcpMgr        = tcpMgr;
     this.socket        = socket;
     this.tcpClientPool = tcpClientPool;
     this.tcpRandKey    = tcpRandKey;
     this.pool          = pool;
     this.nID           = nID;
     this.data          = data;
     this.count         = count;
     lock (TCPCmdWrapper.CountLock)
     {
         TCPCmdWrapper.TotalWrapperCount++;
     }
 }
Exemplo n.º 5
0
 public void initialize(int capacity)
 {
     this.MaxConnectedClientLimit         = capacity;
     this.socketListener                  = new SocketListener(capacity, 6144);
     this.socketListener.SocketClosed    += this.SocketClosed;
     this.socketListener.SocketConnected += this.SocketConnected;
     this.socketListener.SocketReceived  += this.SocketReceived;
     this.socketListener.SocketSended    += this.SocketSended;
     this._tcpClientPool                  = TCPClientPool.getInstance();
     this._tcpClientPool.initialize(100);
     this._tcpLogClientPool = TCPClientPool.getLogInstance();
     this._tcpLogClientPool.initialize(100);
     this.tcpInPacketPool = new TCPInPacketPool(capacity);
     TCPOutPacketPool.getInstance().initialize(capacity);
     this.tcpOutPacketPool = TCPOutPacketPool.getInstance();
     this.dictInPackets    = new Dictionary <TMSKSocket, TCPInPacket>(capacity);
     this.tcpSessions      = new Dictionary <TMSKSocket, TCPSession>();
     TCPCmdDispatcher.getInstance().initialize();
     this.taskExecutor = new ScheduleExecutor(0);
     this.taskExecutor.start();
 }
Exemplo n.º 6
0
        public void release()
        {
            lock (CachedWrapperList)
            {
                if (CachedWrapperList.Count < MaxCachedWrapperCount)
                {
                    CachedWrapperList.Enqueue(this);
                    return;
                }
            }

            this.tcpMgr        = null;
            this.socket        = null;
            this.tcpClientPool = null;
            this.tcpRandKey    = null;
            this.pool          = null;
            this.data          = null;
            lock (CountLock)
            {
                TotalWrapperCount--;
            }
        }