blob: 64ece55f0abc3bd622440dba6ed5a2ea01471a2c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Impostor.Hazel;
using Impostor.Hazel.Udp;
using Impostor.Server.Net.Hazel;
using Impostor.Server.Net.Manager;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
namespace Impostor.Server.Net
{
internal class Matchmaker
{
private readonly ClientManager _clientManager;
private readonly ObjectPool<MessageReader> _readerPool;
private readonly ILogger<Matchmaker> _logger;
private readonly ILogger<HazelConnection> _connectionLogger;
private UdpConnectionListener _connection;
public Matchmaker(
ILogger<Matchmaker> logger,
ClientManager clientManager,
ObjectPool<MessageReader> readerPool,
ILogger<HazelConnection> connectionLogger)
{
_logger = logger;
_clientManager = clientManager;
_readerPool = readerPool;
_connectionLogger = connectionLogger;
}
public async ValueTask StartAsync(IPEndPoint ipEndPoint)
{
var mode = ipEndPoint.AddressFamily switch
{
AddressFamily.InterNetwork => IPMode.IPv4,
AddressFamily.InterNetworkV6 => IPMode.IPv6,
_ => throw new InvalidOperationException()
};
_connection = new UdpConnectionListener(ipEndPoint, _readerPool, mode);
_connection.NewConnection = OnNewConnection;
await _connection.StartAsync();
}
public async ValueTask StopAsync()
{
await _connection.DisposeAsync();
}
private async ValueTask OnNewConnection(NewConnectionEventArgs e)
{
// Handshake.
var clientVersion = e.HandshakeData.ReadInt32();
var name = e.HandshakeData.ReadString();
var connection = new HazelConnection(e.Connection, _connectionLogger);
// Register client
await _clientManager.RegisterConnectionAsync(connection, name, clientVersion);
}
}
}
|