issiauskinau kad checksum yra nahuj reikalingas ir kad onConnect turejo buti uzkomentuotas. Reikia issiaiskinti kodel luzta onLogin clientas. Kitus failus reikia perziureti ir isvalyti siuksles

This commit is contained in:
ErikasKontenis
2019-11-10 19:45:03 +02:00
parent f0e103a475
commit 3d05566759
17 changed files with 233 additions and 232 deletions

View File

@@ -27,98 +27,94 @@ class Protocol;
class ServiceBase
{
public:
virtual bool is_single_socket() const = 0;
virtual bool is_checksummed() const = 0;
virtual uint8_t get_protocol_identifier() const = 0;
virtual const char* get_protocol_name() const = 0;
public:
virtual bool is_single_socket() const = 0;
virtual uint8_t get_protocol_identifier() const = 0;
virtual const char* get_protocol_name() const = 0;
virtual Protocol_ptr make_protocol(const Connection_ptr& c) const = 0;
virtual Protocol_ptr make_protocol(const Connection_ptr& c) const = 0;
};
template <typename ProtocolType>
class Service final : public ServiceBase
{
public:
bool is_single_socket() const override {
return ProtocolType::server_sends_first;
}
bool is_checksummed() const override {
return ProtocolType::use_checksum;
}
uint8_t get_protocol_identifier() const override {
return ProtocolType::protocol_identifier;
}
const char* get_protocol_name() const override {
return ProtocolType::protocol_name();
}
public:
bool is_single_socket() const final {
return ProtocolType::server_sends_first;
}
uint8_t get_protocol_identifier() const final {
return ProtocolType::protocol_identifier;
}
const char* get_protocol_name() const final {
return ProtocolType::protocol_name();
}
Protocol_ptr make_protocol(const Connection_ptr& c) const override {
return std::make_shared<ProtocolType>(c);
}
Protocol_ptr make_protocol(const Connection_ptr& c) const final {
return std::make_shared<ProtocolType>(c);
}
};
class ServicePort : public std::enable_shared_from_this<ServicePort>
{
public:
explicit ServicePort(boost::asio::io_service& io_service) : io_service(io_service) {}
~ServicePort();
public:
explicit ServicePort(boost::asio::io_service& io_service) : io_service(io_service) {}
~ServicePort();
// non-copyable
ServicePort(const ServicePort&) = delete;
ServicePort& operator=(const ServicePort&) = delete;
// non-copyable
ServicePort(const ServicePort&) = delete;
ServicePort& operator=(const ServicePort&) = delete;
static void openAcceptor(std::weak_ptr<ServicePort> weak_service, uint16_t port);
void open(uint16_t port);
void close();
bool is_single_socket() const;
std::string get_protocol_names() const;
static void openAcceptor(std::weak_ptr<ServicePort> weak_service, uint16_t port);
void open(uint16_t port);
void close();
bool is_single_socket() const;
std::string get_protocol_names() const;
bool add_service(const Service_ptr& new_svc);
Protocol_ptr make_protocol(bool checksummed, NetworkMessage& msg, const Connection_ptr& connection) const;
bool add_service(const Service_ptr& new_svc);
Protocol_ptr make_protocol(NetworkMessage& msg, const Connection_ptr& connection) const;
void onStopServer();
void onAccept(Connection_ptr connection, const boost::system::error_code& error);
void onStopServer();
void onAccept(Connection_ptr connection, const boost::system::error_code& error);
protected:
void accept();
protected:
void accept();
boost::asio::io_service& io_service;
std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor;
std::vector<Service_ptr> services;
boost::asio::io_service& io_service;
std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor;
std::vector<Service_ptr> services;
uint16_t serverPort = 0;
bool pendingStart = false;
uint16_t serverPort = 0;
bool pendingStart = false;
};
class ServiceManager
{
public:
ServiceManager() = default;
~ServiceManager();
public:
ServiceManager() = default;
~ServiceManager();
// non-copyable
ServiceManager(const ServiceManager&) = delete;
ServiceManager& operator=(const ServiceManager&) = delete;
// non-copyable
ServiceManager(const ServiceManager&) = delete;
ServiceManager& operator=(const ServiceManager&) = delete;
void run();
void stop();
void run();
void stop();
template <typename ProtocolType>
bool add(uint16_t port);
template <typename ProtocolType>
bool add(uint16_t port);
bool is_running() const {
return acceptors.empty() == false;
}
bool is_running() const {
return acceptors.empty() == false;
}
protected:
void die();
protected:
void die();
std::unordered_map<uint16_t, ServicePort_ptr> acceptors;
std::unordered_map<uint16_t, ServicePort_ptr> acceptors;
boost::asio::io_service io_service;
boost::asio::deadline_timer death_timer { io_service };
bool running = false;
boost::asio::io_service io_service;
boost::asio::deadline_timer death_timer{ io_service };
bool running = false;
};
template <typename ProtocolType>
@@ -137,13 +133,14 @@ bool ServiceManager::add(uint16_t port)
service_port = std::make_shared<ServicePort>(io_service);
service_port->open(port);
acceptors[port] = service_port;
} else {
}
else {
service_port = foundServicePort->second;
if (service_port->is_single_socket() || ProtocolType::server_sends_first) {
std::cout << "ERROR: " << ProtocolType::protocol_name() <<
" and " << service_port->get_protocol_names() <<
" cannot use the same port " << port << '.' << std::endl;
" and " << service_port->get_protocol_names() <<
" cannot use the same port " << port << '.' << std::endl;
return false;
}
}