SafeNet/SafeNetLib/TCPserver.cs
2021-02-24 13:50:23 +02:00

140 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace SafeNetLib
{
public class TCPServer
{
private TcpListener tcpListener;
private Thread listenThread;
public TCPServer()
{
//Console.WriteLine("TCPServer Constructor");
}
public void Start(int portNr)
{
Utils.ConsWrite(DebugMSG_Type.DB, "Debug Server started on port " + portNr);
tcpListener = new TcpListener(IPAddress.Any, portNr);
listenThread = new Thread(new ThreadStart(ListenForClients));
listenThread.IsBackground = true;
listenThread.Start();
Utils.ConsWrite(DebugMSG_Type.DB, "");
Utils.ConsWrite(DebugMSG_Type.DB, "»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»««««««««««««««««««««««««««««««");
Utils.ConsWrite(DebugMSG_Type.DB, "");
}
public void Stop()
{
if (tcpListener != null)
{
tcpListener.Stop();
}
if (listenThread.IsAlive)
{
listenThread.Abort();
}
}
private void ListenForClients()
{
tcpListener.Start();
while (true)
{
//Utils.ConsWrite(DebugMSG_Type.DB, "Debug Waiting for client....");
//blocks until a client has connected to the server
TcpClient client = tcpListener.AcceptTcpClient();
Utils.ConsWrite(DebugMSG_Type.DB, "Debug Client connected from " + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.IsBackground = true;
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
string clientIPAddress = "Debug Client Ip Address is: " + IPAddress.Parse(((
IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString());
if (this.OnConnectionRecv != null)
{
this.OnConnectionRecv(clientStream, clientIPAddress);
}
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
//Console.WriteLine("Message received");
//message has successfully been received
//ASCIIEncoding encoder = new ASCIIEncoding();
//System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
if (this.OnMessageRecv != null)
{
this.OnMessageRecv(message, bytesRead, clientStream);
}
if (this.OnMessageRecvWithIdent != null)
{
this.OnMessageRecvWithIdent(message, bytesRead, clientIPAddress);
}
}
tcpClient.Close();
if (this.OnConnectionEnded != null)
{
this.OnConnectionEnded(clientIPAddress);
}
Utils.ConsWrite(DebugMSG_Type.DB, " Debug Client disconnected");
}
public delegate void MessageRecv(byte[] data, int recv, NetworkStream ns);
public event MessageRecv OnMessageRecv;
public delegate void MessageRecvWithIdent(byte[] data, int recv, string ip);
public event MessageRecvWithIdent OnMessageRecvWithIdent;
public delegate void ConnectionRecv(NetworkStream ns, string IP);
public event ConnectionRecv OnConnectionRecv;
public delegate void ConnectionEnded(string IP);
public event ConnectionEnded OnConnectionEnded;
public void Send(byte[] data, int len, NetworkStream ns)
{
if (ns != null)
{
ns.Write(data, 0, len);
ns.Flush();
}
}
}
}