blob: 45d2be82741ba43f93297328111096e95b2b92ee (
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
67
68
69
70
71
72
73
74
|
using System.Net;
using System.Threading.Tasks;
using Impostor.Api.Net;
using Impostor.Api.Net.Messages;
using Impostor.Hazel;
using Microsoft.Extensions.Logging;
namespace Impostor.Server.Net.Hazel
{
internal class HazelConnection : IHazelConnection
{
private readonly ILogger<HazelConnection> _logger;
public HazelConnection(Connection innerConnection, ILogger<HazelConnection> logger)
{
_logger = logger;
InnerConnection = innerConnection;
innerConnection.DataReceived = ConnectionOnDataReceived;
innerConnection.Disconnected = ConnectionOnDisconnected;
}
public Connection InnerConnection { get; }
public IPEndPoint EndPoint => InnerConnection.EndPoint;
public bool IsConnected => InnerConnection.State == ConnectionState.Connected;
public IClient Client { get; set; }
public ValueTask SendAsync(IMessageWriter writer)
{
return InnerConnection.SendAsync(writer);
}
public ValueTask DisconnectAsync(string reason)
{
return InnerConnection.Disconnect(reason);
}
public void DisposeInnerConnection()
{
InnerConnection.Dispose();
}
private async ValueTask ConnectionOnDisconnected(DisconnectedEventArgs e)
{
if (Client != null)
{
await Client.HandleDisconnectAsync(e.Reason);
}
}
private async ValueTask ConnectionOnDataReceived(DataReceivedEventArgs e)
{
if (Client == null)
{
return;
}
while (true)
{
if (e.Message.Position >= e.Message.Length)
{
break;
}
using (var message = e.Message.ReadMessage())
{
await Client.HandleMessageAsync(message, e.Type);
}
}
}
}
}
|