summaryrefslogtreecommitdiff
path: root/src/network/room_member.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/network/room_member.cpp')
-rw-r--r--src/network/room_member.cpp52
1 files changed, 25 insertions, 27 deletions
diff --git a/src/network/room_member.cpp b/src/network/room_member.cpp
index 8fd226ba5..dac9bacae 100644
--- a/src/network/room_member.cpp
+++ b/src/network/room_member.cpp
@@ -38,13 +38,15 @@ public:
38 std::mutex send_list_mutex; ///< Mutex that controls access to the `send_list` variable. 38 std::mutex send_list_mutex; ///< Mutex that controls access to the `send_list` variable.
39 std::list<Packet> send_list; ///< A list that stores all packets to send the async 39 std::list<Packet> send_list; ///< A list that stores all packets to send the async
40 void MemberLoop(); 40 void MemberLoop();
41
41 void StartLoop(); 42 void StartLoop();
42 43
43 /** 44 /**
44 * Sends data to the room. It will be send on channel 0 with flag RELIABLE 45 * Sends data to the room. It will be send on channel 0 with flag RELIABLE
45 * @param packet The data to send 46 * @param packet The data to send
46 */ 47 */
47 void Send(Packet& packet); 48 void Send(Packet&& packet);
49
48 /** 50 /**
49 * Sends a request to the server, asking for permission to join a room with the specified 51 * Sends a request to the server, asking for permission to join a room with the specified
50 * nickname and preferred mac. 52 * nickname and preferred mac.
@@ -99,11 +101,13 @@ void RoomMember::RoomMemberImpl::MemberLoop() {
99 while (IsConnected()) { 101 while (IsConnected()) {
100 std::lock_guard<std::mutex> lock(network_mutex); 102 std::lock_guard<std::mutex> lock(network_mutex);
101 ENetEvent event; 103 ENetEvent event;
102 if (enet_host_service(client, &event, 1000) > 0) { 104 if (enet_host_service(client, &event, 100) > 0) {
103 switch (event.type) { 105 switch (event.type) {
104 case ENET_EVENT_TYPE_RECEIVE: 106 case ENET_EVENT_TYPE_RECEIVE:
105 switch (event.packet->data[0]) { 107 switch (event.packet->data[0]) {
106 // TODO(B3N30): Handle the other message types 108 case IdWifiPacket:
109 HandleWifiPackets(&event);
110 break;
107 case IdChatMessage: 111 case IdChatMessage:
108 HandleChatPacket(&event); 112 HandleChatPacket(&event);
109 break; 113 break;
@@ -130,8 +134,6 @@ void RoomMember::RoomMemberImpl::MemberLoop() {
130 case IdCloseRoom: 134 case IdCloseRoom:
131 SetState(State::LostConnection); 135 SetState(State::LostConnection);
132 break; 136 break;
133 default:
134 break;
135 } 137 }
136 enet_packet_destroy(event.packet); 138 enet_packet_destroy(event.packet);
137 break; 139 break;
@@ -158,7 +160,7 @@ void RoomMember::RoomMemberImpl::StartLoop() {
158 loop_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::MemberLoop, this); 160 loop_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::MemberLoop, this);
159} 161}
160 162
161void RoomMember::RoomMemberImpl::Send(Packet& packet) { 163void RoomMember::RoomMemberImpl::Send(Packet&& packet) {
162 std::lock_guard<std::mutex> lock(send_list_mutex); 164 std::lock_guard<std::mutex> lock(send_list_mutex);
163 send_list.push_back(std::move(packet)); 165 send_list.push_back(std::move(packet));
164} 166}
@@ -166,11 +168,11 @@ void RoomMember::RoomMemberImpl::Send(Packet& packet) {
166void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname, 168void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname,
167 const MacAddress& preferred_mac) { 169 const MacAddress& preferred_mac) {
168 Packet packet; 170 Packet packet;
169 packet << static_cast<MessageID>(IdJoinRequest); 171 packet << static_cast<u8>(IdJoinRequest);
170 packet << nickname; 172 packet << nickname;
171 packet << preferred_mac; 173 packet << preferred_mac;
172 packet << network_version; 174 packet << network_version;
173 Send(packet); 175 Send(std::move(packet));
174} 176}
175 177
176void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* event) { 178void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* event) {
@@ -178,7 +180,7 @@ void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* ev
178 packet.Append(event->packet->data, event->packet->dataLength); 180 packet.Append(event->packet->data, event->packet->dataLength);
179 181
180 // Ignore the first byte, which is the message id. 182 // Ignore the first byte, which is the message id.
181 packet.IgnoreBytes(sizeof(MessageID)); 183 packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
182 184
183 RoomInformation info{}; 185 RoomInformation info{};
184 packet >> info.name; 186 packet >> info.name;
@@ -203,9 +205,9 @@ void RoomMember::RoomMemberImpl::HandleJoinPacket(const ENetEvent* event) {
203 packet.Append(event->packet->data, event->packet->dataLength); 205 packet.Append(event->packet->data, event->packet->dataLength);
204 206
205 // Ignore the first byte, which is the message id. 207 // Ignore the first byte, which is the message id.
206 packet.IgnoreBytes(sizeof(MessageID)); 208 packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
207 209
208 // Parse the MAC Address from the BitStream 210 // Parse the MAC Address from the packet
209 packet >> mac_address; 211 packet >> mac_address;
210 // TODO(B3N30): Invoke callbacks 212 // TODO(B3N30): Invoke callbacks
211} 213}
@@ -216,9 +218,9 @@ void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) {
216 packet.Append(event->packet->data, event->packet->dataLength); 218 packet.Append(event->packet->data, event->packet->dataLength);
217 219
218 // Ignore the first byte, which is the message id. 220 // Ignore the first byte, which is the message id.
219 packet.IgnoreBytes(sizeof(MessageID)); 221 packet.IgnoreBytes(sizeof(u8)); // Igonore the message type
220 222
221 // Parse the WifiPacket from the BitStream 223 // Parse the WifiPacket from the packet
222 u8 frame_type; 224 u8 frame_type;
223 packet >> frame_type; 225 packet >> frame_type;
224 WifiPacket::PacketType type = static_cast<WifiPacket::PacketType>(frame_type); 226 WifiPacket::PacketType type = static_cast<WifiPacket::PacketType>(frame_type);
@@ -231,10 +233,8 @@ void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) {
231 u32 data_length; 233 u32 data_length;
232 packet >> data_length; 234 packet >> data_length;
233 235
234 std::vector<u8> data(data_length); 236 packet >> wifi_packet.data;
235 packet >> data;
236 237
237 wifi_packet.data = std::move(data);
238 // TODO(B3N30): Invoke callbacks 238 // TODO(B3N30): Invoke callbacks
239} 239}
240 240
@@ -243,7 +243,7 @@ void RoomMember::RoomMemberImpl::HandleChatPacket(const ENetEvent* event) {
243 packet.Append(event->packet->data, event->packet->dataLength); 243 packet.Append(event->packet->data, event->packet->dataLength);
244 244
245 // Ignore the first byte, which is the message id. 245 // Ignore the first byte, which is the message id.
246 packet.IgnoreBytes(sizeof(MessageID)); 246 packet.IgnoreBytes(sizeof(u8));
247 247
248 ChatEntry chat_entry{}; 248 ChatEntry chat_entry{};
249 packet >> chat_entry.nickname; 249 packet >> chat_entry.nickname;
@@ -300,9 +300,8 @@ const std::string& RoomMember::GetNickname() const {
300} 300}
301 301
302const MacAddress& RoomMember::GetMacAddress() const { 302const MacAddress& RoomMember::GetMacAddress() const {
303 if (GetState() == State::Joined) 303 ASSERT_MSG(IsConnected(), "Tried to get MAC address while not connected");
304 return room_member_impl->mac_address; 304 return room_member_impl->mac_address;
305 return MacAddress{};
306} 305}
307 306
308RoomInformation RoomMember::GetRoomInformation() const { 307RoomInformation RoomMember::GetRoomInformation() const {
@@ -351,28 +350,27 @@ bool RoomMember::IsConnected() const {
351 350
352void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) { 351void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) {
353 Packet packet; 352 Packet packet;
354 packet << static_cast<MessageID>(IdWifiPacket); 353 packet << static_cast<u8>(IdWifiPacket);
355 packet << static_cast<u8>(wifi_packet.type); 354 packet << static_cast<u8>(wifi_packet.type);
356 packet << wifi_packet.channel; 355 packet << wifi_packet.channel;
357 packet << wifi_packet.transmitter_address; 356 packet << wifi_packet.transmitter_address;
358 packet << wifi_packet.destination_address; 357 packet << wifi_packet.destination_address;
359 packet << static_cast<u32>(wifi_packet.data.size());
360 packet << wifi_packet.data; 358 packet << wifi_packet.data;
361 room_member_impl->Send(packet); 359 room_member_impl->Send(std::move(packet));
362} 360}
363 361
364void RoomMember::SendChatMessage(const std::string& message) { 362void RoomMember::SendChatMessage(const std::string& message) {
365 Packet packet; 363 Packet packet;
366 packet << static_cast<MessageID>(IdChatMessage); 364 packet << static_cast<u8>(IdChatMessage);
367 packet << message; 365 packet << message;
368 room_member_impl->Send(packet); 366 room_member_impl->Send(std::move(packet));
369} 367}
370 368
371void RoomMember::SendGameName(const std::string& game_name) { 369void RoomMember::SendGameName(const std::string& game_name) {
372 Packet packet; 370 Packet packet;
373 packet << static_cast<MessageID>(IdSetGameName); 371 packet << static_cast<u8>(IdSetGameName);
374 packet << game_name; 372 packet << game_name;
375 room_member_impl->Send(packet); 373 room_member_impl->Send(std::move(packet));
376} 374}
377 375
378void RoomMember::Leave() { 376void RoomMember::Leave() {