Exemplo n.º 1
0
 void OnGUI()
 {
     if (GUILayout.Button("Sent"))
     {
         var package = new PingPongPackage(UDPBoxUtility.DefaultHeadBytes);
         mUDPBoxBroadcast.StartBroadcast(package.Serialize());
     }
 }
Exemplo n.º 2
0
        public override void Process(UDPBox udpBox, byte[] packageBytes, IPEndPoint ipEndPoint)
        {
            if (!mTemplate.Deserialize(packageBytes))
            {
                return;
            }

            mTemplate.PingPong = mTemplate.PingPong == PingPongPackage.EPingPong.Ping ? PingPongPackage.EPingPong.Pong : PingPongPackage.EPingPong.Ping;

            udpBox.SendMessage(mTemplate.Serialize(), ipEndPoint);
        }
Exemplo n.º 3
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;
        }