-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMessageHelper.cs
36 lines (32 loc) · 1.07 KB
/
MessageHelper.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
using ProtoBuf;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Margatroid.Alice
{
static class MessageHelper
{
public static byte[] SerializeMessage(Message message)
{
using var streamBuffer = new MemoryStream();
Serializer.Serialize(streamBuffer, message);
return streamBuffer.ToArray();
}
public static Message DeserializeMessage(byte[] data)
{
using var streamBuffer = new MemoryStream(data);
return Serializer.Deserialize<Message>(streamBuffer);
}
public static async Task<Message> ReceiveMessage(UdpClient udpClient)
{
var receivedResult = await udpClient.ReceiveAsync();
return DeserializeMessage(receivedResult.Buffer);
}
public static async Task SendMessage(UdpClient udpClient, Message message, IPEndPoint remote)
{
var data = SerializeMessage(message);
await udpClient.SendAsync(data, data.Length, remote);
}
}
}