mirror-ac/server/Server.cs

61 lines
1.7 KiB
C#
Raw Normal View History

2023-09-02 17:56:46 +02:00
using System.IO;
using System;
using System.Net;
2023-09-02 15:47:15 +02:00
using System.Net.Sockets;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using server;
2023-09-02 17:56:46 +02:00
using Serilog;
2023-09-02 15:47:15 +02:00
namespace server
{
public class Server
{
private IPEndPoint _ipEndPoint;
private TcpListener _tcpListener;
2023-09-02 17:56:46 +02:00
private ILogger _logger;
2023-09-02 15:47:15 +02:00
2023-09-02 17:56:46 +02:00
public Server(ILogger logger)
2023-09-02 15:47:15 +02:00
{
_ipEndPoint = new IPEndPoint(IPAddress.Any, 8888);
_tcpListener = new TcpListener(_ipEndPoint);
2023-09-02 17:56:46 +02:00
_logger = logger;
2023-09-02 15:47:15 +02:00
}
public async Task Listen()
{
_tcpListener.Start();
2023-09-02 17:56:46 +02:00
_logger.Information("Listening for incoming connections...");
while (true)
{
TcpClient client = await _tcpListener.AcceptTcpClientAsync();
2023-09-02 17:56:46 +02:00
NetworkStream _stream = client.GetStream();
TcpClient clientReference = client;
2023-09-02 15:47:15 +02:00
2023-09-08 20:41:11 +02:00
byte[] buffer = new byte[2048];
int bytesRead = 0;
using (MemoryStream stream = new MemoryStream())
{
while (_stream.DataAvailable)
{
bytesRead = _stream.Read(buffer, 0, buffer.Length);
stream.Write(buffer, 0, bytesRead);
}
byte[] message = stream.ToArray();
2023-09-07 19:49:36 +02:00
ThreadPool.QueueUserWorkItem(state => DispatchMessage(state, clientReference, message, message.Length));
2023-09-08 20:41:11 +02:00
}
2023-09-02 17:56:46 +02:00
}
2023-09-02 15:47:15 +02:00
}
2023-09-07 09:21:00 +02:00
private void DispatchMessage(Object? stateInfo, TcpClient client, byte[] buffer, int bufferSize)
2023-09-07 09:21:00 +02:00
{
Message message = new Message(client, buffer, bufferSize, _logger);
2023-09-07 09:21:00 +02:00
}
2023-09-02 15:47:15 +02:00
}
}