summaryrefslogtreecommitdiff
path: root/Impostor-dev/src/Impostor.Client.App/Program.cs
blob: aa8866adec43427b5c8756a7cf3dbb9b4f03cb61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Impostor.Api.Innersloth;
using Impostor.Api.Net.Messages;
using Impostor.Api.Net.Messages.C2S;
using Impostor.Hazel;
using Impostor.Hazel.Udp;
using Serilog;

namespace Impostor.Client.App
{
    internal static class Program
    {
        private static readonly ManualResetEvent QuitEvent = new ManualResetEvent(false);

        private static async Task Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                .WriteTo.Console()
                .CreateLogger();

            var writeHandshake = MessageWriter.Get(MessageType.Reliable);

            writeHandshake.Write(50516550);
            writeHandshake.Write("AeonLucid");

            var writeGameCreate = MessageWriter.Get(MessageType.Reliable);

            Message00HostGameC2S.Serialize(writeGameCreate, new GameOptionsData
            {
                MaxPlayers = 4,
                NumImpostors = 2
            });

            // TODO: ObjectPool for MessageReaders
            using (var connection = new UdpClientConnection(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 22023), null))
            {
                var e = new ManualResetEvent(false);

                // Register events.
                connection.DataReceived = DataReceived;
                connection.Disconnected = Disconnected;

                // Connect and send handshake.
                await connection.ConnectAsync(writeHandshake.ToByteArray(false));
                Log.Information("Connected.");

                // Create a game.
                await connection.SendAsync(writeGameCreate);
                Log.Information("Requested game creation.");

                // Recycle.
                writeHandshake.Recycle();
                writeGameCreate.Recycle();

                e.WaitOne();
            }
        }

        private static ValueTask DataReceived(DataReceivedEventArgs e)
        {
            Log.Information("Received data.");
            return default;
        }

        private static ValueTask Disconnected(DisconnectedEventArgs e)
        {
            Log.Information("Disconnected: " + e.Reason);
            QuitEvent.Set();
            return default;
        }
    }
}