Пример #1
0
        async Task <ConnectingStaticData> ReceiveStaticDataAsync()
        {
            var sData = ConnectingStaticData.Decode(await Communication.TCPReceiveMessageAsync(serverConnection));

            Console.WriteLine("Received static data from the server.");
            return(sData);
        }
Пример #2
0
        /// <summary>
        /// Begins to receive dynamic data from the server.
        /// </summary>
        /// <returns></returns>
        async Task <ConnectingDynamicData> ReceiveDynamicDataAsync()
        {
            if (StaticData == null)
            {
                throw new InvalidOperationException("Cannot start receiving dynamic data before receiving the static data.");
            }
            await StaticData;

            //Confirm that static part received and started listening for commands.
            await Communication.TCPSendACKAsync(serverConnection);

            Console.WriteLine("Sent server ACK.");
            var dynData = ConnectingDynamicData.Decode(await Communication.TCPReceiveMessageAsync(serverConnection));

            Console.WriteLine("Received dynamic data.");
            serverConnection.Shutdown(SocketShutdown.Send);
            Console.WriteLine("Successfully connected to the server.");
            serverConnection.Close();
            serverConnection = null;
            return(dynData);
        }
Пример #3
0
        /// <summary>
        /// Starts listesting for reliable server updates, any received commands are pushed into serverCommands queue.
        /// </summary>
        /// <param name="listenOn">local IP+port on which should the client listen for the reliable updates.</param>
        /// <returns>when server ends the connection or <paramref name="active"/>'' is set.</returns>
        async Task ListenForReliableServerUpdatesAsync(IPEndPoint listenOn)
        {
            using (var listener = new Socket(serverUpdateAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
            {
                listener.Bind(listenOn);
                listener.Listen(5);

                Console.WriteLine($"Started listening for reliable server updates on {listenOn}");
                relUpdatesFromServer = await Task.Factory.FromAsync(listener.BeginAccept, listener.EndAccept, null);

                Console.WriteLine($"Established connection for reliable server updates");
            }
            while (active)
            {
                var bytes = await Communication.TCPReceiveMessageAsync(relUpdatesFromServer);

                var newLastProcessedCUID = Serialization.DecodeInt(bytes, 0);
                var sUpdate = ServerUpdate.Decode(bytes, 4);

                lock (this)
                {
                    //Only add newer updates.
                    if (newLastProcessedCUID >= LastProcessedClientUpdateID)
                    {
                        if (sUpdate is CmdServerUpdate)                        //Contains command
                        {
                            AddServerCommand((sUpdate as CmdServerUpdate).Cmd);
                        }
                        UpdLastProcCUID(newLastProcessedCUID);
                    }
                }
            }

            relUpdatesFromServer.Shutdown(SocketShutdown.Both);
            relUpdatesFromServer.Close();
        }