示例#1
0
        public void ProcessPackage(byte[] bytes, IPEndPoint ipEndPoint)
        {
            Logger.Log("Process Package Begin: " + bytes.Length + " ipEndPoint: " + ipEndPoint, EUDPBoxLogType.Log);

            if (StatisticsTotalPackageCount == uint.MaxValue)
            {
                StatisticsBadPackageCount   = 0;
                StatisticsTotalPackageCount = 0;
            }

            StatisticsTotalPackageCount++;

            if (UDPBoxUtility.PackageIsBroken(bytes, mPackageHeadBytes))
            {
                StatisticsBadPackageCount++;
                Logger.Log("Package Is Broken!", EUDPBoxLogType.Warning);

                return;
            }

            var flag = false;

            for (int i = 0, iMax = mMessageInterceptList.Count; i < iMax; i++)
            {
                var item = mMessageInterceptList[i];
                flag = item(new MessageInterceptInfo()
                {
                    Bytes = bytes, IPEndPoint = ipEndPoint
                });
                if (flag)
                {
                    Logger.Log("Message Intercepted: " + item + " bytes: " + bytes.Length + " ipEndPoint: " + ipEndPoint, EUDPBoxLogType.Log);
                }
            }
            if (flag)
            {
                return;
            }

            var targetHandler = default(HandlerBase);

            for (int i = 0, iMax = mHandlerList.Count; i < iMax; i++)
            {
                var item = mHandlerList[i];

                if (UDPBoxUtility.ComparePackageID(bytes, mPackageHeadBytes, item.ProcessableID))
                {
                    targetHandler = item;
                    break;
                }
            }

            Logger.Log("Process Package: " + targetHandler, EUDPBoxLogType.Log);

            if (targetHandler != null)
            {
                targetHandler.Process(this, bytes, ipEndPoint);
            }
        }
示例#2
0
        public static UDPBoxBroadcast GenerateStandardUDPBoxBroadcastAndSetup(
            UDPBox_UDPClient udpClient
            , int broadcastSendPort
            , string netPrefixIP
            , UDPBoxContainer container)
        {
            var broadcast        = new UDPBoxBroadcast(udpClient, broadcastSendPort);
            var broadcastPackage = new BroadcastPackage(container.PackageHeadBytes);

            if (container.IsMaster)
            {
                broadcast.ListenBroadcast((bytes, endPoint) =>
                {
                    if (UDPBoxUtility.PackageIsBroken(bytes, container.PackageHeadBytes))
                    {
                        return;
                    }
                    if (!UDPBoxUtility.ComparePackageID(bytes, container.PackageHeadBytes, broadcastPackage.ID))
                    {
                        return;
                    }

                    if (!broadcastPackage.Deserialize(bytes))
                    {
                        return;
                    }

                    var ipEndPoint = new IPEndPoint(IPAddress.Parse(broadcastPackage.IpAddress), broadcastPackage.BeginPort);
                    if (ipEndPoint.Address.Equals(container.SelfIPAddress) &&
                        ipEndPoint.Port < container.UdpBoxEndPort && ipEndPoint.Port >= container.UdpBoxBeginPort)
                    {
                        return;
                    }
                    //Avoid self connect to the self.

                    var establishConnectPackage        = new EstablishConnectPackage(container.PackageHeadBytes);
                    establishConnectPackage.SenderType = EstablishConnectPackage.ESenderType.Server;
                    establishConnectPackage.IpAddress  = container.SelfIPAddress.ToString();
                    establishConnectPackage.BeginPort  = container.UdpBoxBeginPort;
                    establishConnectPackage.EndPort    = container.UdpBoxEndPort;
                    establishConnectPackage.IsReceipt  = false;
                    container.SendUDPMessage(establishConnectPackage.Serialize(), ipEndPoint);
                    //Server notify to client.
                });
            }
            else
            {
                broadcastPackage.IpAddress = container.SelfIPAddress.ToString();
                broadcastPackage.BeginPort = container.UdpBoxBeginPort;
                broadcastPackage.EndPort   = container.UdpBoxEndPort;

                broadcast.StartBroadcast(broadcastPackage.Serialize(), netPrefixIP);
                //Client through broadcast notify to server.
            }

            return(broadcast);
        }
示例#3
0
        bool InterceptAndUpdateConnectState(UDPBox.MessageInterceptInfo messageInterceptInfo)
        {
            if (UDPBoxUtility.PackageIsBroken(messageInterceptInfo.Bytes, PackageHeadBytes))
            {
                return(true);
            }

            var packageID = UDPBoxUtility.GetPackageID(messageInterceptInfo.Bytes, PackageHeadBytes);

            if (packageID == UDPBoxUtility.ESTABLISH_CONNECT_ID)
            {
                if (!mEstablishConnectPackage.Deserialize(messageInterceptInfo.Bytes))
                {
                    return(true);
                }

                var senderType         = mEstablishConnectPackage.SenderType;
                var establishIPAddress = mEstablishConnectPackage.IpAddress;
                var packageBeginPort   = mEstablishConnectPackage.BeginPort;
                var packageEndPort     = mEstablishConnectPackage.EndPort;
                var isReceipt          = mEstablishConnectPackage.IsReceipt;

                if (senderType == EstablishConnectPackage.ESenderType.Server)
                {
                    MasterIPConnectInfo = new ConnectInfo()
                    {
                        IPEndPoint = new IPEndPoint(IPAddress.Parse(establishIPAddress), packageBeginPort),
                        BeginPort  = packageBeginPort,
                        EndPort    = packageEndPort,
                    };
                    OnConnectedMaster?.Invoke();

                    if (!isReceipt)
                    {
                        mEstablishConnectPackage.SenderType = EstablishConnectPackage.ESenderType.Client;
                        mEstablishConnectPackage.IpAddress  = SelfIPAddress.ToString();
                        mEstablishConnectPackage.BeginPort  = UdpBoxBeginPort;
                        mEstablishConnectPackage.EndPort    = UdpBoxEndPort;
                        mEstablishConnectPackage.IsReceipt  = true;
                        SendUDPMessageToRandomPort(mEstablishConnectPackage.Serialize(), packageBeginPort, packageEndPort, IPAddress.Parse(establishIPAddress));
                    }
                }
                else if (senderType == EstablishConnectPackage.ESenderType.Client)
                {
                    var connectInfo = new ConnectInfo()
                    {
                        IPEndPoint = new IPEndPoint(IPAddress.Parse(establishIPAddress), packageBeginPort),
                        BeginPort  = packageBeginPort,
                        EndPort    = packageEndPort,
                    };

                    if (ClientIPConnectList.Count < MAX_CLIENT &&
                        !ClientIPConnectList.Find(m => m.IsPortAndIPContain(connectInfo.IPEndPoint)).Valid)
                    {
                        ClientIPConnectList.Add(connectInfo);
                        OnConnectedClient?.Invoke();
                    }

                    if (!isReceipt)
                    {
                        mEstablishConnectPackage.SenderType = EstablishConnectPackage.ESenderType.Server;
                        mEstablishConnectPackage.IpAddress  = SelfIPAddress.ToString();
                        mEstablishConnectPackage.BeginPort  = UdpBoxBeginPort;
                        mEstablishConnectPackage.EndPort    = UdpBoxEndPort;
                        mEstablishConnectPackage.IsReceipt  = true;
                        SendUDPMessageToRandomPort(mEstablishConnectPackage.Serialize(), packageBeginPort, packageEndPort, IPAddress.Parse(establishIPAddress));
                    }
                }
            }
            else if (packageID == UDPBoxUtility.PING_PONG_ID)
            {
                if (!mPingPongPackageTemplate.Deserialize(messageInterceptInfo.Bytes))
                {
                    return(true);
                }
                if (mPingPongPackageTemplate.PingPong == PingPongPackage.EPingPong.Pong)
                {
                    return(true);
                }

                if (IsMaster)
                {
                    for (int i = 0, iMax = ClientIPConnectList.Count; i < iMax; i++)
                    {
                        var clientEndPoint = ClientIPConnectList[i];

                        if (clientEndPoint.IsPortAndIPContain(messageInterceptInfo.IPEndPoint))
                        {
                            ClientIPConnectList[i] = new ConnectInfo()
                            {
                                AliveTimer          = 0f,
                                IPEndPoint          = clientEndPoint.IPEndPoint,
                                BeginPort           = clientEndPoint.BeginPort,
                                EndPort             = clientEndPoint.EndPort,
                                IsClientEstablished = true
                            };
                        }
                    }
                }
                else
                {
                    if (MasterIPConnectInfo.Valid && MasterIPConnectInfo.IsPortAndIPContain(messageInterceptInfo.IPEndPoint))
                    {
                        MasterIPConnectInfo = new ConnectInfo()
                        {
                            AliveTimer = 0f,
                            IPEndPoint = messageInterceptInfo.IPEndPoint,
                            BeginPort  = MasterIPConnectInfo.BeginPort,
                            EndPort    = MasterIPConnectInfo.EndPort,
                        };
                    }
                }
                return(true);
            }

            return(false);
        }