Пример #1
0
        public bool Init()
        {
            usingStateLock = new object();
            if (usingStateLock == null)
            {
                return(false);
            }

            rcvAsyncEventArgs = new SocketAsyncEventArgs();
            if (rcvAsyncEventArgs == null)
            {
                return(false);
            }
            else
            {
                rcvAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(NetModuleMgr.GetInstance().NetIOComplete);
                rcvAsyncEventArgs.UserToken  = this;
            }

            sendAsyncEventArgs = new SocketAsyncEventArgs();
            if (sendAsyncEventArgs == null)
            {
                return(false);
            }
            else
            {
                sendAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(NetModuleMgr.GetInstance().NetIOComplete);
                sendAsyncEventArgs.UserToken  = this;
            }

            rcvDataBuffer = DataBufferPool.GetInstance().MallocRcvBuffer();
            if (rcvDataBuffer == null)
            {
                return(false);
            }

            sendDataBufferMgr = DataBufferPool.GetInstance().MallocSendBufferMgr();
            if (sendDataBufferMgr == null)
            {
                DataBufferPool.GetInstance().FreeRcvBuffer(rcvDataBuffer);
                rcvDataBuffer = null;
                return(false);
            }

            return(true);
        }
Пример #2
0
 // 关闭对象
 public void Shutdown()
 {
     CloseAllSession();
     NetSessionPool.GetInstance().Release();
     DataBufferPool.GetInstance().Release();
     MsgBufferMgr.GetInstance().Release();
     MsgBufferPool.GetInstance().Release();
     if (netSessionIDList != null)
     {
         netSessionIDList.Clear();
         netSessionIDList = null;
     }
     if (addOrDelEventList != null)
     {
         addOrDelEventList.ReleaseWriteReadList();
         addOrDelEventList = null;
     }
 }
Пример #3
0
        public void Release()
        {
            CloseSocket();
            usingStateLock     = null;
            rcvAsyncEventArgs  = null;
            sendAsyncEventArgs = null;
            if (rcvDataBuffer != null)
            {
                DataBufferPool.GetInstance().FreeRcvBuffer(rcvDataBuffer);
                rcvDataBuffer = null;
            }

            if (sendDataBufferMgr != null)
            {
                DataBufferPool.GetInstance().FreeSendBufferMgr(sendDataBufferMgr);
                sendDataBufferMgr = null;
            }
        }
Пример #4
0
        // 初始化对象
        public bool Init(ref NetModuleInit netModuleInit, HandleNetMessage netMsgHandle)
        {
            if (netMsgHandle == null)
            {
                Trace.Assert(false, "netMsgHandle is null");
                return(false);
            }

            Trace.Assert(netModuleInit.netSessionClosedCallbackFunc != null, "netModuleInit.netSessionClosedCallbackFunc is null");
            Trace.Assert(netModuleInit.netSessionConnectedCallbackFunc != null, "netModuleInit.netSessionConnectedCallbackFunc is null");
            if (netModuleInit.netSessionClosedCallbackFunc == null || netModuleInit.netSessionConnectedCallbackFunc == null)
            {
                return(false);
            }

            netSessionClosedCallbackFunc    = netModuleInit.netSessionClosedCallbackFunc;
            netSessionConnectedCallbackFunc = netModuleInit.netSessionConnectedCallbackFunc;

            netSessionIDList = new List <int>();
            if (netSessionIDList == null)
            {
                return(false);
            }

            netSessionDic = new Dictionary <int, NetSessionImpl>();
            if (netSessionDic == null)
            {
                Trace.Assert(false, "netSessionDic is null");
                return(false);
            }

            addOrDelEventList = new WriteReadList <AddOrDelSessionEvent>();
            if (addOrDelEventList == null ||
                !addOrDelEventList.InitWriteReadList(HandleAddOrDelEvent, FreeEvent))
            {
                Trace.Assert(false, "addOrDelEventList is error");
                return(false);
            }

            // 初始化网络消息池
            if (!MsgBufferPool.GetInstance().Init(netModuleInit.msgBufferCounts,
                                                  netModuleInit.msgBufferSize))
            {
                Trace.Assert(false, "MsgBufferPool init false");
                return(false);
            }

            // 初始化消息管理器
            if (!MsgBufferMgr.GetInstance().Init(netMsgHandle))
            {
                Trace.Assert(false, "MsgBufferMgr init false");
                return(false);
            }

            // 初始化数据缓冲区
            if (!DataBufferPool.GetInstance().Init(netModuleInit.bufferReserves,
                                                   netModuleInit.sendBufSize,
                                                   netModuleInit.sendBufExtend,
                                                   netModuleInit.rcvBufSize,
                                                   netModuleInit.rcvBufExtend))
            {
                Trace.Assert(false, "DataBufferPool init false");
                return(false);
            }

            // 初始化网络会话池
            if (!NetSessionPool.GetInstance().Init(netModuleInit.sessionInitCount,
                                                   netModuleInit.sessionExtendCount))
            {
                Trace.Assert(false, "NetSessionPool init false");
                return(false);
            }

            return(true);
        }