public static void HandleVesselRemoval(ClientObject client, byte[] messageData) { using (MessageReader mr = new MessageReader(messageData)) { //Don't care about the subspace on the server. mr.Read<double>(); string vesselID = mr.Read<string>(); bool isDockingUpdate = mr.Read<bool>(); if (!isDockingUpdate) { DarkLog.Debug("Removing vessel " + vesselID + " from " + client.playerName); } else { DarkLog.Debug("Removing DOCKED vessel " + vesselID + " from " + client.playerName); } if (File.Exists(Path.Combine(Server.universeDirectory, "Vessels", vesselID + ".txt"))) { lock (Server.universeSizeLock) { File.Delete(Path.Combine(Server.universeDirectory, "Vessels", vesselID + ".txt")); } } //Relay the message. ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.VESSEL_REMOVE; newMessage.data = messageData; ClientHandler.SendToAll(client, newMessage, false); } }
public static void HandlePlayerColor(ClientObject client, byte[] messageData) { using (MessageReader mr = new MessageReader(messageData)) { PlayerColorMessageType messageType = (PlayerColorMessageType)mr.Read<int>(); switch (messageType) { case PlayerColorMessageType.SET: { string playerName = mr.Read<string>(); if (playerName != client.playerName) { DarkLog.Debug(client.playerName + " tried to send a color update for " + playerName + ", kicking."); Messages.ConnectionEnd.SendConnectionEnd(client, "Kicked for sending a color update for another player"); return; } client.playerColor = mr.Read<float[]>(); //Relay the message ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.PLAYER_COLOR; newMessage.data = messageData; ClientHandler.SendToAll(client, newMessage, true); } break; } } }
public static void SendScenarioModules(ClientObject client) { int numberOfScenarioModules = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Scenarios", client.playerName)).Length; int currentScenarioModule = 0; string[] scenarioNames = new string[numberOfScenarioModules]; byte[][] scenarioDataArray = new byte[numberOfScenarioModules][]; foreach (string file in Directory.GetFiles(Path.Combine(Server.universeDirectory, "Scenarios", client.playerName))) { //Remove the .txt part for the name scenarioNames[currentScenarioModule] = Path.GetFileNameWithoutExtension(file); scenarioDataArray[currentScenarioModule] = File.ReadAllBytes(file); currentScenarioModule++; } ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.SCENARIO_DATA; using (MessageWriter mw = new MessageWriter()) { mw.Write<string[]>(scenarioNames); foreach (byte[] scenarioData in scenarioDataArray) { if (client.compressionEnabled) { mw.Write<byte[]>(Compression.CompressIfNeeded(scenarioData)); } else { mw.Write<byte[]>(Compression.AddCompressionHeader(scenarioData, false)); } } newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, true); }
public static void SendAllPlayerColors(ClientObject client) { Dictionary<string,float[]> sendColors = new Dictionary<string, float[]>(); foreach (ClientObject otherClient in ClientHandler.GetClients()) { if (otherClient.authenticated && otherClient.playerColor != null) { if (otherClient != client) { sendColors[otherClient.playerName] = otherClient.playerColor; } } } ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.PLAYER_COLOR; using (MessageWriter mw = new MessageWriter()) { mw.Write<int>((int)PlayerColorMessageType.LIST); mw.Write<int>(sendColors.Count); foreach (KeyValuePair<string, float[]> kvp in sendColors) { mw.Write<string>(kvp.Key); mw.Write<float[]>(kvp.Value); } newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, true); }
public static void HandlePingRequest(ClientObject client, byte[] messageData) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.PING_REPLY; newMessage.data = messageData; ClientHandler.SendToClient(client, newMessage, true); }
public static void RunNukeKSC(string commandText) { lock (Server.universeSizeLock) { string[] vesselList = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Vessels")); int numberOfRemovals = 0; foreach (string vesselFile in vesselList) { string vesselID = Path.GetFileNameWithoutExtension(vesselFile); bool landedAtKSC = false; bool landedAtRunway = false; using (StreamReader sr = new StreamReader(vesselFile)) { string currentLine = sr.ReadLine(); while (currentLine != null && !landedAtKSC && !landedAtRunway) { string trimmedLine = currentLine.Trim(); if (trimmedLine.StartsWith("landedAt = ")) { string landedAt = trimmedLine.Substring(trimmedLine.IndexOf("=") + 2); if (landedAt == "KSC") { landedAtKSC = true; } if (landedAt == "Runway") { landedAtRunway = true; } } currentLine = sr.ReadLine(); } } if (landedAtKSC | landedAtRunway) { DarkLog.Normal("Removing vessel: " + vesselID); //Delete it from the universe if (File.Exists(vesselFile)) { File.Delete(vesselFile); } //Send a vessel remove message ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.VESSEL_REMOVE; using (MessageWriter mw = new MessageWriter()) { //Send it with a delete time of 0 so it shows up for all players. mw.Write<int>(0); mw.Write<double>(0); mw.Write<string>(vesselID); mw.Write<bool>(false); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToAll(null, newMessage, false); numberOfRemovals++; } } DarkLog.Normal("Nuked " + numberOfRemovals + " vessels around the KSC"); } }
private static void SendKerbalsComplete(ClientObject client) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.KERBAL_COMPLETE; ClientHandler.SendToClient(client, newMessage, false); //Send vessel list needed for sync to the client VesselRequest.SendVesselList(client); }
public static void HandleVesselUpdate(ClientObject client, byte[] messageData) { //We only relay this message. ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.VESSEL_UPDATE; newMessage.data = messageData; ClientHandler.SendToAll(client, newMessage, false); }
public static void SendConnectionEnd(ClientObject client, string reason) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.CONNECTION_END; using (MessageWriter mw = new MessageWriter()) { mw.Write<string>(reason); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, true); }
public static void SendDMPModMessageToAll(ClientObject excludeClient, string modName, byte[] messageData, bool highPriority) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.MOD_DATA; using (MessageWriter mw = new MessageWriter()) { mw.Write<string>(modName); mw.Write<byte[]>(messageData); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToAll(excludeClient, newMessage, highPriority); }
public static void SendConsoleMessageToClient(ClientObject client, string message) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.CHAT_MESSAGE; using (MessageWriter mw = new MessageWriter()) { mw.Write<int>((int)ChatMessageType.CONSOLE_MESSAGE); mw.Write<string>(message); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, false); }
public static void SendAllAdmins(ClientObject client) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.ADMIN_SYSTEM; using (MessageWriter mw = new MessageWriter()) { mw.Write((int)AdminMessageType.LIST); mw.Write<string[]>(DarkMultiPlayerServer.AdminSystem.fetch.GetAdmins()); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, true); }
public static void SendChatMessageToClient(ClientObject client, string messageText) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.CHAT_MESSAGE; using (MessageWriter mw = new MessageWriter()) { mw.Write<int>((int)ChatMessageType.PRIVATE_MESSAGE); mw.Write<string>(Settings.settingsStore.consoleIdentifier); mw.Write<string>(client.playerName); mw.Write(messageText); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, true); }
public static void SendHandshakeChallange(ClientObject client) { client.challange = new byte[1024]; Random rand = new Random(); rand.NextBytes(client.challange); ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.HANDSHAKE_CHALLANGE; using (MessageWriter mw = new MessageWriter()) { mw.Write<byte[]>(client.challange); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, true); }
private static void SendKerbal(ClientObject client, string kerbalName, byte[] kerbalData) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.KERBAL_REPLY; using (MessageWriter mw = new MessageWriter()) { //Send the vessel with a send time of 0 so it instantly loads on the client. mw.Write<double>(0); mw.Write<string>(kerbalName); mw.Write<byte[]>(kerbalData); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, false); }
public static void SendChatMessageToChannel(string channel, string messageText) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.CHAT_MESSAGE; using (MessageWriter mw = new MessageWriter()) { mw.Write<int>((int)ChatMessageType.CHANNEL_MESSAGE); mw.Write<string>(Settings.settingsStore.consoleIdentifier); // Channel mw.Write<string>(channel); mw.Write(messageText); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToAll(null, newMessage, true); }
public static void SendChatMessageToAll(string messageText) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.CHAT_MESSAGE; using (MessageWriter mw = new MessageWriter()) { mw.Write<int>((int)ChatMessageType.CHANNEL_MESSAGE); mw.Write<string>("Server"); //Global channel mw.Write<string>(""); mw.Write(messageText); newMessage.data = mw.GetMessageBytes(); } SendToAll(null, newMessage, true); }
private static void SendMotdReply(ClientObject client) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.MOTD_REPLY; string newMotd = Settings.settingsStore.serverMotd; newMotd = newMotd.Replace("%name%", client.playerName); newMotd = newMotd.Replace(@"\n", Environment.NewLine); using (MessageWriter mw = new MessageWriter()) { mw.Write<string>(newMotd); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, true); }
public static void HandleSyncTimeRequest(ClientObject client, byte[] messageData) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.SYNC_TIME_REPLY; using (MessageWriter mw = new MessageWriter()) { using (MessageReader mr = new MessageReader(messageData)) { //Client send time mw.Write<long>(mr.Read<long>()); //Server receive time mw.Write<long>(DateTime.UtcNow.Ticks); newMessage.data = mw.GetMessageBytes(); } } ClientHandler.SendToClient(client, newMessage, true); }
public static void SendAllLocks(ClientObject client) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.LOCK_SYSTEM; //Send the dictionary as 2 string[]'s. Dictionary<string,string> lockList = DarkMultiPlayerServer.LockSystem.fetch.GetLockList(); List<string> lockKeys = new List<string>(lockList.Keys); List<string> lockValues = new List<string>(lockList.Values); using (MessageWriter mw = new MessageWriter()) { mw.Write((int)LockMessageType.LIST); mw.Write<string[]>(lockKeys.ToArray()); mw.Write<string[]>(lockValues.ToArray()); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, true); }
public static void SendVesselList(ClientObject client) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.VESSEL_LIST; string[] vesselFiles = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Vessels")); string[] vesselObjects = new string[vesselFiles.Length]; for (int i = 0; i < vesselFiles.Length; i++) { vesselObjects[i] = Common.CalculateSHA256Hash(vesselFiles[i]); } using (MessageWriter mw = new MessageWriter()) { mw.Write<string[]>(vesselObjects); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, false); }
public static void SendServerSettings(ClientObject client) { int numberOfKerbals = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Kerbals")).Length; int numberOfVessels = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Vessels")).Length; int numberOfScenarioModules = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Scenarios", client.playerName)).Length; ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.SERVER_SETTINGS; using (MessageWriter mw = new MessageWriter()) { mw.Write<int>((int)Settings.settingsStore.warpMode); mw.Write<int>((int)Settings.settingsStore.gameMode); mw.Write<bool>(Settings.settingsStore.cheats); //Tack the amount of kerbals, vessels and scenario modules onto this message mw.Write<int>(numberOfKerbals); mw.Write<int>(numberOfVessels); //mw.Write<int>(numberOfScenarioModules); mw.Write<int>(Settings.settingsStore.screenshotHeight); mw.Write<int>(Settings.settingsStore.numberOfAsteroids); mw.Write<string>(Settings.settingsStore.consoleIdentifier); mw.Write<int>((int)Settings.settingsStore.gameDifficulty); mw.Write<float>(Settings.settingsStore.safetyBubbleDistance); if (Settings.settingsStore.gameDifficulty == GameDifficulty.CUSTOM) { mw.Write<bool>(GameplaySettings.settingsStore.allowStockVessels); mw.Write<bool>(GameplaySettings.settingsStore.autoHireCrews); mw.Write<bool>(GameplaySettings.settingsStore.bypassEntryPurchaseAfterResearch); mw.Write<bool>(GameplaySettings.settingsStore.indestructibleFacilities); mw.Write<bool>(GameplaySettings.settingsStore.missingCrewsRespawn); mw.Write<float>(GameplaySettings.settingsStore.reentryHeatScale); mw.Write<float>(GameplaySettings.settingsStore.resourceAbundance); mw.Write<float>(GameplaySettings.settingsStore.fundsGainMultiplier); mw.Write<float>(GameplaySettings.settingsStore.fundsLossMultiplier); mw.Write<float>(GameplaySettings.settingsStore.repGainMultiplier); mw.Write<float>(GameplaySettings.settingsStore.repLossMultiplier); mw.Write<float>(GameplaySettings.settingsStore.scienceGainMultiplier); mw.Write<float>(GameplaySettings.settingsStore.startingFunds); mw.Write<float>(GameplaySettings.settingsStore.startingReputation); mw.Write<float>(GameplaySettings.settingsStore.startingScience); } newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, true); }
public static void HandlePlayerStatus(ClientObject client, byte[] messageData) { using (MessageReader mr = new MessageReader(messageData)) { string playerName = mr.Read<string>(); if (playerName != client.playerName) { DarkLog.Debug(client.playerName + " tried to send an update for " + playerName + ", kicking."); Messages.ConnectionEnd.SendConnectionEnd(client, "Kicked for sending an update for another player"); return; } client.playerStatus.vesselText = mr.Read<string>(); client.playerStatus.statusText = mr.Read<string>(); } //Relay the message ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.PLAYER_STATUS; newMessage.data = messageData; ClientHandler.SendToAll(client, newMessage, false); }
public static void HandleKerbalProto(ClientObject client, byte[] messageData) { //Send kerbal using (MessageReader mr = new MessageReader(messageData)) { //Don't care about subspace / send time. mr.Read<double>(); string kerbalName = mr.Read<string>(); DarkLog.Debug("Saving kerbal " + kerbalName + " from " + client.playerName); byte[] kerbalData = mr.Read<byte[]>(); lock (Server.universeSizeLock) { File.WriteAllBytes(Path.Combine(Server.universeDirectory, "Kerbals", kerbalName + ".txt"), kerbalData); } } ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.KERBAL_REPLY; newMessage.data = messageData; ClientHandler.SendToAll(client, newMessage, false); }
public static void HandleKerbalRemoval(ClientObject client, byte[] messageData) { using (MessageReader mr = new MessageReader(messageData)) { //Don't care about the subspace on the server. mr.Read<double>(); string kerbalName = mr.Read<string>(); DarkLog.Debug("Removing kerbal " + kerbalName + " from " + client.playerName); if (File.Exists(Path.Combine(Server.universeDirectory, "Kerbals", kerbalName + ".txt"))) { lock (Server.universeSizeLock) { File.Delete(Path.Combine(Server.universeDirectory, "Kerbals", kerbalName + ".txt")); } } //Relay the message. ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.KERBAL_REMOVE; newMessage.data = messageData; ClientHandler.SendToAll(client, newMessage, false); } }
public static void SendDMPModMessageToClient(ClientObject client, string modName, byte[] messageData, bool highPriority) { if (modName == null) { //Now that's just being silly :) return; } if (messageData == null) { DarkLog.Debug(modName + " attemped to send a null message"); return; } ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.MOD_DATA; using (MessageWriter mw = new MessageWriter()) { mw.Write<string>(modName); mw.Write<byte[]>(messageData); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage , highPriority); }
public static void SendAllPlayerStatus(ClientObject client) { foreach (ClientObject otherClient in ClientHandler.GetClients()) { if (otherClient.authenticated) { if (otherClient != client) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.PLAYER_STATUS; using (MessageWriter mw = new MessageWriter()) { mw.Write<string>(otherClient.playerName); mw.Write<string>(otherClient.playerStatus.vesselText); mw.Write<string>(otherClient.playerStatus.statusText); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, true); } } } }
public static void SendAllReportedSkewRates(ClientObject client) { foreach (ClientObject otherClient in ClientHandler.GetClients()) { if (otherClient.authenticated) { if (otherClient != client) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.WARP_CONTROL; using (MessageWriter mw = new MessageWriter()) { mw.Write<int>((int)WarpMessageType.REPORT_RATE); mw.Write<string>(otherClient.playerName); mw.Write<float>(otherClient.subspaceRate); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToClient(client, newMessage, true); } } } }
public static void HandleKerbalsRequest(ClientObject client) { //The time sensitive SYNC_TIME is over by this point. using (MessageWriter mw = new MessageWriter()) { mw.Write<string>(client.playerName); ServerMessage joinMessage = new ServerMessage(); joinMessage.type = ServerMessageType.PLAYER_JOIN; joinMessage.data = mw.GetMessageBytes(); ClientHandler.SendToAll(client, joinMessage, true); } Messages.ServerSettings.SendServerSettings(client); Messages.WarpControl.SendSetSubspace(client); Messages.WarpControl.SendAllSubspaces(client); Messages.PlayerColor.SendAllPlayerColors(client); Messages.PlayerStatus.SendAllPlayerStatus(client); Messages.ScenarioData.SendScenarioModules(client); Messages.WarpControl.SendAllReportedSkewRates(client); Messages.CraftLibrary.SendCraftList(client); Messages.Chat.SendPlayerChatChannels(client); Messages.LockSystem.SendAllLocks(client); Messages.AdminSystem.SendAllAdmins(client); //Send kerbals lock (Server.universeSizeLock) { string[] kerbalFiles = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Kerbals")); foreach (string kerbalFile in kerbalFiles) { string kerbalName = Path.GetFileNameWithoutExtension(kerbalFile); byte[] kerbalData = File.ReadAllBytes(kerbalFile); SendKerbal(client, kerbalName, kerbalData); } DarkLog.Debug("Sending " + client.playerName + " " + kerbalFiles.Length + " kerbals..."); } SendKerbalsComplete(client); }
private static void DisconnectClient(ClientObject client) { lock (client.disconnectLock) { if (client.connectionStatus != ConnectionStatus.DISCONNECTED) { DMPPluginHandler.FireOnClientDisconnect(client); if (client.playerName != null) { if (playerChatChannels.ContainsKey(client.playerName)) { playerChatChannels.Remove(client.playerName); } if (playerDownloadedScreenshotIndex.ContainsKey(client.playerName)) { playerDownloadedScreenshotIndex.Remove(client.playerName); } if (playerUploadedScreenshotIndex.ContainsKey(client.playerName)) { playerUploadedScreenshotIndex.Remove(client.playerName); } if (playerWatchScreenshot.ContainsKey(client.playerName)) { playerWatchScreenshot.Remove(client.playerName); } } client.connectionStatus = ConnectionStatus.DISCONNECTED; if (client.authenticated) { ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.PLAYER_DISCONNECT; using (MessageWriter mw = new MessageWriter()) { mw.Write<string>(client.playerName); newMessage.data = mw.GetMessageBytes(); } SendToAll(client, newMessage, true); lockSystem.ReleasePlayerLocks(client.playerName); } deleteClients.Enqueue(client); try { if (client.connection != null) { client.connection.GetStream().Close(); client.connection.Close(); } } catch (Exception e) { DarkLog.Debug("Error closing client connection: " + e.Message); } Server.lastPlayerActivity = Server.serverClock.ElapsedMilliseconds; } } }