mirror of
https://github.com/donnaskiez/ac.git
synced 2024-11-21 22:24:08 +01:00
72 lines
No EOL
2.1 KiB
C#
72 lines
No EOL
2.1 KiB
C#
using System.IO;
|
|
using System;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Reflection.Metadata.Ecma335;
|
|
using System.Text;
|
|
using Serilog;
|
|
using server.Message;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
|
using K4os.Compression.LZ4.Streams.Adapters;
|
|
|
|
namespace server
|
|
{
|
|
public class Server
|
|
{
|
|
private IPEndPoint _ipEndPoint;
|
|
private TcpListener _tcpListener;
|
|
private ILogger _logger;
|
|
|
|
public Server(ILogger logger)
|
|
{
|
|
_ipEndPoint = new IPEndPoint(IPAddress.Any, 8888);
|
|
_tcpListener = new TcpListener(_ipEndPoint);
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task Listen()
|
|
{
|
|
_tcpListener.Start();
|
|
|
|
_logger.Information("Listening for incoming connections...");
|
|
|
|
while (true)
|
|
{
|
|
TcpClient client = await _tcpListener.AcceptTcpClientAsync();
|
|
|
|
NetworkStream _stream = client.GetStream();
|
|
TcpClient clientReference = client;
|
|
|
|
byte[] buffer = new byte[2048];
|
|
int bytesRead = 0;
|
|
|
|
using (MemoryStream stream = new MemoryStream())
|
|
{
|
|
while (_stream.DataAvailable)
|
|
{
|
|
bytesRead = _stream.Read(buffer, 0, buffer.Length);
|
|
|
|
_logger.Information("bytes read: {0}", bytesRead);
|
|
|
|
stream.Write(buffer, 0, bytesRead);
|
|
}
|
|
|
|
byte[] message = stream.ToArray();
|
|
|
|
if (message.Length == 0)
|
|
{
|
|
_logger.Error("Null message received at server");
|
|
continue;
|
|
}
|
|
|
|
ThreadPool.QueueUserWorkItem(state => DispatchMessage(state, clientReference, message, message.Length));
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DispatchMessage(Object? stateInfo, TcpClient client, byte[] buffer, int bufferSize)
|
|
{
|
|
MessageHandler message = new MessageHandler(client, buffer, bufferSize, _logger);
|
|
}
|
|
}
|
|
} |