-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathDipProtocol.cpp
493 lines (400 loc) · 13.5 KB
/
DipProtocol.cpp
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//==============================================================================
//
// DipProtocol.cpp
//
// Copyright (C) 2019-2022 Greg Utas
//
// Diplomacy AI Client - Part of the DAIDE project (www.daide.org.uk).
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the Lesser GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// RSC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the Lesser GNU General Public License
// along with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "DipProtocol.h"
#include <ostream>
#include <string>
#include "BotThread.h"
#include "CliText.h"
#include "Debug.h"
#include "Formatters.h"
#include "Memory.h"
#include "NbAppIds.h"
#include "Singleton.h"
#include "SysIpL3Addr.h"
#include "SysSocket.h"
#include "SysTcpSocket.h"
#include "Token.h"
using std::ostream;
using std::string;
using namespace NetworkBase;
using namespace NodeBase;
//------------------------------------------------------------------------------
namespace Diplomacy
{
void DipHeader::Display(ostream& stream) const
{
stream << "signal: " << int(signal) << CRLF;
stream << "spare: " << int(spare) << CRLF;
stream << "length: " << length << CRLF;
}
//------------------------------------------------------------------------------
void DipMessage::Display(ostream& stream) const
{
switch(header.signal)
{
case IM_MESSAGE:
reinterpret_cast<const IM_Message*>(this)->Display(stream);
break;
case RM_MESSAGE:
reinterpret_cast<const RM_Message*>(this)->Display(stream);
break;
case DM_MESSAGE:
reinterpret_cast<const DM_Message*>(this)->Display(stream);
break;
case FM_MESSAGE:
reinterpret_cast<const FM_Message*>(this)->Display(stream);
break;
case EM_MESSAGE:
reinterpret_cast<const EM_Message*>(this)->Display(stream);
break;
case BM_MESSAGE:
reinterpret_cast<const BM_Message*>(this)->Display(stream);
break;
default:
stream << "Unknown signal: " << int(header.signal) << CRLF;
}
}
//------------------------------------------------------------------------------
void IM_Message::Display(ostream& stream) const
{
stream << "IM message" << CRLF;
header.Display(stream);
stream << "version: " << version << CRLF;
stream << "magic_number: " << strHex(magic_number) << CRLF;
}
//------------------------------------------------------------------------------
void RM_Message::Display(ostream& stream) const
{
stream << "RM message" << CRLF;
header.Display(stream);
}
//------------------------------------------------------------------------------
void DM_Message::Display(ostream& stream) const
{
stream << "DM message" << CRLF;
header.Display(stream);
stream << "tokens:" << CRLF;
auto count = header.length >> 1;
auto segs = count / 10;
for(auto s = 0; s <= segs; ++s)
{
auto begin = s * 10;
auto end = begin + 10;
if(count < end) end = count;
if(begin == end) break;
for(auto t = begin; t < end; ++t)
{
stream << strHex(tokens[t], 4) << SPACE;
}
stream << CRLF;
for(auto t = begin; t < end; ++t)
{
Token token(tokens[t]);
auto str = token.to_str();
auto size = str.size();
auto pad = (size < 6 ? 6 - size : 0);
if(pad > 0) str.insert(0, spaces(pad));
stream << str << SPACE;
}
stream << CRLF;
}
}
//------------------------------------------------------------------------------
void FM_Message::Display(ostream& stream) const
{
stream << "FM message" << CRLF;
header.Display(stream);
}
//------------------------------------------------------------------------------
void EM_Message::Display(ostream& stream) const
{
stream << "EM message" << CRLF;
header.Display(stream);
stream << "error: " << error << CRLF;
}
//------------------------------------------------------------------------------
void BM_Message::Display(ostream& stream) const
{
stream << "BM message" << CRLF;
stream << "signal: " << int(header.signal) << CRLF;
stream << "event: " << int(header.spare) << CRLF;
stream << "length: " << header.length << CRLF;
if(header.length > 0)
{
stream << "bytes: " << CRLF;
strBytes(stream, spaces(2), &first_payload_byte, header.length);
}
}
//==============================================================================
DipInputHandler::DipInputHandler(IpPort* port) : InputHandler(port)
{
Debug::ft("DipInputHandler.ctor");
}
//------------------------------------------------------------------------------
IpBuffer* DipInputHandler::AllocBuff(const byte_t* source,
size_t size, byte_t*& dest, size_t& rcvd, SysTcpSocket* socket) const
{
Debug::ft("DipInputHandler.AllocBuff");
IpBufferPtr buff(socket->AcquireIcMsg());
if(buff == nullptr)
{
// This is the beginning of a new message. Find the PENDING number
// of BYTES that it will contain. We can receive that many bytes,
// but SIZE may be smaller or larger when segmentation or bundling
// occurs. The buffer that we allocate, however, will be able to
// hold the entire message, even if it is segmented.
//
auto header = reinterpret_cast<const DipHeader*>(source);
size_t pending = DipHeaderSize + ntohs(header->length);
rcvd = (pending < size ? pending : size);
buff.reset(new DipIpBuffer(MsgIncoming, pending));
dest = buff->PayloadPtr();
}
else
{
auto payload = buff->PayloadPtr();
auto received = buff->PayloadSize();
auto header = reinterpret_cast<const DipHeader*>(payload);
auto pending = DipHeaderSize + header->length - received;
rcvd = (pending < size ? pending : size);
dest = payload + received;
}
return buff.release();
}
//------------------------------------------------------------------------------
byte_t* DipInputHandler::HostToNetwork
(IpBuffer& buff, byte_t* src, size_t size) const
{
Debug::ft("DipInputHandler.HostToNetwork");
// Some fields are byte-oriented, but most are 16 bits long and
// therefore need to be converted. Conversion is done in place.
//
auto msg = reinterpret_cast<DipHeader*>(buff.PayloadPtr());
switch(msg->signal)
{
case IM_MESSAGE:
{
auto im = reinterpret_cast<IM_Message*>(src);
im->version = htons(im->version);
im->magic_number = htons(im->magic_number);
break;
}
case DM_MESSAGE:
{
auto dm = reinterpret_cast<DM_Message*>(src);
size_t count = msg->length >> 1;
for(size_t i = 0; i < count; ++i)
{
dm->tokens[i] = htons(dm->tokens[i]);
}
break;
}
case EM_MESSAGE:
{
auto em = reinterpret_cast<EM_Message*>(src);
em->error = htons(em->error);
break;
}
}
msg->length = htons(msg->length);
return src;
}
//------------------------------------------------------------------------------
void DipInputHandler::NetworkToHost
(IpBuffer& buff, byte_t* dest, const byte_t* src, size_t size) const
{
Debug::ft("DipInputHandler.NetworkToHost");
// Copy the entire message and then modify it in place, similar to
// HostToNetwork. If this is the FIRST message, convert msg->length
// to host order. If this is the last message (all bytes are present),
// convert the rest of the message now that it is ready for processing.
//
auto dipbuff = static_cast<DipIpBuffer*>(&buff);
auto first = (dipbuff->PayloadSize() == 0);
Memory::Copy(dest, src, size);
dipbuff->BytesAdded(size);
auto msg = reinterpret_cast<DipHeader*>(buff.PayloadPtr());
if(first) msg->length = ntohs(msg->length);
if(dipbuff->PayloadSize() < msg->length) return;
switch(msg->signal)
{
case RM_MESSAGE:
{
auto rm = reinterpret_cast<RM_Message*>(msg);
size_t count = msg->length / 6;
for(size_t i = 0; i < count; ++i)
{
rm->pairs[i].token = ntohs(rm->pairs[i].token);
}
break;
}
case DM_MESSAGE:
{
auto dm = reinterpret_cast<DM_Message*>(msg);
size_t count = msg->length >> 1;
for(size_t i = 0; i < count; ++i)
{
dm->tokens[i] = ntohs(dm->tokens[i]);
}
break;
}
case EM_MESSAGE:
{
auto em = reinterpret_cast<EM_Message*>(msg);
em->error = ntohs(em->error);
break;
}
}
}
//------------------------------------------------------------------------------
void DipInputHandler::ReceiveBuff
(IpBufferPtr& buff, size_t size, Faction faction) const
{
Debug::ft("DipInputHandler.ReceiveBuff");
// If the message is not complete, return it to the socket to await
// more bytes instead of passing it to BotThread for processing.
//
DipIpBufferPtr dipbuff(static_cast<DipIpBuffer*>(buff.release()));
auto payload = dipbuff->PayloadPtr();
auto received = dipbuff->PayloadSize();
auto header = reinterpret_cast<const DipHeader*>(payload);
auto pending = DipHeaderSize + header->length - received;
if(pending == 0)
{
Singleton<BotThread>::Instance()->QueueMsg(dipbuff);
}
else
{
// ADDR has to be obtained before DIPBUFF becomes nullptr...
//
const auto& addr = dipbuff->RxAddr();
addr.GetSocket()->SetIcMsg(dipbuff.release());
}
}
//------------------------------------------------------------------------------
void DipInputHandler::SocketFailed(SysSocket* socket) const
{
Debug::ft("DipInputHandler.SocketFailed");
// Send a message to BotThread, informing it of the failure.
//
DipIpBufferPtr buff(new DipIpBuffer(MsgIncoming, DipHeaderSize));
auto msg = reinterpret_cast<BM_Message*>(buff->PayloadPtr());
msg->header.signal = BM_MESSAGE;
msg->header.spare = SOCKET_FAILURE_EVENT;
msg->header.length = 0;
Singleton<BotThread>::Instance()->QueueMsg(buff);
}
//==============================================================================
BotTcpService::BotTcpService()
{
Debug::ft("BotTcpService.ctor");
}
//------------------------------------------------------------------------------
InputHandler* BotTcpService::CreateHandler(IpPort* port) const
{
Debug::ft("BotTcpService.CreateHandler");
return new DipInputHandler(port);
}
//------------------------------------------------------------------------------
fixed_string BotTcpServiceStr = "DAI/TCP";
fixed_string BotTcpServiceExpl = "Diplomacy AI Protocol";
CliText* BotTcpService::CreateText() const
{
Debug::ft("BotTcpService.CreateText");
return new CliText(BotTcpServiceExpl, BotTcpServiceStr);
}
//------------------------------------------------------------------------------
void BotTcpService::GetAppSocketSizes(size_t& rxSize, size_t& txSize) const
{
Debug::ft("BotTcpService.GetAppSocketSizes");
// Setting txSize to 0 prevents buffering of outgoing messages.
//
rxSize = 2048;
txSize = 0;
}
//==============================================================================
DipIpBuffer::DipIpBuffer(MsgDirection dir, size_t size) :
IpBuffer(dir, 0, size),
currSize_(dir == MsgOutgoing ? size : 0)
{
Debug::ft("DipIpBuffer.ctor");
}
//------------------------------------------------------------------------------
DipIpBuffer::~DipIpBuffer()
{
Debug::ftnt("DipIpBuffer.dtor");
}
//------------------------------------------------------------------------------
DipIpBuffer::DipIpBuffer(const DipIpBuffer& that) : IpBuffer(that),
currSize_(that.currSize_)
{
Debug::ft("DipIpBuffer.ctor(copy)");
}
//------------------------------------------------------------------------------
bool DipIpBuffer::AddBytes(const byte_t* source, size_t size, bool& moved)
{
Debug::ft("DipIpBuffer.AddBytes");
if(!IpBuffer::AddBytes(source, size, moved)) return false;
if(source != nullptr) currSize_ += size;
return true;
}
//------------------------------------------------------------------------------
void DipIpBuffer::BytesAdded(size_t size)
{
Debug::ft("DipIpBuffer.BytesAdded");
currSize_ += size;
}
//------------------------------------------------------------------------------
IpBuffer* DipIpBuffer::Clone() const
{
Debug::ft("DipIpBuffer.Clone");
return new DipIpBuffer(*this);
}
//------------------------------------------------------------------------------
void DipIpBuffer::Display(ostream& stream,
const string& prefix, const Flags& options) const
{
IpBuffer::Display(stream, prefix, options);
stream << prefix << "currSize : " << currSize_ << CRLF;
}
//------------------------------------------------------------------------------
void* DipIpBuffer::operator new(size_t size)
{
Debug::ft("DipIpBuffer.operator new");
return Singleton<DipIpBufferPool>::Instance()->DeqBlock(size);
}
//==============================================================================
constexpr size_t DipIpBufferSize = sizeof(DipIpBuffer);
//------------------------------------------------------------------------------
DipIpBufferPool::DipIpBufferPool() :
ObjectPool(DipIpBufferObjPoolId, MemSlab, DipIpBufferSize, "DipIpBuffers")
{
Debug::ft("DipIpBufferPool.ctor");
}
//------------------------------------------------------------------------------
DipIpBufferPool::~DipIpBufferPool()
{
Debug::ftnt("DipIpBufferPool.dtor");
}
}