-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathMyCustomProtocol.cs
More file actions
52 lines (43 loc) · 1.5 KB
/
Copy pathMyCustomProtocol.cs
File metadata and controls
52 lines (43 loc) · 1.5 KB
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
using System.Threading.Tasks;
using Bedrock.Framework.Protocols;
using Microsoft.AspNetCore.Connections;
using Microsoft.Extensions.Logging;
using Protocols;
namespace ServerApplication
{
public class MyCustomProtocol : ConnectionHandler
{
private readonly ILogger _logger;
public MyCustomProtocol(ILogger<MyCustomProtocol> logger)
{
_logger = logger;
}
public override async Task OnConnectedAsync(ConnectionContext connection)
{
_logger.LogInformation("{ConnectionId} connected.", connection.ConnectionId);
// Use a length prefixed protocol
var protocol = new LengthPrefixedProtocol();
var reader = connection.CreateReader();
var writer = connection.CreateWriter();
while (true)
{
try
{
var result = await reader.ReadAsync(protocol);
var message = result.Message;
_logger.LogInformation("Received a message of {Length} bytes", message.Payload.Length);
if (result.IsCompleted)
{
break;
}
await writer.WriteAsync(protocol, message);
}
finally
{
reader.Advance();
}
}
_logger.LogInformation("{ConnectionId} disconnected.", connection.ConnectionId);
}
}
}