5 using System.Collections;
6 using System.Collections.Generic;
7 using System.Net.Security;
8 using System.Net.Sockets;
9 using System.Security.Cryptography.X509Certificates;
11 using System.Threading;
12 using System.Threading.Tasks;
25 CancellationTokenSource cancelMonitorToken;
26 List<IHandler> handlers =
new List<IHandler>();
27 List<ILogger> loggers =
new List<ILogger>();
28 List<IExceptionHandler> exceptionHandlers =
new List<IExceptionHandler>();
29 Queue<byte[]> messageQueue =
new Queue<byte[]>();
30 SslStream sslStream =
null;
31 static Hashtable certificateErrors =
new Hashtable();
39 if (tcpClient !=
null)
40 return tcpClient.Connected;
45 public string Address {
get => address; }
46 public int Port {
get => port; }
49 public Client(
bool useSsl =
false)
54 #region Public Methods 61 messageQueue.Enqueue(message);
67 public void Log(
string log)
69 foreach (
ILogger logger
in loggers)
80 foreach (
ILogger logger
in loggers)
92 this.address = address;
112 handlers.Add(handler);
122 exceptionHandlers.Add(handler);
147 if (tcpClient !=
null)
149 cancelMonitorToken?.Cancel();
152 tcpClient =
new TcpClient();
153 tcpClient.Connect(address, port);
161 sslStream.AuthenticateAsClient(address);
163 foreach(
ILogger logger
in loggers)
165 logger.
Log(
"Connected to: " + tcpClient.Client.RemoteEndPoint.ToString());
167 foreach (
IHandler handler
in handlers)
174 cancelMonitorToken =
new CancellationTokenSource();
175 Task.Run(
new Action(MonitorConnection), cancelMonitorToken.Token);
180 LogDebug(
"Exception: " + e.Message);
181 LogDebug(
"Stacktrace: " + e.StackTrace);
195 return new Task<bool>(
Connect);
202 cancelMonitorToken?.Cancel();
206 #region Private Methods 207 private static bool ValidateServerCertificate(
object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
217 if (sslPolicyErrors == SslPolicyErrors.None)
224 private void MonitorConnection()
230 if(messageQueue.Count > 0)
232 byte[] messagebytes = messageQueue.Dequeue();
233 Message sendMessage = CommunicationPrimitives.BuildMessage(messagebytes);
234 for(
int i = handlers.Count - 1; i >= 0; i--)
239 CommunicationPrimitives.SendMessage(tcpClient, sendMessage, sslStream);
241 if (tcpClient.Available > 0)
246 Message message = CommunicationPrimitives.GetMessage(tcpClient, sslStream);
247 LogDebug(
"Received a message of size: " + message.MessageLength);
251 foreach (
IHandler handler
in handlers)
261 foreach (
IHandler handler
in handlers)
269 foreach (
IHandler handler
in handlers)
276 LogDebug(
"Exception: " + e.Message);
277 LogDebug(
"Stacktrace: " + e.StackTrace);
void Log(string log)
Logs the message onto the associated loggers.
Task< bool > ConnectAsync()
Attempts to connect to the specified server.
Client AddExceptionHandler(IExceptionHandler handler)
Adds an exception handler to the client.
bool Log(string message)
Logs the received message.
Client SetServerAddress(string address)
Sets the server address.
Client AddHandler(IHandler handler)
Adds a handler onto the client.
bool HandleException(Exception e)
Handles exceptions.
Client SetPort(int port)
Sets the server port.
bool HandleSendMessage(Client client, ref Message message)
Called when a message is being sent to the server.
bool PreHandleReceivedMessage(Client client, ref Message message)
Called before the message is handled. Use this method to modify the message if necesarry.
RemoteCertificateValidationCallback CertificateValidationCallback
Callback function used to determine if the remote certificate is valid.
bool Connect()
Attemps to connect to the specified server.
bool HandleReceivedMessage(Client client, Message message)
Called when a message has been received.
void Tick(Client client)
Called every cycle. This method should perform regular actions on the connection.
bool InitializeConnection(Client client)
Called when the connection is being initialized.
void Disconnect()
Disconnects from the server.
Client AddLogger(ILogger logger)
Adds a logger onto the client.
void QueueMessage(byte[] message)
Add a message to the message queue.
Handler interface for client communication.
void LogDebug(string debugMessage)
Logs the debug message onto the associated loggers.
bool LogDebug(string message)
Logs the received debug message.
Base class for TCP Client.
Handler to be used for handling exception.