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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <atomic>
#include <list>
#include <mutex>
#include <set>
#include <thread>
#include "common/assert.h"
#include "enet/enet.h"
#include "network/packet.h"
#include "network/room_member.h"
namespace Network {
constexpr u32 ConnectionTimeoutMs = 5000;
class RoomMember::RoomMemberImpl {
public:
ENetHost* client = nullptr; ///< ENet network interface.
ENetPeer* server = nullptr; ///< The server peer the client is connected to
/// Information about the clients connected to the same room as us.
MemberList member_information;
/// Information about the room we're connected to.
RoomInformation room_information;
/// The current game name, id and version
GameInfo current_game_info;
std::atomic<State> state{State::Idle}; ///< Current state of the RoomMember.
void SetState(const State new_state);
void SetError(const Error new_error);
bool IsConnected() const;
std::string nickname; ///< The nickname of this member.
std::string username; ///< The username of this member.
mutable std::mutex username_mutex; ///< Mutex for locking username.
MacAddress mac_address; ///< The mac_address of this member.
std::mutex network_mutex; ///< Mutex that controls access to the `client` variable.
/// Thread that receives and dispatches network packets
std::unique_ptr<std::thread> loop_thread;
std::mutex send_list_mutex; ///< Mutex that controls access to the `send_list` variable.
std::list<Packet> send_list; ///< A list that stores all packets to send the async
template <typename T>
using CallbackSet = std::set<CallbackHandle<T>>;
std::mutex callback_mutex; ///< The mutex used for handling callbacks
class Callbacks {
public:
template <typename T>
CallbackSet<T>& Get();
private:
CallbackSet<WifiPacket> callback_set_wifi_packet;
CallbackSet<ChatEntry> callback_set_chat_messages;
CallbackSet<StatusMessageEntry> callback_set_status_messages;
CallbackSet<RoomInformation> callback_set_room_information;
CallbackSet<State> callback_set_state;
CallbackSet<Error> callback_set_error;
CallbackSet<Room::BanList> callback_set_ban_list;
};
Callbacks callbacks; ///< All CallbackSets to all events
void MemberLoop();
void StartLoop();
/**
* Sends data to the room. It will be send on channel 0 with flag RELIABLE
* @param packet The data to send
*/
void Send(Packet&& packet);
/**
* Sends a request to the server, asking for permission to join a room with the specified
* nickname and preferred mac.
* @params nickname The desired nickname.
* @params console_id_hash A hash of the Console ID.
* @params preferred_mac The preferred MAC address to use in the room, the NoPreferredMac tells
* @params password The password for the room
* the server to assign one for us.
*/
void SendJoinRequest(const std::string& nickname_, const std::string& console_id_hash,
const MacAddress& preferred_mac = NoPreferredMac,
const std::string& password = "", const std::string& token = "");
/**
* Extracts a MAC Address from a received ENet packet.
* @param event The ENet event that was received.
*/
void HandleJoinPacket(const ENetEvent* event);
/**
* Extracts RoomInformation and MemberInformation from a received ENet packet.
* @param event The ENet event that was received.
*/
void HandleRoomInformationPacket(const ENetEvent* event);
/**
* Extracts a WifiPacket from a received ENet packet.
* @param event The ENet event that was received.
*/
void HandleWifiPackets(const ENetEvent* event);
/**
* Extracts a chat entry from a received ENet packet and adds it to the chat queue.
* @param event The ENet event that was received.
*/
void HandleChatPacket(const ENetEvent* event);
/**
* Extracts a system message entry from a received ENet packet and adds it to the system message
* queue.
* @param event The ENet event that was received.
*/
void HandleStatusMessagePacket(const ENetEvent* event);
/**
* Extracts a ban list request response from a received ENet packet.
* @param event The ENet event that was received.
*/
void HandleModBanListResponsePacket(const ENetEvent* event);
/**
* Disconnects the RoomMember from the Room
*/
void Disconnect();
template <typename T>
void Invoke(const T& data);
template <typename T>
CallbackHandle<T> Bind(std::function<void(const T&)> callback);
};
// RoomMemberImpl
void RoomMember::RoomMemberImpl::SetState(const State new_state) {
if (state != new_state) {
state = new_state;
Invoke<State>(state);
}
}
void RoomMember::RoomMemberImpl::SetError(const Error new_error) {
Invoke<Error>(new_error);
}
bool RoomMember::RoomMemberImpl::IsConnected() const {
return state == State::Joining || state == State::Joined || state == State::Moderator;
}
void RoomMember::RoomMemberImpl::MemberLoop() {
// Receive packets while the connection is open
while (IsConnected()) {
std::lock_guard lock(network_mutex);
ENetEvent event;
if (enet_host_service(client, &event, 16) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE:
switch (event.packet->data[0]) {
case IdWifiPacket:
HandleWifiPackets(&event);
break;
case IdChatMessage:
HandleChatPacket(&event);
break;
case IdStatusMessage:
HandleStatusMessagePacket(&event);
break;
case IdRoomInformation:
HandleRoomInformationPacket(&event);
break;
case IdJoinSuccess:
case IdJoinSuccessAsMod:
// The join request was successful, we are now in the room.
// If we joined successfully, there must be at least one client in the room: us.
ASSERT_MSG(member_information.size() > 0,
"We have not yet received member information.");
HandleJoinPacket(&event); // Get the MAC Address for the client
if (event.packet->data[0] == IdJoinSuccessAsMod) {
SetState(State::Moderator);
} else {
SetState(State::Joined);
}
break;
case IdModBanListResponse:
HandleModBanListResponsePacket(&event);
break;
case IdRoomIsFull:
SetState(State::Idle);
SetError(Error::RoomIsFull);
break;
case IdNameCollision:
SetState(State::Idle);
SetError(Error::NameCollision);
break;
case IdMacCollision:
SetState(State::Idle);
SetError(Error::MacCollision);
break;
case IdConsoleIdCollision:
SetState(State::Idle);
SetError(Error::ConsoleIdCollision);
break;
case IdVersionMismatch:
SetState(State::Idle);
SetError(Error::WrongVersion);
break;
case IdWrongPassword:
SetState(State::Idle);
SetError(Error::WrongPassword);
break;
case IdCloseRoom:
SetState(State::Idle);
SetError(Error::LostConnection);
break;
case IdHostKicked:
SetState(State::Idle);
SetError(Error::HostKicked);
break;
case IdHostBanned:
SetState(State::Idle);
SetError(Error::HostBanned);
break;
case IdModPermissionDenied:
SetError(Error::PermissionDenied);
break;
case IdModNoSuchUser:
SetError(Error::NoSuchUser);
break;
}
enet_packet_destroy(event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
if (state == State::Joined || state == State::Moderator) {
SetState(State::Idle);
SetError(Error::LostConnection);
}
break;
case ENET_EVENT_TYPE_NONE:
break;
case ENET_EVENT_TYPE_CONNECT:
// The ENET_EVENT_TYPE_CONNECT event can not possibly happen here because we're
// already connected
ASSERT_MSG(false, "Received unexpected connect event while already connected");
break;
}
}
std::list<Packet> packets;
{
std::lock_guard send_lock(send_list_mutex);
packets.swap(send_list);
}
for (const auto& packet : packets) {
ENetPacket* enetPacket = enet_packet_create(packet.GetData(), packet.GetDataSize(),
ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(server, 0, enetPacket);
}
enet_host_flush(client);
}
Disconnect();
};
void RoomMember::RoomMemberImpl::StartLoop() {
loop_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::MemberLoop, this);
}
void RoomMember::RoomMemberImpl::Send(Packet&& packet) {
std::lock_guard lock(send_list_mutex);
send_list.push_back(std::move(packet));
}
void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname_,
const std::string& console_id_hash,
const MacAddress& preferred_mac,
const std::string& password,
const std::string& token) {
Packet packet;
packet << static_cast<u8>(IdJoinRequest);
packet << nickname_;
packet << console_id_hash;
packet << preferred_mac;
packet << network_version;
packet << password;
packet << token;
Send(std::move(packet));
}
void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* event) {
Packet packet;
packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id.
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
RoomInformation info{};
packet >> info.name;
packet >> info.description;
packet >> info.member_slots;
packet >> info.port;
packet >> info.preferred_game.name;
packet >> info.host_username;
room_information.name = info.name;
room_information.description = info.description;
room_information.member_slots = info.member_slots;
room_information.port = info.port;
room_information.preferred_game = info.preferred_game;
room_information.host_username = info.host_username;
u32 num_members;
packet >> num_members;
member_information.resize(num_members);
for (auto& member : member_information) {
packet >> member.nickname;
packet >> member.mac_address;
packet >> member.game_info.name;
packet >> member.game_info.id;
packet >> member.username;
packet >> member.display_name;
packet >> member.avatar_url;
{
std::lock_guard lock(username_mutex);
if (member.nickname == nickname) {
username = member.username;
}
}
}
Invoke(room_information);
}
void RoomMember::RoomMemberImpl::HandleJoinPacket(const ENetEvent* event) {
Packet packet;
packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id.
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
// Parse the MAC Address from the packet
packet >> mac_address;
}
void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) {
WifiPacket wifi_packet{};
Packet packet;
packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id.
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
// Parse the WifiPacket from the packet
u8 frame_type;
packet >> frame_type;
WifiPacket::PacketType type = static_cast<WifiPacket::PacketType>(frame_type);
wifi_packet.type = type;
packet >> wifi_packet.channel;
packet >> wifi_packet.transmitter_address;
packet >> wifi_packet.destination_address;
packet >> wifi_packet.data;
Invoke<WifiPacket>(wifi_packet);
}
void RoomMember::RoomMemberImpl::HandleChatPacket(const ENetEvent* event) {
Packet packet;
packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id.
packet.IgnoreBytes(sizeof(u8));
ChatEntry chat_entry{};
packet >> chat_entry.nickname;
packet >> chat_entry.username;
packet >> chat_entry.message;
Invoke<ChatEntry>(chat_entry);
}
void RoomMember::RoomMemberImpl::HandleStatusMessagePacket(const ENetEvent* event) {
Packet packet;
packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id.
packet.IgnoreBytes(sizeof(u8));
StatusMessageEntry status_message_entry{};
u8 type{};
packet >> type;
status_message_entry.type = static_cast<StatusMessageTypes>(type);
packet >> status_message_entry.nickname;
packet >> status_message_entry.username;
Invoke<StatusMessageEntry>(status_message_entry);
}
void RoomMember::RoomMemberImpl::HandleModBanListResponsePacket(const ENetEvent* event) {
Packet packet;
packet.Append(event->packet->data, event->packet->dataLength);
// Ignore the first byte, which is the message id.
packet.IgnoreBytes(sizeof(u8));
Room::BanList ban_list = {};
packet >> ban_list.first;
packet >> ban_list.second;
Invoke<Room::BanList>(ban_list);
}
void RoomMember::RoomMemberImpl::Disconnect() {
member_information.clear();
room_information.member_slots = 0;
room_information.name.clear();
if (!server)
return;
enet_peer_disconnect(server, 0);
ENetEvent event;
while (enet_host_service(client, &event, ConnectionTimeoutMs) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE:
enet_packet_destroy(event.packet); // Ignore all incoming data
break;
case ENET_EVENT_TYPE_DISCONNECT:
server = nullptr;
return;
case ENET_EVENT_TYPE_NONE:
case ENET_EVENT_TYPE_CONNECT:
break;
}
}
// didn't disconnect gracefully force disconnect
enet_peer_reset(server);
server = nullptr;
}
template <>
RoomMember::RoomMemberImpl::CallbackSet<WifiPacket>& RoomMember::RoomMemberImpl::Callbacks::Get() {
return callback_set_wifi_packet;
}
template <>
RoomMember::RoomMemberImpl::CallbackSet<RoomMember::State>&
RoomMember::RoomMemberImpl::Callbacks::Get() {
return callback_set_state;
}
template <>
RoomMember::RoomMemberImpl::CallbackSet<RoomMember::Error>&
RoomMember::RoomMemberImpl::Callbacks::Get() {
return callback_set_error;
}
template <>
RoomMember::RoomMemberImpl::CallbackSet<RoomInformation>&
RoomMember::RoomMemberImpl::Callbacks::Get() {
return callback_set_room_information;
}
template <>
RoomMember::RoomMemberImpl::CallbackSet<ChatEntry>& RoomMember::RoomMemberImpl::Callbacks::Get() {
return callback_set_chat_messages;
}
template <>
RoomMember::RoomMemberImpl::CallbackSet<StatusMessageEntry>&
RoomMember::RoomMemberImpl::Callbacks::Get() {
return callback_set_status_messages;
}
template <>
RoomMember::RoomMemberImpl::CallbackSet<Room::BanList>&
RoomMember::RoomMemberImpl::Callbacks::Get() {
return callback_set_ban_list;
}
template <typename T>
void RoomMember::RoomMemberImpl::Invoke(const T& data) {
std::lock_guard lock(callback_mutex);
CallbackSet<T> callback_set = callbacks.Get<T>();
for (auto const& callback : callback_set)
(*callback)(data);
}
template <typename T>
RoomMember::CallbackHandle<T> RoomMember::RoomMemberImpl::Bind(
std::function<void(const T&)> callback) {
std::lock_guard lock(callback_mutex);
CallbackHandle<T> handle;
handle = std::make_shared<std::function<void(const T&)>>(callback);
callbacks.Get<T>().insert(handle);
return handle;
}
// RoomMember
RoomMember::RoomMember() : room_member_impl{std::make_unique<RoomMemberImpl>()} {}
RoomMember::~RoomMember() {
ASSERT_MSG(!IsConnected(), "RoomMember is being destroyed while connected");
if (room_member_impl->loop_thread) {
Leave();
}
}
RoomMember::State RoomMember::GetState() const {
return room_member_impl->state;
}
const RoomMember::MemberList& RoomMember::GetMemberInformation() const {
return room_member_impl->member_information;
}
const std::string& RoomMember::GetNickname() const {
return room_member_impl->nickname;
}
const std::string& RoomMember::GetUsername() const {
std::lock_guard lock(room_member_impl->username_mutex);
return room_member_impl->username;
}
const MacAddress& RoomMember::GetMacAddress() const {
ASSERT_MSG(IsConnected(), "Tried to get MAC address while not connected");
return room_member_impl->mac_address;
}
RoomInformation RoomMember::GetRoomInformation() const {
return room_member_impl->room_information;
}
void RoomMember::Join(const std::string& nick, const std::string& console_id_hash,
const char* server_addr, u16 server_port, u16 client_port,
const MacAddress& preferred_mac, const std::string& password,
const std::string& token) {
// If the member is connected, kill the connection first
if (room_member_impl->loop_thread && room_member_impl->loop_thread->joinable()) {
Leave();
}
// If the thread isn't running but the ptr still exists, reset it
else if (room_member_impl->loop_thread) {
room_member_impl->loop_thread.reset();
}
if (!room_member_impl->client) {
room_member_impl->client = enet_host_create(nullptr, 1, NumChannels, 0, 0);
ASSERT_MSG(room_member_impl->client != nullptr, "Could not create client");
}
room_member_impl->SetState(State::Joining);
ENetAddress address{};
enet_address_set_host(&address, server_addr);
address.port = server_port;
room_member_impl->server =
enet_host_connect(room_member_impl->client, &address, NumChannels, 0);
if (!room_member_impl->server) {
room_member_impl->SetState(State::Idle);
room_member_impl->SetError(Error::UnknownError);
return;
}
ENetEvent event{};
int net = enet_host_service(room_member_impl->client, &event, ConnectionTimeoutMs);
if (net > 0 && event.type == ENET_EVENT_TYPE_CONNECT) {
room_member_impl->nickname = nick;
room_member_impl->StartLoop();
room_member_impl->SendJoinRequest(nick, console_id_hash, preferred_mac, password, token);
SendGameInfo(room_member_impl->current_game_info);
} else {
enet_peer_disconnect(room_member_impl->server, 0);
room_member_impl->SetState(State::Idle);
room_member_impl->SetError(Error::CouldNotConnect);
}
}
bool RoomMember::IsConnected() const {
return room_member_impl->IsConnected();
}
void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) {
Packet packet;
packet << static_cast<u8>(IdWifiPacket);
packet << static_cast<u8>(wifi_packet.type);
packet << wifi_packet.channel;
packet << wifi_packet.transmitter_address;
packet << wifi_packet.destination_address;
packet << wifi_packet.data;
room_member_impl->Send(std::move(packet));
}
void RoomMember::SendChatMessage(const std::string& message) {
Packet packet;
packet << static_cast<u8>(IdChatMessage);
packet << message;
room_member_impl->Send(std::move(packet));
}
void RoomMember::SendGameInfo(const GameInfo& game_info) {
room_member_impl->current_game_info = game_info;
if (!IsConnected())
return;
Packet packet;
packet << static_cast<u8>(IdSetGameInfo);
packet << game_info.name;
packet << game_info.id;
room_member_impl->Send(std::move(packet));
}
void RoomMember::SendModerationRequest(RoomMessageTypes type, const std::string& nickname) {
ASSERT_MSG(type == IdModKick || type == IdModBan || type == IdModUnban,
"type is not a moderation request");
if (!IsConnected())
return;
Packet packet;
packet << static_cast<u8>(type);
packet << nickname;
room_member_impl->Send(std::move(packet));
}
void RoomMember::RequestBanList() {
if (!IsConnected())
return;
Packet packet;
packet << static_cast<u8>(IdModGetBanList);
room_member_impl->Send(std::move(packet));
}
RoomMember::CallbackHandle<RoomMember::State> RoomMember::BindOnStateChanged(
std::function<void(const RoomMember::State&)> callback) {
return room_member_impl->Bind(callback);
}
RoomMember::CallbackHandle<RoomMember::Error> RoomMember::BindOnError(
std::function<void(const RoomMember::Error&)> callback) {
return room_member_impl->Bind(callback);
}
RoomMember::CallbackHandle<WifiPacket> RoomMember::BindOnWifiPacketReceived(
std::function<void(const WifiPacket&)> callback) {
return room_member_impl->Bind(callback);
}
RoomMember::CallbackHandle<RoomInformation> RoomMember::BindOnRoomInformationChanged(
std::function<void(const RoomInformation&)> callback) {
return room_member_impl->Bind(callback);
}
RoomMember::CallbackHandle<ChatEntry> RoomMember::BindOnChatMessageRecieved(
std::function<void(const ChatEntry&)> callback) {
return room_member_impl->Bind(callback);
}
RoomMember::CallbackHandle<StatusMessageEntry> RoomMember::BindOnStatusMessageReceived(
std::function<void(const StatusMessageEntry&)> callback) {
return room_member_impl->Bind(callback);
}
RoomMember::CallbackHandle<Room::BanList> RoomMember::BindOnBanListReceived(
std::function<void(const Room::BanList&)> callback) {
return room_member_impl->Bind(callback);
}
template <typename T>
void RoomMember::Unbind(CallbackHandle<T> handle) {
std::lock_guard lock(room_member_impl->callback_mutex);
room_member_impl->callbacks.Get<T>().erase(handle);
}
void RoomMember::Leave() {
room_member_impl->SetState(State::Idle);
room_member_impl->loop_thread->join();
room_member_impl->loop_thread.reset();
enet_host_destroy(room_member_impl->client);
room_member_impl->client = nullptr;
}
template void RoomMember::Unbind(CallbackHandle<WifiPacket>);
template void RoomMember::Unbind(CallbackHandle<RoomMember::State>);
template void RoomMember::Unbind(CallbackHandle<RoomMember::Error>);
template void RoomMember::Unbind(CallbackHandle<RoomInformation>);
template void RoomMember::Unbind(CallbackHandle<ChatEntry>);
template void RoomMember::Unbind(CallbackHandle<StatusMessageEntry>);
template void RoomMember::Unbind(CallbackHandle<Room::BanList>);
} // namespace Network
|