2017-07-07 20:34:15 +01:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "enet/enet.h"
|
|
|
|
#include "network/network.h"
|
|
|
|
|
|
|
|
namespace Network {
|
|
|
|
|
|
|
|
static std::shared_ptr<RoomMember> g_room_member; ///< RoomMember (Client) for network games
|
|
|
|
static std::shared_ptr<Room> g_room; ///< Room (Server) for network games
|
|
|
|
// TODO(B3N30): Put these globals into a networking class
|
|
|
|
|
|
|
|
bool Init() {
|
|
|
|
if (enet_initialize() != 0) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_ERROR(Network, "Error initalizing ENet");
|
2017-07-07 20:34:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
g_room = std::make_shared<Room>();
|
|
|
|
g_room_member = std::make_shared<RoomMember>();
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_DEBUG(Network, "initialized OK");
|
2017-07-07 20:34:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::weak_ptr<Room> GetRoom() {
|
|
|
|
return g_room;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::weak_ptr<RoomMember> GetRoomMember() {
|
|
|
|
return g_room_member;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown() {
|
|
|
|
if (g_room_member) {
|
|
|
|
if (g_room_member->IsConnected())
|
|
|
|
g_room_member->Leave();
|
|
|
|
g_room_member.reset();
|
|
|
|
}
|
|
|
|
if (g_room) {
|
|
|
|
if (g_room->GetState() == Room::State::Open)
|
|
|
|
g_room->Destroy();
|
|
|
|
g_room.reset();
|
|
|
|
}
|
|
|
|
enet_deinitialize();
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_DEBUG(Network, "shutdown OK");
|
2017-07-07 20:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Network
|