Beispiel #1
0
        /// <summary>
        ///     When we get a message from the server, we handle the message here
        ///     and perform any necessary tasks.
        /// </summary>
        private void ListenerOnNetworkReceiveEvent(NetPeer peer, NetDataReader reader)
        {
            try
            {
                // The message type is the first byte, (255 message types)
                var messageType = reader.Data[0];

                // Skip the first byte
                var message = reader.Data.Skip(1).ToArray();

                // Switch between all the messages
                switch (messageType)
                {
                case CommandBase.ConnectionResultCommandId:
                    // We only want this message while connecting
                    if (Status != ClientStatus.Connecting)
                    {
                        break;
                    }

                    // Get the result
                    var connectionResult = ConnectionResultCommand.Deserialize(message);

                    if (connectionResult.Success)
                    {
                        // Log and set that we are connected.
                        CSM.Log($"Successfully connected to server at {peer.EndPoint.Host}:{peer.EndPoint.Port}.");
                        Status = ClientStatus.Connected;
                    }
                    else
                    {
                        CSM.Log($"Could not connect to server at {peer.EndPoint.Host}:{peer.EndPoint.Port}. Disconnecting... Error Message: {connectionResult.Reason}");
                        ConnectionMessage = $"Could not connect to server at {peer.EndPoint.Host}:{peer.EndPoint.Port}. Disconnecting... Error Message: {connectionResult.Reason}";
                        Disconnect();
                    }
                    break;

                // Handle ping commands by returning the ping
                case CommandBase.PingCommandId:
                    // Update the last server ping
                    _lastServerPing = DateTime.UtcNow;
                    // Send back a ping event
                    peer.Send(ArrayHelpers.PrependByte(CommandBase.PingCommandId, new PingCommand().Serialize()), SendOptions.ReliableOrdered);
                    break;

                case CommandBase.SimulationCommandID:
                    var simulation = SimulationCommand.Deserialize(message);

                    SimulationManager.instance.SimulationPaused        = simulation.SimulationPaused;
                    SimulationManager.instance.SelectedSimulationSpeed = simulation.SelectedSimulationSpeed;
                    break;
                }
            }
            catch (Exception ex)
            {
                CSM.Log($"Received an error from {peer.EndPoint.Host}:{peer.EndPoint.Port}. Message: {ex.Message}");
            }
        }
Beispiel #2
0
        /// <summary>
        ///     When we get a message from a client, we handle the message here
        ///     and perform any necessary tasks.
        /// </summary>
        private void ListenerOnNetworkReceiveEvent(NetPeer peer, NetDataReader reader)
        {
            try
            {
                // The message type is the first byte, (255 message types)
                var messageType = reader.Data[0];

                // Skip the first byte
                var message = reader.Data.Skip(1).ToArray();

                // Switch between all the messages
                switch (messageType)
                {
                case CommandBase.ConnectionRequestCommandId:

                    var connectionResult = Commands.ConnectionRequestCommand.Deserialize(message);

                    CSM.Log($"Connection request from {peer.EndPoint.Host}:{peer.EndPoint.Port}. Version: {connectionResult.GameVersion}, ModCount: {connectionResult.ModCount}, ModVersion: {connectionResult.ModVersion}");

                    // TODO, check these values, but for now, just accept the request.
                    SendToClient(peer, CommandBase.ConnectionResultCommandId, new ConnectionResultCommand {
                        Success = true
                    });
                    break;

                case CommandBase.SimulationCommandID:
                    var simulation = SimulationCommand.Deserialize(message);

                    SimulationManager.instance.SimulationPaused        = simulation.SimulationPaused;
                    SimulationManager.instance.SelectedSimulationSpeed = simulation.SelectedSimulationSpeed;
                    break;
                }
            }
            catch (Exception ex)
            {
                CSM.Log($"Received an error from {peer.EndPoint.Host}:{peer.EndPoint.Port}. Message: {ex.Message}");
            }
        }