diff options
| author | 2018-01-11 19:21:20 -0700 | |
|---|---|---|
| committer | 2018-01-12 19:11:03 -0700 | |
| commit | ebf9a784a9f7f4148a669dbb39e7cd50df779a14 (patch) | |
| tree | d585685a1c0a34b903af1d086d62560bf56bb29f /src/network | |
| parent | config: Default CPU core to Unicorn. (diff) | |
| download | yuzu-ebf9a784a9f7f4148a669dbb39e7cd50df779a14.tar.gz yuzu-ebf9a784a9f7f4148a669dbb39e7cd50df779a14.tar.xz yuzu-ebf9a784a9f7f4148a669dbb39e7cd50df779a14.zip | |
Massive removal of unused modules
Diffstat (limited to 'src/network')
| -rw-r--r-- | src/network/CMakeLists.txt | 18 | ||||
| -rw-r--r-- | src/network/network.cpp | 50 | ||||
| -rw-r--r-- | src/network/network.h | 25 | ||||
| -rw-r--r-- | src/network/packet.cpp | 263 | ||||
| -rw-r--r-- | src/network/packet.h | 166 | ||||
| -rw-r--r-- | src/network/room.cpp | 497 | ||||
| -rw-r--r-- | src/network/room.h | 101 | ||||
| -rw-r--r-- | src/network/room_member.cpp | 490 | ||||
| -rw-r--r-- | src/network/room_member.h | 182 |
9 files changed, 0 insertions, 1792 deletions
diff --git a/src/network/CMakeLists.txt b/src/network/CMakeLists.txt deleted file mode 100644 index ac9d028da..000000000 --- a/src/network/CMakeLists.txt +++ /dev/null | |||
| @@ -1,18 +0,0 @@ | |||
| 1 | set(SRCS | ||
| 2 | network.cpp | ||
| 3 | packet.cpp | ||
| 4 | room.cpp | ||
| 5 | room_member.cpp | ||
| 6 | ) | ||
| 7 | |||
| 8 | set(HEADERS | ||
| 9 | network.h | ||
| 10 | packet.h | ||
| 11 | room.h | ||
| 12 | room_member.h | ||
| 13 | ) | ||
| 14 | |||
| 15 | create_directory_groups(${SRCS} ${HEADERS}) | ||
| 16 | |||
| 17 | add_library(network STATIC ${SRCS} ${HEADERS}) | ||
| 18 | target_link_libraries(network PRIVATE common enet) | ||
diff --git a/src/network/network.cpp b/src/network/network.cpp deleted file mode 100644 index 51b5d6a9f..000000000 --- a/src/network/network.cpp +++ /dev/null | |||
| @@ -1,50 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/assert.h" | ||
| 6 | #include "common/logging/log.h" | ||
| 7 | #include "enet/enet.h" | ||
| 8 | #include "network/network.h" | ||
| 9 | |||
| 10 | namespace Network { | ||
| 11 | |||
| 12 | static std::shared_ptr<RoomMember> g_room_member; ///< RoomMember (Client) for network games | ||
| 13 | static std::shared_ptr<Room> g_room; ///< Room (Server) for network games | ||
| 14 | // TODO(B3N30): Put these globals into a networking class | ||
| 15 | |||
| 16 | bool Init() { | ||
| 17 | if (enet_initialize() != 0) { | ||
| 18 | LOG_ERROR(Network, "Error initalizing ENet"); | ||
| 19 | return false; | ||
| 20 | } | ||
| 21 | g_room = std::make_shared<Room>(); | ||
| 22 | g_room_member = std::make_shared<RoomMember>(); | ||
| 23 | LOG_DEBUG(Network, "initialized OK"); | ||
| 24 | return true; | ||
| 25 | } | ||
| 26 | |||
| 27 | std::weak_ptr<Room> GetRoom() { | ||
| 28 | return g_room; | ||
| 29 | } | ||
| 30 | |||
| 31 | std::weak_ptr<RoomMember> GetRoomMember() { | ||
| 32 | return g_room_member; | ||
| 33 | } | ||
| 34 | |||
| 35 | void Shutdown() { | ||
| 36 | if (g_room_member) { | ||
| 37 | if (g_room_member->IsConnected()) | ||
| 38 | g_room_member->Leave(); | ||
| 39 | g_room_member.reset(); | ||
| 40 | } | ||
| 41 | if (g_room) { | ||
| 42 | if (g_room->GetState() == Room::State::Open) | ||
| 43 | g_room->Destroy(); | ||
| 44 | g_room.reset(); | ||
| 45 | } | ||
| 46 | enet_deinitialize(); | ||
| 47 | LOG_DEBUG(Network, "shutdown OK"); | ||
| 48 | } | ||
| 49 | |||
| 50 | } // namespace Network | ||
diff --git a/src/network/network.h b/src/network/network.h deleted file mode 100644 index 6d002d693..000000000 --- a/src/network/network.h +++ /dev/null | |||
| @@ -1,25 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <memory> | ||
| 8 | #include "network/room.h" | ||
| 9 | #include "network/room_member.h" | ||
| 10 | |||
| 11 | namespace Network { | ||
| 12 | |||
| 13 | /// Initializes and registers the network device, the room, and the room member. | ||
| 14 | bool Init(); | ||
| 15 | |||
| 16 | /// Returns a pointer to the room handle | ||
| 17 | std::weak_ptr<Room> GetRoom(); | ||
| 18 | |||
| 19 | /// Returns a pointer to the room member handle | ||
| 20 | std::weak_ptr<RoomMember> GetRoomMember(); | ||
| 21 | |||
| 22 | /// Unregisters the network device, the room, and the room member and shut them down. | ||
| 23 | void Shutdown(); | ||
| 24 | |||
| 25 | } // namespace Network | ||
diff --git a/src/network/packet.cpp b/src/network/packet.cpp deleted file mode 100644 index 7e1a812f3..000000000 --- a/src/network/packet.cpp +++ /dev/null | |||
| @@ -1,263 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #ifdef _WIN32 | ||
| 6 | #include <winsock2.h> | ||
| 7 | #else | ||
| 8 | #include <arpa/inet.h> | ||
| 9 | #endif | ||
| 10 | #include <cstring> | ||
| 11 | #include <string> | ||
| 12 | #include "network/packet.h" | ||
| 13 | |||
| 14 | namespace Network { | ||
| 15 | |||
| 16 | #ifndef htonll | ||
| 17 | u64 htonll(u64 x) { | ||
| 18 | return ((1 == htonl(1)) ? (x) : ((uint64_t)htonl((x)&0xFFFFFFFF) << 32) | htonl((x) >> 32)); | ||
| 19 | } | ||
| 20 | #endif | ||
| 21 | |||
| 22 | #ifndef ntohll | ||
| 23 | u64 ntohll(u64 x) { | ||
| 24 | return ((1 == ntohl(1)) ? (x) : ((uint64_t)ntohl((x)&0xFFFFFFFF) << 32) | ntohl((x) >> 32)); | ||
| 25 | } | ||
| 26 | #endif | ||
| 27 | |||
| 28 | void Packet::Append(const void* in_data, std::size_t size_in_bytes) { | ||
| 29 | if (in_data && (size_in_bytes > 0)) { | ||
| 30 | std::size_t start = data.size(); | ||
| 31 | data.resize(start + size_in_bytes); | ||
| 32 | std::memcpy(&data[start], in_data, size_in_bytes); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | void Packet::Read(void* out_data, std::size_t size_in_bytes) { | ||
| 37 | if (out_data && CheckSize(size_in_bytes)) { | ||
| 38 | std::memcpy(out_data, &data[read_pos], size_in_bytes); | ||
| 39 | read_pos += size_in_bytes; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | void Packet::Clear() { | ||
| 44 | data.clear(); | ||
| 45 | read_pos = 0; | ||
| 46 | is_valid = true; | ||
| 47 | } | ||
| 48 | |||
| 49 | const void* Packet::GetData() const { | ||
| 50 | return !data.empty() ? &data[0] : nullptr; | ||
| 51 | } | ||
| 52 | |||
| 53 | void Packet::IgnoreBytes(u32 length) { | ||
| 54 | read_pos += length; | ||
| 55 | } | ||
| 56 | |||
| 57 | std::size_t Packet::GetDataSize() const { | ||
| 58 | return data.size(); | ||
| 59 | } | ||
| 60 | |||
| 61 | bool Packet::EndOfPacket() const { | ||
| 62 | return read_pos >= data.size(); | ||
| 63 | } | ||
| 64 | |||
| 65 | Packet::operator bool() const { | ||
| 66 | return is_valid ? &Packet::CheckSize : nullptr; | ||
| 67 | } | ||
| 68 | |||
| 69 | Packet& Packet::operator>>(bool& out_data) { | ||
| 70 | u8 value; | ||
| 71 | if (*this >> value) { | ||
| 72 | out_data = (value != 0); | ||
| 73 | } | ||
| 74 | return *this; | ||
| 75 | } | ||
| 76 | |||
| 77 | Packet& Packet::operator>>(s8& out_data) { | ||
| 78 | Read(&out_data, sizeof(out_data)); | ||
| 79 | return *this; | ||
| 80 | } | ||
| 81 | |||
| 82 | Packet& Packet::operator>>(u8& out_data) { | ||
| 83 | Read(&out_data, sizeof(out_data)); | ||
| 84 | return *this; | ||
| 85 | } | ||
| 86 | |||
| 87 | Packet& Packet::operator>>(s16& out_data) { | ||
| 88 | s16 value; | ||
| 89 | Read(&value, sizeof(value)); | ||
| 90 | out_data = ntohs(value); | ||
| 91 | return *this; | ||
| 92 | } | ||
| 93 | |||
| 94 | Packet& Packet::operator>>(u16& out_data) { | ||
| 95 | u16 value; | ||
| 96 | Read(&value, sizeof(value)); | ||
| 97 | out_data = ntohs(value); | ||
| 98 | return *this; | ||
| 99 | } | ||
| 100 | |||
| 101 | Packet& Packet::operator>>(s32& out_data) { | ||
| 102 | s32 value; | ||
| 103 | Read(&value, sizeof(value)); | ||
| 104 | out_data = ntohl(value); | ||
| 105 | return *this; | ||
| 106 | } | ||
| 107 | |||
| 108 | Packet& Packet::operator>>(u32& out_data) { | ||
| 109 | u32 value; | ||
| 110 | Read(&value, sizeof(value)); | ||
| 111 | out_data = ntohl(value); | ||
| 112 | return *this; | ||
| 113 | } | ||
| 114 | |||
| 115 | Packet& Packet::operator>>(s64& out_data) { | ||
| 116 | s64 value; | ||
| 117 | Read(&value, sizeof(value)); | ||
| 118 | out_data = ntohll(value); | ||
| 119 | return *this; | ||
| 120 | } | ||
| 121 | |||
| 122 | Packet& Packet::operator>>(u64& out_data) { | ||
| 123 | u64 value; | ||
| 124 | Read(&value, sizeof(value)); | ||
| 125 | out_data = ntohll(value); | ||
| 126 | return *this; | ||
| 127 | } | ||
| 128 | |||
| 129 | Packet& Packet::operator>>(float& out_data) { | ||
| 130 | Read(&out_data, sizeof(out_data)); | ||
| 131 | return *this; | ||
| 132 | } | ||
| 133 | |||
| 134 | Packet& Packet::operator>>(double& out_data) { | ||
| 135 | Read(&out_data, sizeof(out_data)); | ||
| 136 | return *this; | ||
| 137 | } | ||
| 138 | |||
| 139 | Packet& Packet::operator>>(char* out_data) { | ||
| 140 | // First extract string length | ||
| 141 | u32 length = 0; | ||
| 142 | *this >> length; | ||
| 143 | |||
| 144 | if ((length > 0) && CheckSize(length)) { | ||
| 145 | // Then extract characters | ||
| 146 | std::memcpy(out_data, &data[read_pos], length); | ||
| 147 | out_data[length] = '\0'; | ||
| 148 | |||
| 149 | // Update reading position | ||
| 150 | read_pos += length; | ||
| 151 | } | ||
| 152 | |||
| 153 | return *this; | ||
| 154 | } | ||
| 155 | |||
| 156 | Packet& Packet::operator>>(std::string& out_data) { | ||
| 157 | // First extract string length | ||
| 158 | u32 length = 0; | ||
| 159 | *this >> length; | ||
| 160 | |||
| 161 | out_data.clear(); | ||
| 162 | if ((length > 0) && CheckSize(length)) { | ||
| 163 | // Then extract characters | ||
| 164 | out_data.assign(&data[read_pos], length); | ||
| 165 | |||
| 166 | // Update reading position | ||
| 167 | read_pos += length; | ||
| 168 | } | ||
| 169 | |||
| 170 | return *this; | ||
| 171 | } | ||
| 172 | |||
| 173 | Packet& Packet::operator<<(bool in_data) { | ||
| 174 | *this << static_cast<u8>(in_data); | ||
| 175 | return *this; | ||
| 176 | } | ||
| 177 | |||
| 178 | Packet& Packet::operator<<(s8 in_data) { | ||
| 179 | Append(&in_data, sizeof(in_data)); | ||
| 180 | return *this; | ||
| 181 | } | ||
| 182 | |||
| 183 | Packet& Packet::operator<<(u8 in_data) { | ||
| 184 | Append(&in_data, sizeof(in_data)); | ||
| 185 | return *this; | ||
| 186 | } | ||
| 187 | |||
| 188 | Packet& Packet::operator<<(s16 in_data) { | ||
| 189 | s16 toWrite = htons(in_data); | ||
| 190 | Append(&toWrite, sizeof(toWrite)); | ||
| 191 | return *this; | ||
| 192 | } | ||
| 193 | |||
| 194 | Packet& Packet::operator<<(u16 in_data) { | ||
| 195 | u16 toWrite = htons(in_data); | ||
| 196 | Append(&toWrite, sizeof(toWrite)); | ||
| 197 | return *this; | ||
| 198 | } | ||
| 199 | |||
| 200 | Packet& Packet::operator<<(s32 in_data) { | ||
| 201 | s32 toWrite = htonl(in_data); | ||
| 202 | Append(&toWrite, sizeof(toWrite)); | ||
| 203 | return *this; | ||
| 204 | } | ||
| 205 | |||
| 206 | Packet& Packet::operator<<(u32 in_data) { | ||
| 207 | u32 toWrite = htonl(in_data); | ||
| 208 | Append(&toWrite, sizeof(toWrite)); | ||
| 209 | return *this; | ||
| 210 | } | ||
| 211 | |||
| 212 | Packet& Packet::operator<<(s64 in_data) { | ||
| 213 | s64 toWrite = htonll(in_data); | ||
| 214 | Append(&toWrite, sizeof(toWrite)); | ||
| 215 | return *this; | ||
| 216 | } | ||
| 217 | |||
| 218 | Packet& Packet::operator<<(u64 in_data) { | ||
| 219 | u64 toWrite = htonll(in_data); | ||
| 220 | Append(&toWrite, sizeof(toWrite)); | ||
| 221 | return *this; | ||
| 222 | } | ||
| 223 | |||
| 224 | Packet& Packet::operator<<(float in_data) { | ||
| 225 | Append(&in_data, sizeof(in_data)); | ||
| 226 | return *this; | ||
| 227 | } | ||
| 228 | |||
| 229 | Packet& Packet::operator<<(double in_data) { | ||
| 230 | Append(&in_data, sizeof(in_data)); | ||
| 231 | return *this; | ||
| 232 | } | ||
| 233 | |||
| 234 | Packet& Packet::operator<<(const char* in_data) { | ||
| 235 | // First insert string length | ||
| 236 | u32 length = static_cast<u32>(std::strlen(in_data)); | ||
| 237 | *this << length; | ||
| 238 | |||
| 239 | // Then insert characters | ||
| 240 | Append(in_data, length * sizeof(char)); | ||
| 241 | |||
| 242 | return *this; | ||
| 243 | } | ||
| 244 | |||
| 245 | Packet& Packet::operator<<(const std::string& in_data) { | ||
| 246 | // First insert string length | ||
| 247 | u32 length = static_cast<u32>(in_data.size()); | ||
| 248 | *this << length; | ||
| 249 | |||
| 250 | // Then insert characters | ||
| 251 | if (length > 0) | ||
| 252 | Append(in_data.c_str(), length * sizeof(std::string::value_type)); | ||
| 253 | |||
| 254 | return *this; | ||
| 255 | } | ||
| 256 | |||
| 257 | bool Packet::CheckSize(std::size_t size) { | ||
| 258 | is_valid = is_valid && (read_pos + size <= data.size()); | ||
| 259 | |||
| 260 | return is_valid; | ||
| 261 | } | ||
| 262 | |||
| 263 | } // namespace Network | ||
diff --git a/src/network/packet.h b/src/network/packet.h deleted file mode 100644 index 5a2e58dc2..000000000 --- a/src/network/packet.h +++ /dev/null | |||
| @@ -1,166 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <vector> | ||
| 9 | #include "common/common_types.h" | ||
| 10 | |||
| 11 | namespace Network { | ||
| 12 | |||
| 13 | /// A class that serializes data for network transfer. It also handles endianess | ||
| 14 | class Packet { | ||
| 15 | public: | ||
| 16 | Packet() = default; | ||
| 17 | ~Packet() = default; | ||
| 18 | |||
| 19 | /** | ||
| 20 | * Append data to the end of the packet | ||
| 21 | * @param data Pointer to the sequence of bytes to append | ||
| 22 | * @param size_in_bytes Number of bytes to append | ||
| 23 | */ | ||
| 24 | void Append(const void* data, std::size_t size_in_bytes); | ||
| 25 | |||
| 26 | /** | ||
| 27 | * Reads data from the current read position of the packet | ||
| 28 | * @param out_data Pointer where the data should get written to | ||
| 29 | * @param size_in_bytes Number of bytes to read | ||
| 30 | */ | ||
| 31 | void Read(void* out_data, std::size_t size_in_bytes); | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Clear the packet | ||
| 35 | * After calling Clear, the packet is empty. | ||
| 36 | */ | ||
| 37 | void Clear(); | ||
| 38 | |||
| 39 | /** | ||
| 40 | * Ignores bytes while reading | ||
| 41 | * @param length THe number of bytes to ignore | ||
| 42 | */ | ||
| 43 | void IgnoreBytes(u32 length); | ||
| 44 | |||
| 45 | /** | ||
| 46 | * Get a pointer to the data contained in the packet | ||
| 47 | * @return Pointer to the data | ||
| 48 | */ | ||
| 49 | const void* GetData() const; | ||
| 50 | |||
| 51 | /** | ||
| 52 | * This function returns the number of bytes pointed to by | ||
| 53 | * what getData returns. | ||
| 54 | * @return Data size, in bytes | ||
| 55 | */ | ||
| 56 | std::size_t GetDataSize() const; | ||
| 57 | |||
| 58 | /** | ||
| 59 | * This function is useful to know if there is some data | ||
| 60 | * left to be read, without actually reading it. | ||
| 61 | * @return True if all data was read, false otherwise | ||
| 62 | */ | ||
| 63 | bool EndOfPacket() const; | ||
| 64 | |||
| 65 | explicit operator bool() const; | ||
| 66 | |||
| 67 | /// Overloads of operator >> to read data from the packet | ||
| 68 | Packet& operator>>(bool& out_data); | ||
| 69 | Packet& operator>>(s8& out_data); | ||
| 70 | Packet& operator>>(u8& out_data); | ||
| 71 | Packet& operator>>(s16& out_data); | ||
| 72 | Packet& operator>>(u16& out_data); | ||
| 73 | Packet& operator>>(s32& out_data); | ||
| 74 | Packet& operator>>(u32& out_data); | ||
| 75 | Packet& operator>>(s64& out_data); | ||
| 76 | Packet& operator>>(u64& out_data); | ||
| 77 | Packet& operator>>(float& out_data); | ||
| 78 | Packet& operator>>(double& out_data); | ||
| 79 | Packet& operator>>(char* out_data); | ||
| 80 | Packet& operator>>(std::string& out_data); | ||
| 81 | template <typename T> | ||
| 82 | Packet& operator>>(std::vector<T>& out_data); | ||
| 83 | template <typename T, std::size_t S> | ||
| 84 | Packet& operator>>(std::array<T, S>& out_data); | ||
| 85 | |||
| 86 | /// Overloads of operator << to write data into the packet | ||
| 87 | Packet& operator<<(bool in_data); | ||
| 88 | Packet& operator<<(s8 in_data); | ||
| 89 | Packet& operator<<(u8 in_data); | ||
| 90 | Packet& operator<<(s16 in_data); | ||
| 91 | Packet& operator<<(u16 in_data); | ||
| 92 | Packet& operator<<(s32 in_data); | ||
| 93 | Packet& operator<<(u32 in_data); | ||
| 94 | Packet& operator<<(s64 in_data); | ||
| 95 | Packet& operator<<(u64 in_data); | ||
| 96 | Packet& operator<<(float in_data); | ||
| 97 | Packet& operator<<(double in_data); | ||
| 98 | Packet& operator<<(const char* in_data); | ||
| 99 | Packet& operator<<(const std::string& in_data); | ||
| 100 | template <typename T> | ||
| 101 | Packet& operator<<(const std::vector<T>& in_data); | ||
| 102 | template <typename T, std::size_t S> | ||
| 103 | Packet& operator<<(const std::array<T, S>& data); | ||
| 104 | |||
| 105 | private: | ||
| 106 | /** | ||
| 107 | * Check if the packet can extract a given number of bytes | ||
| 108 | * This function updates accordingly the state of the packet. | ||
| 109 | * @param size Size to check | ||
| 110 | * @return True if size bytes can be read from the packet | ||
| 111 | */ | ||
| 112 | bool CheckSize(std::size_t size); | ||
| 113 | |||
| 114 | // Member data | ||
| 115 | std::vector<char> data; ///< Data stored in the packet | ||
| 116 | std::size_t read_pos = 0; ///< Current reading position in the packet | ||
| 117 | bool is_valid = true; ///< Reading state of the packet | ||
| 118 | }; | ||
| 119 | |||
| 120 | template <typename T> | ||
| 121 | Packet& Packet::operator>>(std::vector<T>& out_data) { | ||
| 122 | // First extract the size | ||
| 123 | u32 size = 0; | ||
| 124 | *this >> size; | ||
| 125 | out_data.resize(size); | ||
| 126 | |||
| 127 | // Then extract the data | ||
| 128 | for (std::size_t i = 0; i < out_data.size(); ++i) { | ||
| 129 | T character = 0; | ||
| 130 | *this >> character; | ||
| 131 | out_data[i] = character; | ||
| 132 | } | ||
| 133 | return *this; | ||
| 134 | } | ||
| 135 | |||
| 136 | template <typename T, std::size_t S> | ||
| 137 | Packet& Packet::operator>>(std::array<T, S>& out_data) { | ||
| 138 | for (std::size_t i = 0; i < out_data.size(); ++i) { | ||
| 139 | T character = 0; | ||
| 140 | *this >> character; | ||
| 141 | out_data[i] = character; | ||
| 142 | } | ||
| 143 | return *this; | ||
| 144 | } | ||
| 145 | |||
| 146 | template <typename T> | ||
| 147 | Packet& Packet::operator<<(const std::vector<T>& in_data) { | ||
| 148 | // First insert the size | ||
| 149 | *this << static_cast<u32>(in_data.size()); | ||
| 150 | |||
| 151 | // Then insert the data | ||
| 152 | for (std::size_t i = 0; i < in_data.size(); ++i) { | ||
| 153 | *this << in_data[i]; | ||
| 154 | } | ||
| 155 | return *this; | ||
| 156 | } | ||
| 157 | |||
| 158 | template <typename T, std::size_t S> | ||
| 159 | Packet& Packet::operator<<(const std::array<T, S>& in_data) { | ||
| 160 | for (std::size_t i = 0; i < in_data.size(); ++i) { | ||
| 161 | *this << in_data[i]; | ||
| 162 | } | ||
| 163 | return *this; | ||
| 164 | } | ||
| 165 | |||
| 166 | } // namespace Network | ||
diff --git a/src/network/room.cpp b/src/network/room.cpp deleted file mode 100644 index 261049ab0..000000000 --- a/src/network/room.cpp +++ /dev/null | |||
| @@ -1,497 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <atomic> | ||
| 7 | #include <mutex> | ||
| 8 | #include <random> | ||
| 9 | #include <thread> | ||
| 10 | #include "enet/enet.h" | ||
| 11 | #include "network/packet.h" | ||
| 12 | #include "network/room.h" | ||
| 13 | |||
| 14 | namespace Network { | ||
| 15 | |||
| 16 | /// Maximum number of concurrent connections allowed to this room. | ||
| 17 | static constexpr u32 MaxConcurrentConnections = 10; | ||
| 18 | |||
| 19 | class Room::RoomImpl { | ||
| 20 | public: | ||
| 21 | // This MAC address is used to generate a 'Nintendo' like Mac address. | ||
| 22 | const MacAddress NintendoOUI; | ||
| 23 | std::mt19937 random_gen; ///< Random number generator. Used for GenerateMacAddress | ||
| 24 | |||
| 25 | ENetHost* server = nullptr; ///< Network interface. | ||
| 26 | |||
| 27 | std::atomic<State> state{State::Closed}; ///< Current state of the room. | ||
| 28 | RoomInformation room_information; ///< Information about this room. | ||
| 29 | |||
| 30 | struct Member { | ||
| 31 | std::string nickname; ///< The nickname of the member. | ||
| 32 | GameInfo game_info; ///< The current game of the member | ||
| 33 | MacAddress mac_address; ///< The assigned mac address of the member. | ||
| 34 | ENetPeer* peer; ///< The remote peer. | ||
| 35 | }; | ||
| 36 | using MemberList = std::vector<Member>; | ||
| 37 | MemberList members; ///< Information about the members of this room | ||
| 38 | mutable std::mutex member_mutex; ///< Mutex for locking the members list | ||
| 39 | /// This should be a std::shared_mutex as soon as C++17 is supported | ||
| 40 | |||
| 41 | RoomImpl() | ||
| 42 | : random_gen(std::random_device()()), NintendoOUI{0x00, 0x1F, 0x32, 0x00, 0x00, 0x00} {} | ||
| 43 | |||
| 44 | /// Thread that receives and dispatches network packets | ||
| 45 | std::unique_ptr<std::thread> room_thread; | ||
| 46 | |||
| 47 | /// Thread function that will receive and dispatch messages until the room is destroyed. | ||
| 48 | void ServerLoop(); | ||
| 49 | void StartLoop(); | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Parses and answers a room join request from a client. | ||
| 53 | * Validates the uniqueness of the username and assigns the MAC address | ||
| 54 | * that the client will use for the remainder of the connection. | ||
| 55 | */ | ||
| 56 | void HandleJoinRequest(const ENetEvent* event); | ||
| 57 | |||
| 58 | /** | ||
| 59 | * Returns whether the nickname is valid, ie. isn't already taken by someone else in the room. | ||
| 60 | */ | ||
| 61 | bool IsValidNickname(const std::string& nickname) const; | ||
| 62 | |||
| 63 | /** | ||
| 64 | * Returns whether the MAC address is valid, ie. isn't already taken by someone else in the | ||
| 65 | * room. | ||
| 66 | */ | ||
| 67 | bool IsValidMacAddress(const MacAddress& address) const; | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Sends a ID_ROOM_NAME_COLLISION message telling the client that the name is invalid. | ||
| 71 | */ | ||
| 72 | void SendNameCollision(ENetPeer* client); | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Sends a ID_ROOM_MAC_COLLISION message telling the client that the MAC is invalid. | ||
| 76 | */ | ||
| 77 | void SendMacCollision(ENetPeer* client); | ||
| 78 | |||
| 79 | /** | ||
| 80 | * Sends a ID_ROOM_VERSION_MISMATCH message telling the client that the version is invalid. | ||
| 81 | */ | ||
| 82 | void SendVersionMismatch(ENetPeer* client); | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Notifies the member that its connection attempt was successful, | ||
| 86 | * and it is now part of the room. | ||
| 87 | */ | ||
| 88 | void SendJoinSuccess(ENetPeer* client, MacAddress mac_address); | ||
| 89 | |||
| 90 | /** | ||
| 91 | * Notifies the members that the room is closed, | ||
| 92 | */ | ||
| 93 | void SendCloseMessage(); | ||
| 94 | |||
| 95 | /** | ||
| 96 | * Sends the information about the room, along with the list of members | ||
| 97 | * to every connected client in the room. | ||
| 98 | * The packet has the structure: | ||
| 99 | * <MessageID>ID_ROOM_INFORMATION | ||
| 100 | * <String> room_name | ||
| 101 | * <u32> member_slots: The max number of clients allowed in this room | ||
| 102 | * <u32> num_members: the number of currently joined clients | ||
| 103 | * This is followed by the following three values for each member: | ||
| 104 | * <String> nickname of that member | ||
| 105 | * <MacAddress> mac_address of that member | ||
| 106 | * <String> game_name of that member | ||
| 107 | */ | ||
| 108 | void BroadcastRoomInformation(); | ||
| 109 | |||
| 110 | /** | ||
| 111 | * Generates a free MAC address to assign to a new client. | ||
| 112 | * The first 3 bytes are the NintendoOUI 0x00, 0x1F, 0x32 | ||
| 113 | */ | ||
| 114 | MacAddress GenerateMacAddress(); | ||
| 115 | |||
| 116 | /** | ||
| 117 | * Broadcasts this packet to all members except the sender. | ||
| 118 | * @param event The ENet event containing the data | ||
| 119 | */ | ||
| 120 | void HandleWifiPacket(const ENetEvent* event); | ||
| 121 | |||
| 122 | /** | ||
| 123 | * Extracts a chat entry from a received ENet packet and adds it to the chat queue. | ||
| 124 | * @param event The ENet event that was received. | ||
| 125 | */ | ||
| 126 | void HandleChatPacket(const ENetEvent* event); | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Extracts the game name from a received ENet packet and broadcasts it. | ||
| 130 | * @param event The ENet event that was received. | ||
| 131 | */ | ||
| 132 | void HandleGameNamePacket(const ENetEvent* event); | ||
| 133 | |||
| 134 | /** | ||
| 135 | * Removes the client from the members list if it was in it and announces the change | ||
| 136 | * to all other clients. | ||
| 137 | */ | ||
| 138 | void HandleClientDisconnection(ENetPeer* client); | ||
| 139 | }; | ||
| 140 | |||
| 141 | // RoomImpl | ||
| 142 | void Room::RoomImpl::ServerLoop() { | ||
| 143 | while (state != State::Closed) { | ||
| 144 | ENetEvent event; | ||
| 145 | if (enet_host_service(server, &event, 100) > 0) { | ||
| 146 | switch (event.type) { | ||
| 147 | case ENET_EVENT_TYPE_RECEIVE: | ||
| 148 | switch (event.packet->data[0]) { | ||
| 149 | case IdJoinRequest: | ||
| 150 | HandleJoinRequest(&event); | ||
| 151 | break; | ||
| 152 | case IdSetGameInfo: | ||
| 153 | HandleGameNamePacket(&event); | ||
| 154 | break; | ||
| 155 | case IdWifiPacket: | ||
| 156 | HandleWifiPacket(&event); | ||
| 157 | break; | ||
| 158 | case IdChatMessage: | ||
| 159 | HandleChatPacket(&event); | ||
| 160 | break; | ||
| 161 | } | ||
| 162 | enet_packet_destroy(event.packet); | ||
| 163 | break; | ||
| 164 | case ENET_EVENT_TYPE_DISCONNECT: | ||
| 165 | HandleClientDisconnection(event.peer); | ||
| 166 | break; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | } | ||
| 170 | // Close the connection to all members: | ||
| 171 | SendCloseMessage(); | ||
| 172 | } | ||
| 173 | |||
| 174 | void Room::RoomImpl::StartLoop() { | ||
| 175 | room_thread = std::make_unique<std::thread>(&Room::RoomImpl::ServerLoop, this); | ||
| 176 | } | ||
| 177 | |||
| 178 | void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) { | ||
| 179 | Packet packet; | ||
| 180 | packet.Append(event->packet->data, event->packet->dataLength); | ||
| 181 | packet.IgnoreBytes(sizeof(u8)); // Igonore the message type | ||
| 182 | std::string nickname; | ||
| 183 | packet >> nickname; | ||
| 184 | |||
| 185 | MacAddress preferred_mac; | ||
| 186 | packet >> preferred_mac; | ||
| 187 | |||
| 188 | u32 client_version; | ||
| 189 | packet >> client_version; | ||
| 190 | |||
| 191 | if (!IsValidNickname(nickname)) { | ||
| 192 | SendNameCollision(event->peer); | ||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | if (preferred_mac != NoPreferredMac) { | ||
| 197 | // Verify if the preferred mac is available | ||
| 198 | if (!IsValidMacAddress(preferred_mac)) { | ||
| 199 | SendMacCollision(event->peer); | ||
| 200 | return; | ||
| 201 | } | ||
| 202 | } else { | ||
| 203 | // Assign a MAC address of this client automatically | ||
| 204 | preferred_mac = GenerateMacAddress(); | ||
| 205 | } | ||
| 206 | |||
| 207 | if (client_version != network_version) { | ||
| 208 | SendVersionMismatch(event->peer); | ||
| 209 | return; | ||
| 210 | } | ||
| 211 | |||
| 212 | // At this point the client is ready to be added to the room. | ||
| 213 | Member member{}; | ||
| 214 | member.mac_address = preferred_mac; | ||
| 215 | member.nickname = nickname; | ||
| 216 | member.peer = event->peer; | ||
| 217 | |||
| 218 | { | ||
| 219 | std::lock_guard<std::mutex> lock(member_mutex); | ||
| 220 | members.push_back(std::move(member)); | ||
| 221 | } | ||
| 222 | |||
| 223 | // Notify everyone that the room information has changed. | ||
| 224 | BroadcastRoomInformation(); | ||
| 225 | SendJoinSuccess(event->peer, preferred_mac); | ||
| 226 | } | ||
| 227 | |||
| 228 | bool Room::RoomImpl::IsValidNickname(const std::string& nickname) const { | ||
| 229 | // A nickname is valid if it is not already taken by anybody else in the room. | ||
| 230 | // TODO(B3N30): Check for empty names, spaces, etc. | ||
| 231 | std::lock_guard<std::mutex> lock(member_mutex); | ||
| 232 | return std::all_of(members.begin(), members.end(), | ||
| 233 | [&nickname](const auto& member) { return member.nickname != nickname; }); | ||
| 234 | } | ||
| 235 | |||
| 236 | bool Room::RoomImpl::IsValidMacAddress(const MacAddress& address) const { | ||
| 237 | // A MAC address is valid if it is not already taken by anybody else in the room. | ||
| 238 | std::lock_guard<std::mutex> lock(member_mutex); | ||
| 239 | return std::all_of(members.begin(), members.end(), | ||
| 240 | [&address](const auto& member) { return member.mac_address != address; }); | ||
| 241 | } | ||
| 242 | |||
| 243 | void Room::RoomImpl::SendNameCollision(ENetPeer* client) { | ||
| 244 | Packet packet; | ||
| 245 | packet << static_cast<u8>(IdNameCollision); | ||
| 246 | |||
| 247 | ENetPacket* enet_packet = | ||
| 248 | enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); | ||
| 249 | enet_peer_send(client, 0, enet_packet); | ||
| 250 | enet_host_flush(server); | ||
| 251 | } | ||
| 252 | |||
| 253 | void Room::RoomImpl::SendMacCollision(ENetPeer* client) { | ||
| 254 | Packet packet; | ||
| 255 | packet << static_cast<u8>(IdMacCollision); | ||
| 256 | |||
| 257 | ENetPacket* enet_packet = | ||
| 258 | enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); | ||
| 259 | enet_peer_send(client, 0, enet_packet); | ||
| 260 | enet_host_flush(server); | ||
| 261 | } | ||
| 262 | |||
| 263 | void Room::RoomImpl::SendVersionMismatch(ENetPeer* client) { | ||
| 264 | Packet packet; | ||
| 265 | packet << static_cast<u8>(IdVersionMismatch); | ||
| 266 | packet << network_version; | ||
| 267 | |||
| 268 | ENetPacket* enet_packet = | ||
| 269 | enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); | ||
| 270 | enet_peer_send(client, 0, enet_packet); | ||
| 271 | enet_host_flush(server); | ||
| 272 | } | ||
| 273 | |||
| 274 | void Room::RoomImpl::SendJoinSuccess(ENetPeer* client, MacAddress mac_address) { | ||
| 275 | Packet packet; | ||
| 276 | packet << static_cast<u8>(IdJoinSuccess); | ||
| 277 | packet << mac_address; | ||
| 278 | ENetPacket* enet_packet = | ||
| 279 | enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); | ||
| 280 | enet_peer_send(client, 0, enet_packet); | ||
| 281 | enet_host_flush(server); | ||
| 282 | } | ||
| 283 | |||
| 284 | void Room::RoomImpl::SendCloseMessage() { | ||
| 285 | Packet packet; | ||
| 286 | packet << static_cast<u8>(IdCloseRoom); | ||
| 287 | ENetPacket* enet_packet = | ||
| 288 | enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); | ||
| 289 | std::lock_guard<std::mutex> lock(member_mutex); | ||
| 290 | for (auto& member : members) { | ||
| 291 | enet_peer_send(member.peer, 0, enet_packet); | ||
| 292 | } | ||
| 293 | enet_host_flush(server); | ||
| 294 | for (auto& member : members) { | ||
| 295 | enet_peer_disconnect(member.peer, 0); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | |||
| 299 | void Room::RoomImpl::BroadcastRoomInformation() { | ||
| 300 | Packet packet; | ||
| 301 | packet << static_cast<u8>(IdRoomInformation); | ||
| 302 | packet << room_information.name; | ||
| 303 | packet << room_information.member_slots; | ||
| 304 | |||
| 305 | packet << static_cast<u32>(members.size()); | ||
| 306 | { | ||
| 307 | std::lock_guard<std::mutex> lock(member_mutex); | ||
| 308 | for (const auto& member : members) { | ||
| 309 | packet << member.nickname; | ||
| 310 | packet << member.mac_address; | ||
| 311 | packet << member.game_info.name; | ||
| 312 | packet << member.game_info.id; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | ENetPacket* enet_packet = | ||
| 317 | enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); | ||
| 318 | enet_host_broadcast(server, 0, enet_packet); | ||
| 319 | enet_host_flush(server); | ||
| 320 | } | ||
| 321 | |||
| 322 | MacAddress Room::RoomImpl::GenerateMacAddress() { | ||
| 323 | MacAddress result_mac = | ||
| 324 | NintendoOUI; // The first three bytes of each MAC address will be the NintendoOUI | ||
| 325 | std::uniform_int_distribution<> dis(0x00, 0xFF); // Random byte between 0 and 0xFF | ||
| 326 | do { | ||
| 327 | for (size_t i = 3; i < result_mac.size(); ++i) { | ||
| 328 | result_mac[i] = dis(random_gen); | ||
| 329 | } | ||
| 330 | } while (!IsValidMacAddress(result_mac)); | ||
| 331 | return result_mac; | ||
| 332 | } | ||
| 333 | |||
| 334 | void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) { | ||
| 335 | Packet in_packet; | ||
| 336 | in_packet.Append(event->packet->data, event->packet->dataLength); | ||
| 337 | in_packet.IgnoreBytes(sizeof(u8)); // Message type | ||
| 338 | in_packet.IgnoreBytes(sizeof(u8)); // WifiPacket Type | ||
| 339 | in_packet.IgnoreBytes(sizeof(u8)); // WifiPacket Channel | ||
| 340 | in_packet.IgnoreBytes(sizeof(MacAddress)); // WifiPacket Transmitter Address | ||
| 341 | MacAddress destination_address; | ||
| 342 | in_packet >> destination_address; | ||
| 343 | |||
| 344 | Packet out_packet; | ||
| 345 | out_packet.Append(event->packet->data, event->packet->dataLength); | ||
| 346 | ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(), | ||
| 347 | ENET_PACKET_FLAG_RELIABLE); | ||
| 348 | |||
| 349 | if (destination_address == BroadcastMac) { // Send the data to everyone except the sender | ||
| 350 | std::lock_guard<std::mutex> lock(member_mutex); | ||
| 351 | for (const auto& member : members) { | ||
| 352 | if (member.peer != event->peer) | ||
| 353 | enet_peer_send(member.peer, 0, enet_packet); | ||
| 354 | } | ||
| 355 | } else { // Send the data only to the destination client | ||
| 356 | std::lock_guard<std::mutex> lock(member_mutex); | ||
| 357 | auto member = std::find_if(members.begin(), members.end(), | ||
| 358 | [destination_address](const Member& member) -> bool { | ||
| 359 | return member.mac_address == destination_address; | ||
| 360 | }); | ||
| 361 | if (member != members.end()) { | ||
| 362 | enet_peer_send(member->peer, 0, enet_packet); | ||
| 363 | } | ||
| 364 | } | ||
| 365 | enet_host_flush(server); | ||
| 366 | } | ||
| 367 | |||
| 368 | void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) { | ||
| 369 | Packet in_packet; | ||
| 370 | in_packet.Append(event->packet->data, event->packet->dataLength); | ||
| 371 | |||
| 372 | in_packet.IgnoreBytes(sizeof(u8)); // Igonore the message type | ||
| 373 | std::string message; | ||
| 374 | in_packet >> message; | ||
| 375 | auto CompareNetworkAddress = [event](const Member member) -> bool { | ||
| 376 | return member.peer == event->peer; | ||
| 377 | }; | ||
| 378 | |||
| 379 | std::lock_guard<std::mutex> lock(member_mutex); | ||
| 380 | const auto sending_member = std::find_if(members.begin(), members.end(), CompareNetworkAddress); | ||
| 381 | if (sending_member == members.end()) { | ||
| 382 | return; // Received a chat message from a unknown sender | ||
| 383 | } | ||
| 384 | |||
| 385 | Packet out_packet; | ||
| 386 | out_packet << static_cast<u8>(IdChatMessage); | ||
| 387 | out_packet << sending_member->nickname; | ||
| 388 | out_packet << message; | ||
| 389 | |||
| 390 | ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(), | ||
| 391 | ENET_PACKET_FLAG_RELIABLE); | ||
| 392 | for (const auto& member : members) { | ||
| 393 | if (member.peer != event->peer) | ||
| 394 | enet_peer_send(member.peer, 0, enet_packet); | ||
| 395 | } | ||
| 396 | enet_host_flush(server); | ||
| 397 | } | ||
| 398 | |||
| 399 | void Room::RoomImpl::HandleGameNamePacket(const ENetEvent* event) { | ||
| 400 | Packet in_packet; | ||
| 401 | in_packet.Append(event->packet->data, event->packet->dataLength); | ||
| 402 | |||
| 403 | in_packet.IgnoreBytes(sizeof(u8)); // Igonore the message type | ||
| 404 | GameInfo game_info; | ||
| 405 | in_packet >> game_info.name; | ||
| 406 | in_packet >> game_info.id; | ||
| 407 | |||
| 408 | { | ||
| 409 | std::lock_guard<std::mutex> lock(member_mutex); | ||
| 410 | auto member = | ||
| 411 | std::find_if(members.begin(), members.end(), [event](const Member& member) -> bool { | ||
| 412 | return member.peer == event->peer; | ||
| 413 | }); | ||
| 414 | if (member != members.end()) { | ||
| 415 | member->game_info = game_info; | ||
| 416 | } | ||
| 417 | } | ||
| 418 | BroadcastRoomInformation(); | ||
| 419 | } | ||
| 420 | |||
| 421 | void Room::RoomImpl::HandleClientDisconnection(ENetPeer* client) { | ||
| 422 | // Remove the client from the members list. | ||
| 423 | { | ||
| 424 | std::lock_guard<std::mutex> lock(member_mutex); | ||
| 425 | members.erase( | ||
| 426 | std::remove_if(members.begin(), members.end(), | ||
| 427 | [client](const Member& member) { return member.peer == client; }), | ||
| 428 | members.end()); | ||
| 429 | } | ||
| 430 | |||
| 431 | // Announce the change to all clients. | ||
| 432 | enet_peer_disconnect(client, 0); | ||
| 433 | BroadcastRoomInformation(); | ||
| 434 | } | ||
| 435 | |||
| 436 | // Room | ||
| 437 | Room::Room() : room_impl{std::make_unique<RoomImpl>()} {} | ||
| 438 | |||
| 439 | Room::~Room() = default; | ||
| 440 | |||
| 441 | void Room::Create(const std::string& name, const std::string& server_address, u16 server_port) { | ||
| 442 | ENetAddress address; | ||
| 443 | address.host = ENET_HOST_ANY; | ||
| 444 | if (!server_address.empty()) { | ||
| 445 | enet_address_set_host(&address, server_address.c_str()); | ||
| 446 | } | ||
| 447 | address.port = server_port; | ||
| 448 | |||
| 449 | room_impl->server = enet_host_create(&address, MaxConcurrentConnections, NumChannels, 0, 0); | ||
| 450 | // TODO(B3N30): Allow specifying the maximum number of concurrent connections. | ||
| 451 | room_impl->state = State::Open; | ||
| 452 | |||
| 453 | room_impl->room_information.name = name; | ||
| 454 | room_impl->room_information.member_slots = MaxConcurrentConnections; | ||
| 455 | room_impl->StartLoop(); | ||
| 456 | } | ||
| 457 | |||
| 458 | Room::State Room::GetState() const { | ||
| 459 | return room_impl->state; | ||
| 460 | } | ||
| 461 | |||
| 462 | const RoomInformation& Room::GetRoomInformation() const { | ||
| 463 | return room_impl->room_information; | ||
| 464 | } | ||
| 465 | |||
| 466 | std::vector<Room::Member> Room::GetRoomMemberList() const { | ||
| 467 | std::vector<Room::Member> member_list; | ||
| 468 | std::lock_guard<std::mutex> lock(room_impl->member_mutex); | ||
| 469 | for (const auto& member_impl : room_impl->members) { | ||
| 470 | Member member; | ||
| 471 | member.nickname = member_impl.nickname; | ||
| 472 | member.mac_address = member_impl.mac_address; | ||
| 473 | member.game_info = member_impl.game_info; | ||
| 474 | member_list.push_back(member); | ||
| 475 | } | ||
| 476 | return member_list; | ||
| 477 | }; | ||
| 478 | |||
| 479 | void Room::Destroy() { | ||
| 480 | room_impl->state = State::Closed; | ||
| 481 | room_impl->room_thread->join(); | ||
| 482 | room_impl->room_thread.reset(); | ||
| 483 | |||
| 484 | if (room_impl->server) { | ||
| 485 | enet_host_destroy(room_impl->server); | ||
| 486 | } | ||
| 487 | room_impl->room_information = {}; | ||
| 488 | room_impl->server = nullptr; | ||
| 489 | { | ||
| 490 | std::lock_guard<std::mutex> lock(room_impl->member_mutex); | ||
| 491 | room_impl->members.clear(); | ||
| 492 | } | ||
| 493 | room_impl->room_information.member_slots = 0; | ||
| 494 | room_impl->room_information.name.clear(); | ||
| 495 | } | ||
| 496 | |||
| 497 | } // namespace Network | ||
diff --git a/src/network/room.h b/src/network/room.h deleted file mode 100644 index 8285a4d0c..000000000 --- a/src/network/room.h +++ /dev/null | |||
| @@ -1,101 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <memory> | ||
| 9 | #include <string> | ||
| 10 | #include <vector> | ||
| 11 | #include "common/common_types.h" | ||
| 12 | |||
| 13 | namespace Network { | ||
| 14 | |||
| 15 | constexpr u32 network_version = 1; ///< The version of this Room and RoomMember | ||
| 16 | |||
| 17 | constexpr u16 DefaultRoomPort = 1234; | ||
| 18 | constexpr size_t NumChannels = 1; // Number of channels used for the connection | ||
| 19 | |||
| 20 | struct RoomInformation { | ||
| 21 | std::string name; ///< Name of the server | ||
| 22 | u32 member_slots; ///< Maximum number of members in this room | ||
| 23 | }; | ||
| 24 | |||
| 25 | struct GameInfo { | ||
| 26 | std::string name{""}; | ||
| 27 | u64 id{0}; | ||
| 28 | }; | ||
| 29 | |||
| 30 | using MacAddress = std::array<u8, 6>; | ||
| 31 | /// A special MAC address that tells the room we're joining to assign us a MAC address | ||
| 32 | /// automatically. | ||
| 33 | constexpr MacAddress NoPreferredMac = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; | ||
| 34 | |||
| 35 | // 802.11 broadcast MAC address | ||
| 36 | constexpr MacAddress BroadcastMac = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; | ||
| 37 | |||
| 38 | // The different types of messages that can be sent. The first byte of each packet defines the type | ||
| 39 | enum RoomMessageTypes : u8 { | ||
| 40 | IdJoinRequest = 1, | ||
| 41 | IdJoinSuccess, | ||
| 42 | IdRoomInformation, | ||
| 43 | IdSetGameInfo, | ||
| 44 | IdWifiPacket, | ||
| 45 | IdChatMessage, | ||
| 46 | IdNameCollision, | ||
| 47 | IdMacCollision, | ||
| 48 | IdVersionMismatch, | ||
| 49 | IdCloseRoom | ||
| 50 | }; | ||
| 51 | |||
| 52 | /// This is what a server [person creating a server] would use. | ||
| 53 | class Room final { | ||
| 54 | public: | ||
| 55 | enum class State : u8 { | ||
| 56 | Open, ///< The room is open and ready to accept connections. | ||
| 57 | Closed, ///< The room is not opened and can not accept connections. | ||
| 58 | }; | ||
| 59 | |||
| 60 | struct Member { | ||
| 61 | std::string nickname; ///< The nickname of the member. | ||
| 62 | GameInfo game_info; ///< The current game of the member | ||
| 63 | MacAddress mac_address; ///< The assigned mac address of the member. | ||
| 64 | }; | ||
| 65 | |||
| 66 | Room(); | ||
| 67 | ~Room(); | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Gets the current state of the room. | ||
| 71 | */ | ||
| 72 | State GetState() const; | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Gets the room information of the room. | ||
| 76 | */ | ||
| 77 | const RoomInformation& GetRoomInformation() const; | ||
| 78 | |||
| 79 | /** | ||
| 80 | * Gets a list of the mbmers connected to the room. | ||
| 81 | */ | ||
| 82 | std::vector<Member> GetRoomMemberList() const; | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Creates the socket for this room. Will bind to default address if | ||
| 86 | * server is empty string. | ||
| 87 | */ | ||
| 88 | void Create(const std::string& name, const std::string& server = "", | ||
| 89 | u16 server_port = DefaultRoomPort); | ||
| 90 | |||
| 91 | /** | ||
| 92 | * Destroys the socket | ||
| 93 | */ | ||
| 94 | void Destroy(); | ||
| 95 | |||
| 96 | private: | ||
| 97 | class RoomImpl; | ||
| 98 | std::unique_ptr<RoomImpl> room_impl; | ||
| 99 | }; | ||
| 100 | |||
| 101 | } // namespace Network | ||
diff --git a/src/network/room_member.cpp b/src/network/room_member.cpp deleted file mode 100644 index f229ec6fd..000000000 --- a/src/network/room_member.cpp +++ /dev/null | |||
| @@ -1,490 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <atomic> | ||
| 6 | #include <list> | ||
| 7 | #include <mutex> | ||
| 8 | #include <set> | ||
| 9 | #include <thread> | ||
| 10 | #include "common/assert.h" | ||
| 11 | #include "enet/enet.h" | ||
| 12 | #include "network/packet.h" | ||
| 13 | #include "network/room_member.h" | ||
| 14 | |||
| 15 | namespace Network { | ||
| 16 | |||
| 17 | constexpr u32 ConnectionTimeoutMs = 5000; | ||
| 18 | |||
| 19 | class RoomMember::RoomMemberImpl { | ||
| 20 | public: | ||
| 21 | ENetHost* client = nullptr; ///< ENet network interface. | ||
| 22 | ENetPeer* server = nullptr; ///< The server peer the client is connected to | ||
| 23 | |||
| 24 | /// Information about the clients connected to the same room as us. | ||
| 25 | MemberList member_information; | ||
| 26 | /// Information about the room we're connected to. | ||
| 27 | RoomInformation room_information; | ||
| 28 | |||
| 29 | /// The current game name, id and version | ||
| 30 | GameInfo current_game_info; | ||
| 31 | |||
| 32 | std::atomic<State> state{State::Idle}; ///< Current state of the RoomMember. | ||
| 33 | void SetState(const State new_state); | ||
| 34 | bool IsConnected() const; | ||
| 35 | |||
| 36 | std::string nickname; ///< The nickname of this member. | ||
| 37 | MacAddress mac_address; ///< The mac_address of this member. | ||
| 38 | |||
| 39 | std::mutex network_mutex; ///< Mutex that controls access to the `client` variable. | ||
| 40 | /// Thread that receives and dispatches network packets | ||
| 41 | std::unique_ptr<std::thread> loop_thread; | ||
| 42 | std::mutex send_list_mutex; ///< Mutex that controls access to the `send_list` variable. | ||
| 43 | std::list<Packet> send_list; ///< A list that stores all packets to send the async | ||
| 44 | |||
| 45 | template <typename T> | ||
| 46 | using CallbackSet = std::set<CallbackHandle<T>>; | ||
| 47 | std::mutex callback_mutex; ///< The mutex used for handling callbacks | ||
| 48 | |||
| 49 | class Callbacks { | ||
| 50 | public: | ||
| 51 | template <typename T> | ||
| 52 | CallbackSet<T>& Get(); | ||
| 53 | |||
| 54 | private: | ||
| 55 | CallbackSet<WifiPacket> callback_set_wifi_packet; | ||
| 56 | CallbackSet<ChatEntry> callback_set_chat_messages; | ||
| 57 | CallbackSet<RoomInformation> callback_set_room_information; | ||
| 58 | CallbackSet<State> callback_set_state; | ||
| 59 | }; | ||
| 60 | Callbacks callbacks; ///< All CallbackSets to all events | ||
| 61 | |||
| 62 | void MemberLoop(); | ||
| 63 | |||
| 64 | void StartLoop(); | ||
| 65 | |||
| 66 | /** | ||
| 67 | * Sends data to the room. It will be send on channel 0 with flag RELIABLE | ||
| 68 | * @param packet The data to send | ||
| 69 | */ | ||
| 70 | void Send(Packet&& packet); | ||
| 71 | |||
| 72 | /** | ||
| 73 | * Sends a request to the server, asking for permission to join a room with the specified | ||
| 74 | * nickname and preferred mac. | ||
| 75 | * @params nickname The desired nickname. | ||
| 76 | * @params preferred_mac The preferred MAC address to use in the room, the NoPreferredMac tells | ||
| 77 | * the server to assign one for us. | ||
| 78 | */ | ||
| 79 | void SendJoinRequest(const std::string& nickname, | ||
| 80 | const MacAddress& preferred_mac = NoPreferredMac); | ||
| 81 | |||
| 82 | /** | ||
| 83 | * Extracts a MAC Address from a received ENet packet. | ||
| 84 | * @param event The ENet event that was received. | ||
| 85 | */ | ||
| 86 | void HandleJoinPacket(const ENetEvent* event); | ||
| 87 | /** | ||
| 88 | * Extracts RoomInformation and MemberInformation from a received RakNet packet. | ||
| 89 | * @param event The ENet event that was received. | ||
| 90 | */ | ||
| 91 | void HandleRoomInformationPacket(const ENetEvent* event); | ||
| 92 | |||
| 93 | /** | ||
| 94 | * Extracts a WifiPacket from a received ENet packet. | ||
| 95 | * @param event The ENet event that was received. | ||
| 96 | */ | ||
| 97 | void HandleWifiPackets(const ENetEvent* event); | ||
| 98 | |||
| 99 | /** | ||
| 100 | * Extracts a chat entry from a received ENet packet and adds it to the chat queue. | ||
| 101 | * @param event The ENet event that was received. | ||
| 102 | */ | ||
| 103 | void HandleChatPacket(const ENetEvent* event); | ||
| 104 | |||
| 105 | /** | ||
| 106 | * Disconnects the RoomMember from the Room | ||
| 107 | */ | ||
| 108 | void Disconnect(); | ||
| 109 | |||
| 110 | template <typename T> | ||
| 111 | void Invoke(const T& data); | ||
| 112 | |||
| 113 | template <typename T> | ||
| 114 | CallbackHandle<T> Bind(std::function<void(const T&)> callback); | ||
| 115 | }; | ||
| 116 | |||
| 117 | // RoomMemberImpl | ||
| 118 | void RoomMember::RoomMemberImpl::SetState(const State new_state) { | ||
| 119 | if (state != new_state) { | ||
| 120 | state = new_state; | ||
| 121 | Invoke<State>(state); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | bool RoomMember::RoomMemberImpl::IsConnected() const { | ||
| 126 | return state == State::Joining || state == State::Joined; | ||
| 127 | } | ||
| 128 | |||
| 129 | void RoomMember::RoomMemberImpl::MemberLoop() { | ||
| 130 | // Receive packets while the connection is open | ||
| 131 | while (IsConnected()) { | ||
| 132 | std::lock_guard<std::mutex> lock(network_mutex); | ||
| 133 | ENetEvent event; | ||
| 134 | if (enet_host_service(client, &event, 100) > 0) { | ||
| 135 | switch (event.type) { | ||
| 136 | case ENET_EVENT_TYPE_RECEIVE: | ||
| 137 | switch (event.packet->data[0]) { | ||
| 138 | case IdWifiPacket: | ||
| 139 | HandleWifiPackets(&event); | ||
| 140 | break; | ||
| 141 | case IdChatMessage: | ||
| 142 | HandleChatPacket(&event); | ||
| 143 | break; | ||
| 144 | case IdRoomInformation: | ||
| 145 | HandleRoomInformationPacket(&event); | ||
| 146 | break; | ||
| 147 | case IdJoinSuccess: | ||
| 148 | // The join request was successful, we are now in the room. | ||
| 149 | // If we joined successfully, there must be at least one client in the room: us. | ||
| 150 | ASSERT_MSG(member_information.size() > 0, | ||
| 151 | "We have not yet received member information."); | ||
| 152 | HandleJoinPacket(&event); // Get the MAC Address for the client | ||
| 153 | SetState(State::Joined); | ||
| 154 | break; | ||
| 155 | case IdNameCollision: | ||
| 156 | SetState(State::NameCollision); | ||
| 157 | break; | ||
| 158 | case IdMacCollision: | ||
| 159 | SetState(State::MacCollision); | ||
| 160 | break; | ||
| 161 | case IdVersionMismatch: | ||
| 162 | SetState(State::WrongVersion); | ||
| 163 | break; | ||
| 164 | case IdCloseRoom: | ||
| 165 | SetState(State::LostConnection); | ||
| 166 | break; | ||
| 167 | } | ||
| 168 | enet_packet_destroy(event.packet); | ||
| 169 | break; | ||
| 170 | case ENET_EVENT_TYPE_DISCONNECT: | ||
| 171 | SetState(State::LostConnection); | ||
| 172 | break; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | { | ||
| 176 | std::lock_guard<std::mutex> lock(send_list_mutex); | ||
| 177 | for (const auto& packet : send_list) { | ||
| 178 | ENetPacket* enetPacket = enet_packet_create(packet.GetData(), packet.GetDataSize(), | ||
| 179 | ENET_PACKET_FLAG_RELIABLE); | ||
| 180 | enet_peer_send(server, 0, enetPacket); | ||
| 181 | } | ||
| 182 | enet_host_flush(client); | ||
| 183 | send_list.clear(); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | Disconnect(); | ||
| 187 | }; | ||
| 188 | |||
| 189 | void RoomMember::RoomMemberImpl::StartLoop() { | ||
| 190 | loop_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::MemberLoop, this); | ||
| 191 | } | ||
| 192 | |||
| 193 | void RoomMember::RoomMemberImpl::Send(Packet&& packet) { | ||
| 194 | std::lock_guard<std::mutex> lock(send_list_mutex); | ||
| 195 | send_list.push_back(std::move(packet)); | ||
| 196 | } | ||
| 197 | |||
| 198 | void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname, | ||
| 199 | const MacAddress& preferred_mac) { | ||
| 200 | Packet packet; | ||
| 201 | packet << static_cast<u8>(IdJoinRequest); | ||
| 202 | packet << nickname; | ||
| 203 | packet << preferred_mac; | ||
| 204 | packet << network_version; | ||
| 205 | Send(std::move(packet)); | ||
| 206 | } | ||
| 207 | |||
| 208 | void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* event) { | ||
| 209 | Packet packet; | ||
| 210 | packet.Append(event->packet->data, event->packet->dataLength); | ||
| 211 | |||
| 212 | // Ignore the first byte, which is the message id. | ||
| 213 | packet.IgnoreBytes(sizeof(u8)); // Igonore the message type | ||
| 214 | |||
| 215 | RoomInformation info{}; | ||
| 216 | packet >> info.name; | ||
| 217 | packet >> info.member_slots; | ||
| 218 | room_information.name = info.name; | ||
| 219 | room_information.member_slots = info.member_slots; | ||
| 220 | |||
| 221 | u32 num_members; | ||
| 222 | packet >> num_members; | ||
| 223 | member_information.resize(num_members); | ||
| 224 | |||
| 225 | for (auto& member : member_information) { | ||
| 226 | packet >> member.nickname; | ||
| 227 | packet >> member.mac_address; | ||
| 228 | packet >> member.game_info.name; | ||
| 229 | packet >> member.game_info.id; | ||
| 230 | } | ||
| 231 | Invoke(room_information); | ||
| 232 | } | ||
| 233 | |||
| 234 | void RoomMember::RoomMemberImpl::HandleJoinPacket(const ENetEvent* event) { | ||
| 235 | Packet packet; | ||
| 236 | packet.Append(event->packet->data, event->packet->dataLength); | ||
| 237 | |||
| 238 | // Ignore the first byte, which is the message id. | ||
| 239 | packet.IgnoreBytes(sizeof(u8)); // Igonore the message type | ||
| 240 | |||
| 241 | // Parse the MAC Address from the packet | ||
| 242 | packet >> mac_address; | ||
| 243 | SetState(State::Joined); | ||
| 244 | } | ||
| 245 | |||
| 246 | void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) { | ||
| 247 | WifiPacket wifi_packet{}; | ||
| 248 | Packet packet; | ||
| 249 | packet.Append(event->packet->data, event->packet->dataLength); | ||
| 250 | |||
| 251 | // Ignore the first byte, which is the message id. | ||
| 252 | packet.IgnoreBytes(sizeof(u8)); // Igonore the message type | ||
| 253 | |||
| 254 | // Parse the WifiPacket from the packet | ||
| 255 | u8 frame_type; | ||
| 256 | packet >> frame_type; | ||
| 257 | WifiPacket::PacketType type = static_cast<WifiPacket::PacketType>(frame_type); | ||
| 258 | |||
| 259 | wifi_packet.type = type; | ||
| 260 | packet >> wifi_packet.channel; | ||
| 261 | packet >> wifi_packet.transmitter_address; | ||
| 262 | packet >> wifi_packet.destination_address; | ||
| 263 | |||
| 264 | u32 data_length; | ||
| 265 | packet >> data_length; | ||
| 266 | |||
| 267 | packet >> wifi_packet.data; | ||
| 268 | |||
| 269 | Invoke<WifiPacket>(wifi_packet); | ||
| 270 | } | ||
| 271 | |||
| 272 | void RoomMember::RoomMemberImpl::HandleChatPacket(const ENetEvent* event) { | ||
| 273 | Packet packet; | ||
| 274 | packet.Append(event->packet->data, event->packet->dataLength); | ||
| 275 | |||
| 276 | // Ignore the first byte, which is the message id. | ||
| 277 | packet.IgnoreBytes(sizeof(u8)); | ||
| 278 | |||
| 279 | ChatEntry chat_entry{}; | ||
| 280 | packet >> chat_entry.nickname; | ||
| 281 | packet >> chat_entry.message; | ||
| 282 | Invoke<ChatEntry>(chat_entry); | ||
| 283 | } | ||
| 284 | |||
| 285 | void RoomMember::RoomMemberImpl::Disconnect() { | ||
| 286 | member_information.clear(); | ||
| 287 | room_information.member_slots = 0; | ||
| 288 | room_information.name.clear(); | ||
| 289 | |||
| 290 | if (!server) | ||
| 291 | return; | ||
| 292 | enet_peer_disconnect(server, 0); | ||
| 293 | |||
| 294 | ENetEvent event; | ||
| 295 | while (enet_host_service(client, &event, ConnectionTimeoutMs) > 0) { | ||
| 296 | switch (event.type) { | ||
| 297 | case ENET_EVENT_TYPE_RECEIVE: | ||
| 298 | enet_packet_destroy(event.packet); // Ignore all incoming data | ||
| 299 | break; | ||
| 300 | case ENET_EVENT_TYPE_DISCONNECT: | ||
| 301 | server = nullptr; | ||
| 302 | return; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | // didn't disconnect gracefully force disconnect | ||
| 306 | enet_peer_reset(server); | ||
| 307 | server = nullptr; | ||
| 308 | } | ||
| 309 | |||
| 310 | template <> | ||
| 311 | RoomMember::RoomMemberImpl::CallbackSet<WifiPacket>& RoomMember::RoomMemberImpl::Callbacks::Get() { | ||
| 312 | return callback_set_wifi_packet; | ||
| 313 | } | ||
| 314 | |||
| 315 | template <> | ||
| 316 | RoomMember::RoomMemberImpl::CallbackSet<RoomMember::State>& | ||
| 317 | RoomMember::RoomMemberImpl::Callbacks::Get() { | ||
| 318 | return callback_set_state; | ||
| 319 | } | ||
| 320 | |||
| 321 | template <> | ||
| 322 | RoomMember::RoomMemberImpl::CallbackSet<RoomInformation>& | ||
| 323 | RoomMember::RoomMemberImpl::Callbacks::Get() { | ||
| 324 | return callback_set_room_information; | ||
| 325 | } | ||
| 326 | |||
| 327 | template <> | ||
| 328 | RoomMember::RoomMemberImpl::CallbackSet<ChatEntry>& RoomMember::RoomMemberImpl::Callbacks::Get() { | ||
| 329 | return callback_set_chat_messages; | ||
| 330 | } | ||
| 331 | |||
| 332 | template <typename T> | ||
| 333 | void RoomMember::RoomMemberImpl::Invoke(const T& data) { | ||
| 334 | std::lock_guard<std::mutex> lock(callback_mutex); | ||
| 335 | CallbackSet<T> callback_set = callbacks.Get<T>(); | ||
| 336 | for (auto const& callback : callback_set) | ||
| 337 | (*callback)(data); | ||
| 338 | } | ||
| 339 | |||
| 340 | template <typename T> | ||
| 341 | RoomMember::CallbackHandle<T> RoomMember::RoomMemberImpl::Bind( | ||
| 342 | std::function<void(const T&)> callback) { | ||
| 343 | std::lock_guard<std::mutex> lock(callback_mutex); | ||
| 344 | CallbackHandle<T> handle; | ||
| 345 | handle = std::make_shared<std::function<void(const T&)>>(callback); | ||
| 346 | callbacks.Get<T>().insert(handle); | ||
| 347 | return handle; | ||
| 348 | } | ||
| 349 | |||
| 350 | // RoomMember | ||
| 351 | RoomMember::RoomMember() : room_member_impl{std::make_unique<RoomMemberImpl>()} { | ||
| 352 | room_member_impl->client = enet_host_create(nullptr, 1, NumChannels, 0, 0); | ||
| 353 | ASSERT_MSG(room_member_impl->client != nullptr, "Could not create client"); | ||
| 354 | } | ||
| 355 | |||
| 356 | RoomMember::~RoomMember() { | ||
| 357 | ASSERT_MSG(!IsConnected(), "RoomMember is being destroyed while connected"); | ||
| 358 | enet_host_destroy(room_member_impl->client); | ||
| 359 | } | ||
| 360 | |||
| 361 | RoomMember::State RoomMember::GetState() const { | ||
| 362 | return room_member_impl->state; | ||
| 363 | } | ||
| 364 | |||
| 365 | const RoomMember::MemberList& RoomMember::GetMemberInformation() const { | ||
| 366 | return room_member_impl->member_information; | ||
| 367 | } | ||
| 368 | |||
| 369 | const std::string& RoomMember::GetNickname() const { | ||
| 370 | return room_member_impl->nickname; | ||
| 371 | } | ||
| 372 | |||
| 373 | const MacAddress& RoomMember::GetMacAddress() const { | ||
| 374 | ASSERT_MSG(IsConnected(), "Tried to get MAC address while not connected"); | ||
| 375 | return room_member_impl->mac_address; | ||
| 376 | } | ||
| 377 | |||
| 378 | RoomInformation RoomMember::GetRoomInformation() const { | ||
| 379 | return room_member_impl->room_information; | ||
| 380 | } | ||
| 381 | |||
| 382 | void RoomMember::Join(const std::string& nick, const char* server_addr, u16 server_port, | ||
| 383 | u16 client_port, const MacAddress& preferred_mac) { | ||
| 384 | // If the member is connected, kill the connection first | ||
| 385 | if (room_member_impl->loop_thread && room_member_impl->loop_thread->joinable()) { | ||
| 386 | room_member_impl->SetState(State::Error); | ||
| 387 | room_member_impl->loop_thread->join(); | ||
| 388 | room_member_impl->loop_thread.reset(); | ||
| 389 | } | ||
| 390 | // If the thread isn't running but the ptr still exists, reset it | ||
| 391 | else if (room_member_impl->loop_thread) { | ||
| 392 | room_member_impl->loop_thread.reset(); | ||
| 393 | } | ||
| 394 | |||
| 395 | ENetAddress address{}; | ||
| 396 | enet_address_set_host(&address, server_addr); | ||
| 397 | address.port = server_port; | ||
| 398 | room_member_impl->server = | ||
| 399 | enet_host_connect(room_member_impl->client, &address, NumChannels, 0); | ||
| 400 | |||
| 401 | if (!room_member_impl->server) { | ||
| 402 | room_member_impl->SetState(State::Error); | ||
| 403 | return; | ||
| 404 | } | ||
| 405 | |||
| 406 | ENetEvent event{}; | ||
| 407 | int net = enet_host_service(room_member_impl->client, &event, ConnectionTimeoutMs); | ||
| 408 | if (net > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { | ||
| 409 | room_member_impl->nickname = nick; | ||
| 410 | room_member_impl->SetState(State::Joining); | ||
| 411 | room_member_impl->StartLoop(); | ||
| 412 | room_member_impl->SendJoinRequest(nick, preferred_mac); | ||
| 413 | SendGameInfo(room_member_impl->current_game_info); | ||
| 414 | } else { | ||
| 415 | room_member_impl->SetState(State::CouldNotConnect); | ||
| 416 | } | ||
| 417 | } | ||
| 418 | |||
| 419 | bool RoomMember::IsConnected() const { | ||
| 420 | return room_member_impl->IsConnected(); | ||
| 421 | } | ||
| 422 | |||
| 423 | void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) { | ||
| 424 | Packet packet; | ||
| 425 | packet << static_cast<u8>(IdWifiPacket); | ||
| 426 | packet << static_cast<u8>(wifi_packet.type); | ||
| 427 | packet << wifi_packet.channel; | ||
| 428 | packet << wifi_packet.transmitter_address; | ||
| 429 | packet << wifi_packet.destination_address; | ||
| 430 | packet << wifi_packet.data; | ||
| 431 | room_member_impl->Send(std::move(packet)); | ||
| 432 | } | ||
| 433 | |||
| 434 | void RoomMember::SendChatMessage(const std::string& message) { | ||
| 435 | Packet packet; | ||
| 436 | packet << static_cast<u8>(IdChatMessage); | ||
| 437 | packet << message; | ||
| 438 | room_member_impl->Send(std::move(packet)); | ||
| 439 | } | ||
| 440 | |||
| 441 | void RoomMember::SendGameInfo(const GameInfo& game_info) { | ||
| 442 | room_member_impl->current_game_info = game_info; | ||
| 443 | if (!IsConnected()) | ||
| 444 | return; | ||
| 445 | |||
| 446 | Packet packet; | ||
| 447 | packet << static_cast<u8>(IdSetGameInfo); | ||
| 448 | packet << game_info.name; | ||
| 449 | packet << game_info.id; | ||
| 450 | room_member_impl->Send(std::move(packet)); | ||
| 451 | } | ||
| 452 | |||
| 453 | RoomMember::CallbackHandle<RoomMember::State> RoomMember::BindOnStateChanged( | ||
| 454 | std::function<void(const RoomMember::State&)> callback) { | ||
| 455 | return room_member_impl->Bind(callback); | ||
| 456 | } | ||
| 457 | |||
| 458 | RoomMember::CallbackHandle<WifiPacket> RoomMember::BindOnWifiPacketReceived( | ||
| 459 | std::function<void(const WifiPacket&)> callback) { | ||
| 460 | return room_member_impl->Bind(callback); | ||
| 461 | } | ||
| 462 | |||
| 463 | RoomMember::CallbackHandle<RoomInformation> RoomMember::BindOnRoomInformationChanged( | ||
| 464 | std::function<void(const RoomInformation&)> callback) { | ||
| 465 | return room_member_impl->Bind(callback); | ||
| 466 | } | ||
| 467 | |||
| 468 | RoomMember::CallbackHandle<ChatEntry> RoomMember::BindOnChatMessageRecieved( | ||
| 469 | std::function<void(const ChatEntry&)> callback) { | ||
| 470 | return room_member_impl->Bind(callback); | ||
| 471 | } | ||
| 472 | |||
| 473 | template <typename T> | ||
| 474 | void RoomMember::Unbind(CallbackHandle<T> handle) { | ||
| 475 | std::lock_guard<std::mutex> lock(room_member_impl->callback_mutex); | ||
| 476 | room_member_impl->callbacks.Get<T>().erase(handle); | ||
| 477 | } | ||
| 478 | |||
| 479 | void RoomMember::Leave() { | ||
| 480 | room_member_impl->SetState(State::Idle); | ||
| 481 | room_member_impl->loop_thread->join(); | ||
| 482 | room_member_impl->loop_thread.reset(); | ||
| 483 | } | ||
| 484 | |||
| 485 | template void RoomMember::Unbind(CallbackHandle<WifiPacket>); | ||
| 486 | template void RoomMember::Unbind(CallbackHandle<RoomMember::State>); | ||
| 487 | template void RoomMember::Unbind(CallbackHandle<RoomInformation>); | ||
| 488 | template void RoomMember::Unbind(CallbackHandle<ChatEntry>); | ||
| 489 | |||
| 490 | } // namespace Network | ||
diff --git a/src/network/room_member.h b/src/network/room_member.h deleted file mode 100644 index 98770a234..000000000 --- a/src/network/room_member.h +++ /dev/null | |||
| @@ -1,182 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <functional> | ||
| 8 | #include <memory> | ||
| 9 | #include <string> | ||
| 10 | #include <vector> | ||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "network/room.h" | ||
| 13 | |||
| 14 | namespace Network { | ||
| 15 | |||
| 16 | /// Information about the received WiFi packets. | ||
| 17 | /// Acts as our own 802.11 header. | ||
| 18 | struct WifiPacket { | ||
| 19 | enum class PacketType : u8 { Beacon, Data, Authentication, AssociationResponse }; | ||
| 20 | PacketType type; ///< The type of 802.11 frame. | ||
| 21 | std::vector<u8> data; ///< Raw 802.11 frame data, starting at the management frame header | ||
| 22 | /// for management frames. | ||
| 23 | MacAddress transmitter_address; ///< Mac address of the transmitter. | ||
| 24 | MacAddress destination_address; ///< Mac address of the receiver. | ||
| 25 | u8 channel; ///< WiFi channel where this frame was transmitted. | ||
| 26 | }; | ||
| 27 | |||
| 28 | /// Represents a chat message. | ||
| 29 | struct ChatEntry { | ||
| 30 | std::string nickname; ///< Nickname of the client who sent this message. | ||
| 31 | std::string message; ///< Body of the message. | ||
| 32 | }; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * This is what a client [person joining a server] would use. | ||
| 36 | * It also has to be used if you host a game yourself (You'd create both, a Room and a | ||
| 37 | * RoomMembership for yourself) | ||
| 38 | */ | ||
| 39 | class RoomMember final { | ||
| 40 | public: | ||
| 41 | enum class State : u8 { | ||
| 42 | Idle, ///< Default state | ||
| 43 | Error, ///< Some error [permissions to network device missing or something] | ||
| 44 | Joining, ///< The client is attempting to join a room. | ||
| 45 | Joined, ///< The client is connected to the room and is ready to send/receive packets. | ||
| 46 | LostConnection, ///< Connection closed | ||
| 47 | |||
| 48 | // Reasons why connection was rejected | ||
| 49 | NameCollision, ///< Somebody is already using this name | ||
| 50 | MacCollision, ///< Somebody is already using that mac-address | ||
| 51 | WrongVersion, ///< The room version is not the same as for this RoomMember | ||
| 52 | CouldNotConnect ///< The room is not responding to a connection attempt | ||
| 53 | }; | ||
| 54 | |||
| 55 | struct MemberInformation { | ||
| 56 | std::string nickname; ///< Nickname of the member. | ||
| 57 | GameInfo game_info; ///< Name of the game they're currently playing, or empty if they're | ||
| 58 | /// not playing anything. | ||
| 59 | MacAddress mac_address; ///< MAC address associated with this member. | ||
| 60 | }; | ||
| 61 | using MemberList = std::vector<MemberInformation>; | ||
| 62 | |||
| 63 | // The handle for the callback functions | ||
| 64 | template <typename T> | ||
| 65 | using CallbackHandle = std::shared_ptr<std::function<void(const T&)>>; | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Unbinds a callback function from the events. | ||
| 69 | * @param handle The connection handle to disconnect | ||
| 70 | */ | ||
| 71 | template <typename T> | ||
| 72 | void Unbind(CallbackHandle<T> handle); | ||
| 73 | |||
| 74 | RoomMember(); | ||
| 75 | ~RoomMember(); | ||
| 76 | |||
| 77 | /** | ||
| 78 | * Returns the status of our connection to the room. | ||
| 79 | */ | ||
| 80 | State GetState() const; | ||
| 81 | |||
| 82 | /** | ||
| 83 | * Returns information about the members in the room we're currently connected to. | ||
| 84 | */ | ||
| 85 | const MemberList& GetMemberInformation() const; | ||
| 86 | |||
| 87 | /** | ||
| 88 | * Returns the nickname of the RoomMember. | ||
| 89 | */ | ||
| 90 | const std::string& GetNickname() const; | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Returns the MAC address of the RoomMember. | ||
| 94 | */ | ||
| 95 | const MacAddress& GetMacAddress() const; | ||
| 96 | |||
| 97 | /** | ||
| 98 | * Returns information about the room we're currently connected to. | ||
| 99 | */ | ||
| 100 | RoomInformation GetRoomInformation() const; | ||
| 101 | |||
| 102 | /** | ||
| 103 | * Returns whether we're connected to a server or not. | ||
| 104 | */ | ||
| 105 | bool IsConnected() const; | ||
| 106 | |||
| 107 | /** | ||
| 108 | * Attempts to join a room at the specified address and port, using the specified nickname. | ||
| 109 | * This may fail if the username is already taken. | ||
| 110 | */ | ||
| 111 | void Join(const std::string& nickname, const char* server_addr = "127.0.0.1", | ||
| 112 | const u16 serverPort = DefaultRoomPort, const u16 clientPort = 0, | ||
| 113 | const MacAddress& preferred_mac = NoPreferredMac); | ||
| 114 | |||
| 115 | /** | ||
| 116 | * Sends a WiFi packet to the room. | ||
| 117 | * @param packet The WiFi packet to send. | ||
| 118 | */ | ||
| 119 | void SendWifiPacket(const WifiPacket& packet); | ||
| 120 | |||
| 121 | /** | ||
| 122 | * Sends a chat message to the room. | ||
| 123 | * @param message The contents of the message. | ||
| 124 | */ | ||
| 125 | void SendChatMessage(const std::string& message); | ||
| 126 | |||
| 127 | /** | ||
| 128 | * Sends the current game info to the room. | ||
| 129 | * @param game_info The game information. | ||
| 130 | */ | ||
| 131 | void SendGameInfo(const GameInfo& game_info); | ||
| 132 | |||
| 133 | /** | ||
| 134 | * Binds a function to an event that will be triggered every time the State of the member | ||
| 135 | * changed. The function wil be called every time the event is triggered. The callback function | ||
| 136 | * must not bind or unbind a function. Doing so will cause a deadlock | ||
| 137 | * @param callback The function to call | ||
| 138 | * @return A handle used for removing the function from the registered list | ||
| 139 | */ | ||
| 140 | CallbackHandle<State> BindOnStateChanged(std::function<void(const State&)> callback); | ||
| 141 | |||
| 142 | /** | ||
| 143 | * Binds a function to an event that will be triggered every time a WifiPacket is received. | ||
| 144 | * The function wil be called everytime the event is triggered. | ||
| 145 | * The callback function must not bind or unbind a function. Doing so will cause a deadlock | ||
| 146 | * @param callback The function to call | ||
| 147 | * @return A handle used for removing the function from the registered list | ||
| 148 | */ | ||
| 149 | CallbackHandle<WifiPacket> BindOnWifiPacketReceived( | ||
| 150 | std::function<void(const WifiPacket&)> callback); | ||
| 151 | |||
| 152 | /** | ||
| 153 | * Binds a function to an event that will be triggered every time the RoomInformation changes. | ||
| 154 | * The function wil be called every time the event is triggered. | ||
| 155 | * The callback function must not bind or unbind a function. Doing so will cause a deadlock | ||
| 156 | * @param callback The function to call | ||
| 157 | * @return A handle used for removing the function from the registered list | ||
| 158 | */ | ||
| 159 | CallbackHandle<RoomInformation> BindOnRoomInformationChanged( | ||
| 160 | std::function<void(const RoomInformation&)> callback); | ||
| 161 | |||
| 162 | /** | ||
| 163 | * Binds a function to an event that will be triggered every time a ChatMessage is received. | ||
| 164 | * The function wil be called every time the event is triggered. | ||
| 165 | * The callback function must not bind or unbind a function. Doing so will cause a deadlock | ||
| 166 | * @param callback The function to call | ||
| 167 | * @return A handle used for removing the function from the registered list | ||
| 168 | */ | ||
| 169 | CallbackHandle<ChatEntry> BindOnChatMessageRecieved( | ||
| 170 | std::function<void(const ChatEntry&)> callback); | ||
| 171 | |||
| 172 | /** | ||
| 173 | * Leaves the current room. | ||
| 174 | */ | ||
| 175 | void Leave(); | ||
| 176 | |||
| 177 | private: | ||
| 178 | class RoomMemberImpl; | ||
| 179 | std::unique_ptr<RoomMemberImpl> room_member_impl; | ||
| 180 | }; | ||
| 181 | |||
| 182 | } // namespace Network | ||