public static async Task <Packet> ReadFromStreamAsync(NetworkStream stream) { Packet packet = new Packet(); byte[] sizeBuffer = new byte[4]; stream.Read(sizeBuffer, 0, 4); packet.PacketSize = BitConverter.ToInt32(sizeBuffer, 0); byte[] packetBuffer = new byte[packet.PacketSize]; if (packet.PacketSize != 0) { int readProgress = 0; while (readProgress < packet.PacketSize) { int read = await stream.ReadAsync(packetBuffer, readProgress, packet.PacketSize - readProgress); readProgress += read; if (read == 0) { throw new Exception("NetworkStream failed to read data. Connection may have been lost!"); } } } packet.Opcode = (Opcode)BitConverter.ToInt32(packetBuffer, 0); packet.Type = (PacketType)BitConverter.ToInt32(packetBuffer, 4); Array.Copy(packetBuffer, 8, packet.Data, 0, packet.DataSize); Console.WriteLine("Recieved {0}", packet.DataAsString()); return(packet); }
private void OnGetChatMessage(Packet packet) { var message = packet.DataAsString(); if (packet.Opcode == Opcode.ChatMessage) { GotChatResponse = true; if (message.Trim() == "Server received, But no response!!") { return; } string[] messages = message.Split('\n'); foreach (string newMessage in messages) { if (string.IsNullOrWhiteSpace(newMessage)) { continue; } string[] splitMessage = newMessage.Split(new char[] { ':' }, 2); if (newMessage.StartsWith("SERVER:") && newMessage == "SERVER: " + LastSentAdminMessage) { if (SentMessageUpdated != null) { var chatLog = new ChatLogEventArgs() { Timestamp = packet.Timestamp, IsAdmin = true }; chatLog.Message = newMessage.Replace("SERVER: ", ""); SentMessageUpdated(this, chatLog); } } else { if (ChatLogUpdated != null) { var chatLog = new ChatLogEventArgs() { Message = splitMessage[1], Sender = splitMessage[0], Timestamp = packet.Timestamp, IsAdmin = false }; ChatLogUpdated(this, chatLog); } } } } }
private void OnScheduledTask(Packet packet) { var message = packet.DataAsString(); if (packet.Opcode == Opcode.ScheduledTask) { if (ConsoleLogUpdated != null) { ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "TASK SERVER RESPONSE: " + message, Timestamp = packet.Timestamp }); } } }
private void OnWhitelistPlayer(Packet packet) { var message = packet.DataAsString(); if (message.Trim() == "Server received, But no response!!") { return; } if (packet.Opcode == Opcode.Whitelist) { if (ConsoleLogUpdated != null) { ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "PLAYER ADDED TO WHITELIST COMMAND EXECUTED: " + message, Timestamp = packet.Timestamp }); } } }
private void OnConsoleLogUpdated(Packet packet) { var message = packet.DataAsString(); if (message.Trim() == "Server received, But no response!!") { message = "Command Executed Successfully!"; } if (packet.Opcode == Opcode.Generic || packet.Opcode == Opcode.ServerResponse) { if (ConsoleLogUpdated != null) { ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "SERVER CONSOLE: " + message, Timestamp = packet.Timestamp }); } } }
private void OnBanPlayer(Packet packet) { var message = packet.DataAsString(); if (message.Trim() == "Server received, But no response!!") { return; } if (packet.Opcode == Opcode.BanPlayer) { if (ConsoleLogUpdated != null) { ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "BAN PLAYER COMMAND EXECUTED: " + message, Timestamp = packet.Timestamp }); } GetPlayers(); } }
private void OnGetPlayers(Packet packet) { try { var players = new List<Player>(); var message = packet.DataAsString(); if (message.Trim() == "No Players Connected") { if (CurrentPlayerCountUpdated != null) CurrentPlayerCountUpdated(this, new PlayerCountEventArgs() { PlayerCount = players.Count }); if (PlayersUpdated != null) PlayersUpdated(this, new PlayersEventArgs() { Players = players }); if (ConsoleLogUpdated != null) ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "Server Response: No Players Connected", Timestamp = packet.Timestamp }); return; } var str = packet.DataAsString(); string[] lines = str.Split('\n'); string[] cleanLines = lines.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray(); foreach (string line in cleanLines) { var lineData = line.Replace("...", " "); string[] split1 = lineData.Split(new char[] { '.' }, 2); string[] split2 = split1[1].Split(new char[] { ',' }, 2); string playerNumber = split1[0].Trim(); string name = split2[0].Trim(); string steamId = split2[1].Trim(); players.Add(new Player { PlayerNumber = int.Parse(playerNumber), Name = name, SteamID = UInt64.Parse(steamId) }); } if (CurrentPlayerCountUpdated != null) CurrentPlayerCountUpdated(this, new PlayerCountEventArgs() { PlayerCount = players.Count }); if (PlayersUpdated != null) PlayersUpdated(this, new PlayersEventArgs() { Players = players }); if (ConsoleLogUpdated != null) ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "Server Response: Player List Updated", Timestamp = packet.Timestamp }); } catch (Exception ex) { Debug.WriteLine(ex.InnerException); } }
private void OnBanPlayer(Packet packet) { var message = packet.DataAsString(); if (message.Trim() == "Server received, But no response!!") return; if (packet.Opcode == Opcode.BanPlayer) { if (ConsoleLogUpdated != null) ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "BAN PLAYER COMMAND EXECUTED: " + message, Timestamp = packet.Timestamp }); GetPlayers(); } }
private void OnGetChatMessage(Packet packet) { var message = packet.DataAsString(); if (message.Trim() == "Server received, But no response!!") return; if (packet.Opcode == Opcode.ChatMessage) { if (ChatLogUpdated != null) if (message.StartsWith("SERVER:")) { ChatLogUpdated(this, new ChatLogEventArgs() { Message = message, Timestamp = packet.Timestamp, IsAdmin = true }); } else { ChatLogUpdated(this, new ChatLogEventArgs() { Message = message, Timestamp = packet.Timestamp, IsAdmin = false }); } } }
private void OnScheduledTask(Packet packet) { var message = packet.DataAsString(); if (packet.Opcode == Opcode.ScheduledTask) { if (ConsoleLogUpdated != null) ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "TASK SERVER RESPONSE: " + message, Timestamp = packet.Timestamp }); } }
private void OnConsoleLogUpdated(Packet packet) { var message = packet.DataAsString(); if (message.Trim() == "Server received, But no response!!") message = "Command Executed Successfully!"; if(packet.Opcode == Opcode.Generic || packet.Opcode == Opcode.ServerResponse) { if (ConsoleLogUpdated != null) ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "SERVER CONSOLE: " + message, Timestamp = packet.Timestamp }); } }
public static async Task<Packet> ReadFromStreamAsync(NetworkStream stream) { Packet packet = new Packet(); byte[] sizeBuffer = new byte[4]; stream.Read(sizeBuffer, 0, 4); packet.PacketSize = BitConverter.ToInt32(sizeBuffer, 0); byte[] packetBuffer = new byte[packet.PacketSize]; if(packet.PacketSize != 0) { int readProgress = 0; while(readProgress < packet.PacketSize) { int read = await stream.ReadAsync(packetBuffer, readProgress, packet.PacketSize - readProgress); readProgress += read; if(read == 0) { throw new Exception("NetworkStream failed to read data. Connection may have been lost!"); } } } packet.Opcode = (Opcode)BitConverter.ToInt32(packetBuffer, 0); packet.Type = (PacketType)BitConverter.ToInt32(packetBuffer, 4); Array.Copy(packetBuffer, 8, packet.Data, 0, packet.DataSize); Console.WriteLine("Recieved {0}", packet.DataAsString()); return packet; }
private void OnWhitelistPlayer(Packet packet) { var message = packet.DataAsString(); if (message.Trim() == "Server received, But no response!!") return; if (packet.Opcode == Opcode.Whitelist) { if (ConsoleLogUpdated != null) ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "PLAYER ADDED TO WHITELIST COMMAND EXECUTED: " + message, Timestamp = packet.Timestamp }); } }
private void OnGetChatMessage(Packet packet) { var message = packet.DataAsString(); if (packet.Opcode == Opcode.ChatMessage) { GotChatResponse = true; if (message.Trim() == "Server received, But no response!!") return; string[] messages = message.Split('\n'); foreach (string newMessage in messages) { if (string.IsNullOrWhiteSpace(newMessage)) continue; string[] splitMessage = newMessage.Split(new char[] { ':' }, 2); if (newMessage.StartsWith("SERVER:") && newMessage == "SERVER: " + LastSentAdminMessage) { if (SentMessageUpdated != null) { var chatLog = new ChatLogEventArgs() { Timestamp = packet.Timestamp, IsAdmin = true }; chatLog.Message = newMessage.Replace("SERVER: ", ""); SentMessageUpdated(this, chatLog); } } else { if (ChatLogUpdated != null) { var chatLog = new ChatLogEventArgs() { Message = splitMessage[1], Sender = splitMessage[0], Timestamp = packet.Timestamp, IsAdmin = false }; ChatLogUpdated(this, chatLog); } } } } }
private void OnGetPlayers(Packet packet) { try { var players = new List <Player>(); var message = packet.DataAsString(); if (message.Trim() == "No Players Connected") { CurrentPlayerCountUpdated(this, new PlayerCountEventArgs() { PlayerCount = players.Count }); PlayersUpdated(this, new PlayersEventArgs() { Players = players }); if (ConsoleLogUpdated != null) { ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "Server Response: No Players Connected", Timestamp = packet.Timestamp }); } return; } var str = packet.DataAsString(); string[] lines = str.Split('\n'); string[] cleanLines = lines.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray(); foreach (string line in cleanLines) { var lineData = line.Replace("...", ""); string[] split1 = lineData.Split(new char[] { '.' }, 2); string[] split2 = split1[1].Split(new char[] { ',' }, 2); string playerNumber = split1[0].Trim(); string name = split2[0].Trim(); string steamId = split2[1].Trim(); players.Add(new Player { PlayerNumber = int.Parse(playerNumber), Name = name, SteamID = UInt64.Parse(steamId) }); } CurrentPlayerCountUpdated(this, new PlayerCountEventArgs() { PlayerCount = players.Count }); PlayersUpdated(this, new PlayersEventArgs() { Players = players }); if (ConsoleLogUpdated != null) { ConsoleLogUpdated(this, new ConsoleLogEventArgs() { Message = "Server Response: Player List Updated", Timestamp = packet.Timestamp }); } } catch (Exception ex) { Debug.WriteLine(ex.InnerException); } }