2using System.Collections.Concurrent;
3using System.Collections.Generic;
4using System.Net.Sockets;
6using System.Threading.Tasks;
21 new ConcurrentQueue<BadConsoleReadPacket>();
31 private readonly
object m_Lock =
new object();
36 private readonly ConcurrentQueue<BadConsolePacket>
m_OutgoingPackets =
new ConcurrentQueue<BadConsolePacket>();
228 BadConsole.
WriteLine($
"[Console Host] Waiting for Connection on {m_Listener.LocalEndpoint}");
229 bool accepted =
false;
248 DateTime lastHeartBeat = DateTime.Now;
257 lastHeartBeat = DateTime.Now;
258 byte[] len =
new byte[
sizeof(int)];
259 NetworkStream stream =
m_Client.GetStream();
260 int read = stream.Read(len, 0, len.Length);
262 if (read != len.Length)
267 byte[] packet =
new byte[BitConverter.ToInt32(len, 0)];
268 read = stream.Read(packet, 0, packet.Length);
270 if (read != packet.Length)
303 NetworkStream stream =
m_Client.GetStream();
304 List<byte> packetData =
new List<byte>();
305 byte[] packetBytes = packet.Serialize();
306 packetData.AddRange(BitConverter.GetBytes(packetBytes.Length));
307 packetData.AddRange(packetBytes);
308 stream.Write(packetData.ToArray(), 0, packetData.Count);
317 if (lastHeartBeat + TimeSpan.FromMilliseconds(
HeartBeatTimeOut) < DateTime.Now)
328 NetworkStream stream =
m_Client.GetStream();
329 List<byte> packetData =
new List<byte>();
331 packetData.AddRange(BitConverter.GetBytes(packetBytes.Length));
332 packetData.AddRange(packetBytes);
333 stream.Write(packetData.ToArray(), 0, packetData.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.
Implements a Host for the Remote Console.
TcpClient? m_Client
The Connected Client.
static int ReadSleepTimeout
The Time Interval that the Read Thread will sleep if no data is available.
bool m_ExitRequested
If true the host will exit.
void MessageThread()
The Message Thread Loop.
readonly ConcurrentQueue< BadConsolePacket > m_OutgoingPackets
The Outgoing Packet Queue.
bool IsConnected
Is true if a client is connected to this host.
void Clear()
Clears the console.
ConsoleColor BackgroundColor
The Background Color.
static int HeartBeatInterval
The Interval in which the Host will send Heartbeat Packets to the Client.
BadNetworkConsoleHost(TcpListener listner)
Constructs a new Host from a TCP Listener.
readonly ConcurrentQueue< BadConsoleReadPacket > m_IncomingPackets
Queue of Incoming Packets.
static int HeartBeatTimeOut
The Timeout after which the Host will disconnect the Client if no Heartbeat Packet was received.
string ReadLine()
Reads a line from the console.The line that was read
Task< string > ReadLineAsync()
Reads a line from the console asynchronously.The line that was read
void Stop()
Stops the Host.
static int AcceptSleepTimeout
The Time Interval that the Accept Thread will sleep if no data is available.
static int ReceiveSleepTimeout
The Time Interval that the Write Thread will sleep if no data is available.
void WriteLine(string str)
Writes a string to the console and appends a newline.
ConsoleColor m_BackgroundColor
The Background Color.
void Start()
Starts the Host.
Thread? m_MessageThread
The Message Thread.
readonly object m_Lock
Lock Object.
BadNetworkConsoleHost(TcpClient client)
Constructs a new Host from a TCP Client.
readonly? TcpListener m_Listener
The Used TCP Listener.
void Write(string str)
Writes a string to the console.
ConsoleColor ForegroundColor
The Foreground Color.
ConsoleColor m_ForegroundColor
The Foreground Color.
void Disconnect()
Sends a Disconnect Packet to the Client.
Gets used to clear the console on the client.
static readonly BadConsoleClearPacket Packet
Static Instance of this Packet.
Gets used to change the color of the console.
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.
Gets used as handshake between the client and the server.
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.
string Message
The Line that was read.
Gets sent to the client to write a message to the console.
Interface that abstracts the console.
Contains the network packets for the remote console.
Contains the Implementation of the Remote Console Abstraction over TCP.