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
|
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <atomic>
#include <mutex>
#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;
std::atomic<State> state{State::Idle}; ///< Current state of the RoomMember.
void SetState(const State new_state);
bool IsConnected() const;
std::string nickname; ///< The nickname of this member.
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> receive_thread;
void ReceiveLoop();
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 preferred_mac The preferred MAC address to use in the room, the NoPreferredMac tells
* the server to assign one for us.
*/
void SendJoinRequest(const std::string& nickname,
const MacAddress& preferred_mac = NoPreferredMac);
/**
* 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 RakNet 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);
};
// RoomMemberImpl
void RoomMember::RoomMemberImpl::SetState(const State new_state) {
state = new_state;
// TODO(B3N30): Invoke the callback functions
}
bool RoomMember::RoomMemberImpl::IsConnected() const {
return state == State::Joining || state == State::Joined;
}
void RoomMember::RoomMemberImpl::ReceiveLoop() {
// Receive packets while the connection is open
while (IsConnected()) {
std::lock_guard<std::mutex> lock(network_mutex);
ENetEvent event;
if (enet_host_service(client, &event, 1000) > 0) {
if (event.type == ENET_EVENT_TYPE_RECEIVE) {
switch (event.packet->data[0]) {
// TODO(B3N30): Handle the other message types
case IdChatMessage:
HandleChatPacket(&event);
break;
case IdRoomInformation:
HandleRoomInformationPacket(&event);
break;
case IdJoinSuccess:
// 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
SetState(State::Joined);
break;
case IdNameCollision:
SetState(State::NameCollision);
enet_packet_destroy(event.packet);
enet_peer_disconnect(server, 0);
enet_peer_reset(server);
return;
break;
case IdMacCollision:
SetState(State::MacCollision);
enet_packet_destroy(event.packet);
enet_peer_disconnect(server, 0);
enet_peer_reset(server);
return;
break;
default:
break;
}
enet_packet_destroy(event.packet);
}
}
}
};
void RoomMember::RoomMemberImpl::StartLoop() {
receive_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::ReceiveLoop, this);
}
void RoomMember::RoomMemberImpl::Send(Packet& packet) {
ENetPacket* enetPacket =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(server, 0, enetPacket);
enet_host_flush(client);
}
void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname,
const MacAddress& preferred_mac) {
Packet packet;
packet << static_cast<MessageID>(IdJoinRequest);
packet << nickname;
packet << preferred_mac;
Send(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(MessageID));
RoomInformation info{};
packet >> info.name;
packet >> info.member_slots;
room_information.name = info.name;
room_information.member_slots = info.member_slots;
uint32_t 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_name;
}
// TODO(B3N30): Invoke callbacks
}
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(MessageID));
// Parse the MAC Address from the BitStream
packet >> mac_address;
// TODO(B3N30): Invoke callbacks
}
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(MessageID));
// Parse the WifiPacket from the BitStream
uint8_t 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;
uint32_t data_length;
packet >> data_length;
std::vector<uint8_t> data(data_length);
packet >> data;
wifi_packet.data = std::move(data);
// TODO(B3N30): Invoke callbacks
}
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(MessageID));
ChatEntry chat_entry{};
packet >> chat_entry.nickname;
packet >> chat_entry.message;
// TODO(B3N30): Invoke callbacks
}
// RoomMember
RoomMember::RoomMember() : room_member_impl{std::make_unique<RoomMemberImpl>()} {
room_member_impl->client = enet_host_create(nullptr, 1, NumChannels, 0, 0);
ASSERT_MSG(room_member_impl->client != nullptr, "Could not create client");
}
RoomMember::~RoomMember() {
ASSERT_MSG(!IsConnected(), "RoomMember is being destroyed while connected");
enet_host_destroy(room_member_impl->client);
}
RoomMember::State RoomMember::GetState() const {
return room_member_impl->state;
}
const RoomMember::MemberList& RoomMember::GetMemberInformation() const {
return room_member_impl->member_information;
}
RoomInformation RoomMember::GetRoomInformation() const {
return room_member_impl->room_information;
}
void RoomMember::Join(const std::string& nick, const char* server_addr, u16 server_port,
u16 client_port) {
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::Error);
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->SetState(State::Joining);
room_member_impl->StartLoop();
room_member_impl->SendJoinRequest(nick);
} else {
room_member_impl->SetState(State::CouldNotConnect);
}
}
bool RoomMember::IsConnected() const {
return room_member_impl->IsConnected();
}
void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) {
Packet packet;
packet << static_cast<MessageID>(IdWifiPacket);
packet << static_cast<uint8_t>(wifi_packet.type);
packet << wifi_packet.channel;
packet << wifi_packet.transmitter_address;
packet << wifi_packet.destination_address;
packet << static_cast<uint32_t>(wifi_packet.data.size());
packet << wifi_packet.data;
room_member_impl->Send(packet);
}
void RoomMember::SendChatMessage(const std::string& message) {
Packet packet;
packet << static_cast<MessageID>(IdChatMessage);
packet << message;
room_member_impl->Send(packet);
}
void RoomMember::Leave() {
ASSERT_MSG(room_member_impl->receive_thread != nullptr, "Must be in a room to leave it.");
{
std::lock_guard<std::mutex> lock(room_member_impl->network_mutex);
enet_peer_disconnect(room_member_impl->server, 0);
room_member_impl->SetState(State::Idle);
}
room_member_impl->receive_thread->join();
room_member_impl->receive_thread.reset();
enet_peer_reset(room_member_impl->server);
}
} // namespace Network
|