Пример #1
0
        public NetworkingClientHandler(ClientType type, string ip, int port = 25570)
        {
            AlwaysPresent = true;

            ClientType = type;

            switch (type)
            {
            case ClientType.Host:
                ReceiveClient = new NetworkingClient(false, ip, port);
                break;

            case ClientType.Peer:
                ReceiveClient = new NetworkingClient(false, ip, port);
                ReceiveClient.OnStartedUPnPMapping += () => { upnpMappingStartTime = Time.Current; };
                SendClient = new NetworkingClient(true, ip, port);
                break;

            case ClientType.Server:
                throw new NotImplementedException();
            }

            Logger.Log("Created a RulesetNetworkingClientHandler", LoggingTarget.Network);

            if (ClientInfo == null)
            {
                ClientInfo = new ClientInfo
                {
                    Port = port,
                    IP   = ip
                }
            }
            ;
        }
Пример #2
0
 /// <summary>
 /// Send a Packet to all clients InGame
 /// </summary>
 /// <param name="packet"></param>
 public void SendToInGameClients(Packet packet)
 {
     if (SendClient == null)
     {
         foreach (ClientInfo clientInfo in InGameClients)
         {
             NetworkingClient client = new NetworkingClient(true, clientInfo.IP, clientInfo.Port);
             client.SendPacket(packet);
         }
     }
 }
Пример #3
0
        /// <summary>
        /// Poke!
        /// </summary>
        /// <param name="clientInfo"></param>
        protected void TestConnection(ClientInfo clientInfo)
        {
            clientInfo.ConncetionTryCount++;
            NetworkingClient client = new NetworkingClient(true, clientInfo.IP, clientInfo.Port);

            client.SendPacket(new BasicPacket(ClientInfo)
            {
                Test = true
            });
            Logger.Log("Testing a client's connection - " + clientInfo.IP + ":" + clientInfo.Port, LoggingTarget.Network);
        }
Пример #4
0
 /// <summary>
 /// Send to all but the one that sent it
 /// </summary>
 /// <param name="packet"></param>
 public void ShareWithOtherPeers(Packet packet)
 {
     if (ClientType == ClientType.Host || ClientType == ClientType.Server)
     {
         foreach (ClientInfo clientInfo in ConncetedClients)
         {
             if (packet.ClientInfo.IP != clientInfo.IP)
             {
                 NetworkingClient client = new NetworkingClient(true, clientInfo.IP, clientInfo.Port);
                 client.SendPacket(packet);
             }
         }
     }
 }
Пример #5
0
 /// <summary>
 /// Send a Packet to all clients Loaded
 /// </summary>
 /// <param name="packet"></param>
 public void SendToLoadedClients(Packet packet)
 {
     if (ClientType == ClientType.Host || ClientType == ClientType.Server)
     {
         foreach (ClientInfo clientInfo in LoadedClients)
         {
             NetworkingClient client = new NetworkingClient(true, clientInfo.IP, clientInfo.Port);
             client.SendPacket(packet);
         }
     }
     else
     {
         Logger.Log("Tried to send packets to loaded peers, we are a peer!", LoggingTarget.Network, LogLevel.Error);
     }
 }
Пример #6
0
        protected override void Update()
        {
            base.Update();

            if (Time.Current >= upnpMappingStartTime + 2000 && ClientType == ClientType.Peer)
            {
                ConnectToHost();
            }
            else if (Time.Current >= constructionTime + 3000 && ClientType == ClientType.Peer)
            {
                ConnectToHost();
            }

PacketRestart:
            Packet p = null;

            if (ReceiveClient.UdpClient.Available > 0)
            {
                p = ReceiveClient.ReceivePacket();
            }

            if (p is BasicPacket packet)
            {
                //Hosts
                if (SendClient == null)
                {
                    if (packet.Disconnect)
                    {
                        OnClientDisconnect?.Invoke(packet.ClientInfo);
                        foreach (ClientInfo client in ConnectingClients)
                        {
                            if (client.IP == packet.ClientInfo.IP)
                            {
                                ConnectingClients.Remove(client);
                                Logger.Log("A Connecting Client has Disconnected", LoggingTarget.Network);
                                break;
                            }
                        }
                        foreach (ClientInfo client in ConncetedClients)
                        {
                            if (client.IP == packet.ClientInfo.IP)
                            {
                                ConncetedClients.Remove(client);
                                Logger.Log("A Client has Disconnected", LoggingTarget.Network);
                                break;
                            }
                        }
                        foreach (ClientInfo client in InMatchClients)
                        {
                            if (client.IP == packet.ClientInfo.IP)
                            {
                                InMatchClients.Remove(client);
                                break;
                            }
                        }
                        foreach (ClientInfo client in LoadedClients)
                        {
                            if (client.IP == packet.ClientInfo.IP)
                            {
                                LoadedClients.Remove(client);
                                break;
                            }
                        }
                        foreach (ClientInfo client in InGameClients)
                        {
                            if (client.IP == packet.ClientInfo.IP)
                            {
                                InGameClients.Remove(client);
                                break;
                            }
                        }
                    }

                    if (packet.Connect)
                    {
                        packet.ClientInfo.LastConnectionTime = Time.Current;
                        ConnectingClients.Add(packet.ClientInfo);

                        NetworkingClient client = new NetworkingClient(true, packet.ClientInfo.IP, packet.ClientInfo.Port);

                        List <ClientInfo> playerList = new List <ClientInfo>
                        {
                            ClientInfo
                        };

                        foreach (ClientInfo clientInfo in ConncetedClients)
                        {
                            playerList.Add(clientInfo);
                        }

                        client.SendPacket(new BasicPacket(ClientInfo)
                        {
                            PlayerList = playerList,
                            Connect    = true,
                            Ip         = client.IP
                        });

                        Logger.Log("A Client is Connecting. . .", LoggingTarget.Network);
                    }

                    if (packet.RequestPlayerList)
                    {
                        NetworkingClient client = new NetworkingClient(true, packet.ClientInfo.IP, packet.ClientInfo.Port);

                        List <ClientInfo> playerList = new List <ClientInfo>
                        {
                            ClientInfo
                        };

                        foreach (ClientInfo clientInfo in ConncetedClients)
                        {
                            playerList.Add(clientInfo);
                        }

                        client.SendPacket(new BasicPacket(ClientInfo)
                        {
                            PlayerList        = playerList,
                            RequestPlayerList = true
                        });

                        Logger.Log("A Client is Connecting. . .", LoggingTarget.Network);
                    }

                    if (packet.Loaded)
                    {
                        foreach (ClientInfo client in InMatchClients)
                        {
                            if (client.IP == packet.ClientInfo.IP)
                            {
                                Logger.Log("A Client has Loaded and is ready to start", LoggingTarget.Network);
                                InMatchClients.Remove(client);
                                LoadedClients.Add(client);
                                break;
                            }
                        }
                    }

                    if (packet.GameStarted)
                    {
                        foreach (ClientInfo client in LoadedClients)
                        {
                            if (client.IP == packet.ClientInfo.IP)
                            {
                                Logger.Log("A Client has started!", LoggingTarget.Network);
                                LoadedClients.Remove(client);
                                InGameClients.Add(client);
                                break;
                            }
                        }
                    }

                    if (packet.Test)
                    {
                        foreach (ClientInfo client in ConnectingClients)
                        {
                            if (client.IP == packet.ClientInfo.IP)
                            {
                                client.Ping = (int)Time.Current - (int)client.LastConnectionTime;
                                ConnectingClients.Remove(client);
                                ConncetedClients.Add(client);
                                InMatchClients.Add(client);
                                OnClientJoin?.Invoke(client);
                                client.LastConnectionTime = Time.Current;
                                client.ConncetionTryCount = 0;
                                Logger.Log("Successfully connected to a Client! Ping: " + client.Ping, LoggingTarget.Network);
                                break;
                            }
                        }
                        foreach (ClientInfo client in ConncetedClients)
                        {
                            if (client.IP == packet.ClientInfo.IP)
                            {
                                client.Ping = (int)Time.Current - (int)client.LastConnectionTime;
                                client.LastConnectionTime = Time.Current;
                                client.ConncetionTryCount = 0;
                                Logger.Log("Successfully maintained connection to a Client! Ping: " + client.Ping, LoggingTarget.Network);
                            }
                        }
                    }
                }

                if (InMatchClients.Count == 0 && LoadedClients.Count > 0 && Loaded && !InGame)
                {
                    SendStartGame();
                }

                //Peers
                else if (SendClient != null)
                {
                    if (packet.Connect)
                    {
                        if (!InGame && !InMatch)
                        {
                            InMatch       = true;
                            ClientInfo.IP = packet.Ip;
                            OnConnectedToHost?.Invoke(packet.PlayerList);
                        }
                        Logger.Log("Connected to Host!", LoggingTarget.Network);
                    }

                    if (packet.Test)
                    {
                        SendToHost(new BasicPacket(ClientInfo)
                        {
                            Test = true
                        });
                        Logger.Log("Received connection test info from host, returning. . .", LoggingTarget.Network);
                    }

                    if (packet.RequestPlayerList)
                    {
                        OnReceivePlayerList?.Invoke(packet.PlayerList);
                    }

                    if (packet.StartGame)
                    {
                        StartGame?.Invoke();
                        SendToHost(new BasicPacket(ClientInfo)
                        {
                            GameStarted = true
                        });
                        InGame = true;
                    }

                    if (packet.Abort)
                    {
                        OnAbort?.Invoke();
                        InGame = false;
                        Loaded = false;
                    }

                    if (packet.LoadGame)
                    {
                        Logger.Log("Received instructions to LoadGame for " + packet.PlayerList.Count + " players", LoggingTarget.Network);
                        OnLoadGame?.Invoke(packet.PlayerList);
                    }
                }
            }

            if (ConncetedClients.Count == 0 && ConnectingClients.Count == 0 && Loaded && !InGame)
            {
                SendStartGame();
            }

            if (p != null)
            {
                OnPacketReceive?.Invoke(p);
            }

            if (ReceiveClient.UdpClient.Available > 0)
            {
                goto PacketRestart;
            }

            foreach (ClientInfo client in ConnectingClients)
            {
                if (client.LastConnectionTime + TimeOutTime / 10 <= Time.Current && client.ConncetionTryCount == 0)
                {
                    TestConnection(client);
                }

                if (client.LastConnectionTime + TimeOutTime / 6 <= Time.Current && client.ConncetionTryCount == 1)
                {
                    TestConnection(client);
                }

                if (client.LastConnectionTime + TimeOutTime / 3 <= Time.Current && client.ConncetionTryCount == 2)
                {
                    TestConnection(client);
                }

                if (client.LastConnectionTime + TimeOutTime <= Time.Current)
                {
                    ConnectingClients.Remove(client);
                    Logger.Log("Connection to a connecting client lost! - " + client.IP + ":" + client.Port, LoggingTarget.Network, LogLevel.Error);
                    break;
                }
            }

            foreach (ClientInfo client in ConncetedClients)
            {
                if (client.LastConnectionTime + TimeOutTime / 6 <= Time.Current && client.ConncetionTryCount == 0)
                {
                    TestConnection(client);
                }

                if (client.LastConnectionTime + TimeOutTime / 3 <= Time.Current && client.ConncetionTryCount == 1)
                {
                    TestConnection(client);
                }

                if (client.LastConnectionTime + TimeOutTime / 2 <= Time.Current && client.ConncetionTryCount == 2)
                {
                    TestConnection(client);
                }

                if (client.LastConnectionTime + TimeOutTime <= Time.Current)
                {
                    ConncetedClients.Remove(client);
                    InGameClients.Remove(client);
                    LoadedClients.Remove(client);
                    InGameClients.Remove(client);
                    Logger.Log("Connection to a connected client lost! - " + client.IP + ":" + client.Port, LoggingTarget.Network, LogLevel.Error);
                    break;
                }
            }
        }