Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        //static string remoteHost = "172.26.187.156";
        //static string host = "172.26.187.156";

        public static async void ConnectHost(Object obj)
        {
            int port = (int)obj;
            UdpClientConnection conn = new UdpClientConnection(host, port, remoteHost, 1230);

            var connectOK = await conn.ConnectAsync();

            if (connectOK)
            {
                Console.WriteLine("Client");
                Thread.Sleep(1000);
                sendMsg(conn, port);
            }
            else
            {
                Console.WriteLine("client connect server failed");
            }
            conn.Close();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Connect to the specified ip:port endpoint for the matchmaker, then send the specified
        /// message. Returns a task that resolves to a tuple of the established connection and the
        /// first message received from the matchmaker in response to the sent message (usually
        /// the join/host confirmal message). Will throw if the connection closes prematurely or
        /// otherwise errors. Otherwise, the task itself is responsible for disposing of the
        /// connection once the server disconnects.
        /// </summary>
        private static async Task <(UdpClientConnection, MessageReader)> ConnectToMMAndSend(IPAddress address,
                                                                                            ushort port, Action <MessageWriter> writeMessage)
        {
            var firstMessageTask = new TaskCompletionSource <MessageReader>();

            var connection = new UdpClientConnection(new IPEndPoint(address, port));

            connection.KeepAliveInterval    = 1000;
            connection.DisconnectTimeout    = 10000;
            connection.ResendPingMultiplier = 1.2f;

            // Set up an event handler to resolve the task on first non-reselect message received.
            Action <DataReceivedEventArgs> onDataReceived = null;

            onDataReceived = args =>
            {
                try
                {
                    var msg = args.Message.ReadMessage();
                    if (msg.Tag == (byte)MMTags.ReselectServer)
                    {
                        return;                                          // not interested
                    }
                    firstMessageTask.TrySetResult(msg);
                    connection.DataReceived -= onDataReceived;
                }
                finally
                {
                    args.Message.Recycle();
                }
            };
            connection.DataReceived += onDataReceived;

            // Set up an event handler to set an exception for the task on early disconnect.
            connection.Disconnected += (sender, args) =>
            {
                connection.Dispose();
                firstMessageTask.TrySetException(new AUException("Connection to matchmaker prematurely exited"));
            };

            // Connect to the endpoint.
            connection.ConnectAsync(HANDSHAKE);
            await connection.ConnectWaitLock.AsTask();

            // Send the contents.
            connection.SendReliableMessage(writeMessage);

            // Wait for the response to arrive.
            var response = await firstMessageTask.Task;

            // If this is not a redirect, return the result.
            if (response.Tag != (byte)MMTags.Redirect)
            {
                return(connection, response);
            }

            // This is a redirect, so do this again but with the new data.
            var newIp   = response.ReadUInt32();
            var newPort = response.ReadUInt16();

            // Reconnect to new host.
            return(await ConnectToMMAndSend(new IPAddress(newIp), newPort, writeMessage));
        }
Ejemplo n.º 4
0
 public void ConnectAsync(IPAddress address, int port)
 {
     CreateConnection(address, port);
     connection.ConnectAsync();
 }