diff options
author | B3n30 <bene_thomas@web.de> | 2017-07-07 21:34:15 +0200 |
---|---|---|
committer | bunnei <bunneidev@gmail.com> | 2017-07-07 21:34:15 +0200 |
commit | 2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8 (patch) | |
tree | b5298b3c528d9acc1f3fa48f19c9f9c19c97d036 /src/network/room_member.cpp | |
parent | Merge pull request #2814 from Kloen/macro-remove (diff) | |
download | yuzu-2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8.tar yuzu-2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8.tar.gz yuzu-2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8.tar.bz2 yuzu-2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8.tar.lz yuzu-2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8.tar.xz yuzu-2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8.tar.zst yuzu-2e37ce01c9dcdc5932cf5fe47a1ade0e0d2b7cf8.zip |
Diffstat (limited to '')
-rw-r--r-- | src/network/room_member.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/network/room_member.cpp b/src/network/room_member.cpp new file mode 100644 index 000000000..c87f009f4 --- /dev/null +++ b/src/network/room_member.cpp @@ -0,0 +1,74 @@ +// Copyright 2017 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/assert.h" +#include "enet/enet.h" +#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 + + std::atomic<State> state{State::Idle}; ///< Current state of the RoomMember. + + std::string nickname; ///< The nickname of this member. +}; + +RoomMember::RoomMember() : room_member_impl{std::make_unique<RoomMemberImpl>()} { + room_member_impl->client = enet_host_create(nullptr, 1, NumChannels, 0, 0); + ASSERT_MSG(room_member_impl->client != nullptr, "Could not create client"); +} + +RoomMember::~RoomMember() { + ASSERT_MSG(!IsConnected(), "RoomMember is being destroyed while connected"); + enet_host_destroy(room_member_impl->client); +} + +RoomMember::State RoomMember::GetState() const { + return room_member_impl->state; +} + +void RoomMember::Join(const std::string& nick, const char* server_addr, u16 server_port, + u16 client_port) { + 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) { + room_member_impl->state = State::Error; + 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; + room_member_impl->state = State::Joining; + // TODO(B3N30): Send a join request with the nickname to the server + // TODO(B3N30): Start the receive thread + } else { + room_member_impl->state = State::CouldNotConnect; + } +} + +bool RoomMember::IsConnected() const { + return room_member_impl->state == State::Joining || room_member_impl->state == State::Joined; +} + +void RoomMember::Leave() { + enet_peer_disconnect(room_member_impl->server, 0); + room_member_impl->state = State::Idle; + // TODO(B3N30): Close the receive thread + enet_peer_reset(room_member_impl->server); +} + +} // namespace Network |