// 弹出消息 private bool PopMsgFrmRcvBuffer() { if (IsReadClose()) { return(true); } byte[] readBuffer = null; Int32 readOffset = 0; Int32 readLength = 0; UInt16 msgCommand = 0; do { // 获得可读数据 if (!rcvDataBuffer.GetRcvMessage(ref msgCommand, ref readBuffer, ref readOffset, ref readLength, ref setSecurityPolicy)) { return(false); } if (readBuffer != null && readLength > 0) { // 从流中读取数据写入消息对象 MsgBufferMgr.GetInstance().PushNetMessage(msgCommand, readBuffer, readOffset, readLength, (INetSession)this); // 移动读取位置(数据头 + 数据长度) rcvDataBuffer.ReadData(null, readLength + NetGlobalData.GetInstance().GetMsgHeadSize()); } else { return(true); } }while(true); }
// 关闭对象 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; } }
// 初始化对象 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); }
// 私有函数 #region // 处理接收到的网络消息 private void HandleNetMsg() { MsgBufferMgr.GetInstance().HandleList(); }