mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-10-26 14:00:22 +00:00
move TIoPollThread out and use it in TServer
the previous IoCtx was never being polled
This commit is contained in:
6 files changed
+85
-48
No files matched your search
@@ -39,6 +39,7 @@ set(PRJ_HEADERS
|
|||||||
include/TConfig.h
|
include/TConfig.h
|
||||||
include/TConsole.h
|
include/TConsole.h
|
||||||
include/THeartbeatThread.h
|
include/THeartbeatThread.h
|
||||||
|
include/TIoPollThread.h
|
||||||
include/TLuaEngine.h
|
include/TLuaEngine.h
|
||||||
include/TLuaPlugin.h
|
include/TLuaPlugin.h
|
||||||
include/TNetwork.h
|
include/TNetwork.h
|
||||||
@@ -66,6 +67,7 @@ set(PRJ_SOURCES
|
|||||||
src/TConfig.cpp
|
src/TConfig.cpp
|
||||||
src/TConsole.cpp
|
src/TConsole.cpp
|
||||||
src/THeartbeatThread.cpp
|
src/THeartbeatThread.cpp
|
||||||
|
src/TIoPollThread.cpp
|
||||||
src/TLuaEngine.cpp
|
src/TLuaEngine.cpp
|
||||||
src/TLuaPlugin.cpp
|
src/TLuaPlugin.cpp
|
||||||
src/TNetwork.cpp
|
src/TNetwork.cpp
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
// BeamMP, the BeamNG.drive multiplayer mod.
|
||||||
|
// Copyright (C) 2024 BeamMP Ltd., BeamMP team and contributors.
|
||||||
|
//
|
||||||
|
// BeamMP Ltd. can be contacted by electronic mail via contact@beammp.com.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published
|
||||||
|
// by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <boost/asio/io_context.hpp>
|
||||||
|
#include <boost/asio/executor_work_guard.hpp>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
class TIoPollThread {
|
||||||
|
public:
|
||||||
|
TIoPollThread();
|
||||||
|
~TIoPollThread();
|
||||||
|
|
||||||
|
boost::asio::io_context& IoCtx() noexcept { return mIoCtx; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
boost::asio::io_context mIoCtx;
|
||||||
|
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> mWorkGuard;
|
||||||
|
std::jthread mThread;
|
||||||
|
};
|
||||||
@@ -23,24 +23,10 @@
|
|||||||
#include "TConnectionLimiter.h"
|
#include "TConnectionLimiter.h"
|
||||||
#include "TResourceManager.h"
|
#include "TResourceManager.h"
|
||||||
#include "TServer.h"
|
#include "TServer.h"
|
||||||
#include <boost/asio/io_context.hpp>
|
|
||||||
#include <boost/asio/ip/udp.hpp>
|
#include <boost/asio/ip/udp.hpp>
|
||||||
|
|
||||||
struct TConnection;
|
struct TConnection;
|
||||||
|
|
||||||
class TIoPollThread {
|
|
||||||
public:
|
|
||||||
TIoPollThread();
|
|
||||||
~TIoPollThread();
|
|
||||||
|
|
||||||
boost::asio::io_context& IoCtx() noexcept { return mIoCtx; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
boost::asio::io_context mIoCtx;
|
|
||||||
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> mWorkGuard;
|
|
||||||
std::jthread mThread;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TNetwork {
|
class TNetwork {
|
||||||
public:
|
public:
|
||||||
TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager);
|
TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager);
|
||||||
@@ -76,7 +62,6 @@ private:
|
|||||||
std::thread mUDPThread;
|
std::thread mUDPThread;
|
||||||
std::thread mTCPThread;
|
std::thread mTCPThread;
|
||||||
std::mutex mOpenIDMutex;
|
std::mutex mOpenIDMutex;
|
||||||
TIoPollThread mIoCtxPoller;
|
|
||||||
TConnectionLimiter mConnectionLimiter;
|
TConnectionLimiter mConnectionLimiter;
|
||||||
|
|
||||||
std::vector<uint8_t> UDPRcvFromClient(boost::asio::ip::udp::endpoint& ClientEndpoint);
|
std::vector<uint8_t> UDPRcvFromClient(boost::asio::ip::udp::endpoint& ClientEndpoint);
|
||||||
|
|||||||
+3
-3
@@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
#include "IThreaded.h"
|
#include "IThreaded.h"
|
||||||
#include "RWMutex.h"
|
#include "RWMutex.h"
|
||||||
|
#include "TIoPollThread.h"
|
||||||
#include "TScopedTimer.h"
|
#include "TScopedTimer.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -50,11 +51,10 @@ public:
|
|||||||
|
|
||||||
const TScopedTimer UptimeTimer;
|
const TScopedTimer UptimeTimer;
|
||||||
|
|
||||||
// asio io context
|
io_context& IoCtx() { return mIoCtxPoller.IoCtx(); }
|
||||||
io_context& IoCtx() { return mIoCtx; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
io_context mIoCtx {};
|
TIoPollThread mIoCtxPoller;
|
||||||
TClientSet mClients;
|
TClientSet mClients;
|
||||||
mutable RWMutex mClientsMutex;
|
mutable RWMutex mClientsMutex;
|
||||||
static void ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Network);
|
static void ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Network);
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
// BeamMP, the BeamNG.drive multiplayer mod.
|
||||||
|
// Copyright (C) 2024 BeamMP Ltd., BeamMP team and contributors.
|
||||||
|
//
|
||||||
|
// BeamMP Ltd. can be contacted by electronic mail via contact@beammp.com.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published
|
||||||
|
// by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include "TIoPollThread.h"
|
||||||
|
|
||||||
|
TIoPollThread::TIoPollThread()
|
||||||
|
: mWorkGuard(boost::asio::make_work_guard(mIoCtx))
|
||||||
|
, mThread([this](std::stop_token StopToken) {
|
||||||
|
while (!StopToken.stop_requested()) {
|
||||||
|
try {
|
||||||
|
mIoCtx.run();
|
||||||
|
break;
|
||||||
|
} catch (...) {
|
||||||
|
mIoCtx.restart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}) { }
|
||||||
|
|
||||||
|
TIoPollThread::~TIoPollThread() {
|
||||||
|
mWorkGuard.reset();
|
||||||
|
mIoCtx.stop();
|
||||||
|
}
|
||||||
+7
-30
@@ -94,7 +94,6 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R
|
|||||||
, mPPSMonitor(PPSMonitor)
|
, mPPSMonitor(PPSMonitor)
|
||||||
, mUDPSock(Server.IoCtx())
|
, mUDPSock(Server.IoCtx())
|
||||||
, mResourceManager(ResourceManager)
|
, mResourceManager(ResourceManager)
|
||||||
, mIoCtxPoller()
|
|
||||||
, mConnectionLimiter(MAX_CONCURRENT_CONNECTIONS, MAX_GLOBAL_CONNECTIONS) {
|
, mConnectionLimiter(MAX_CONCURRENT_CONNECTIONS, MAX_GLOBAL_CONNECTIONS) {
|
||||||
Application::SetSubsystemStatus("TCPNetwork", Application::Status::Starting);
|
Application::SetSubsystemStatus("TCPNetwork", Application::Status::Starting);
|
||||||
Application::SetSubsystemStatus("UDPNetwork", Application::Status::Starting);
|
Application::SetSubsystemStatus("UDPNetwork", Application::Status::Starting);
|
||||||
@@ -362,8 +361,6 @@ void TNetwork::Identify(TConnection&& RawConnection, TConnectionLimiter::TGuard&
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::string HashPassword(const std::string& str) {
|
std::string HashPassword(const std::string& str) {
|
||||||
std::stringstream ret;
|
std::stringstream ret;
|
||||||
unsigned char* hash = SHA256(reinterpret_cast<const unsigned char*>(str.c_str()), str.length(), nullptr);
|
unsigned char* hash = SHA256(reinterpret_cast<const unsigned char*>(str.c_str()), str.length(), nullptr);
|
||||||
@@ -663,19 +660,17 @@ void TNetwork::ClientKick(TClient& c, const std::string& R) {
|
|||||||
DisconnectClient(c, "Kicked");
|
DisconnectClient(c, "Kicked");
|
||||||
}
|
}
|
||||||
|
|
||||||
void TNetwork::DisconnectClient(const std::weak_ptr<TClient> &c, const std::string &R)
|
void TNetwork::DisconnectClient(const std::weak_ptr<TClient>& c, const std::string& R) {
|
||||||
{
|
|
||||||
if (auto locked = c.lock()) {
|
if (auto locked = c.lock()) {
|
||||||
DisconnectClient(*locked, R);
|
DisconnectClient(*locked, R);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
beammp_debugf("Tried to disconnect a non existant client with reason: {}", R);
|
beammp_debugf("Tried to disconnect a non existant client with reason: {}", R);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TNetwork::DisconnectClient(TClient &c, const std::string &R)
|
void TNetwork::DisconnectClient(TClient& c, const std::string& R) {
|
||||||
{
|
if (c.IsDisconnected())
|
||||||
if (c.IsDisconnected()) return;
|
return;
|
||||||
c.Disconnect(R);
|
c.Disconnect(R);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -781,7 +776,7 @@ static boost::system::error_code ReadSocketWithTimeout(
|
|||||||
std::chrono::steady_clock::duration timeout);
|
std::chrono::steady_clock::duration timeout);
|
||||||
|
|
||||||
boost::system::error_code TNetwork::ReadWithTimeout(TConnection& Connection, void* Buf, size_t Len, std::chrono::steady_clock::duration Timeout) {
|
boost::system::error_code TNetwork::ReadWithTimeout(TConnection& Connection, void* Buf, size_t Len, std::chrono::steady_clock::duration Timeout) {
|
||||||
return ReadSocketWithTimeout(mIoCtxPoller.IoCtx(), Connection.Socket, Buf, Len, Timeout);
|
return ReadSocketWithTimeout(mServer.IoCtx(), Connection.Socket, Buf, Len, Timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
static boost::system::error_code ReadSocketWithTimeout(
|
static boost::system::error_code ReadSocketWithTimeout(
|
||||||
@@ -892,7 +887,7 @@ TEST_CASE("ReadSocketWithTimeout can timeout then retry successfully") {
|
|||||||
REQUIRE(!Ec);
|
REQUIRE(!Ec);
|
||||||
|
|
||||||
uint8_t Received = 0;
|
uint8_t Received = 0;
|
||||||
CHECK(ReadSocketWithTimeout(TimerThread.get(), ServerSocket, &Received, 1, std::chrono::milliseconds(20)) == error::timed_out);
|
CHECK(ReadSocketWithTimeout(TimerThread.IoCtx(), ServerSocket, &Received, 1, std::chrono::milliseconds(20)) == error::timed_out);
|
||||||
|
|
||||||
const uint8_t Sent = 0x42;
|
const uint8_t Sent = 0x42;
|
||||||
boost::asio::write(ClientSocket, boost::asio::buffer(&Sent, 1), Ec);
|
boost::asio::write(ClientSocket, boost::asio::buffer(&Sent, 1), Ec);
|
||||||
@@ -1293,21 +1288,3 @@ std::vector<uint8_t> TNetwork::UDPRcvFromClient(boost::asio::ip::udp::endpoint&
|
|||||||
beammp_assert(Rcv <= Ret.size());
|
beammp_assert(Rcv <= Ret.size());
|
||||||
return std::vector<uint8_t>(Ret.begin(), Ret.begin() + Rcv);
|
return std::vector<uint8_t>(Ret.begin(), Ret.begin() + Rcv);
|
||||||
}
|
}
|
||||||
|
|
||||||
TIoPollThread::TIoPollThread()
|
|
||||||
: mWorkGuard(boost::asio::make_work_guard(mIoCtx))
|
|
||||||
, mThread([this](std::stop_token StopToken) {
|
|
||||||
while (!StopToken.stop_requested()) {
|
|
||||||
try {
|
|
||||||
mIoCtx.run();
|
|
||||||
break;
|
|
||||||
} catch (...) {
|
|
||||||
mIoCtx.restart();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}) { }
|
|
||||||
|
|
||||||
TIoPollThread::~TIoPollThread() {
|
|
||||||
mWorkGuard.reset();
|
|
||||||
mIoCtx.stop();
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user