/// <summary> /// This method is used to parse an incoming message on the client /// and execute the appropriate actions. /// </summary> /// <param name="reader">The incoming packet including the command type byte.</param> /// <param name="peer">The peer object of the sending client.</param> /// <returns>If the command should be forwarded to other clients.</returns> public static bool Parse(NetPacketReader reader, NetPeer peer) { Parse(reader, out CommandHandler handler, out CommandBase cmd); if (handler == null) { return(false); } // Handle connection request as special case if (cmd.GetType() == typeof(ConnectionRequestCommand)) { ((ConnectionRequestHandler)handler).HandleOnServer((ConnectionRequestCommand)cmd, peer); return(false); } // Make sure we know about the connected client on the server if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Server && !MultiplayerManager.Instance.CurrentServer.ConnectedPlayers.ContainsKey(peer.Id)) { _logger.Warn("Client tried to send packet but never joined with a ConnectionRequestCommand. Ignoring..."); return(false); } if (TransactionHandler.CheckReceived(handler, cmd)) { return(handler.RelayOnServer); } handler.Parse(cmd); return(handler.RelayOnServer); }
/// <summary> /// This method is used to send a command to the server. /// Does only work if the current game acts as a client. /// </summary> /// <param name="command">The command to send.</param> public static void SendToServer(CommandBase command) { if (MultiplayerManager.Instance.CurrentClient.Status == ClientStatus.Disconnected) { return; } TransactionHandler.StartTransaction(command); SetSenderId(command); MultiplayerManager.Instance.CurrentClient.SendToServer(command); }
/// <summary> /// This method is used to send a command to all connected clients. /// Does only work if the current game acts as a server. /// </summary> /// <param name="command">The command to send.</param> public static void SendToClients(CommandBase command) { if (MultiplayerManager.Instance.CurrentRole != MultiplayerRole.Server) { return; } TransactionHandler.StartTransaction(command); SetSenderId(command); MultiplayerManager.Instance.CurrentServer.SendToClients(command); }
/// <summary> /// This method is used to send a command to a connected partner. /// If the current game acts as a server, the command is sent to all clients. /// If it acts as a client, the command is sent to the server. /// </summary> /// <param name="command">The command to send.</param> public static void SendToAll(CommandBase command) { // Check if this command belongs to a transaction TransactionHandler.CheckSendTransaction(command); if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Client) { SendToServer(command); } else if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Server) { SendToClients(command); } }
/// <summary> /// This method is used to parse an incoming message on the client /// and execute the appropriate actions. /// </summary> /// <param name="reader">The incoming packet including the command type byte.</param> public static void ParseOnClient(NetPacketReader reader) { Parse(reader, out CommandHandler handler, out byte[] message); if (handler == null) { return; } if (TransactionHandler.CheckReceived(handler, message, null)) { return; } handler.ParseOnClient(message); }