/// <summary> /// /// </summary> internal ClientSocketManager(Connecter connecter, ConnectHandler connectHandler, ReceiveQueue receiveBuffer) { if (connecter == null) { throw new Exception("ClientSocketHandler.ClientSocketManager(...) - listener == null error!"); } if (connectHandler == null) { throw new Exception("ClientSocketHandler.ClientSocketManager(...) - serviceHandle == null error!"); } if (receiveBuffer == null) { throw new Exception("ClientSocketHandler.ClientSocketManager(...) - receiveBuffer == null error!"); } m_Connecter = connecter; m_ConnectHandle = connectHandler; m_ReceiveBuffer = receiveBuffer; { // 清空数据 m_ReceiveBuffer.Clear(); } // 初始化数据 表示还没调用过Free(...)函数 m_LockFree.SetValid(); }
/// <summary> /// /// </summary> internal ClientSocketManager(Listener listener, ServiceHandler serviceHandler, ReceiveQueue receiveBuffer) { if (listener == null) { throw new Exception("ClientSocketHandler.ClientSocketManager(...) - listener == null error!"); } if (serviceHandler == null) { throw new Exception("ClientSocketHandler.ClientSocketManager(...) - serviceHandle == null error!"); } if (receiveBuffer == null) { throw new Exception("ClientSocketHandler.ClientSocketManager(...) - receiveBuffer == null error!"); } m_Listener = listener; m_ServiceHandle = serviceHandler; m_ReceiveBuffer = receiveBuffer; { // 清空数据 m_ReceiveBuffer.Clear(); } m_ServiceHandle.EventProcessData += new EventHandler <ProcessDataAtServerEventArgs>(this.OnListenerProcessMessageBlock); m_ServiceHandle.EventDisconnect += new EventHandler <DisconnectAtServerEventArgs>(this.OnListenerDisconnect); // 初始化数据 表示还没调用过Free(...)函数 m_LockFree.SetValid(); }
/// <summary> /// 释放指定的客户端到内存池里面 /// </summary> /// <param name="clientSocketHandler"></param> internal void Free(ReceiveQueue receiveQueue) { // 返回至内存池(ClientSocketHandler) m_ReceiveQueuePool.ReleasePoolContent(receiveQueue); }
/// <summary> /// 时间片的处理 /// </summary> internal void Slice() { // 检查有多少新的连接用户 CheckListener(); // 检查有多少新的连出用户 CheckConnecter(); NetState[] netStateArray = null; // 转换成数组减少lock处理的时间 m_LockNetStateQueue.Enter(); { do { if (m_NetStateQueue.Count == 0) { break; } netStateArray = m_NetStateQueue.ToArray(); // 清空数据,下面处理这些数据 m_NetStateQueue.Clear(); } while (false); } m_LockNetStateQueue.Exit(); // 如果没有需要处理的数据则返回 if (netStateArray == null) { return; } // 处理接收到的数据 for (int iIndex = 0; iIndex < netStateArray.Length; iIndex++) { NetState netState = netStateArray[iIndex]; if (netState.Running == true) { ReceiveQueue receiveQueueBuffer = netState.ReceiveBuffer; if (receiveQueueBuffer == null) { throw new Exception("MessagePump.Slice(...) - receiveQueueBuffer == null error!"); } // 检测是否已经在处理数据中,防止多线程中多次处理里面的数据 if (receiveQueueBuffer.InProcessReceive() == false) { continue; } OnProcessReceive(netState); // 已经结束处理 receiveQueueBuffer.OutProcessReceive(); } // 表示当前已不再处理列表中(减少处理列表的长度) netState.OutProcessQueue(); // 再次检测是否还有没处理完接受到的数据,如果有,再次进入处理列表(可能是半包或还有没来得急处理的数据,等待数据的完整,1秒以后再次处理,仅调用一次) if (netState.Running == true) { if (netState.ReceiveBuffer.Length > 0) { TimeSlice.StartTimeSlice(TimeSpan.FromSeconds(1.0), new TimeSliceStateCallback <NetState>(this.OnceAgainReceive), netState); } } } }