Exemple #1
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);
        }
Exemple #2
0
        void RefreshConnectStateInWorkThread()
        {
            var deltaTime = UDPBoxUtility.GetDeltaTime(mLastWorkThreadTime);

            if (IsMaster)
            {
                if (ClientIPConnectList.Count > 0)
                {
                    State = EState.HasClients;
                }
                else
                {
                    State = EState.NoClients;
                }

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

                    if (!item.Valid)
                    {
                        continue;
                    }

                    item = new ConnectInfo()
                    {
                        AliveTimer          = item.AliveTimer + deltaTime,
                        IPEndPoint          = item.IPEndPoint,
                        BeginPort           = item.BeginPort,
                        EndPort             = item.EndPort,
                        IsClientEstablished = item.IsClientEstablished,
                    };

                    ClientIPConnectList[i] = item;
                }
            }
            else
            {
                if (MasterIPConnectInfo.Valid)
                {
                    State = EState.HasServer;

                    MasterIPConnectInfo = new ConnectInfo()
                    {
                        AliveTimer          = MasterIPConnectInfo.AliveTimer + deltaTime,
                        IPEndPoint          = MasterIPConnectInfo.IPEndPoint,
                        BeginPort           = MasterIPConnectInfo.BeginPort,
                        EndPort             = MasterIPConnectInfo.EndPort,
                        IsClientEstablished = MasterIPConnectInfo.IsClientEstablished,
                    };
                }
                else
                {
                    State = EState.NoServer;
                }
            }
            //update ticks.

            if (IsMaster)
            {
                for (int i = ClientIPConnectList.Count - 1; i >= 0; i--)
                {
                    var item = ClientIPConnectList[i];

                    if (!item.Valid)
                    {
                        continue;
                    }

                    if (!item.IsClientEstablished && item.AliveTimer > TIME_OUT_NOT_ESTABLISH_CONNECT_FLOAT)
                    {
                        ClientIPConnectList.RemoveAt(i);
                    }
                    else if (item.AliveTimer > TIME_OUT_CONNECT_FLOAT)
                    {
                        ClientIPConnectList.RemoveAt(i);
                        OnClientTimeOut?.Invoke(item.IPEndPoint);
                    }
                }
            }
            else
            {
                if (MasterIPConnectInfo.Valid)
                {
                    if (MasterIPConnectInfo.AliveTimer > TIME_OUT_CONNECT_FLOAT)
                    {
                        MasterIPConnectInfo = new ConnectInfo();
                        OnMasterIPTimeOut?.Invoke();
                    }
                }
            }
            //update timeout.

            if (mPingTargetThreadTimer <= 0f)
            {
                mPingPongPackageTemplate.PingPong = PingPongPackage.EPingPong.Ping;

                if (IsMaster)
                {
                    for (int i = 0, iMax = ClientIPConnectList.Count; i < iMax; i++)
                    {
                        var item = ClientIPConnectList[i];
                        if (item.Valid && item.IsClientEstablished)
                        {
                            SendUDPMessageToRandomPort(mPingPongPackageTemplate.Serialize(), item);
                        }
                    }
                }
                else
                {
                    if (MasterIPConnectInfo.Valid)
                    {
                        SendUDPMessageToRandomPort(mPingPongPackageTemplate.Serialize(), MasterIPConnectInfo);
                    }
                }

                mPingTargetThreadTimer = PING_CONNECT_TIME_FLOAT;
            }
            else
            {
                mPingTargetThreadTimer -= deltaTime;
            }//ping connect.

            mLastWorkThreadTime = DateTime.Now.Ticks;
        }