Ejemplo n.º 1
0
        private void SendThreadUpdate()
        {
            while (!bClosed)
            {
                int nPackageCount = 0;
                while (!mSendPackageQueue.IsEmpty)
                {
                    NetEndPointPackage mNetPackage = null;
                    if (!mSendPackageQueue.TryDequeue(out mNetPackage))
                    {
                        break;
                    }

                    this.SendNetStream(mNetPackage);

                    nPackageCount++;
                    if (nPackageCount > 50)
                    {
                        nPackageCount = 0;
                        Thread.Sleep(1);
                    }
                }

                Thread.Sleep(50);
            }

            DebugSystem.LogWarning("Server SendThread Safe Quit !");
        }
Ejemplo n.º 2
0
        public void SendNetPackage(NetPackage mNetPackage)
        {
            NetEndPointPackage mPackage = ObjectPoolManager.Instance.mNetEndPointPackagePool.Pop();

            mPackage.mRemoteEndPoint = remoteEndPoint;
            mPackage.mPackage        = mNetPackage;

            SocketUdp_Server_Basic.Instance.SendNetPackage(mPackage);
        }
Ejemplo n.º 3
0
        private void SendNetStream(NetEndPointPackage mEndPointPacakge)
        {
            EndPoint   remoteEndPoint = mEndPointPacakge.mRemoteEndPoint;
            NetPackage mPackage       = mEndPointPacakge.mPackage;

            DebugSystem.Assert(mPackage.Length >= ServerConfig.nUdpPackageFixedHeadSize, "发送长度要大于等于 包头: " + mPackage.Length);
            int nSendLength = mSocket.SendTo(mPackage.buffer, 0, mPackage.Length, SocketFlags.None, remoteEndPoint);

            DebugSystem.Assert(nSendLength > 0, "Server 发送失败: " + nSendLength);

            ObjectPoolManager.Instance.mNetEndPointPackagePool.recycle(mEndPointPacakge);
        }
Ejemplo n.º 4
0
        private void ReceiveThreadUpdate()
        {
            while (true)
            {
                int length = 0;
                try {
                    EndPoint tempEndPoint           = new IPEndPoint(IPAddress.Broadcast, 0);
                    NetUdpFixedSizePackage mPackage = ObjectPoolManager.Instance.mUdpFixedSizePackagePool.Pop();
                    length          = mSocket.ReceiveFrom(mPackage.buffer, ref tempEndPoint);
                    mPackage.Length = length;

                    if (length > 0)
                    {
                        NetEndPointPackage mEndPointPackage = ObjectPoolManager.Instance.mNetEndPointPackagePool.Pop();
                        mEndPointPackage.mPackage        = mPackage;
                        mEndPointPackage.mRemoteEndPoint = tempEndPoint;
                        mReceivePackageQueue.Enqueue(mEndPointPackage);
                    }
                    else
                    {
                        ObjectPoolManager.Instance.mUdpFixedSizePackagePool.recycle(mPackage);

                        DebugSystem.LogError("接受长度: " + length);
                        break;
                    }
                } catch (SocketException e) {
                    if (e.SocketErrorCode == SocketError.Interrupted)
                    {
                        DebugSystem.LogWarning("阻塞Socket 调用已被取消");
                    }
                    else
                    {
                        DebugSystem.LogWarning("SocketException: " + e.SocketErrorCode);
                    }
                    break;
                } catch (Exception e) {
                    DebugSystem.LogError("服务器 异常: " + e.Message + " | " + e.StackTrace);

                    m_state = NETSTATE.DISCONNECTED;
                    peer_event mEvent = new peer_event();
                    mEvent.mNetEventType = NETEVENT.DISCONNECTED;
                    mEvent.msg           = e.Message;

                    break;
                }
            }

            DebugSystem.LogWarning("Server ReceiveThread Safe Quit !");
        }
Ejemplo n.º 5
0
        private void HandleReceiveData()
        {
            while (true)
            {
                int nHandlePackageCount = 0;
                while (!mReceivePackageQueue.IsEmpty)
                {
                    NetEndPointPackage mEndPointPackage = null;
                    if (!mReceivePackageQueue.TryDequeue(out mEndPointPackage))
                    {
                        break;
                    }

                    IPEndPoint point    = mEndPointPackage.mRemoteEndPoint as IPEndPoint;
                    UInt16     tempPort = (UInt16)point.Port;

                    ClientPeer mPeer = null;
                    if (!ClientPeerManager.Instance.IsExist(tempPort))
                    {
                        mPeer = ObjectPoolManager.Instance.mClientPeerPool.Pop();
                        mPeer.AcceptClient(mSocket, point);
                        ClientPeerManager.Instance.AddClient(mPeer);

                        DebugSystem.Log("增加 客户端信息: " + tempPort);
                    }

                    mPeer = ClientPeerManager.Instance.FindClient(tempPort);
                    mPeer.ReceiveUdpSocketFixedPackage(mEndPointPackage.mPackage as NetUdpFixedSizePackage);

                    nHandlePackageCount++;
                    if (nHandlePackageCount > 50)
                    {
                        nHandlePackageCount = 0;
                        Thread.Sleep(1);
                    }
                }

                if (bClosed)
                {
                    break;
                }

                Thread.Sleep(50);
            }

            DebugSystem.LogWarning("Server HandleReceiveLogicThread Safe Quit !");
        }
Ejemplo n.º 6
0
 public void SendNetPackage(NetEndPointPackage mNetPackage)
 {
     mSendPackageQueue.Enqueue(mNetPackage);
 }