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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "zen.h"
#include <zencore/compactbinarybuilder.h>
#include <zencore/compactbinaryvalidation.h>
#include <zencore/fmtutils.h>
#include <zencore/session.h>
#include <zencore/stream.h>
#include "cache/structuredcachestore.h"
#include "diag/formatters.h"
#include "diag/logging.h"
// cpr ////////////////////////////////////////////////////////////////////
//
// For some reason, these don't seem to stick, so we disable the warnings
//# define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING 1
//# define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS 1
#pragma warning(push)
#pragma warning(disable : 4004)
#pragma warning(disable : 4996)
#include <cpr/cpr.h>
#pragma warning(pop)
#include <xxhash.h>
#include <gsl/gsl-lite.hpp>
namespace zen {
namespace detail {
struct MessageHeader
{
static const uint32_t kMagic = 0x11'99'77'22;
uint32_t Magic = kMagic;
uint32_t Checksum = 0;
uint16_t MessageSize = 0; // Size *including* this field and the reserved field
uint16_t Reserved = 0;
void SetPayload(const void* PayloadData, uint64_t PayloadSize)
{
memcpy(Payload(), PayloadData, PayloadSize);
MessageSize = gsl::narrow<uint16_t>(PayloadSize + sizeof MessageSize + sizeof Reserved);
Checksum = ComputeChecksum();
}
inline CbObject GetMessage() const
{
if (IsOk())
{
MemoryView MessageView(Payload(), MessageSize - sizeof MessageSize - sizeof Reserved);
CbValidateError ValidationResult = ValidateCompactBinary(MessageView, CbValidateMode::All);
if (ValidationResult == CbValidateError::None)
{
return CbObject{SharedBuffer::MakeView(MessageView)};
}
}
return {};
}
uint32_t TotalSize() const { return MessageSize + sizeof Checksum + sizeof Magic; }
uint32_t ComputeChecksum() const { return gsl::narrow_cast<uint32_t>(XXH3_64bits(&MessageSize, MessageSize)); }
inline bool IsOk() const { return Magic == kMagic && Checksum == ComputeChecksum(); }
private:
inline void* Payload() { return &Reserved + 1; }
inline const void* Payload() const { return &Reserved + 1; }
};
} // namespace detail
// Note that currently this just implements an UDP echo service for testing purposes
Mesh::Mesh(asio::io_context& IoContext) : m_Log(logging::Get("mesh")), m_IoContext(IoContext), m_SessionId(zen::GetSessionId())
{
}
Mesh::~Mesh()
{
Stop();
}
void
Mesh::Start(uint16_t Port)
{
ZEN_ASSERT(Port);
ZEN_ASSERT(m_Port == 0);
m_Port = Port;
m_UdpSocket = std::make_unique<asio::ip::udp::socket>(m_IoContext, asio::ip::udp::endpoint(asio::ip::udp::v4(), m_Port));
m_Thread = std::make_unique<std::thread>([this] { Run(); });
};
void
Mesh::Stop()
{
using namespace std::literals;
if (!m_Port)
{
// Never started, nothing to do here
return;
}
CbObjectWriter Msg;
Msg << "bye"sv << m_SessionId;
BroadcastPacket(Msg);
m_State = kExiting;
std::error_code Ec;
m_Timer.cancel(Ec);
m_UdpSocket->close(Ec);
m_IoContext.stop();
if (m_Thread)
{
m_Thread->join();
m_Thread.reset();
}
}
void
Mesh::EnqueueTick()
{
m_Timer.expires_after(std::chrono::seconds(10));
m_Timer.async_wait([&](const std::error_code& Ec) {
if (!Ec)
{
OnTick();
}
else
{
if (m_State != kExiting)
{
ZEN_WARN("Mesh timer error: {}", Ec.message());
}
}
});
}
void
Mesh::OnTick()
{
using namespace std::literals;
CbObjectWriter Msg;
// Basic service information
Msg.BeginArray("s");
Msg << m_SessionId << m_Port << /* event sequence # */ uint32_t(0);
Msg.EndArray();
BroadcastPacket(Msg);
EnqueueTick();
}
void
Mesh::BroadcastPacket(CbObjectWriter& Obj)
{
std::error_code ErrorCode;
asio::ip::udp::socket BroadcastSocket(m_IoContext);
BroadcastSocket.open(asio::ip::udp::v4(), ErrorCode);
if (!ErrorCode)
{
BroadcastSocket.set_option(asio::ip::udp::socket::reuse_address(true));
BroadcastSocket.set_option(asio::socket_base::broadcast(true));
asio::ip::udp::endpoint BroadcastEndpoint(asio::ip::address_v4::broadcast(), m_Port);
uint8_t MessageBuffer[kMaxMessageSize];
detail::MessageHeader* Message = reinterpret_cast<detail::MessageHeader*>(MessageBuffer);
*Message = {};
MemoryOutStream MemOut;
BinaryWriter Writer(MemOut);
Obj.Save(Writer);
// TODO: check that it fits in a packet!
Message->SetPayload(MemOut.Data(), MemOut.Size());
BroadcastSocket.send_to(asio::buffer(Message, Message->TotalSize()), BroadcastEndpoint, 0, ErrorCode);
if (!ErrorCode)
{
BroadcastSocket.close(ErrorCode);
}
if (ErrorCode)
{
ZEN_WARN("packet broadcast failed: {}", ErrorCode.message());
}
}
else
{
ZEN_WARN("failed to open broadcast socket: {}", ErrorCode.message());
}
}
void
Mesh::Run()
{
m_State = kRunning;
EnqueueTick();
IssueReceive();
m_IoContext.run();
}
void
Mesh::IssueReceive()
{
using namespace std::literals;
m_UdpSocket->async_receive_from(
asio::buffer(m_MessageBuffer, sizeof m_MessageBuffer),
m_SenderEndpoint,
[this](std::error_code ec, size_t BytesReceived) {
if (!ec && BytesReceived)
{
std::error_code ErrorCode;
std::string SenderIp = m_SenderEndpoint.address().to_string(ErrorCode);
// Process message
uint32_t& Magic = *reinterpret_cast<uint32_t*>(m_MessageBuffer);
switch (Magic)
{
case detail::MessageHeader::kMagic:
{
detail::MessageHeader& Header = *reinterpret_cast<detail::MessageHeader*>(m_MessageBuffer);
if (CbObject Msg = Header.GetMessage())
{
const asio::ip::address& Ip = m_SenderEndpoint.address();
if (auto Field = Msg["s"sv])
{
// Announce
CbArrayView Ci = Field.AsArrayView();
auto It = Ci.CreateViewIterator();
const Oid SessionId = It->AsObjectId();
if (SessionId != Oid::Zero && SessionId != m_SessionId)
{
// const uint16_t Port = (++It)->AsUInt16(m_SenderEndpoint.port());
// const uint32_t Lsn = (++It)->AsUInt32();
ZEN_TRACE("received hey from {} ({})", SenderIp, SessionId);
RwLock::ExclusiveLockScope _(m_SessionsLock);
PeerInfo& Info = m_KnownPeers[SessionId];
Info.LastSeen = std::time(nullptr);
Info.SessionId = SessionId;
if (std::find(begin(Info.SeenOnIP), end(Info.SeenOnIP), Ip) == Info.SeenOnIP.end())
{
Info.SeenOnIP.push_back(Ip);
}
}
}
else if (auto Bye = Msg["bye"sv])
{
Oid SessionId = Field.AsObjectId();
ZEN_DEBUG("received bye from {} ({})", SenderIp, SessionId);
// We could verify that it's sent from a known IP before erasing the
// session, if we want to be paranoid
RwLock::ExclusiveLockScope _(m_SessionsLock);
m_KnownPeers.erase(SessionId);
}
else
{
// Unknown message type, just ignore
}
}
else
{
ZEN_WARN("received malformed message from {}", SenderIp);
}
}
break;
default:
ZEN_WARN("received malformed data from {}", SenderIp);
break;
}
}
IssueReceive();
});
}
//////////////////////////////////////////////////////////////////////////
namespace detail {
struct ZenCacheSessionState
{
ZenCacheSessionState(ZenStructuredCacheClient& Client) : OwnerClient(Client) {}
~ZenCacheSessionState() {}
void Reset() {}
ZenStructuredCacheClient& OwnerClient;
cpr::Session Session;
};
} // namespace detail
//////////////////////////////////////////////////////////////////////////
ZenStructuredCacheClient::ZenStructuredCacheClient(std::string_view ServiceUrl) : m_ServiceUrl(ServiceUrl)
{
}
ZenStructuredCacheClient::~ZenStructuredCacheClient()
{
}
detail::ZenCacheSessionState*
ZenStructuredCacheClient::AllocSessionState()
{
detail::ZenCacheSessionState* State = nullptr;
if (RwLock::ExclusiveLockScope _(m_SessionStateLock); !m_SessionStateCache.empty())
{
State = m_SessionStateCache.front();
m_SessionStateCache.pop_front();
}
if (State == nullptr)
{
State = new detail::ZenCacheSessionState(*this);
}
State->Reset();
return State;
}
void
ZenStructuredCacheClient::FreeSessionState(detail::ZenCacheSessionState* State)
{
RwLock::ExclusiveLockScope _(m_SessionStateLock);
m_SessionStateCache.push_front(State);
}
//////////////////////////////////////////////////////////////////////////
using namespace std::literals;
ZenStructuredCacheSession::ZenStructuredCacheSession(ZenStructuredCacheClient& OuterClient)
: m_Log(zen::logging::Get("zenclient"sv))
, m_Client(OuterClient)
{
m_SessionState = m_Client.AllocSessionState();
}
ZenStructuredCacheSession::~ZenStructuredCacheSession()
{
m_Client.FreeSessionState(m_SessionState);
}
ZenCacheResult
ZenStructuredCacheSession::SayHello()
{
ExtendableStringBuilder<256> Uri;
Uri << m_Client.ServiceUrl() << "/test/hello";
cpr::Session& Session = m_SessionState->Session;
Session.SetOption(cpr::Url{Uri.c_str()});
cpr::Response Response = Session.Get();
return {.Bytes = Response.downloaded_bytes, .ElapsedSeconds = Response.elapsed, .Success = Response.status_code == 200};
}
ZenCacheResult
ZenStructuredCacheSession::GetCacheRecord(std::string_view BucketId, const IoHash& Key, ZenContentType Type)
{
ExtendableStringBuilder<256> Uri;
Uri << m_Client.ServiceUrl() << "/z$/" << BucketId << "/" << Key.ToHexString();
cpr::Session& Session = m_SessionState->Session;
Session.SetOption(cpr::Url{Uri.c_str()});
Session.SetHeader(cpr::Header{{"Accept",
Type == ZenContentType::kCbPackage ? "application/x-ue-cbpkg"
: Type == ZenContentType::kCbObject ? "application/x-ue-cb"
: "application/octet-stream"}});
cpr::Response Response = Session.Get();
ZEN_DEBUG("GET {}", Response);
const bool Success = Response.status_code == 200;
const IoBuffer Buffer = Success ? IoBufferBuilder::MakeCloneFromMemory(Response.text.data(), Response.text.size()) : IoBuffer();
return {.Response = Buffer, .Bytes = Response.downloaded_bytes, .ElapsedSeconds = Response.elapsed, .Success = Success};
}
ZenCacheResult
ZenStructuredCacheSession::GetCachePayload(std::string_view BucketId, const IoHash& Key, const IoHash& PayloadId)
{
ExtendableStringBuilder<256> Uri;
Uri << m_Client.ServiceUrl() << "/z$/" << BucketId << "/" << Key.ToHexString() << "/" << PayloadId.ToHexString();
cpr::Session& Session = m_SessionState->Session;
Session.SetOption(cpr::Url{Uri.c_str()});
Session.SetHeader(cpr::Header{{"Accept", "application/x-ue-comp"}});
cpr::Response Response = Session.Get();
ZEN_DEBUG("GET {}", Response);
const bool Success = Response.status_code == 200;
const IoBuffer Buffer = Success ? IoBufferBuilder::MakeCloneFromMemory(Response.text.data(), Response.text.size()) : IoBuffer();
return {.Response = Buffer, .Bytes = Response.downloaded_bytes, .ElapsedSeconds = Response.elapsed, .Success = Success};
}
ZenCacheResult
ZenStructuredCacheSession::PutCacheRecord(std::string_view BucketId, const IoHash& Key, IoBuffer Value, ZenContentType Type)
{
ExtendableStringBuilder<256> Uri;
Uri << m_Client.ServiceUrl() << "/z$/" << BucketId << "/" << Key.ToHexString();
cpr::Session& Session = m_SessionState->Session;
Session.SetOption(cpr::Url{Uri.c_str()});
Session.SetHeader(cpr::Header{{"Content-Type",
Type == ZenContentType::kCbPackage ? "application/x-ue-cbpkg"
: Type == ZenContentType::kCbObject ? "application/x-ue-cb"
: "application/octet-stream"}});
Session.SetBody(cpr::Body{static_cast<const char*>(Value.Data()), Value.Size()});
cpr::Response Response = Session.Put();
ZEN_DEBUG("PUT {}", Response);
return {.Bytes = Response.uploaded_bytes,
.ElapsedSeconds = Response.elapsed,
.Success = (Response.status_code == 200 || Response.status_code == 201)};
}
ZenCacheResult
ZenStructuredCacheSession::PutCachePayload(std::string_view BucketId, const IoHash& Key, const IoHash& PayloadId, IoBuffer Payload)
{
ExtendableStringBuilder<256> Uri;
Uri << m_Client.ServiceUrl() << "/z$/" << BucketId << "/" << Key.ToHexString() << "/" << PayloadId.ToHexString();
cpr::Session& Session = m_SessionState->Session;
Session.SetOption(cpr::Url{Uri.c_str()});
Session.SetHeader(cpr::Header{{"Content-Type", "application/x-ue-comp"}});
Session.SetBody(cpr::Body{static_cast<const char*>(Payload.Data()), Payload.Size()});
cpr::Response Response = Session.Put();
ZEN_DEBUG("PUT {}", Response);
return {.Bytes = Response.uploaded_bytes,
.ElapsedSeconds = Response.elapsed,
.Success = (Response.status_code == 200 || Response.status_code == 201)};
}
} // namespace zen
|