Beispiel #1
0
        private static void HandleToClient(string source, MessageReader packet)
        {
            var messageType = (MessageType)packet.Tag;

            Console.ForegroundColor = ConsoleColor.Cyan;
            //Console.WriteLine($"{source,-15} Client received: {packet.Tag,-2} {messageType}");

            try
            {
                var reader = packet.GetHazelReader();
                var body   = reader.PeekToEnd();
                if (SaveMessages)
                {
                    File.WriteAllBytes(Path.Combine(MessageFolder, $"recv_{packet.Tag}_{allId++}.bin"), body);
                }

                switch (messageType)
                {
                case MessageType.ReselectServer:
                    var reselect = ReselectServer.Deserialize(reader);
                    DumpToConsole(reselect);
                    break;

                case MessageType.Redirect:
                    var redirect = Redirect.Deserialize(reader);
                    DumpToConsole(redirect);
                    break;

                case MessageType.HostGame:
                    var host = HostGameResponse.Deserialize(reader);
                    DumpToConsole(host);
                    break;

                case MessageType.JoinGame:
                    var join = JoinGameResponse.Deserialize(reader);
                    DumpToConsole(join);
                    break;

                case MessageType.GameData:
                    var gamedata = GameData.Deserialize(reader);
                    foreach (var item in gamedata)
                    {
                        HandleGameData(item, false);
                    }

                    //Directory.CreateDirectory("gamedata");
                    //File.WriteAllBytes(Path.Combine("gamedata", $"recv_data_{gamedataId++}.bin"), body);
                    break;

                case MessageType.GameDataTo:
                    var gamedatato = GameDataTo.Deserialize(reader);
                    foreach (var item in gamedatato)
                    {
                        HandleGameDataTo(item, false);
                    }
                    break;

                case MessageType.JoinedGame:
                    var joined = JoinedGame.Deserialize(reader);
                    DumpToConsole(joined);
                    break;

                case MessageType.AlterGame:
                    var alter = AlterGameResponse.Deserialize(reader);
                    DumpToConsole(alter);
                    break;

                case MessageType.GetGameListV2:
                    var gamelist = GetGameListV2Response.Deserialize(reader);
                    DumpToConsole(gamelist);
                    break;

                case MessageType.RemovePlayer:
                    var removeplayer = RemovePlayerResponse.Deserialize(reader);
                    DumpToConsole(removeplayer);
                    break;

                case MessageType.StartGame:
                    var start = StartGame.Deserialize(reader);
                    DumpToConsole(start);
                    if (BreakOnGameStart)
                    {
                        Console.WriteLine("Press any key to continue...");
                        Console.ReadKey();
                    }
                    break;

                case MessageType.EndGame:
                    var end = EndGame.Deserialize(reader);
                    DumpToConsole(end);
                    if (BreakOnGameEnd)
                    {
                        Console.WriteLine("Press any key to continue...");
                        Console.ReadKey();
                    }

                    //Clear Entities to prevent collisions
                    EntityTracker.entities.Clear();
                    break;

                default:
                    Console.WriteLine($"Unhandled Message: {messageType} size: {body.Length}");
                    return;
                }

                if (reader.GetBytesLeft() > 0 && LogNotConsumed)
                {
                    Console.WriteLine($"[{messageType}]{reader.GetBytesLeft()} bytes not cunsumed");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error handling ToClient: " + ex.Message);
            }
        }