Esempio n. 1
0
 private void Sock_DataReceived(object sender, SimpleTcp.DataReceivedEventArgs e)
 {
     foreach (var item in e.Data)
     {
         recvBuf.Enqueue(item);
     }
 }
Esempio n. 2
0
 public void DataReceived(object sender, SimpleTcp.DataReceivedEventArgs e)
 {
     UnPack.UnPackReceiveData(e.Data);
 }
        private void OnReceivedData(object sender, SimpleTcp.DataReceivedEventArgs ev)
        {
            try
            {
                string receivedJsonString = Encoding.UTF8.GetString(ev.Data);

                if (!receivedJsonString.TryDeserializeJson(out DataBase dataBase))
                {
                    return;
                }

                if (receivedJsonString.TryDeserializeJson(out Ping ping) && ping != null)
                {
                    Logger.Get.Info($"Ping latency: {(ping.Received - ping.Sent).Milliseconds} ms");
                }
                else if (receivedJsonString.TryDeserializeJson(out Query query))
                {
                    Logger.Get.Warn($"Received: {query.QueryType}");
                    switch (query.QueryType)
                    {
                    case QueryType.PlayerCount:
                    {
                        PlayerCountStat stat = new PlayerCountStat()
                        {
                            DateTime    = DateTime.Now,
                            MaxPlayers  = (ushort)Server.Get.Slots,
                            PlayerCount = (ushort)Server.Get.Players.Count
                        };
                        Response response = new Response()
                        {
                            SameMachine   = SyncordPlugin.Config.DiscordBotAddress == "127.0.0.1",
                            SLFullAddress = $"{SyncordPlugin.ServerIPv4}:{Server.Get.Port}",
                            Time          = DateTime.Now,
                            QueryType     = query.QueryType,
                            JsonContent   = stat.Serialize()
                        };
                        TcpClient.SendAsJson(response);
                        break;
                    }

                    case QueryType.ServerFps:
                    {
                        FpsStat fpsStat = new FpsStat()
                        {
                            DateTime = DateTime.Now,
                            Fps      = _pluginEventHandler.ServerFps,
                            IsIdle   = IdleMode.IdleModeActive
                        };
                        Response response = new Response()
                        {
                            SameMachine   = SyncordPlugin.Config.DiscordBotAddress == "127.0.0.1",
                            SLFullAddress = $"{SyncordPlugin.ServerIPv4}:{Server.Get.Port}",
                            Time          = DateTime.Now,
                            QueryType     = query.QueryType,
                            JsonContent   = fpsStat.Serialize()
                        };
                        TcpClient.SendAsJson(response);
                        break;
                    }
                    }
                }
            }
Esempio n. 4
0
        private void ReceivedDataFromSLServer(object sender, SimpleTcp.DataReceivedEventArgs ev)
        {
            string jsonStr = Encoding.UTF8.GetString(ev.Data);

            if (!jsonStr.TryDeserializeJson(out DataBase dataBase))
            {
                return;
            }

            //  If Bot & SL Server are on the same machine, make the identifier / key the localhost variant
            //  Why?
            // - The user shall only have to enter 127.0.0.1 in the config instead of the possibly complicated public IPv4
            // - One less headache to worry about when you have the bot and the SL Server on the same machine
            //   while also having a dynamic ip - You don't have to re-type the IP every changing interval
            string ipAddress = dataBase.SameMachine ? $"127.0.0.1:{dataBase.SLFullAddress.Split(':')[1]}" : dataBase.SLFullAddress;

            if (Bot.BotConfig.DebugMode)
            {
                Console.WriteLine($"Received the following (From same machine? {dataBase.SameMachine} | {dataBase.SLFullAddress}): >>{jsonStr}<<{Environment.NewLine}----------------------");
            }

            switch (dataBase.MessageType)
            {
            case MessageType.Event:
            {
                if (jsonStr.TryDeserializeJson(out PlayerJoinLeave joinLeave))
                {
                    if (joinLeave.Identifier == "join")
                    {
                        Console.WriteLine($"{joinLeave.Player.Nickname} joined {joinLeave.SLFullAddress}!");

                        var embedQueueElement = _embedQueues.Find(_ => _.PlayerJoinedQueue.ContainsKey(ipAddress));
                        if (embedQueueElement == null)
                        {
                            _logger.Warning($"ReceivedDataFromSLServer: Received join data from unconfigured SL Server: {dataBase.SLFullAddress}");
                            return;
                        }
                        Queue <PlayerJoinLeave> joinQueue = embedQueueElement.PlayerJoinedQueue[ipAddress];
                        if (joinQueue == null)
                        {
                            _logger.Warning($"ReceivedDataFromSLServer: A configured SL Server has a null-queue");
                            return;
                        }
                        joinQueue.Enqueue(joinLeave);
                    }
                    else if (joinLeave.Identifier == "leave")
                    {
                        Debug.WriteLine($"{joinLeave.Player.Nickname} left {joinLeave.SLFullAddress}!");
                        var embedQueueElement = _embedQueues.Find(_ => _.PlayerLeftQueue.ContainsKey(ipAddress));
                        if (embedQueueElement == null)
                        {
                            _logger.Warning($"ReceivedDataFromSLServer: Received leave data from unconfigured SL Server");
                            return;
                        }
                        Queue <PlayerJoinLeave> joinQueue = embedQueueElement.PlayerLeftQueue[ipAddress];
                        if (joinQueue == null)
                        {
                            _logger.Warning($"ReceivedDataFromSLServer: A configured SL Server has a null-queue");
                            return;
                        }
                        joinQueue.Enqueue(joinLeave);
                    }
                }
                else if (jsonStr.TryDeserializeJson(out RoundEnd roundEnd))
                {
                    Debug.WriteLine($"Round ended for {roundEnd.SLFullAddress}!");

                    var embedQueueElement = _embedQueues.Find(_ => _.RoundEndQueue.ContainsKey(ipAddress));
                    if (embedQueueElement == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: Received round end data from unconfigured SL Server: {dataBase.SLFullAddress}");
                        return;
                    }

                    Queue <RoundEnd> roundEndQueue = embedQueueElement.RoundEndQueue[ipAddress];
                    if (roundEndQueue == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: A configured SL Server has a null-queue");
                        return;
                    }

                    roundEndQueue.Enqueue(roundEnd);
                }
                else if (jsonStr.TryDeserializeJson(out PlayerDeath playerDeath))
                {
                    Debug.WriteLine($"Player Death for {playerDeath.SLFullAddress}!");

                    var embedQueueElement = _embedQueues.Find(_ => _.PlayerDeathQueue.ContainsKey(ipAddress));
                    if (embedQueueElement == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: Received join data from unconfigured SL Server: {dataBase.SLFullAddress}");
                        return;
                    }

                    Queue <PlayerDeath> deathQueue = embedQueueElement.PlayerDeathQueue[ipAddress];
                    if (deathQueue == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: A configured SL Server has a null-queue");
                        return;
                    }

                    deathQueue.Enqueue(playerDeath);
                }
                else if (jsonStr.TryDeserializeJson(out PlayerBan playerBan))
                {
                    Debug.WriteLine($"Player Death for {playerBan.SLFullAddress}!");

                    var embedQueueElement = _embedQueues.Find(_ => _.PlayerBanQueue.ContainsKey(ipAddress));
                    if (embedQueueElement == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: Received join data from unconfigured SL Server: {dataBase.SLFullAddress}");
                        return;
                    }

                    Queue <PlayerBan> banQueue = embedQueueElement.PlayerBanQueue[ipAddress];
                    if (banQueue == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: A configured SL Server has a null-queue");
                        return;
                    }

                    banQueue.Enqueue(playerBan);
                }

                break;
            }

            case MessageType.Query:
            {
                if (jsonStr.TryDeserializeJson(out Ping ping))
                {
                    ping.Received = DateTime.Now;
                    _tcpServer.SendAsJson(ev.IpPort, ping);
                }

                break;
            }

            case MessageType.Response:
            {
                if (jsonStr.TryDeserializeJson(out Response response))
                {
                    switch (response.QueryType)
                    {
                    case QueryType.PlayerCount:
                    {
                        break;
                    }

                    case QueryType.ServerFps:
                    {
                        break;
                    }
                    }
                }

                break;
            }
            }
        }
Esempio n. 5
0
 private void Events_DataReceived(object?sender, SimpleTcp.DataReceivedEventArgs e)
 {
     Debug.WriteLine("Recived Data: " + e.Data, "ClientTCP");
 }