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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "zen.h"
#include <zencore/compactbinarybuilder.h>
#include <zencore/compactbinaryvalidation.h>
#include <zencore/fmtutils.h>
#include <zencore/stream.h>
#include <spdlog/spdlog.h>
#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_IoContext(IoContext)
{
}
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)
{
spdlog::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);
BroadcastSocket.close();
}
else
{
spdlog::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 Sender = 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();
spdlog::info("received hey from {} ({})", Sender, 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();
spdlog::info("received bye from {} ({})", Sender, 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
{
spdlog::warn("received malformed message from {}", Sender);
}
}
break;
default:
spdlog::warn("received malformed data from {}", Sender);
break;
}
IssueReceive();
}
});
}
} // namespace zen
|