Untitled Diff

Created Diff never expires
0 removals
206 lines
1 addition
207 lines
/**
/**
* The Forgotten Server - a free and open-source MMORPG server emulator
* The Forgotten Server - a free and open-source MMORPG server emulator
* Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
* Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
*
*
* This program is free software; you can redistribute it and/or modify
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License along
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
*/


#include "otpch.h"
#include "otpch.h"


#include "outputmessage.h"
#include "outputmessage.h"
#include "server.h"
#include "server.h"
#include "scheduler.h"
#include "scheduler.h"
#include "configmanager.h"
#include "configmanager.h"
#include "ban.h"
#include "ban.h"


extern ConfigManager g_config;
extern ConfigManager g_config;
Ban g_bans;
Ban g_bans;


ServiceManager::~ServiceManager()
ServiceManager::~ServiceManager()
{
{
stop();
stop();
}
}


void ServiceManager::die()
void ServiceManager::die()
{
{
io_service.stop();
io_service.stop();
}
}


void ServiceManager::run()
void ServiceManager::run()
{
{
assert(!running);
assert(!running);
running = true;
running = true;
io_service.run();
io_service.run();
}
}


void ServiceManager::stop()
void ServiceManager::stop()
{
{
if (!running) {
if (!running) {
return;
return;
}
}


running = false;
running = false;


for (auto& servicePortIt : acceptors) {
for (auto& servicePortIt : acceptors) {
try {
try {
io_service.post(std::bind(&ServicePort::onStopServer, servicePortIt.second));
io_service.post(std::bind(&ServicePort::onStopServer, servicePortIt.second));
} catch (boost::system::system_error& e) {
} catch (boost::system::system_error& e) {
std::cout << "[ServiceManager::stop] Network Error: " << e.what() << std::endl;
std::cout << "[ServiceManager::stop] Network Error: " << e.what() << std::endl;
}
}
}
}


acceptors.clear();
acceptors.clear();


death_timer.expires_from_now(boost::posix_time::seconds(3));
death_timer.expires_from_now(boost::posix_time::seconds(3));
death_timer.async_wait(std::bind(&ServiceManager::die, this));
death_timer.async_wait(std::bind(&ServiceManager::die, this));
}
}


ServicePort::~ServicePort()
ServicePort::~ServicePort()
{
{
close();
close();
}
}


bool ServicePort::is_single_socket() const
bool ServicePort::is_single_socket() const
{
{
return !services.empty() && services.front()->is_single_socket();
return !services.empty() && services.front()->is_single_socket();
}
}


std::string ServicePort::get_protocol_names() const
std::string ServicePort::get_protocol_names() const
{
{
if (services.empty()) {
if (services.empty()) {
return std::string();
return std::string();
}
}


std::string str = services.front()->get_protocol_name();
std::string str = services.front()->get_protocol_name();
for (size_t i = 1; i < services.size(); ++i) {
for (size_t i = 1; i < services.size(); ++i) {
str.push_back(',');
str.push_back(',');
str.push_back(' ');
str.push_back(' ');
str.append(services[i]->get_protocol_name());
str.append(services[i]->get_protocol_name());
}
}
return str;
return str;
}
}


void ServicePort::accept()
void ServicePort::accept()
{
{
if (!acceptor) {
if (!acceptor) {
return;
return;
}
}


auto connection = ConnectionManager::getInstance().createConnection(io_service, shared_from_this());
auto connection = ConnectionManager::getInstance().createConnection(io_service, shared_from_this());
acceptor->async_accept(connection->getSocket(), std::bind(&ServicePort::onAccept, shared_from_this(), connection, std::placeholders::_1));
acceptor->async_accept(connection->getSocket(), std::bind(&ServicePort::onAccept, shared_from_this(), connection, std::placeholders::_1));
}
}


void ServicePort::onAccept(Connection_ptr connection, const boost::system::error_code& error)
void ServicePort::onAccept(Connection_ptr connection, const boost::system::error_code& error)
{
{
if (!error) {
if (!error) {
if (services.empty()) {
if (services.empty()) {
return;
return;
}
}


auto remote_ip = connection->getIP();
auto remote_ip = connection->getIP();
if (remote_ip != 0 && g_bans.acceptConnection(remote_ip)) {
if (remote_ip != 0 && g_bans.acceptConnection(remote_ip)) {
Service_ptr service = services.front();
Service_ptr service = services.front();
if (service->is_single_socket()) {
if (service->is_single_socket()) {
connection->accept(service->make_protocol(connection));
connection->accept(service->make_protocol(connection));
} else {
} else {
connection->accept();
connection->accept();
}
}
} else {
} else {
connection->close(Connection::FORCE_CLOSE);
connection->close(Connection::FORCE_CLOSE);
}
}


accept();
accept();
} else if (error != boost::asio::error::operation_aborted) {
} else if (error != boost::asio::error::operation_aborted) {
if (!pendingStart) {
if (!pendingStart) {
close();
close();
pendingStart = true;
pendingStart = true;
g_scheduler.addEvent(createSchedulerTask(15000,
g_scheduler.addEvent(createSchedulerTask(15000,
std::bind(&ServicePort::openAcceptor, std::weak_ptr<ServicePort>(shared_from_this()), serverPort)));
std::bind(&ServicePort::openAcceptor, std::weak_ptr<ServicePort>(shared_from_this()), serverPort)));
}
}
}
}
}
}


Protocol_ptr ServicePort::make_protocol(bool checksummed, NetworkMessage& msg, const Connection_ptr& connection) const
Protocol_ptr ServicePort::make_protocol(bool checksummed, NetworkMessage& msg, const Connection_ptr& connection) const
{
{
uint8_t protocolID = msg.getByte();
uint8_t protocolID = msg.getByte();
for (auto& service : services) {
for (auto& service : services) {
if (protocolID != service->get_protocol_identifier()) {
if (protocolID != service->get_protocol_identifier()) {
continue;
continue;
}
}


if ((checksummed && service->is_checksummed()) || !service->is_checksummed()) {
if ((checksummed && service->is_checksummed()) || !service->is_checksummed()) {
return service->make_protocol(connection);
return service->make_protocol(connection);
}
}
}
}
return nullptr;
return nullptr;
}
}


void ServicePort::onStopServer()
void ServicePort::onStopServer()
{
{
close();
close();
}
}


void ServicePort::openAcceptor(std::weak_ptr<ServicePort> weak_service, uint16_t port)
void ServicePort::openAcceptor(std::weak_ptr<ServicePort> weak_service, uint16_t port)
{
{
if (auto service = weak_service.lock()) {
if (auto service = weak_service.lock()) {
service->open(port);
service->open(port);
}
}
}
}


void ServicePort::open(uint16_t port)
void ServicePort::open(uint16_t port)
{
{
close();
close();


serverPort = port;
serverPort = port;
pendingStart = false;
pendingStart = false;


try {
try {
if (g_config.getBoolean(ConfigManager::BIND_ONLY_GLOBAL_ADDRESS)) {
if (g_config.getBoolean(ConfigManager::BIND_ONLY_GLOBAL_ADDRESS)) {
acceptor.reset(new boost::asio::ip::tcp::acceptor(io_service, boost::asio::ip::tcp::endpoint(
acceptor.reset(new boost::asio::ip::tcp::acceptor(io_service, boost::asio::ip::tcp::endpoint(
boost::asio::ip::address(boost::asio::ip::address_v4::from_string(g_config.getString(ConfigManager::IP))), serverPort)));
boost::asio::ip::address(boost::asio::ip::address_v4::from_string(g_config.getString(ConfigManager::IP))), serverPort)));
} else {
} else {
acceptor.reset(new boost::asio::ip::tcp::acceptor(io_service, boost::asio::ip::tcp::endpoint(
acceptor.reset(new boost::asio::ip::tcp::acceptor(io_service, boost::asio::ip::tcp::endpoint(
boost::asio::ip::address(boost::asio::ip::address_v4(INADDR_ANY)), serverPort)));
boost::asio::ip::address(boost::asio::ip::address_v4(INADDR_ANY)), serverPort)));
}
}


acceptor->set_option(boost::asio::ip::tcp::no_delay(true));
acceptor->set_option(boost::asio::ip::tcp::no_delay(true));


accept();
accept();
} catch (boost::system::system_error& e) {
} catch (boost::system::system_error& e) {
std::cout << "[ServicePort::open] Error: " << e.what() << std::endl;
std::cout << "[ServicePort::open] Error: " << e.what() << std::endl;


pendingStart = true;
pendingStart = true;
g_scheduler.addEvent(createSchedulerTask(15000,
g_scheduler.addEvent(createSchedulerTask(15000,
std::bind(&ServicePort::openAcceptor, std::weak_ptr<ServicePort>(shared_from_this()), port)));
std::bind(&ServicePort::openAcceptor, std::weak_ptr<ServicePort>(shared_from_this()), port)));
}
}
}
}


void ServicePort::close()
void ServicePort::close()
{
{
if (acceptor && acceptor->is_open()) {
if (acceptor && acceptor->is_open()) {
boost::system::error_code error;
boost::system::error_code error;
acceptor->close(error);
acceptor->close(error);
}
}
}
}


bool ServicePort::add_service(const Service_ptr& new_svc)
bool ServicePort::add_service(const Service_ptr& new_svc)
{
{
if (std::any_of(services.begin(), services.end(), [](const Service_ptr& svc) {return svc->is_single_socket();})) {
if (std::any_of(services.begin(), services.end(), [](const Service_ptr& svc) {return svc->is_single_socket();})) {
return false;
return false;
}
}


services.push_back(new_svc);
services.push_back(new_svc);
return true;
return true;
}
}