Moved players and playfields lists into "Global" struct
This commit is contained in:
parent
2f2dabe817
commit
660ceef213
5 changed files with 27 additions and 8 deletions
|
@ -15,6 +15,7 @@ message Error {
|
||||||
AuthenticationRequired = 6;
|
AuthenticationRequired = 6;
|
||||||
IllegalOperation = 7;
|
IllegalOperation = 7;
|
||||||
OutOfRange = 8;
|
OutOfRange = 8;
|
||||||
|
NotYourTurn = 9;
|
||||||
}
|
}
|
||||||
Code code = 2;
|
Code code = 2;
|
||||||
}
|
}
|
||||||
|
@ -33,6 +34,7 @@ message Operation {
|
||||||
SettingsSync = 5;
|
SettingsSync = 5;
|
||||||
PlayfieldSync = 6;
|
PlayfieldSync = 6;
|
||||||
RobotUpdate = 7;
|
RobotUpdate = 7;
|
||||||
|
YourTurn = 8;
|
||||||
}
|
}
|
||||||
Code code = 1;
|
Code code = 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ asio::awaitable<void> Client::handlePacket(const Packet& packet) {
|
||||||
case Generic::Operation::SimpleAuth: {
|
case Generic::Operation::SimpleAuth: {
|
||||||
const Generic::SimpleAuth& authData = *static_cast<Generic::SimpleAuth*>(packet.data.get());
|
const Generic::SimpleAuth& authData = *static_cast<Generic::SimpleAuth*>(packet.data.get());
|
||||||
// Check that username is not already taken
|
// 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);
|
throw Error("Username has already been taken", Generic::Error::UsernameAlreadyTaken);
|
||||||
}
|
}
|
||||||
// Check that username is "good" (no unprintable characters and stuff like that)
|
// Check that username is "good" (no unprintable characters and stuff like that)
|
||||||
|
@ -115,10 +115,10 @@ asio::awaitable<void> Server::listen(int port) {
|
||||||
asio::ip::tcp::acceptor acceptor(ioc, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port));
|
asio::ip::tcp::acceptor acceptor(ioc, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port));
|
||||||
// Handle all incoming connections
|
// Handle all incoming connections
|
||||||
while (true) {
|
while (true) {
|
||||||
auto client = std::make_shared<Client>(ioc, clients);
|
auto client = std::make_shared<Client>(ioc, global);
|
||||||
co_await acceptor.async_accept(client->getSocket(), asio::use_awaitable);
|
co_await acceptor.async_accept(client->getSocket(), asio::use_awaitable);
|
||||||
asio::co_spawn(ioc, client->connect(), asio::detached);
|
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) {
|
} catch (std::exception& e) {
|
||||||
std::cerr << "Failed to stay alive: " << e.what() << std::endl;
|
std::cerr << "Failed to stay alive: " << e.what() << std::endl;
|
||||||
|
|
|
@ -15,10 +15,17 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace World {
|
||||||
|
class Playfield;
|
||||||
|
}
|
||||||
|
|
||||||
namespace Connection {
|
namespace Connection {
|
||||||
using namespace boost;
|
using namespace boost;
|
||||||
|
|
||||||
|
|
||||||
|
class Global;
|
||||||
|
|
||||||
|
|
||||||
struct Packet {
|
struct Packet {
|
||||||
Generic::Operation op;
|
Generic::Operation op;
|
||||||
std::unique_ptr<google::protobuf::Message> data = nullptr;
|
std::unique_ptr<google::protobuf::Message> data = nullptr;
|
||||||
|
@ -42,13 +49,13 @@ struct FatalError : public Error {
|
||||||
|
|
||||||
|
|
||||||
class Client : public std::enable_shared_from_this<Client> {
|
class Client : public std::enable_shared_from_this<Client> {
|
||||||
std::vector<std::shared_ptr<Client>>& clients;
|
Global& global;
|
||||||
asio::ip::tcp::socket socket;
|
asio::ip::tcp::socket socket;
|
||||||
std::string username;
|
std::string username;
|
||||||
bool good = true;
|
bool good = true;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Client(asio::io_context& ioc, std::vector<std::shared_ptr<Client>>& clients) : socket(ioc), clients(clients) {}
|
Client(asio::io_context& ioc, Global& global) : socket(ioc), global(global) {}
|
||||||
|
|
||||||
asio::awaitable<Packet> receivePacket();
|
asio::awaitable<Packet> receivePacket();
|
||||||
asio::awaitable<void> sendProtobuf(const google::protobuf::Message& buffer);
|
asio::awaitable<void> sendProtobuf(const google::protobuf::Message& buffer);
|
||||||
|
@ -98,9 +105,15 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct Global {
|
||||||
|
std::vector<std::shared_ptr<Client>> clients;
|
||||||
|
std::vector<std::shared_ptr<World::Playfield>> playfields;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
asio::io_context& ioc;
|
asio::io_context& ioc;
|
||||||
std::vector<std::shared_ptr<Client>> clients;
|
Global global;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Server(asio::io_context& ioc) : ioc(ioc) {}
|
Server(asio::io_context& ioc) : ioc(ioc) {}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#include "World.hpp"
|
#include "World.hpp"
|
||||||
|
#include "Connection.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
#ifndef _WORLD_HPP
|
#ifndef _WORLD_HPP
|
||||||
#define _WORLD_HPP
|
#define _WORLD_HPP
|
||||||
#include "generic.pb.h"
|
#include "generic.pb.h"
|
||||||
#include "Connection.hpp"
|
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <cmath>
|
||||||
#include <boost/asio/awaitable.hpp>
|
#include <boost/asio/awaitable.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace Connection {
|
||||||
|
class Client;
|
||||||
|
}
|
||||||
|
|
||||||
namespace World {
|
namespace World {
|
||||||
void fillSettings(Generic::Settings& settings);
|
void fillSettings(Generic::Settings& settings);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue