2017-07-07 21:34:15 +02:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2017-07-08 15:24:47 +02:00
|
|
|
#include <atomic>
|
2017-07-14 09:20:39 +02:00
|
|
|
#include <list>
|
2017-07-08 15:24:47 +02:00
|
|
|
#include <mutex>
|
2017-08-19 19:14:33 +02:00
|
|
|
#include <set>
|
2017-07-08 15:24:47 +02:00
|
|
|
#include <thread>
|
2017-07-07 21:34:15 +02:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "enet/enet.h"
|
2017-07-08 18:31:35 +02:00
|
|
|
#include "network/packet.h"
|
2017-07-07 21:34:15 +02:00
|
|
|
#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
|
|
|
|
|
2017-07-08 18:31:35 +02:00
|
|
|
/// 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;
|
|
|
|
|
2017-08-19 19:14:33 +02:00
|
|
|
/// The current game name, id and version
|
|
|
|
GameInfo current_game_info;
|
|
|
|
|
2017-07-07 21:34:15 +02:00
|
|
|
std::atomic<State> state{State::Idle}; ///< Current state of the RoomMember.
|
2017-07-08 15:24:47 +02:00
|
|
|
void SetState(const State new_state);
|
|
|
|
bool IsConnected() const;
|
2017-07-07 21:34:15 +02:00
|
|
|
|
2018-10-27 09:35:01 +02:00
|
|
|
std::string nickname; ///< The nickname of this member.
|
|
|
|
|
|
|
|
std::string username; ///< The username of this member.
|
|
|
|
mutable std::mutex username_mutex; ///< Mutex for locking username.
|
|
|
|
|
2017-07-08 18:31:35 +02:00
|
|
|
MacAddress mac_address; ///< The mac_address of this member.
|
2017-07-08 15:24:47 +02:00
|
|
|
|
|
|
|
std::mutex network_mutex; ///< Mutex that controls access to the `client` variable.
|
|
|
|
/// Thread that receives and dispatches network packets
|
2017-07-14 09:20:39 +02:00
|
|
|
std::unique_ptr<std::thread> loop_thread;
|
|
|
|
std::mutex send_list_mutex; ///< Mutex that controls access to the `send_list` variable.
|
|
|
|
std::list<Packet> send_list; ///< A list that stores all packets to send the async
|
2017-08-19 19:14:33 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using CallbackSet = std::set<CallbackHandle<T>>;
|
|
|
|
std::mutex callback_mutex; ///< The mutex used for handling callbacks
|
|
|
|
|
|
|
|
class Callbacks {
|
|
|
|
public:
|
|
|
|
template <typename T>
|
|
|
|
CallbackSet<T>& Get();
|
|
|
|
|
|
|
|
private:
|
|
|
|
CallbackSet<WifiPacket> callback_set_wifi_packet;
|
|
|
|
CallbackSet<ChatEntry> callback_set_chat_messages;
|
2018-11-09 14:55:57 +01:00
|
|
|
CallbackSet<StatusMessageEntry> callback_set_status_messages;
|
2017-08-19 19:14:33 +02:00
|
|
|
CallbackSet<RoomInformation> callback_set_room_information;
|
|
|
|
CallbackSet<State> callback_set_state;
|
|
|
|
};
|
|
|
|
Callbacks callbacks; ///< All CallbackSets to all events
|
|
|
|
|
2017-07-14 09:20:39 +02:00
|
|
|
void MemberLoop();
|
2017-07-15 21:24:11 +02:00
|
|
|
|
2017-07-08 15:24:47 +02:00
|
|
|
void StartLoop();
|
2017-07-08 18:31:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends data to the room. It will be send on channel 0 with flag RELIABLE
|
|
|
|
* @param packet The data to send
|
|
|
|
*/
|
2017-07-15 21:24:11 +02:00
|
|
|
void Send(Packet&& packet);
|
|
|
|
|
2017-07-08 18:31:35 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
2018-10-31 16:07:03 +01:00
|
|
|
* @params console_id_hash A hash of the Console ID.
|
2017-07-08 18:31:35 +02:00
|
|
|
* @params preferred_mac The preferred MAC address to use in the room, the NoPreferredMac tells
|
2017-11-19 19:52:37 +01:00
|
|
|
* @params password The password for the room
|
2017-07-08 18:31:35 +02:00
|
|
|
* the server to assign one for us.
|
|
|
|
*/
|
2018-10-31 16:07:03 +01:00
|
|
|
void SendJoinRequest(const std::string& nickname, const std::string& console_id_hash,
|
2017-11-19 19:52:37 +01:00
|
|
|
const MacAddress& preferred_mac = NoPreferredMac,
|
2018-10-27 09:35:01 +02:00
|
|
|
const std::string& password = "", const std::string& token = "");
|
2017-07-08 18:31:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts a MAC Address from a received ENet packet.
|
|
|
|
* @param event The ENet event that was received.
|
|
|
|
*/
|
|
|
|
void HandleJoinPacket(const ENetEvent* event);
|
|
|
|
/**
|
2018-09-08 03:17:24 +02:00
|
|
|
* Extracts RoomInformation and MemberInformation from a received ENet packet.
|
2017-07-08 18:31:35 +02:00
|
|
|
* @param event The ENet event that was received.
|
|
|
|
*/
|
|
|
|
void HandleRoomInformationPacket(const ENetEvent* event);
|
2017-07-09 10:40:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts a WifiPacket from a received ENet packet.
|
|
|
|
* @param event The ENet event that was received.
|
|
|
|
*/
|
|
|
|
void HandleWifiPackets(const ENetEvent* event);
|
2017-07-09 12:26:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2017-07-09 15:06:02 +02:00
|
|
|
|
2018-11-09 14:55:57 +01:00
|
|
|
/**
|
|
|
|
* Extracts a system message entry from a received ENet packet and adds it to the system message
|
|
|
|
* queue.
|
|
|
|
* @param event The ENet event that was received.
|
|
|
|
*/
|
|
|
|
void HandleStatusMessagePacket(const ENetEvent* event);
|
|
|
|
|
2017-07-09 15:06:02 +02:00
|
|
|
/**
|
|
|
|
* Disconnects the RoomMember from the Room
|
|
|
|
*/
|
|
|
|
void Disconnect();
|
2017-08-19 19:14:33 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void Invoke(const T& data);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
CallbackHandle<T> Bind(std::function<void(const T&)> callback);
|
2017-07-07 21:34:15 +02:00
|
|
|
};
|
|
|
|
|
2017-07-08 15:24:47 +02:00
|
|
|
// RoomMemberImpl
|
|
|
|
void RoomMember::RoomMemberImpl::SetState(const State new_state) {
|
2017-08-19 19:14:33 +02:00
|
|
|
if (state != new_state) {
|
|
|
|
state = new_state;
|
|
|
|
Invoke<State>(state);
|
|
|
|
}
|
2017-07-08 15:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RoomMember::RoomMemberImpl::IsConnected() const {
|
|
|
|
return state == State::Joining || state == State::Joined;
|
|
|
|
}
|
|
|
|
|
2017-07-14 09:20:39 +02:00
|
|
|
void RoomMember::RoomMemberImpl::MemberLoop() {
|
2017-07-08 15:24:47 +02:00
|
|
|
// Receive packets while the connection is open
|
|
|
|
while (IsConnected()) {
|
|
|
|
std::lock_guard<std::mutex> lock(network_mutex);
|
|
|
|
ENetEvent event;
|
2017-07-15 21:24:11 +02:00
|
|
|
if (enet_host_service(client, &event, 100) > 0) {
|
2017-07-09 15:06:02 +02:00
|
|
|
switch (event.type) {
|
|
|
|
case ENET_EVENT_TYPE_RECEIVE:
|
2017-07-08 15:24:47 +02:00
|
|
|
switch (event.packet->data[0]) {
|
2017-07-15 21:24:11 +02:00
|
|
|
case IdWifiPacket:
|
|
|
|
HandleWifiPackets(&event);
|
|
|
|
break;
|
2017-07-09 12:26:03 +02:00
|
|
|
case IdChatMessage:
|
|
|
|
HandleChatPacket(&event);
|
|
|
|
break;
|
2018-11-09 14:55:57 +01:00
|
|
|
case IdStatusMessage:
|
|
|
|
HandleStatusMessagePacket(&event);
|
|
|
|
break;
|
2017-07-08 18:31:35 +02:00
|
|
|
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;
|
2018-04-20 09:34:37 +02:00
|
|
|
case IdRoomIsFull:
|
|
|
|
SetState(State::RoomIsFull);
|
|
|
|
break;
|
2017-07-08 15:24:47 +02:00
|
|
|
case IdNameCollision:
|
|
|
|
SetState(State::NameCollision);
|
|
|
|
break;
|
|
|
|
case IdMacCollision:
|
|
|
|
SetState(State::MacCollision);
|
|
|
|
break;
|
2018-10-31 16:07:03 +01:00
|
|
|
case IdConsoleIdCollision:
|
|
|
|
SetState(State::ConsoleIdCollision);
|
|
|
|
break;
|
2017-07-14 09:20:39 +02:00
|
|
|
case IdVersionMismatch:
|
|
|
|
SetState(State::WrongVersion);
|
|
|
|
break;
|
2017-11-19 19:52:37 +01:00
|
|
|
case IdWrongPassword:
|
|
|
|
SetState(State::WrongPassword);
|
|
|
|
break;
|
2017-07-15 11:39:27 +02:00
|
|
|
case IdCloseRoom:
|
|
|
|
SetState(State::LostConnection);
|
|
|
|
break;
|
2017-07-08 15:24:47 +02:00
|
|
|
}
|
|
|
|
enet_packet_destroy(event.packet);
|
2017-07-09 15:06:02 +02:00
|
|
|
break;
|
|
|
|
case ENET_EVENT_TYPE_DISCONNECT:
|
|
|
|
SetState(State::LostConnection);
|
2017-07-14 09:20:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(send_list_mutex);
|
|
|
|
for (const auto& packet : send_list) {
|
|
|
|
ENetPacket* enetPacket = enet_packet_create(packet.GetData(), packet.GetDataSize(),
|
|
|
|
ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(server, 0, enetPacket);
|
2017-07-08 15:24:47 +02:00
|
|
|
}
|
2017-07-14 09:20:39 +02:00
|
|
|
enet_host_flush(client);
|
|
|
|
send_list.clear();
|
2017-07-08 15:24:47 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-09 15:06:02 +02:00
|
|
|
Disconnect();
|
2017-07-08 15:24:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void RoomMember::RoomMemberImpl::StartLoop() {
|
2017-07-14 09:20:39 +02:00
|
|
|
loop_thread = std::make_unique<std::thread>(&RoomMember::RoomMemberImpl::MemberLoop, this);
|
2017-07-08 15:24:47 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 21:24:11 +02:00
|
|
|
void RoomMember::RoomMemberImpl::Send(Packet&& packet) {
|
2017-07-14 09:20:39 +02:00
|
|
|
std::lock_guard<std::mutex> lock(send_list_mutex);
|
|
|
|
send_list.push_back(std::move(packet));
|
2017-07-08 18:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname,
|
2018-10-31 16:07:03 +01:00
|
|
|
const std::string& console_id_hash,
|
2017-11-19 19:52:37 +01:00
|
|
|
const MacAddress& preferred_mac,
|
2018-10-27 09:35:01 +02:00
|
|
|
const std::string& password,
|
|
|
|
const std::string& token) {
|
2017-07-08 18:31:35 +02:00
|
|
|
Packet packet;
|
2017-07-15 21:24:11 +02:00
|
|
|
packet << static_cast<u8>(IdJoinRequest);
|
2017-07-08 18:31:35 +02:00
|
|
|
packet << nickname;
|
2018-10-31 16:07:03 +01:00
|
|
|
packet << console_id_hash;
|
2017-07-08 18:31:35 +02:00
|
|
|
packet << preferred_mac;
|
2017-07-14 09:20:39 +02:00
|
|
|
packet << network_version;
|
2017-11-19 19:52:37 +01:00
|
|
|
packet << password;
|
2018-10-27 09:35:01 +02:00
|
|
|
packet << token;
|
2017-07-15 21:24:11 +02:00
|
|
|
Send(std::move(packet));
|
2017-07-08 18:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2018-09-08 03:12:52 +02:00
|
|
|
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
|
2017-07-08 18:31:35 +02:00
|
|
|
|
|
|
|
RoomInformation info{};
|
|
|
|
packet >> info.name;
|
2018-04-30 09:40:51 +02:00
|
|
|
packet >> info.description;
|
2017-07-08 18:31:35 +02:00
|
|
|
packet >> info.member_slots;
|
2017-11-19 19:52:37 +01:00
|
|
|
packet >> info.port;
|
|
|
|
packet >> info.preferred_game;
|
2017-07-08 18:31:35 +02:00
|
|
|
room_information.name = info.name;
|
2018-04-30 09:40:51 +02:00
|
|
|
room_information.description = info.description;
|
2017-07-08 18:31:35 +02:00
|
|
|
room_information.member_slots = info.member_slots;
|
2017-11-19 19:52:37 +01:00
|
|
|
room_information.port = info.port;
|
|
|
|
room_information.preferred_game = info.preferred_game;
|
2017-07-08 18:31:35 +02:00
|
|
|
|
2017-07-09 15:06:02 +02:00
|
|
|
u32 num_members;
|
2017-07-08 18:31:35 +02:00
|
|
|
packet >> num_members;
|
|
|
|
member_information.resize(num_members);
|
|
|
|
|
|
|
|
for (auto& member : member_information) {
|
|
|
|
packet >> member.nickname;
|
|
|
|
packet >> member.mac_address;
|
2017-08-19 19:14:33 +02:00
|
|
|
packet >> member.game_info.name;
|
|
|
|
packet >> member.game_info.id;
|
2018-10-27 09:35:01 +02:00
|
|
|
packet >> member.username;
|
|
|
|
packet >> member.display_name;
|
|
|
|
packet >> member.avatar_url;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(username_mutex);
|
|
|
|
if (member.nickname == nickname) {
|
|
|
|
username = member.username;
|
|
|
|
}
|
|
|
|
}
|
2017-07-08 18:31:35 +02:00
|
|
|
}
|
2017-08-19 19:14:33 +02:00
|
|
|
Invoke(room_information);
|
2017-07-08 18:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2018-09-08 03:12:52 +02:00
|
|
|
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
|
2017-07-08 18:31:35 +02:00
|
|
|
|
2017-07-15 21:24:11 +02:00
|
|
|
// Parse the MAC Address from the packet
|
2017-07-08 18:31:35 +02:00
|
|
|
packet >> mac_address;
|
2017-08-19 19:14:33 +02:00
|
|
|
SetState(State::Joined);
|
2017-07-08 18:31:35 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 10:40:11 +02:00
|
|
|
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.
|
2018-09-08 03:12:52 +02:00
|
|
|
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
|
2017-07-09 10:40:11 +02:00
|
|
|
|
2017-07-15 21:24:11 +02:00
|
|
|
// Parse the WifiPacket from the packet
|
2017-07-09 15:06:02 +02:00
|
|
|
u8 frame_type;
|
2017-07-09 10:40:11 +02:00
|
|
|
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;
|
2017-07-15 21:24:11 +02:00
|
|
|
packet >> wifi_packet.data;
|
2017-07-09 10:40:11 +02:00
|
|
|
|
2017-08-19 19:14:33 +02:00
|
|
|
Invoke<WifiPacket>(wifi_packet);
|
2017-07-09 10:40:11 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 12:26:03 +02:00
|
|
|
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.
|
2017-07-15 21:24:11 +02:00
|
|
|
packet.IgnoreBytes(sizeof(u8));
|
2017-07-09 12:26:03 +02:00
|
|
|
|
|
|
|
ChatEntry chat_entry{};
|
|
|
|
packet >> chat_entry.nickname;
|
2018-10-27 09:35:01 +02:00
|
|
|
packet >> chat_entry.username;
|
2017-07-09 12:26:03 +02:00
|
|
|
packet >> chat_entry.message;
|
2017-08-19 19:14:33 +02:00
|
|
|
Invoke<ChatEntry>(chat_entry);
|
2017-07-09 12:26:03 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 14:55:57 +01:00
|
|
|
void RoomMember::RoomMemberImpl::HandleStatusMessagePacket(const ENetEvent* event) {
|
|
|
|
Packet packet;
|
|
|
|
packet.Append(event->packet->data, event->packet->dataLength);
|
|
|
|
|
|
|
|
// Ignore the first byte, which is the message id.
|
|
|
|
packet.IgnoreBytes(sizeof(u8));
|
|
|
|
|
|
|
|
StatusMessageEntry status_message_entry{};
|
|
|
|
u8 type{};
|
|
|
|
packet >> type;
|
|
|
|
status_message_entry.type = static_cast<StatusMessageTypes>(type);
|
|
|
|
packet >> status_message_entry.nickname;
|
|
|
|
packet >> status_message_entry.username;
|
|
|
|
Invoke<StatusMessageEntry>(status_message_entry);
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:06:02 +02:00
|
|
|
void RoomMember::RoomMemberImpl::Disconnect() {
|
|
|
|
member_information.clear();
|
|
|
|
room_information.member_slots = 0;
|
|
|
|
room_information.name.clear();
|
|
|
|
|
2017-07-14 09:20:39 +02:00
|
|
|
if (!server)
|
2017-07-09 15:06:02 +02:00
|
|
|
return;
|
2017-07-14 09:20:39 +02:00
|
|
|
enet_peer_disconnect(server, 0);
|
2017-07-09 15:06:02 +02:00
|
|
|
|
|
|
|
ENetEvent event;
|
|
|
|
while (enet_host_service(client, &event, ConnectionTimeoutMs) > 0) {
|
|
|
|
switch (event.type) {
|
|
|
|
case ENET_EVENT_TYPE_RECEIVE:
|
|
|
|
enet_packet_destroy(event.packet); // Ignore all incoming data
|
|
|
|
break;
|
|
|
|
case ENET_EVENT_TYPE_DISCONNECT:
|
|
|
|
server = nullptr;
|
|
|
|
return;
|
2017-11-29 03:09:53 +01:00
|
|
|
case ENET_EVENT_TYPE_NONE:
|
|
|
|
case ENET_EVENT_TYPE_CONNECT:
|
|
|
|
break;
|
2017-07-09 15:06:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// didn't disconnect gracefully force disconnect
|
|
|
|
enet_peer_reset(server);
|
|
|
|
server = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-19 19:14:33 +02:00
|
|
|
template <>
|
|
|
|
RoomMember::RoomMemberImpl::CallbackSet<WifiPacket>& RoomMember::RoomMemberImpl::Callbacks::Get() {
|
|
|
|
return callback_set_wifi_packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
RoomMember::RoomMemberImpl::CallbackSet<RoomMember::State>&
|
|
|
|
RoomMember::RoomMemberImpl::Callbacks::Get() {
|
|
|
|
return callback_set_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
RoomMember::RoomMemberImpl::CallbackSet<RoomInformation>&
|
|
|
|
RoomMember::RoomMemberImpl::Callbacks::Get() {
|
|
|
|
return callback_set_room_information;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
RoomMember::RoomMemberImpl::CallbackSet<ChatEntry>& RoomMember::RoomMemberImpl::Callbacks::Get() {
|
|
|
|
return callback_set_chat_messages;
|
|
|
|
}
|
|
|
|
|
2018-11-09 14:55:57 +01:00
|
|
|
template <>
|
|
|
|
RoomMember::RoomMemberImpl::CallbackSet<StatusMessageEntry>&
|
|
|
|
RoomMember::RoomMemberImpl::Callbacks::Get() {
|
|
|
|
return callback_set_status_messages;
|
|
|
|
}
|
|
|
|
|
2017-08-19 19:14:33 +02:00
|
|
|
template <typename T>
|
|
|
|
void RoomMember::RoomMemberImpl::Invoke(const T& data) {
|
|
|
|
std::lock_guard<std::mutex> lock(callback_mutex);
|
|
|
|
CallbackSet<T> callback_set = callbacks.Get<T>();
|
|
|
|
for (auto const& callback : callback_set)
|
|
|
|
(*callback)(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
RoomMember::CallbackHandle<T> RoomMember::RoomMemberImpl::Bind(
|
|
|
|
std::function<void(const T&)> callback) {
|
|
|
|
std::lock_guard<std::mutex> lock(callback_mutex);
|
|
|
|
CallbackHandle<T> handle;
|
|
|
|
handle = std::make_shared<std::function<void(const T&)>>(callback);
|
|
|
|
callbacks.Get<T>().insert(handle);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2017-07-08 15:24:47 +02:00
|
|
|
// RoomMember
|
2017-11-19 19:52:37 +01:00
|
|
|
RoomMember::RoomMember() : room_member_impl{std::make_unique<RoomMemberImpl>()} {}
|
2017-07-07 21:34:15 +02:00
|
|
|
|
|
|
|
RoomMember::~RoomMember() {
|
|
|
|
ASSERT_MSG(!IsConnected(), "RoomMember is being destroyed while connected");
|
2017-11-19 19:52:37 +01:00
|
|
|
if (room_member_impl->loop_thread) {
|
|
|
|
Leave();
|
|
|
|
}
|
2017-07-07 21:34:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RoomMember::State RoomMember::GetState() const {
|
|
|
|
return room_member_impl->state;
|
|
|
|
}
|
|
|
|
|
2017-07-08 18:31:35 +02:00
|
|
|
const RoomMember::MemberList& RoomMember::GetMemberInformation() const {
|
|
|
|
return room_member_impl->member_information;
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:06:02 +02:00
|
|
|
const std::string& RoomMember::GetNickname() const {
|
|
|
|
return room_member_impl->nickname;
|
|
|
|
}
|
|
|
|
|
2018-10-27 09:35:01 +02:00
|
|
|
const std::string& RoomMember::GetUsername() const {
|
|
|
|
std::lock_guard<std::mutex> lock(room_member_impl->username_mutex);
|
|
|
|
return room_member_impl->username;
|
|
|
|
}
|
|
|
|
|
2017-07-09 15:06:02 +02:00
|
|
|
const MacAddress& RoomMember::GetMacAddress() const {
|
2017-07-15 21:24:11 +02:00
|
|
|
ASSERT_MSG(IsConnected(), "Tried to get MAC address while not connected");
|
|
|
|
return room_member_impl->mac_address;
|
2017-07-09 15:06:02 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 18:31:35 +02:00
|
|
|
RoomInformation RoomMember::GetRoomInformation() const {
|
|
|
|
return room_member_impl->room_information;
|
|
|
|
}
|
|
|
|
|
2018-10-31 16:07:03 +01:00
|
|
|
void RoomMember::Join(const std::string& nick, const std::string& console_id_hash,
|
|
|
|
const char* server_addr, u16 server_port, u16 client_port,
|
2018-10-27 09:35:01 +02:00
|
|
|
const MacAddress& preferred_mac, const std::string& password,
|
|
|
|
const std::string& token) {
|
2017-07-09 15:06:02 +02:00
|
|
|
// If the member is connected, kill the connection first
|
2017-07-14 09:20:39 +02:00
|
|
|
if (room_member_impl->loop_thread && room_member_impl->loop_thread->joinable()) {
|
2017-11-19 19:52:37 +01:00
|
|
|
Leave();
|
2017-07-09 15:06:02 +02:00
|
|
|
}
|
|
|
|
// If the thread isn't running but the ptr still exists, reset it
|
2017-07-14 09:20:39 +02:00
|
|
|
else if (room_member_impl->loop_thread) {
|
|
|
|
room_member_impl->loop_thread.reset();
|
2017-07-09 15:06:02 +02:00
|
|
|
}
|
|
|
|
|
2017-11-19 19:52:37 +01:00
|
|
|
if (!room_member_impl->client) {
|
|
|
|
room_member_impl->client = enet_host_create(nullptr, 1, NumChannels, 0, 0);
|
|
|
|
ASSERT_MSG(room_member_impl->client != nullptr, "Could not create client");
|
|
|
|
}
|
|
|
|
|
2018-04-19 18:23:39 +02:00
|
|
|
room_member_impl->SetState(State::Joining);
|
|
|
|
|
2017-07-07 21:34:15 +02:00
|
|
|
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) {
|
2017-07-08 15:24:47 +02:00
|
|
|
room_member_impl->SetState(State::Error);
|
2017-07-07 21:34:15 +02:00
|
|
|
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;
|
2017-07-08 15:24:47 +02:00
|
|
|
room_member_impl->StartLoop();
|
2018-10-27 09:35:01 +02:00
|
|
|
room_member_impl->SendJoinRequest(nick, console_id_hash, preferred_mac, password, token);
|
2017-08-19 19:14:33 +02:00
|
|
|
SendGameInfo(room_member_impl->current_game_info);
|
2017-07-07 21:34:15 +02:00
|
|
|
} else {
|
2017-11-19 19:52:37 +01:00
|
|
|
enet_peer_disconnect(room_member_impl->server, 0);
|
2017-07-08 15:24:47 +02:00
|
|
|
room_member_impl->SetState(State::CouldNotConnect);
|
2017-07-07 21:34:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RoomMember::IsConnected() const {
|
2017-07-08 15:24:47 +02:00
|
|
|
return room_member_impl->IsConnected();
|
2017-07-07 21:34:15 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 10:40:11 +02:00
|
|
|
void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) {
|
|
|
|
Packet packet;
|
2017-07-15 21:24:11 +02:00
|
|
|
packet << static_cast<u8>(IdWifiPacket);
|
2017-07-09 15:06:02 +02:00
|
|
|
packet << static_cast<u8>(wifi_packet.type);
|
2017-07-09 10:40:11 +02:00
|
|
|
packet << wifi_packet.channel;
|
|
|
|
packet << wifi_packet.transmitter_address;
|
|
|
|
packet << wifi_packet.destination_address;
|
|
|
|
packet << wifi_packet.data;
|
2017-07-15 21:24:11 +02:00
|
|
|
room_member_impl->Send(std::move(packet));
|
2017-07-09 10:40:11 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 12:26:03 +02:00
|
|
|
void RoomMember::SendChatMessage(const std::string& message) {
|
|
|
|
Packet packet;
|
2017-07-15 21:24:11 +02:00
|
|
|
packet << static_cast<u8>(IdChatMessage);
|
2017-07-09 12:26:03 +02:00
|
|
|
packet << message;
|
2017-07-15 21:24:11 +02:00
|
|
|
room_member_impl->Send(std::move(packet));
|
2017-07-09 12:26:03 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 19:14:33 +02:00
|
|
|
void RoomMember::SendGameInfo(const GameInfo& game_info) {
|
|
|
|
room_member_impl->current_game_info = game_info;
|
|
|
|
if (!IsConnected())
|
|
|
|
return;
|
|
|
|
|
2017-07-09 15:06:02 +02:00
|
|
|
Packet packet;
|
2017-08-19 19:14:33 +02:00
|
|
|
packet << static_cast<u8>(IdSetGameInfo);
|
|
|
|
packet << game_info.name;
|
|
|
|
packet << game_info.id;
|
2017-07-15 21:24:11 +02:00
|
|
|
room_member_impl->Send(std::move(packet));
|
2017-07-09 15:06:02 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 19:14:33 +02:00
|
|
|
RoomMember::CallbackHandle<RoomMember::State> RoomMember::BindOnStateChanged(
|
|
|
|
std::function<void(const RoomMember::State&)> callback) {
|
|
|
|
return room_member_impl->Bind(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
RoomMember::CallbackHandle<WifiPacket> RoomMember::BindOnWifiPacketReceived(
|
|
|
|
std::function<void(const WifiPacket&)> callback) {
|
|
|
|
return room_member_impl->Bind(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
RoomMember::CallbackHandle<RoomInformation> RoomMember::BindOnRoomInformationChanged(
|
|
|
|
std::function<void(const RoomInformation&)> callback) {
|
|
|
|
return room_member_impl->Bind(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
RoomMember::CallbackHandle<ChatEntry> RoomMember::BindOnChatMessageRecieved(
|
|
|
|
std::function<void(const ChatEntry&)> callback) {
|
|
|
|
return room_member_impl->Bind(callback);
|
|
|
|
}
|
|
|
|
|
2018-11-09 14:55:57 +01:00
|
|
|
RoomMember::CallbackHandle<StatusMessageEntry> RoomMember::BindOnStatusMessageReceived(
|
|
|
|
std::function<void(const StatusMessageEntry&)> callback) {
|
|
|
|
return room_member_impl->Bind(callback);
|
|
|
|
}
|
|
|
|
|
2017-08-19 19:14:33 +02:00
|
|
|
template <typename T>
|
|
|
|
void RoomMember::Unbind(CallbackHandle<T> handle) {
|
|
|
|
std::lock_guard<std::mutex> lock(room_member_impl->callback_mutex);
|
|
|
|
room_member_impl->callbacks.Get<T>().erase(handle);
|
|
|
|
}
|
|
|
|
|
2017-07-07 21:34:15 +02:00
|
|
|
void RoomMember::Leave() {
|
2017-07-09 15:06:02 +02:00
|
|
|
room_member_impl->SetState(State::Idle);
|
2017-07-14 09:20:39 +02:00
|
|
|
room_member_impl->loop_thread->join();
|
|
|
|
room_member_impl->loop_thread.reset();
|
2017-11-19 19:52:37 +01:00
|
|
|
|
|
|
|
enet_host_destroy(room_member_impl->client);
|
|
|
|
room_member_impl->client = nullptr;
|
2017-07-07 21:34:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 19:14:33 +02:00
|
|
|
template void RoomMember::Unbind(CallbackHandle<WifiPacket>);
|
|
|
|
template void RoomMember::Unbind(CallbackHandle<RoomMember::State>);
|
|
|
|
template void RoomMember::Unbind(CallbackHandle<RoomInformation>);
|
|
|
|
template void RoomMember::Unbind(CallbackHandle<ChatEntry>);
|
2018-11-09 14:55:57 +01:00
|
|
|
template void RoomMember::Unbind(CallbackHandle<StatusMessageEntry>);
|
2017-08-19 19:14:33 +02:00
|
|
|
|
2017-07-07 21:34:15 +02:00
|
|
|
} // namespace Network
|