增强MultiplexManager构造函数,添加io_context、isHost和localPort参数,并在TCPServer中相应更新
This commit is contained in:
@@ -2,8 +2,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
MultiplexManager::MultiplexManager(ISteamNetworkingSockets* steamInterface, HSteamNetConnection steamConn)
|
MultiplexManager::MultiplexManager(ISteamNetworkingSockets* steamInterface, HSteamNetConnection steamConn,
|
||||||
: steamInterface_(steamInterface), steamConn_(steamConn), nextId_(1) {}
|
boost::asio::io_context& io_context, bool& isHost, int& localPort)
|
||||||
|
: steamInterface_(steamInterface), steamConn_(steamConn), nextId_(1),
|
||||||
|
io_context_(io_context), isHost_(isHost), localPort_(localPort) {}
|
||||||
|
|
||||||
MultiplexManager::~MultiplexManager() {
|
MultiplexManager::~MultiplexManager() {
|
||||||
// Close all sockets
|
// Close all sockets
|
||||||
@@ -65,6 +67,24 @@ void MultiplexManager::handleTunnelPacket(const char* data, size_t len) {
|
|||||||
size_t dataLen = len - sizeof(uint32_t) * 2;
|
size_t dataLen = len - sizeof(uint32_t) * 2;
|
||||||
const char* packetData = data + sizeof(uint32_t) * 2;
|
const char* packetData = data + sizeof(uint32_t) * 2;
|
||||||
auto socket = getClient(id);
|
auto socket = getClient(id);
|
||||||
|
if (!socket && isHost_ && localPort_ > 0) {
|
||||||
|
// 如果是主持且没有对应的 TCP Client,创建一个连接到本地端口
|
||||||
|
std::cout << "Creating new TCP client for id " << id << " connecting to localhost:" << localPort_ << std::endl;
|
||||||
|
try {
|
||||||
|
auto newSocket = std::make_shared<tcp::socket>(io_context_);
|
||||||
|
tcp::resolver resolver(io_context_);
|
||||||
|
auto endpoints = resolver.resolve("127.0.0.1", std::to_string(localPort_));
|
||||||
|
boost::asio::connect(*newSocket, endpoints);
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(mapMutex_);
|
||||||
|
clientMap_[id] = newSocket;
|
||||||
|
socket = newSocket;
|
||||||
|
std::cout << "Successfully created TCP client for id " << id << std::endl;
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "Failed to create TCP client for id " << id << ": " << e.what() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (socket) {
|
if (socket) {
|
||||||
boost::asio::async_write(*socket, boost::asio::buffer(packetData, dataLen), [](const boost::system::error_code&, std::size_t) {});
|
boost::asio::async_write(*socket, boost::asio::buffer(packetData, dataLen), [](const boost::system::error_code&, std::size_t) {});
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ using boost::asio::ip::tcp;
|
|||||||
|
|
||||||
class MultiplexManager {
|
class MultiplexManager {
|
||||||
public:
|
public:
|
||||||
MultiplexManager(ISteamNetworkingSockets* steamInterface, HSteamNetConnection steamConn);
|
MultiplexManager(ISteamNetworkingSockets* steamInterface, HSteamNetConnection steamConn,
|
||||||
|
boost::asio::io_context& io_context, bool& isHost, int& localPort);
|
||||||
~MultiplexManager();
|
~MultiplexManager();
|
||||||
|
|
||||||
uint32_t addClient(std::shared_ptr<tcp::socket> socket);
|
uint32_t addClient(std::shared_ptr<tcp::socket> socket);
|
||||||
@@ -30,4 +31,7 @@ private:
|
|||||||
std::unordered_map<uint32_t, std::shared_ptr<tcp::socket>> clientMap_;
|
std::unordered_map<uint32_t, std::shared_ptr<tcp::socket>> clientMap_;
|
||||||
std::mutex mapMutex_;
|
std::mutex mapMutex_;
|
||||||
uint32_t nextId_;
|
uint32_t nextId_;
|
||||||
|
boost::asio::io_context& io_context_;
|
||||||
|
bool& isHost_;
|
||||||
|
int& localPort_;
|
||||||
};
|
};
|
||||||
@@ -15,7 +15,8 @@ bool TCPServer::start() {
|
|||||||
acceptor_.bind(endpoint);
|
acceptor_.bind(endpoint);
|
||||||
acceptor_.listen();
|
acceptor_.listen();
|
||||||
|
|
||||||
multiplexManager_ = std::make_unique<MultiplexManager>(manager_->getInterface(), manager_->getConnection());
|
multiplexManager_ = std::make_unique<MultiplexManager>(manager_->getInterface(), manager_->getConnection(),
|
||||||
|
io_context_, manager_->getIsHost(), *manager_->getLocalPort());
|
||||||
|
|
||||||
running_ = true;
|
running_ = true;
|
||||||
serverThread_ = std::thread([this]() {
|
serverThread_ = std::thread([this]() {
|
||||||
|
|||||||
Reference in New Issue
Block a user