-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAMQPserver.cs
283 lines (227 loc) · 9.24 KB
/
AMQPserver.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using Godot;
using System;
using System.IO;
using Amqp;
using Amqp.Framing;
using Amqp.Types;
using ProtoBuf;
using redhatgamedev.srt.v1;
public partial class AMQPserver : Node
{
public Serilog.Core.Logger _serilogger;
// TODO: make config file
String url = "amqp://localhost:5672";
String commandInQueue = "COMMAND.IN";
String securityInQueue = "SECURITY.IN";
// for receiving game event updates from clients
ReceiverLink commandInReceiver;
// for receiving security updates from clients
ReceiverLink securityInReceiver;
String gameEventOutQueue = "GAME.EVENT.OUT";
String securityOutQueue = "SECURITY.OUT";
// for sending game events to all clients
SenderLink gameEventOutSender;
// for sending security events to all clients
SenderLink securityOutSender;
// for debug sending updates
SenderLink commandInSender;
SenderLink securityInSender;
ConnectionFactory factory;
Connection amqpConnection;
Session amqpSession;
// for the server we're interfaced with
Server MyServer;
void CommandReceived(IReceiverLink receiver, Message message)
{
_serilogger.Verbose("AMQPserver.cs: Client game event received!");
// accept the message so that it gets removed from the queue
receiver.Accept(message);
byte[] binaryBody = (byte[])message.Body;
MemoryStream st = new MemoryStream(binaryBody, false);
// prep a command buffer for processing the message
Command commandBuffer;
commandBuffer = Serializer.Deserialize<Command>(st);
MyServer.GameEventQueue.Enqueue(commandBuffer);
}
void SecurityReceived(IReceiverLink receiver, Message message)
{
_serilogger.Debug("AMQPserver.cs: Client security event received!");
// accept the message so that it gets removed from the queue
receiver.Accept(message);
byte[] binaryBody = (byte[])message.Body;
MemoryStream st = new MemoryStream(binaryBody, false);
// prep a command buffer for processing the message
Security securityBuffer;
securityBuffer = Serializer.Deserialize<Security>(st);
MyServer.SecurityEventQueue.Enqueue(securityBuffer);
}
public void SendGameEvent(GameEvent gameEvent)
{
_serilogger.Verbose("AMQPserver.cs: Sending game event");
// serialize it into a byte stream
MemoryStream st = new MemoryStream();
Serializer.Serialize<GameEvent>(st, gameEvent);
byte[] msgBytes = st.ToArray();
Message msg = new Message(msgBytes);
// create and destroy messages are mega important, so don't set a TTL for those
// updates are not as important, so set a low TTL for those
if (!(gameEvent.game_event_type == GameEvent.GameEventType.GameEventTypeCreate
| gameEvent.game_event_type == GameEvent.GameEventType.GameEventTypeDestroy))
{
msg.Header = new Header() { Ttl = 50 };
msg.Properties = new Properties() { CreationTime = DateTime.UtcNow };
}
// don't care about the ack on our message being received
gameEventOutSender.Send(msg, null, null);
// this should work but there's something weird and it blows up the
// connection
//gameEventOutSender.Send(msg);
}
// only used for debug
public void SendCommand(Command CommandBuffer)
{
_serilogger.Verbose("AMQPServer.cs: Sending command");
// serialize it into a byte stream
MemoryStream st = new MemoryStream();
Serializer.Serialize<Command>(st, CommandBuffer);
byte[] msgBytes = st.ToArray();
Message msg = new Message(msgBytes);
// don't care about the ack on our message being received
commandInSender.Send(msg, null, null);
// this should work but there's something weird and it blows up the
// connection
//commandInSender.Send(msg);
}
public void SendSecurity(Security security)
{
_serilogger.Debug("AMQPServer.cs: Sending security command");
// serialize it into a byte stream
MemoryStream st = new MemoryStream();
Serializer.Serialize<Security>(st, security);
byte[] msgBytes = st.ToArray();
Message msg = new Message(msgBytes);
// don't care about the ack on our message being received
securityOutSender.Send(msg, null, null);
// this should work but there's something weird and it blows up the
// connection
//commandInSender.Send(msg);
}
// used for the debug client parts of the server
public void SendSecurityDebug(Security security)
{
_serilogger.Debug("AMQPServer.cs: Sending security command");
// serialize it into a byte stream
MemoryStream st = new MemoryStream();
Serializer.Serialize<Security>(st, security);
byte[] msgBytes = st.ToArray();
Message msg = new Message(msgBytes);
// don't care about the ack on our message being received
securityInSender.Send(msg, null, null);
// this should work but there's something weird and it blows up the
// connection
//commandInSender.Send(msg);
}
async void InitializeAMQP()
{
// TODO: should probably wrap in some kind of try and catch failure to connect?
// is this even async?
// TODO: include connection details
_serilogger.Information("AMQPserver.cs: Initializing AMQP connection");
Connection.DisableServerCertValidation = true;
try
{
//Trace.TraceLevel = TraceLevel.Frame;
//Trace.TraceListener = (l, f, a) => Console.WriteLine(DateTime.Now.ToString("[hh:mm:ss.fff]") + " " + string.Format(f, a));
factory = new ConnectionFactory();
Address address = new Address(url);
amqpConnection = await factory.CreateAsync(address);
amqpSession = new Session(amqpConnection);
}
catch (Exception ex)
{
_serilogger.Error("ServerConnection.cs: AMQP connection/session failed for " + url);
_serilogger.Error($"ServerConnection.cs: {ex.Message}");
// TODO: let player know
return;
}
// set up queues and topics ////////////////////////////////////////////////
// topics are multicast
// queues are anycast
// https://stackoverflow.com/a/51595195
// output to clients ///////////////////////////////////////////////////////
// multicast topic for the server to send game event updates to clients
Target gameEventOutTarget = new Target
{
Address = gameEventOutQueue,
Capabilities = new Symbol[] { new Symbol("topic") }
};
gameEventOutSender = new SenderLink(amqpSession, "srt-game-server-command-sender", gameEventOutTarget, null);
// multicast topic for the server to send security updates to clients
Target securityOutTarget = new Target
{
Address = securityOutQueue,
Capabilities = new Symbol[] { new Symbol("topic") }
};
securityOutSender = new SenderLink(amqpSession, "srt-game-server-security-sender", securityOutTarget, null);
// inputs from clients /////////////////////////////////////////////////////
// anycast queue for the server to receive command events from clients
Source commandInSource = new Source
{
Address = commandInQueue,
Capabilities = new Symbol[] { new Symbol("queue") }
};
commandInReceiver = new ReceiverLink(amqpSession, "srt-game-server-command-receiver", commandInSource, null);
commandInReceiver.Start(10, CommandReceived);
// anycast queue for the server to receive security events from clients
Source securityInSource = new Source
{
Address = securityInQueue,
Capabilities = new Symbol[] { new Symbol("queue") }
};
securityInReceiver = new ReceiverLink(amqpSession, "srt-game-server-security-receiver", securityInSource, null);
securityInReceiver.Start(10, SecurityReceived);
// DEBUG stuff /////////////////////////////////////////////////////////////
// we send commands to the COMMAND.IN from the debug UI
Target commandInTarget = new Target
{
Address = commandInQueue,
Capabilities = new Symbol[] { new Symbol("queue") }
};
commandInSender = new SenderLink(amqpSession, "srt-game-server-debug-command-sender", commandInTarget, null);
// we send security messages to SECURITY.IN from the debug UI
Target securityInTarget = new Target
{
Address = securityInQueue,
Capabilities = new Symbol[] { new Symbol("queue") }
};
securityInSender = new SenderLink(amqpSession, "srt-game-server-debug-security-sender", securityInTarget, null);
_serilogger.Information("AMQPserver.cs: Finished initializing AMQP connection");
}
public void LoadConfig()
{
var serverConfig = new ConfigFile();
Godot.Error err = serverConfig.Load("Config/server.cfg");
// if the file was loaded successfully, read the vars
if (err == Godot.Error.Ok)
{
url = (String) serverConfig.GetValue("amqp","server_string");
}
// pull values from env -- will get nulls if any vars are not set
String envAMQPUrl = System.Environment.GetEnvironmentVariable("SRT_AMQP_URL");
if (envAMQPUrl != null) url = envAMQPUrl;
_serilogger.Information($"AMQPServer.cs: AMQP url is {url}");
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
MyServer = GetNode<Server>("/root/Server");
_serilogger = MyServer._serilogger;
LoadConfig();
InitializeAMQP();
}
// // Called every frame. 'delta' is the elapsed time since the previous frame.
// public override void _Process(float delta)
// {
//
// }
}