summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/network/room.cpp19
-rw-r--r--src/network/room_member.cpp46
-rw-r--r--src/network/room_member.h18
3 files changed, 82 insertions, 1 deletions
diff --git a/src/network/room.cpp b/src/network/room.cpp
index 3502264e1..3caa3aeae 100644
--- a/src/network/room.cpp
+++ b/src/network/room.cpp
@@ -98,6 +98,12 @@ public:
98 * The first 3 bytes are the NintendoOUI 0x00, 0x1F, 0x32 98 * The first 3 bytes are the NintendoOUI 0x00, 0x1F, 0x32
99 */ 99 */
100 MacAddress GenerateMacAddress(); 100 MacAddress GenerateMacAddress();
101
102 /**
103 * Broadcasts this packet to all members except the sender.
104 * @param event The ENet event containing the data
105 */
106 void HandleWifiPacket(const ENetEvent* event);
101}; 107};
102 108
103// RoomImpl 109// RoomImpl
@@ -111,7 +117,10 @@ void Room::RoomImpl::ServerLoop() {
111 case IdJoinRequest: 117 case IdJoinRequest:
112 HandleJoinRequest(&event); 118 HandleJoinRequest(&event);
113 break; 119 break;
114 // TODO(B3N30): Handle the other message types 120 // TODO(B3N30): Handle the other message types
121 case IdWifiPacket:
122 HandleWifiPacket(&event);
123 break;
115 } 124 }
116 enet_packet_destroy(event.packet); 125 enet_packet_destroy(event.packet);
117 break; 126 break;
@@ -247,6 +256,14 @@ MacAddress Room::RoomImpl::GenerateMacAddress() {
247 return result_mac; 256 return result_mac;
248} 257}
249 258
259void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
260 for (auto it = members.begin(); it != members.end(); ++it) {
261 if (it->peer != event->peer)
262 enet_peer_send(it->peer, 0, event->packet);
263 }
264 enet_host_flush(server);
265}
266
250// Room 267// Room
251Room::Room() : room_impl{std::make_unique<RoomImpl>()} {} 268Room::Room() : room_impl{std::make_unique<RoomImpl>()} {}
252 269
diff --git a/src/network/room_member.cpp b/src/network/room_member.cpp
index 09573ee43..f919e4de0 100644
--- a/src/network/room_member.cpp
+++ b/src/network/room_member.cpp
@@ -62,6 +62,12 @@ public:
62 * @param event The ENet event that was received. 62 * @param event The ENet event that was received.
63 */ 63 */
64 void HandleRoomInformationPacket(const ENetEvent* event); 64 void HandleRoomInformationPacket(const ENetEvent* event);
65
66 /**
67 * Extracts a WifiPacket from a received ENet packet.
68 * @param event The ENet event that was received.
69 */
70 void HandleWifiPackets(const ENetEvent* event);
65}; 71};
66 72
67// RoomMemberImpl 73// RoomMemberImpl
@@ -174,6 +180,34 @@ void RoomMember::RoomMemberImpl::HandleJoinPacket(const ENetEvent* event) {
174 // TODO(B3N30): Invoke callbacks 180 // TODO(B3N30): Invoke callbacks
175} 181}
176 182
183void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) {
184 WifiPacket wifi_packet{};
185 Packet packet;
186 packet.Append(event->packet->data, event->packet->dataLength);
187
188 // Ignore the first byte, which is the message id.
189 packet.IgnoreBytes(sizeof(MessageID));
190
191 // Parse the WifiPacket from the BitStream
192 uint8_t frame_type;
193 packet >> frame_type;
194 WifiPacket::PacketType type = static_cast<WifiPacket::PacketType>(frame_type);
195
196 wifi_packet.type = type;
197 packet >> wifi_packet.channel;
198 packet >> wifi_packet.transmitter_address;
199 packet >> wifi_packet.destination_address;
200
201 uint32_t data_length;
202 packet >> data_length;
203
204 std::vector<uint8_t> data(data_length);
205 packet >> data;
206
207 wifi_packet.data = std::move(data);
208 // TODO(B3N30): Invoke callbacks
209}
210
177// RoomMember 211// RoomMember
178RoomMember::RoomMember() : room_member_impl{std::make_unique<RoomMemberImpl>()} { 212RoomMember::RoomMember() : room_member_impl{std::make_unique<RoomMemberImpl>()} {
179 room_member_impl->client = enet_host_create(nullptr, 1, NumChannels, 0, 0); 213 room_member_impl->client = enet_host_create(nullptr, 1, NumChannels, 0, 0);
@@ -227,6 +261,18 @@ bool RoomMember::IsConnected() const {
227 return room_member_impl->IsConnected(); 261 return room_member_impl->IsConnected();
228} 262}
229 263
264void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) {
265 Packet packet;
266 packet << static_cast<MessageID>(IdWifiPacket);
267 packet << static_cast<uint8_t>(wifi_packet.type);
268 packet << wifi_packet.channel;
269 packet << wifi_packet.transmitter_address;
270 packet << wifi_packet.destination_address;
271 packet << static_cast<uint32_t>(wifi_packet.data.size());
272 packet << wifi_packet.data;
273 room_member_impl->Send(packet);
274}
275
230void RoomMember::Leave() { 276void RoomMember::Leave() {
231 ASSERT_MSG(room_member_impl->receive_thread != nullptr, "Must be in a room to leave it."); 277 ASSERT_MSG(room_member_impl->receive_thread != nullptr, "Must be in a room to leave it.");
232 { 278 {
diff --git a/src/network/room_member.h b/src/network/room_member.h
index f8bdbaea8..d23f5d4b6 100644
--- a/src/network/room_member.h
+++ b/src/network/room_member.h
@@ -12,6 +12,18 @@
12 12
13namespace Network { 13namespace Network {
14 14
15/// Information about the received WiFi packets.
16/// Acts as our own 802.11 header.
17struct WifiPacket {
18 enum class PacketType { Beacon, Data, Management };
19 PacketType type; ///< The type of 802.11 frame, Beacon / Data.
20 std::vector<uint8_t> data; ///< Raw 802.11 frame data, starting at the management frame header
21 /// for management frames.
22 MacAddress transmitter_address; ///< Mac address of the transmitter.
23 MacAddress destination_address; ///< Mac address of the receiver.
24 uint8_t channel; ///< WiFi channel where this frame was transmitted.
25};
26
15/** 27/**
16 * This is what a client [person joining a server] would use. 28 * This is what a client [person joining a server] would use.
17 * It also has to be used if you host a game yourself (You'd create both, a Room and a 29 * It also has to be used if you host a game yourself (You'd create both, a Room and a
@@ -70,6 +82,12 @@ public:
70 const u16 serverPort = DefaultRoomPort, const u16 clientPort = 0); 82 const u16 serverPort = DefaultRoomPort, const u16 clientPort = 0);
71 83
72 /** 84 /**
85 * Sends a WiFi packet to the room.
86 * @param packet The WiFi packet to send.
87 */
88 void SendWifiPacket(const WifiPacket& packet);
89
90 /**
73 * Leaves the current room. 91 * Leaves the current room.
74 */ 92 */
75 void Leave(); 93 void Leave();