/// <summary> /// /// </summary> internal ClientSocketManager(Connecter connecter, ConnectHandler connectHandler, ReceiveQueue receiveBuffer) { if (connecter == null) { throw new ArgumentNullException("ClientSocketHandler.ClientSocketManager(...) - listener == null error!"); } if (connectHandler == null) { throw new ArgumentNullException("ClientSocketHandler.ClientSocketManager(...) - serviceHandle == null error!"); } if (receiveBuffer == null) { throw new ArgumentNullException("receiveBuffer", "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 ArgumentNullException("listener", "ClientSocketHandler.ClientSocketManager(...) - listener == null error!"); } if (serviceHandler == null) { throw new ArgumentNullException("serviceHandler", "ClientSocketHandler.ClientSocketManager(...) - serviceHandle == null error!"); } if (receiveBuffer == null) { throw new ArgumentNullException("receiveBuffer", "ClientSocketHandler.ClientSocketManager(...) - receiveBuffer == null error!"); } m_Listener = listener; m_ServiceHandle = serviceHandler; m_ReceiveBuffer = receiveBuffer; { // 清空数据 m_ReceiveBuffer.Clear(); } m_ServiceHandle.EventProcessData += OnListenerProcessMessageBlock; m_ServiceHandle.EventDisconnect += OnListenerDisconnect; // 初始化数据 表示还没调用过Free(...)函数 m_LockFree.SetValid(); }
/// <summary> /// 时间片的处理 /// </summary> internal void Slice() { // 检查有多少新的连接用户 CheckListener(); // 检查有多少新的连出用户 CheckConnecter(); NetState netState = null; if (m_NetStateQueue.TryDequeue(out netState) == false) { return; } // 如果没有需要处理的数据则返回 if (netState == null) { return; } // 处理接收到的数据 if (netState.Running == true) { ReceiveQueue receiveQueueBuffer = netState.ReceiveBuffer; if (receiveQueueBuffer == null) { throw new ArgumentNullException("receiveQueueBuffer", "MessagePump.Slice(...) - receiveQueueBuffer == null error!"); } // 检测是否已经在处理数据中,防止多线程中多次处理里面的数据 if (receiveQueueBuffer.InProcessReceive() == false) { return; } // 累积过多,会导致线程堵塞,全都在处理自我的数据包逻辑 if (netState.ReceiveBuffer.Length >= netState.ReceiveCachedMaxSize) { netState.Dispose(); // 需要注释 Logs.WriteLine(true, LogMessageType.MSG_WARNING, "NetState[{0}] Receive(...) - ReceiveBufferLength[{1}] >= ReceiveCachedMaxSize[{2}] warning (接收缓存的数据包过大)!", ToString(), netState.SendQueue.WaitSendSize, netState.ReceiveCachedMaxSize); } else { OnProcessReceive(netState); } // 已经结束处理 receiveQueueBuffer.OutProcessReceive(); } // 表示当前已不再处理列表中(减少处理列表的长度) netState.OutProcessQueue(); // 再次检测是否还有没处理完接受到的数据,如果有,再次进入处理列表(可能是半包或还有没来得急处理的数据,等待数据的完整,1秒以后再次处理,仅调用一次) if (netState.Running == true) { if (netState.ReceiveBuffer.Length > 0) { TimeSlice.StartTimeSlice(TimeSpan.FromSeconds(1.0), OnceAgainReceive, netState); } } // 有数据过来需要发送全局信号处理数据包 if (m_NetStateQueue.IsEmpty == false) { m_World.SendWorldSignal(); } }