mirror-ac/server/Server.cs

53 lines
1.5 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
private const int MAX_BUFFER_SIZE = 8192;
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)
{
using TcpClient _client = await _tcpListener.AcceptTcpClientAsync();
NetworkStream _stream = _client.GetStream();
2023-09-07 19:49:36 +02:00
byte[] buffer = new byte[MAX_BUFFER_SIZE];
int bufferSize = 0;
NetworkStream clientStreamReference = _stream;
2023-09-02 15:47:15 +02:00
2023-09-07 19:49:36 +02:00
bufferSize = _stream.Read(buffer, 0, MAX_BUFFER_SIZE);
ThreadPool.QueueUserWorkItem(state => DispatchMessage(state, clientStreamReference, buffer, bufferSize) );
2023-09-02 17:56:46 +02:00
}
2023-09-02 15:47:15 +02:00
}
2023-09-07 09:21:00 +02:00
2023-09-07 19:49:36 +02:00
private void DispatchMessage(Object? stateInfo, NetworkStream clientStreamReference, byte[] buffer, int bufferSize)
2023-09-07 09:21:00 +02:00
{
2023-09-07 19:49:36 +02:00
Message message = new Message(clientStreamReference, buffer, bufferSize, _logger);
2023-09-07 09:21:00 +02:00
}
2023-09-02 15:47:15 +02:00
}
}