コード例 #1
0
        // 释放关闭的会话
        private void CloseAllShutdownSession()
        {
            if (netSessionIDList.Count() <= 0)
            {
                return;
            }

            NetSessionImpl netSession = null;

            foreach (Int32 netSessionID in netSessionIDList)
            {
                if (netSessionDic.ContainsKey(netSessionID))
                {
                    netSession = netSessionDic[netSessionID];
                    if (netSession != null)
                    {
                        NetSessionPool.GetInstance().FreeNetSession(netSession);
                    }

                    // 从活动表中删除会话
                    netSessionDic.Remove(netSessionID);
                }
            }

            netSessionIDList.Clear();
        }
コード例 #2
0
        // 连接远程地址
        public INetSession Connect(string remoteAddr, Int32 remotePort, bool isSync, ref bool isSuccess)
        {
            isSuccess = false;
            if (remoteAddr == null || remotePort <= 0)
            {
                Trace.Assert(false, "Connect param error");
                return(null);
            }

            NetSessionImpl netSession = NetSessionPool.GetInstance().MallocNetSession();

            if (netSession == null)
            {
                Trace.Assert(false, "malloc session null");
                return(null);
            }

            // 同步连接
            if (isSync)
            {
                if (!netSession.PostConnect(remoteAddr, remotePort, true))
                {
                    NetSessionPool.GetInstance().FreeNetSession(netSession);
                    return(null);
                }

                // 设置会话的状态
                NetSessionState sessionState = new NetSessionState();
                sessionState.Reset((Int32)NETSESSIONSTATE.netSessionStateUsing);
                netSession.SetSessionState(ref sessionState);

                // 投递接收操作
                if (!netSession.PostRcv())
                {
                    // 关闭会话,返回会话池
                    NetSessionPool.GetInstance().FreeNetSession(netSession);
                    return(null);
                }

                // 添加到活动会话队列
                netSessionDic.Add(netSession.GetSessionID(), netSession);

                isSuccess = true;
                return((INetSession)netSession);
            }
            // 异步连接
            else
            {
                if (netSession.PostConnect(remoteAddr, remotePort, false))
                {
                    isSuccess = true;
                }
            }

            return(null);
        }
コード例 #3
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;
     }
 }
コード例 #4
0
        // 关闭所有会话
        private void CloseAllSession()
        {
            if (netSessionDic == null || netSessionDic.Count() <= 0)
            {
                return;
            }

            NetSessionImpl netSession = null;

            foreach (KeyValuePair <Int32, NetSessionImpl> netSessionPair in netSessionDic)
            {
                netSession = netSessionPair.Value;
                if (netSession == null)
                {
                    NetSessionPool.GetInstance().FreeNetSession(netSession);
                }
            }

            netSessionDic.Clear();
            netSessionDic = null;
        }
コード例 #5
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);
        }
コード例 #6
0
        // 处理添加、删除网络会话事件
        #region
        public void HandleAddOrDelEvent(AddOrDelSessionEvent addOrDelEvent)
        {
            NetSessionImpl netSession = addOrDelEvent.netSession;

            if (netSession == null)
            {
                return;
            }

            switch (addOrDelEvent.eventType)
            {
            case ADDORDELSESSIONEVENT.addNetSession:
            {
                // 设置会话的状态
                NetSessionState sessionState = new NetSessionState();
                sessionState.Reset((Int32)NETSESSIONSTATE.netSessionStateUsing);
                netSession.SetSessionState(ref sessionState);

                if (!netSession.PostRcv())
                {
                    // 关闭会话,返回会话池
                    NetSessionPool.GetInstance().FreeNetSession(netSession);
                }
                else
                {
                    netSessionDic.Add(netSession.GetSessionID(), netSession);

                    // 通知上层
                    if (netSessionConnectedCallbackFunc != null)
                    {
                        netSessionConnectedCallbackFunc((INetSession)netSession, addOrDelEvent.eventPlace, true);
                    }
                }
            }
            break;

            case ADDORDELSESSIONEVENT.delNetSession:
            {
                // 通知应用层
                if (addOrDelEvent.eventPlace != EVENTSESSIONPLACE.appDelSession)
                {
                    if (netSessionClosedCallbackFunc != null)
                    {
                        netSessionClosedCallbackFunc(addOrDelEvent.netSession, addOrDelEvent.eventPlace);
                    }

                    NetSessionState sessionState = new NetSessionState();
                    sessionState.Reset((Int32)NETSESSIONSTATE.netSessionStateMax);
                    sessionState.appUseState = (Int32)NETSESSIONSTATE.netSessionStateShutdown;
                    addOrDelEvent.netSession.SetSessionState(ref sessionState);
                }
            }
            break;

            case ADDORDELSESSIONEVENT.asyncConnectFailed:
            {
                // 通知上层
                if (netSessionConnectedCallbackFunc != null)
                {
                    netSessionConnectedCallbackFunc((INetSession)netSession, addOrDelEvent.eventPlace, false);
                }

                // 关闭会话,返回会话池
                NetSessionPool.GetInstance().FreeNetSession(netSession);
            }
            break;

            default:
                break;
            }
        }