From 660ceef213fd31c7fe4f877a3fca9f2516ead84c Mon Sep 17 00:00:00 2001 From: niansa Date: Tue, 10 May 2022 15:19:33 +0200 Subject: [PATCH] Moved players and playfields lists into "Global" struct --- generic.proto | 2 ++ server/Connection.cpp | 6 +++--- server/Connection.hpp | 19 ++++++++++++++++--- server/World.cpp | 2 +- server/World.hpp | 6 +++++- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/generic.proto b/generic.proto index 10785d1..6c87710 100644 --- a/generic.proto +++ b/generic.proto @@ -15,6 +15,7 @@ message Error { AuthenticationRequired = 6; IllegalOperation = 7; OutOfRange = 8; + NotYourTurn = 9; } Code code = 2; } @@ -33,6 +34,7 @@ message Operation { SettingsSync = 5; PlayfieldSync = 6; RobotUpdate = 7; + YourTurn = 8; } Code code = 1; } diff --git a/server/Connection.cpp b/server/Connection.cpp index 5ca23e4..97b00f0 100644 --- a/server/Connection.cpp +++ b/server/Connection.cpp @@ -58,7 +58,7 @@ asio::awaitable Client::handlePacket(const Packet& packet) { case Generic::Operation::SimpleAuth: { const Generic::SimpleAuth& authData = *static_cast(packet.data.get()); // Check that username is not already taken - if (std::find_if(clients.begin(), clients.end(), [&, this] (const auto& o) {return o->getUsername() == authData.username();}) != clients.end()) { + if (std::find_if(global.clients.begin(), global.clients.end(), [&, this] (const auto& o) {return o->getUsername() == authData.username();}) != global.clients.end()) { throw Error("Username has already been taken", Generic::Error::UsernameAlreadyTaken); } // Check that username is "good" (no unprintable characters and stuff like that) @@ -115,10 +115,10 @@ asio::awaitable Server::listen(int port) { asio::ip::tcp::acceptor acceptor(ioc, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port)); // Handle all incoming connections while (true) { - auto client = std::make_shared(ioc, clients); + auto client = std::make_shared(ioc, global); co_await acceptor.async_accept(client->getSocket(), asio::use_awaitable); asio::co_spawn(ioc, client->connect(), asio::detached); - clients.push_back(std::move(client)); + global.clients.push_back(std::move(client)); } } catch (std::exception& e) { std::cerr << "Failed to stay alive: " << e.what() << std::endl; diff --git a/server/Connection.hpp b/server/Connection.hpp index 2ebe024..133d29d 100644 --- a/server/Connection.hpp +++ b/server/Connection.hpp @@ -15,10 +15,17 @@ +namespace World { +class Playfield; +} + namespace Connection { using namespace boost; +class Global; + + struct Packet { Generic::Operation op; std::unique_ptr data = nullptr; @@ -42,13 +49,13 @@ struct FatalError : public Error { class Client : public std::enable_shared_from_this { - std::vector>& clients; + Global& global; asio::ip::tcp::socket socket; std::string username; bool good = true; public: - Client(asio::io_context& ioc, std::vector>& clients) : socket(ioc), clients(clients) {} + Client(asio::io_context& ioc, Global& global) : socket(ioc), global(global) {} asio::awaitable receivePacket(); asio::awaitable sendProtobuf(const google::protobuf::Message& buffer); @@ -98,9 +105,15 @@ public: }; +struct Global { + std::vector> clients; + std::vector> playfields; +}; + + class Server { asio::io_context& ioc; - std::vector> clients; + Global global; public: Server(asio::io_context& ioc) : ioc(ioc) {} diff --git a/server/World.cpp b/server/World.cpp index b54f907..6a5ccf0 100644 --- a/server/World.cpp +++ b/server/World.cpp @@ -1,8 +1,8 @@ #include "World.hpp" +#include "Connection.hpp" #include #include -#include diff --git a/server/World.hpp b/server/World.hpp index d10a773..2787e05 100644 --- a/server/World.hpp +++ b/server/World.hpp @@ -1,14 +1,18 @@ #ifndef _WORLD_HPP #define _WORLD_HPP #include "generic.pb.h" -#include "Connection.hpp" #include #include +#include #include +namespace Connection { +class Client; +} + namespace World { void fillSettings(Generic::Settings& settings);