Esempio n. 1
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
            {
                // Handle ConnectionRequest as special case
                if (reader.Data[0] == 0)
                {
                    Command.Parse(reader.Data, out CommandHandler handler, out byte[] message);
                    ConnectionRequestHandler requestHandler = (ConnectionRequestHandler)handler;
                    requestHandler.HandleOnServer(message, peer);
                    return;
                }

                // Parse this message
                ConnectedPlayers.TryGetValue(peer.ConnectId, out Player player);
                Command.ParseOnServer(reader.Data, player);

                // Send this message to all other clients
                var peers = _netServer.GetPeers();
                foreach (var client in peers)
                {
                    // Don't send the message back to the client that sent it.
                    if (client.ConnectId == peer.ConnectId)
                    {
                        continue;
                    }

                    // Send the message so the other client can stay in sync
                    client.Send(reader.Data, SendOptions.ReliableOrdered);
                }
            }
            catch (Exception ex)
            {
                CSM.Log($"Encountered an error from {peer.EndPoint.Host}:{peer.EndPoint.Port} while reading command. Message: {ex.Message}");
            }
        }
Esempio n. 2
0
 /// <summary>
 ///     Called whenever an error happens, we
 ///     log this to the console for now.
 /// </summary>
 private void ListenerOnNetworkErrorEvent(NetEndPoint endpoint, int socketerrorcode)
 {
     CSM.Log($"Received an error from {endpoint.Host}:{endpoint.Port}. Code: {socketerrorcode}");
 }
Esempio n. 3
0
 public void HandlePlayerConnect(Player player)
 {
     CSM.Log($"Player {player.Username} has connected!");
     MultiplayerManager.Instance.PlayerList.Add(player.Username);
     Command.HandleClientConnect(player);
 }
Esempio n. 4
0
 public override void HandleOnClient(ClientConnectCommand command)
 {
     CSM.Log($"Player {command.Username} has connected!");
     MultiplayerManager.Instance.PlayerList.Add(command.Username);
 }
Esempio n. 5
0
        /// <summary>
        ///     Attempt to connect to a server
        /// </summary>
        /// <param name="clientConfig">Client config params</param>
        /// <returns>True if the client is connected to the server, false if not</returns>
        public bool Connect(ClientConfig clientConfig)
        {
            // if we are currently trying to connect, cancel
            // and try again.
            if (Status == ClientStatus.Connecting)
            {
                Disconnect();
            }

            // The client is already connected so we need to
            // disconnect.
            if (Status == ClientStatus.Connected)
            {
                Disconnect();
            }

            // Set the config
            _clientConfig = clientConfig;

            // Let the user know that we are trying to connect to a server
            CSM.Log($"Attempting to connect to server at {_clientConfig.HostAddress}:{_clientConfig.Port}...");

            // Start the client, if client setup fails, return out and
            // tell the user
            var result = _netClient.Start();

            if (!result)
            {
                ConnectionMessage = "The client failed to start.";
                Disconnect(); // make sure we are fully disconnected
                return(false);
            }

            // Try connect to server, update the status to say that
            // we are trying to connect.
            _netClient.Connect(_clientConfig.HostAddress, _clientConfig.Port);

            // Start processing networking
            Status = ClientStatus.Connecting;

            // Setup processing thread
            _clientProcessingThread = new Thread(ProcessEvents);
            _clientProcessingThread.Start();

            // We need to wait in a loop for 30 seconds (waiting 500ms each time)
            // while we wait for a successful connection (Status = Connected) or a
            // failed connection (Status = Disconnected).
            var waitWatch = new Stopwatch();

            waitWatch.Start();

            while (waitWatch.Elapsed < TimeSpan.FromSeconds(30))
            {
                // If we connect, exit the loop and return true
                if (Status == ClientStatus.Connected)
                {
                    return(true);
                }

                // The client cannot connect for some reason, the ConnectionMessage
                // variable will contain why.
                if (Status == ClientStatus.Disconnected)
                {
                    Disconnect(); // make sure we are fully disconnected
                    return(false);
                }

                // Wait 500ms
                Thread.Sleep(500);
            }

            // We have timed out
            ConnectionMessage = "Could not connect to server, timed out.";

            // Did not connect
            Disconnect(); // make sure we are fully disconnected
            return(false);
        }