Exemplo n.º 1
0
        /// <summary>
        /// Starts listesting for server updates, any received updates are pushed into serverCommands queue.
        /// </summary>
        /// <param name="listenOn">local IP+port on which should the client listen for the updates.</param>
        /// <returns>when server ends the connection or <paramref name="active"/>'' is set.</returns>
        async Task ListenForServerCommandsAsync(IPEndPoint listenOn)
        {
            updatesFromServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            updatesFromServer.Bind(listenOn);
            Console.WriteLine($"Started listening for server updates on {listenOn}");
            while (active)
            {
                var bytes = await Communication.UDPReceiveMessageAsync(updatesFromServer, 1024);

                var newLastProcessedCUID = Serialization.DecodeInt(bytes.Item1, 0);
                var cmd = ServerCommand.Decode(bytes.Item1, 4);
                lock (this)
                {
                    //Only add newer updates.
                    if (newLastProcessedCUID >= LastProcessedClientUpdateID)
                    {
                        AddServerCommand(cmd);
                        UpdLastProcCUID(newLastProcessedCUID);
                    }
                }
            }
            updatesFromServer.Shutdown(SocketShutdown.Both);
            updatesFromServer.Close();
        }