public void Send(NetUtil.PacketType type, params string[] data) { JObject packet = new JObject { { "type", type.ToString() }, { "data", new JArray(data) }, }; // Convert the string data to byte data using ASCII encoding. byte[] byteData = Encoding.ASCII.GetBytes(packet.ToString()); // Begin sending the data to the remote device. clientSocket.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), clientSocket); }
private void TreatMessage(NetUtil.PacketType type, string[] messages) { switch (type) { case NetUtil.PacketType.KERNL: if (messages.Length > 0) { userScreen.HandleKernel(messages); } break; case NetUtil.PacketType.MESSG: if (messages.Length > 0) { string printMessage = messages[0]; userScreen.Write(printMessage); } break; case NetUtil.PacketType.LOGRE: if (messages.Length > 0) { if (messages[0] == "0") // LOGRE:0 = You're logged in { MainMenu.loginState = MainMenu.LoginState.LOGGED; } else if (messages[0] == "1") // LOGRE:1 = Invalid account { MainMenu.loginState = MainMenu.LoginState.INVALID; } } break; case NetUtil.PacketType.START: userScreen.homeIP = messages[0]; break; case NetUtil.PacketType.OSMSG: break; case NetUtil.PacketType.FX: userScreen.HandleFX(messages); break; default: throw new InvalidOperationException($"Netmanager attempted treat message with invalid type { type.ToString() }"); } }
private void TreatMessage(NetUtil.PacketType type, string[] messages) { switch (type) { case NetUtil.PacketType.KERNL: if (messages.Length > 0) { userScreen.HandleKernel(messages); } break; case NetUtil.PacketType.MESSG: if (messages.Length > 0) { string printMessage = messages[0]; userScreen.Write(printMessage); } break; case NetUtil.PacketType.LOGRE: if (messages.Length > 0) { if (messages[0] == "0") // LOGRE:0 = You're logged in { MainMenu.loginState = MainMenu.LoginState.LOGGED; } else if (messages[0] == "1") // LOGRE:1 = Invalid account { MainMenu.loginState = MainMenu.LoginState.INVALID; } else if (messages[0] == "2") // LOGRE:2 = The server rejected your connection for some reason (ban?) { if (string.IsNullOrWhiteSpace(messages[1]) == false) { MainMenu.serverRejectReason = messages[1]; } MainMenu.loginState = MainMenu.LoginState.SERVER_REJECTED; } } break; case NetUtil.PacketType.START: userScreen.homeIP = messages[0]; nodesToSync = messages[1]; gotNodes = true; break; case NetUtil.PacketType.OSMSG: break; case NetUtil.PacketType.FX: userScreen.HandleFX(messages); break; case NetUtil.PacketType.MUSIC: if (messages.Length > 0) { if (messages[1] == "1") { Hacknet.MusicManager.playSongImmediatley(messages[0]); } else { Hacknet.MusicManager.transitionToSong(messages[0]); } } break; case NetUtil.PacketType.DSCON: Disconnect(true, messages[0]); disconnectHandled = true; break; default: throw new InvalidOperationException($"Netmanager attempted treat message with invalid type { type.ToString() }"); } }
private void TreatMessage(NetUtil.PacketType type, string[] messages) { switch (type) { case NetUtil.PacketType.KERNL: if (messages.Length > 0) { userScreen.HandleKernel(messages); } break; case NetUtil.PacketType.MESSG: if (messages.Length > 0) { string printMessage = messages[0]; userScreen.Write(printMessage); } break; case NetUtil.PacketType.LOGRE: if (messages.Length > 0) { if (messages[0] == "0") // LOGRE:0 = You're logged in { MainMenu.loginState = MainMenu.LoginState.LOGGED; } else if (messages[0] == "1") // LOGRE:1 = Invalid account { MainMenu.loginState = MainMenu.LoginState.INVALID; } else if (messages[0] == "2") // LOGRE:2 = The server rejected your connection for some reason (ban?) { if (string.IsNullOrWhiteSpace(messages[1]) == false) { MainMenu.serverRejectReason = messages[1]; } MainMenu.loginState = MainMenu.LoginState.SERVER_REJECTED; } } break; case NetUtil.PacketType.START: userScreen.homeIP = messages[0]; nodesToSync = messages[1]; gotNodes = true; break; case NetUtil.PacketType.OSMSG: break; case NetUtil.PacketType.FX: userScreen.HandleFX(messages); break; case NetUtil.PacketType.MUSIC: if (messages.Length > 0) { string songLocation = $"Mods\\HNMPMusic\\{messages[0]}.wav"; if (messages[0] == "shuffle") { if (Directory.Exists("Mods\\HNMPMusic")) { string[] files = Directory.GetFiles("Mods\\HNMPMusic"); List <string> songFiles = new List <string>(); foreach (string file in files) { if (Path.GetExtension(file) == ".wav") { songFiles.Add(file); } } MusicManager.Shuffle(songFiles); } else { userScreen.Write("Mods\\HNMPMusic does not exist."); } } else if (messages[0] == "list") { if (Directory.Exists("Mods\\HNMPMusic")) { string[] files = Directory.GetFiles("Mods\\HNMPMusic"); List <string> songFiles = new List <string>(); foreach (string file in files) { if (Path.GetExtension(file) == ".wav") { songFiles.Add(file); } } int i = 0; List <string> names = new List <string>(); foreach (string name in songFiles) { int index = name.LastIndexOf('\\'); names.Add(name.Substring(index + 1).Replace(".wav", "")); i++; } StringBuilder builder = new StringBuilder(); builder.Append(" \nYour Music Library:\n \n"); foreach (string name in names) { builder.Append(($"{name}\n")); } userScreen.Write(builder.ToString()); } else { userScreen.Write("Mods\\HNMPMusic does not exist."); } } else if (messages[0] == "stop") { MusicManager.Stop(); userScreen.Write("The music has been stopped!"); } else { if (File.Exists(songLocation)) { MusicManager.Play(songLocation); } else { userScreen.Write($"\"{songLocation}\" does not exist."); } } } break; case NetUtil.PacketType.DSCON: Disconnect(true, messages[0]); disconnectHandled = true; break; default: throw new InvalidOperationException($"Netmanager attempted treat message with invalid type { type.ToString() }"); } }