2using System.Collections.Generic;
3using System.Net.Sockets;
5using System.Threading.Tasks;
53 public BadNetworkConsoleClient(TcpClient client, Func<BadNetworkConsoleClient, IBadNetworkConsoleClientCommandParser> parserFactory)
80 public BadNetworkConsoleClient(
string address,
int port, Func<BadNetworkConsoleClient, IBadNetworkConsoleClientCommandParser> parserFactory)
169 Console.WriteLine(wp.Message);
173 Console.Write(wp.Message);
177 Console.BackgroundColor = cs.
Color;
181 Console.ForegroundColor = cs.
Color;
204 byte[] len =
new byte[
sizeof(int)];
205 NetworkStream stream =
m_Client.GetStream();
206 int read = stream.Read(len, 0, len.Length);
208 if (read != len.Length)
213 byte[] packet =
new byte[BitConverter.ToInt32(len, 0)];
214 read = stream.Read(packet, 0, packet.Length);
216 if (read != packet.Length)
234 NetworkStream str =
m_Client.GetStream();
235 List<byte> packetData =
new List<byte>();
237 packetData.AddRange(BitConverter.GetBytes(packetBytes.Length));
238 packetData.AddRange(packetBytes);
239 str.Write(packetData.ToArray(), 0, packetData.Count);
249 List<byte> packet =
new List<byte>();
250 packet.AddRange(BitConverter.GetBytes(packetData.Length));
251 packet.AddRange(packetData);
252 m_Client.GetStream().Write(packet.ToArray(), 0, packet.Count);
260 DateTime lastHeartBeat = DateTime.Now;
264 Task<string> task = Task.Run(Console.ReadLine);
271 lastHeartBeat = DateTime.Now;
282 string message = task.Result ??
"client::disconnect";
284 if (message.StartsWith(
"client::"))
286 string cmd = message.Substring(
"client::".Length);
292 lastHeartBeat = DateTime.Now;
294 List<byte> packet =
new List<byte>();
295 packet.AddRange(BitConverter.GetBytes(packetData.Length));
296 packet.AddRange(packetData);
297 m_Client.GetStream().Write(packet.ToArray(), 0, packet.Count);
Wrapper class for the console abstraction.
static void WriteLine(string str)
Writes a string to the console and appends a newline.
Exception that is thrown when the remote console encounters an error.
The Default Command Parser for the Remote Console Client.
void AddCommand(Func< BadNetworkConsoleClient, BadNetworkConsoleClientCommand > command)
Adds a Command to the Command List.
Implements the Client for the Remote Console.
bool m_ExitRequested
If true the client will exit.
static int ConsoleReadInputSleep
The Interval in which packets are read from the server.
readonly string m_Address
The Address of the Remote Console.
Thread? m_ReadThread
The Read Thread.
void ProcessPacket(BadConsolePacket packet)
Processes the given Packet.
readonly int m_Port
The Port of the Remote Console.
void Start()
Starts the Client.
static IBadNetworkConsoleClientCommandParser DefaultParserFactory(BadNetworkConsoleClient client)
The Default Parser Factory.
readonly IBadNetworkConsoleClientCommandParser m_Parser
The Parser.
static int ConsoleWriteSleep
The Interval in which packets are sent to the server.
BadNetworkConsoleClient(TcpClient client, Func< BadNetworkConsoleClient, IBadNetworkConsoleClientCommandParser > parserFactory)
Creates a new Client from the given TcpClient.
void Stop()
Stops the Client.
static int HeartBeatSendInterval
The Interval in which the Heartbeat is sent.
void Write()
The Write Thread.
BadNetworkConsoleClient(string address, int port, Func< BadNetworkConsoleClient, IBadNetworkConsoleClientCommandParser > parserFactory)
Creates a new Client from the given Address and Port.
BadNetworkConsoleClient(string address, int port)
Creates a new Client from the given Address and Port.
BadNetworkConsoleClient(TcpClient client)
Creates a new Client from the given TcpClient.
readonly TcpClient m_Client
The TCP Client.
void Read()
The Read Thread.
void SendHeartBeat()
Sends a Heartbeat to the Server.
Commands that disconnects the client from the server.
Command that lists all commands available to the user.
Gets used to clear the console on the client.
Gets used to change the color of the console.
ConsoleColor Color
The Color to change to.
Gets used to disconnect the client and the server.
override byte[] Serialize()
static readonly BadConsoleDisconnectPacket Packet
Static Instance of this Packet.
Gets used to send a HeartBeat to the client.
static readonly BadConsoleHeartBeatPacket Packet
Static Instance of this Packet.
override byte[] Serialize()
Gets used as handshake between the client and the server.
int HeartBeatInterval
The Heartbeat interval that the server uses.
Implements the base class for all BadConsole Packets.
static BadConsolePacket Deserialize(byte[] data)
Deserializes a BadConsolePacket from the given data.
Gets used to send a Read Result to the server.
override byte[] Serialize()
Gets sent to the client to write a message to the console.
Used to parse commands on the client.
void ExecuteCommand(string command)
Executes a command on the client.
Contains Console Commands for the Remote Console Client.
Contains the Console Client Implementation for the Remote Console Abstraction over TCP.
Contains the network packets for the remote console.