예제 #1
0
        /// <summary>
        /// 有实例产生时调用
        /// </summary>
        /// <param name="clientSocket"></param>
        /// <param name="world"></param>
        public NetState(ClientSession <NetState> clientSocket, WorldBase world)
            : this()
        {
            if (clientSocket == null)
            {
                throw new ArgumentNullException("clientSocket", "NetState.NetState(...) - clientSocket == null error!");
            }

            m_Socket = clientSocket;

            IPAddress ipAddress;

            IPAddress.TryParse(clientSocket.RemoteOnlyIP, out ipAddress);

            if (ipAddress == null)
            {
                m_NetAddress = new IPEndPoint(IPAddress.None, clientSocket.RemotePort);
            }
            else
            {
                m_NetAddress = new IPEndPoint(ipAddress, clientSocket.RemotePort);
            }
            World = world;

            ReceiveBuffer = new ReceiveQueue();
        }
예제 #2
0
파일: Program.cs 프로젝트: RainsSoft/DogSE
 public Session()
 {
     RQ = new ReceiveQueue();
 }
예제 #3
0
파일: NetState.cs 프로젝트: RainsSoft/DogSE
        /// <summary>
        /// 有实例产生时调用
        /// </summary>
        /// <param name="clientSocket"></param>
        /// <param name="world"></param>
        public NetState(ClientSession<NetState> clientSocket, WorldBase world)
            : this()
        {
            if (clientSocket == null)
                throw new ArgumentNullException("clientSocket", "NetState.NetState(...) - clientSocket == null error!");

            m_Socket = clientSocket;

            IPAddress ipAddress;
            IPAddress.TryParse(clientSocket.RemoteOnlyIP, out ipAddress);

            if (ipAddress == null)
                m_NetAddress = new IPEndPoint(IPAddress.None, clientSocket.RemotePort);
            else
                m_NetAddress = new IPEndPoint(ipAddress, clientSocket.RemotePort);
            World = world;

            ReceiveBuffer = new ReceiveQueue();
        }
예제 #4
0
        /// <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();
            }
        }