2022-07-25 08:18:30 -07:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2022-07-05 17:20:39 -07:00
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "enet/enet.h"
|
|
|
|
#include "network/network.h"
|
|
|
|
|
|
|
|
namespace Network {
|
|
|
|
|
2022-07-22 07:31:13 -07:00
|
|
|
RoomNetwork::RoomNetwork() {
|
2022-07-25 08:08:20 -07:00
|
|
|
m_room = std::make_shared<Room>();
|
|
|
|
m_room_member = std::make_shared<RoomMember>();
|
2022-07-22 07:31:13 -07:00
|
|
|
}
|
2022-07-05 17:20:39 -07:00
|
|
|
|
2022-07-22 07:31:13 -07:00
|
|
|
bool RoomNetwork::Init() {
|
2022-07-05 17:20:39 -07:00
|
|
|
if (enet_initialize() != 0) {
|
2022-09-23 04:38:23 -07:00
|
|
|
LOG_ERROR(Network, "Error initializing ENet");
|
2022-07-05 17:20:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-25 08:08:20 -07:00
|
|
|
m_room = std::make_shared<Room>();
|
|
|
|
m_room_member = std::make_shared<RoomMember>();
|
2022-07-05 17:20:39 -07:00
|
|
|
LOG_DEBUG(Network, "initialized OK");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-22 07:31:13 -07:00
|
|
|
std::weak_ptr<Room> RoomNetwork::GetRoom() {
|
2022-07-25 08:08:20 -07:00
|
|
|
return m_room;
|
2022-07-05 17:20:39 -07:00
|
|
|
}
|
|
|
|
|
2022-07-22 07:31:13 -07:00
|
|
|
std::weak_ptr<RoomMember> RoomNetwork::GetRoomMember() {
|
2022-07-25 08:08:20 -07:00
|
|
|
return m_room_member;
|
2022-07-05 17:20:39 -07:00
|
|
|
}
|
|
|
|
|
2022-07-22 07:31:13 -07:00
|
|
|
void RoomNetwork::Shutdown() {
|
2022-07-25 08:08:20 -07:00
|
|
|
if (m_room_member) {
|
|
|
|
if (m_room_member->IsConnected())
|
|
|
|
m_room_member->Leave();
|
|
|
|
m_room_member.reset();
|
2022-07-05 17:20:39 -07:00
|
|
|
}
|
2022-07-25 08:08:20 -07:00
|
|
|
if (m_room) {
|
|
|
|
if (m_room->GetState() == Room::State::Open)
|
|
|
|
m_room->Destroy();
|
|
|
|
m_room.reset();
|
2022-07-05 17:20:39 -07:00
|
|
|
}
|
|
|
|
enet_deinitialize();
|
|
|
|
LOG_DEBUG(Network, "shutdown OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Network
|