-
Notifications
You must be signed in to change notification settings - Fork 0
/
UdpClientConnection.cs
43 lines (34 loc) · 973 Bytes
/
UdpClientConnection.cs
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
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace Suhock.Osc;
public sealed class UdpClientConnection : IOscConnection
{
private readonly UdpClient _client;
public UdpClientConnection(string address, int port) : this(new UdpClient(address, port))
{
}
public UdpClientConnection(UdpClient client)
{
_client = client;
}
public byte[] Receive()
{
IPEndPoint? ep = null;
return _client.Receive(ref ep);
}
public async Task<byte[]> ReceiveAsync(CancellationToken cancellationToken)
{
return (await _client.ReceiveAsync(cancellationToken)).Buffer;
}
public void Send(ReadOnlySpan<byte> data)
{
_client.Send(data);
}
public async Task SendAsync(ReadOnlyMemory<byte> data, CancellationToken cancellationToken)
{
await _client.SendAsync(data, cancellationToken);
}
}