mirror of
https://gitlab.com/Syroot/Worms.git
synced 2025-01-28 14:47:57 +03:00
46 lines
1.6 KiB
C#
46 lines
1.6 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace Syroot.Worms.OnlineWorms.Server
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Represents a server listening for incoming client connections and dispatching them into <see cref="Client"/>
|
|||
|
/// instances.
|
|||
|
/// </summary>
|
|||
|
public class Server
|
|||
|
{
|
|||
|
// ---- FIELDS -------------------------------------------------------------------------------------------------
|
|||
|
|
|||
|
private readonly List<Client> _clients = new List<Client>();
|
|||
|
|
|||
|
// ---- METHODS (INTERNAL) -------------------------------------------------------------------------------------
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Starts the server by accepting new client connections under the given <paramref name="port"/> and
|
|||
|
/// dispatching them into asynchronous handling threads. This call is blocking.
|
|||
|
/// </summary>
|
|||
|
/// <param name="port">The port on which to listen for new client connections.</param>
|
|||
|
internal void Listen(int port)
|
|||
|
{
|
|||
|
TcpListener tcpListener = new TcpListener(IPAddress.Any, port);
|
|||
|
tcpListener.Start();
|
|||
|
Console.WriteLine($"Listening on port {port}...");
|
|||
|
|
|||
|
while (true)
|
|||
|
{
|
|||
|
Client client = new Client(tcpListener.AcceptTcpClient(), this);
|
|||
|
_clients.Add(client);
|
|||
|
|
|||
|
Task.Run(client.Accept).ContinueWith(_ =>
|
|||
|
{
|
|||
|
_clients.Remove(client);
|
|||
|
client.Dispose();
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|