-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimulationClient.cs
58 lines (49 loc) · 1.54 KB
/
SimulationClient.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Net;
using System.Net.Sockets;
using FiraMessage.SimToRef;
using Google.Protobuf;
using Environment = FiraMessage.SimToRef.Environment;
namespace Referee
{
public interface ISimulateClient
{
Environment Simulate(Packet request);
}
public class SimulateClient : ISimulateClient, IDisposable
{
private readonly UdpClient sendClient;
private readonly UdpClient receiveClient;
public SimulateClient(string address, int sendPort, int listenPort)
{
sendClient = new UdpClient();
sendClient.Connect(address, sendPort);
receiveClient = new UdpClient(listenPort);
}
public Environment Simulate(Packet request)
{
var bytes = request.ToByteArray();
sendClient.Send(bytes, bytes.Length);
var endpoint = new IPEndPoint(IPAddress.Any, 0);
var receive = receiveClient.Receive(ref endpoint);
var environment = Environment.Parser.ParseFrom(receive);
return environment;
}
public void Dispose()
{
sendClient.Close();
}
}
public class GrpcSimulateClient : ISimulateClient
{
private readonly Simulate.SimulateClient client;
public GrpcSimulateClient(Simulate.SimulateClient client)
{
this.client = client;
}
public Environment Simulate(Packet request)
{
return client.Simulate(request);
}
}
}