重构网络管理,移除TCPClient和TCPServer类,整合多路复用功能,优化连接管理和数据传输逻辑
This commit is contained in:
102
net/multiplex_manager.cpp
Normal file
102
net/multiplex_manager.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include "multiplex_manager.h"
|
||||
#include "nanoid/nanoid.h"
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
MultiplexManager::MultiplexManager(ISteamNetworkingSockets* steamInterface, HSteamNetConnection steamConn,
|
||||
boost::asio::io_context& io_context, bool& isHost, int& localPort)
|
||||
: steamInterface_(steamInterface), steamConn_(steamConn),
|
||||
io_context_(io_context), isHost_(isHost), localPort_(localPort) {}
|
||||
|
||||
MultiplexManager::~MultiplexManager() {
|
||||
// Close all sockets
|
||||
std::lock_guard<std::mutex> lock(mapMutex_);
|
||||
for (auto& pair : clientMap_) {
|
||||
pair.second->close();
|
||||
}
|
||||
clientMap_.clear();
|
||||
}
|
||||
|
||||
std::string MultiplexManager::addClient(std::shared_ptr<tcp::socket> socket) {
|
||||
std::lock_guard<std::mutex> lock(mapMutex_);
|
||||
std::string id = nanoid::generate(6);
|
||||
clientMap_[id] = socket;
|
||||
return id;
|
||||
}
|
||||
|
||||
void MultiplexManager::removeClient(const std::string& id) {
|
||||
std::lock_guard<std::mutex> lock(mapMutex_);
|
||||
auto it = clientMap_.find(id);
|
||||
if (it != clientMap_.end()) {
|
||||
it->second->close();
|
||||
clientMap_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<tcp::socket> MultiplexManager::getClient(const std::string& id) {
|
||||
std::lock_guard<std::mutex> lock(mapMutex_);
|
||||
auto it = clientMap_.find(id);
|
||||
if (it != clientMap_.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void MultiplexManager::sendTunnelPacket(const std::string& id, const char* data, size_t len, int type) {
|
||||
// Packet format: string id (6 chars + null), uint32_t type, then data if type==0
|
||||
size_t idLen = id.size() + 1; // include null terminator
|
||||
size_t packetSize = idLen + sizeof(uint32_t) + (type == 0 ? len : 0);
|
||||
std::vector<char> packet(packetSize);
|
||||
std::memcpy(&packet[0], id.c_str(), idLen);
|
||||
uint32_t* pType = reinterpret_cast<uint32_t*>(&packet[idLen]);
|
||||
*pType = type;
|
||||
if (type == 0 && data) {
|
||||
std::memcpy(&packet[idLen + sizeof(uint32_t)], data, len);
|
||||
}
|
||||
steamInterface_->SendMessageToConnection(steamConn_, packet.data(), packet.size(), k_nSteamNetworkingSend_Reliable, nullptr);
|
||||
}
|
||||
|
||||
void MultiplexManager::handleTunnelPacket(const char* data, size_t len) {
|
||||
size_t idLen = 7; // 6 + null
|
||||
if (len < idLen + sizeof(uint32_t)) {
|
||||
std::cerr << "Invalid tunnel packet size" << std::endl;
|
||||
return;
|
||||
}
|
||||
std::string id(data, 6);
|
||||
uint32_t type = *reinterpret_cast<const uint32_t*>(data + idLen);
|
||||
if (type == 0) {
|
||||
// Data packet
|
||||
size_t dataLen = len - idLen - sizeof(uint32_t);
|
||||
const char* packetData = data + idLen + sizeof(uint32_t);
|
||||
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) {
|
||||
boost::asio::async_write(*socket, boost::asio::buffer(packetData, dataLen), [](const boost::system::error_code&, std::size_t) {});
|
||||
} else {
|
||||
std::cerr << "No client found for id " << id << std::endl;
|
||||
}
|
||||
} else if (type == 1) {
|
||||
// Disconnect packet
|
||||
removeClient(id);
|
||||
std::cout << "Client " << id << " disconnected" << std::endl;
|
||||
} else {
|
||||
std::cerr << "Unknown packet type " << type << std::endl;
|
||||
}
|
||||
}
|
||||
37
net/multiplex_manager.h
Normal file
37
net/multiplex_manager.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <boost/asio.hpp>
|
||||
#include <steam_api.h>
|
||||
#include <isteamnetworkingsockets.h>
|
||||
#include <steamnetworkingtypes.h>
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
class MultiplexManager {
|
||||
public:
|
||||
MultiplexManager(ISteamNetworkingSockets* steamInterface, HSteamNetConnection steamConn,
|
||||
boost::asio::io_context& io_context, bool& isHost, int& localPort);
|
||||
~MultiplexManager();
|
||||
|
||||
std::string addClient(std::shared_ptr<tcp::socket> socket);
|
||||
void removeClient(const std::string& id);
|
||||
std::shared_ptr<tcp::socket> getClient(const std::string& id);
|
||||
|
||||
void sendTunnelPacket(const std::string& id, const char* data, size_t len, int type);
|
||||
|
||||
void handleTunnelPacket(const char* data, size_t len);
|
||||
|
||||
private:
|
||||
ISteamNetworkingSockets* steamInterface_;
|
||||
HSteamNetConnection steamConn_;
|
||||
std::unordered_map<std::string, std::shared_ptr<tcp::socket>> clientMap_;
|
||||
std::mutex mapMutex_;
|
||||
boost::asio::io_context& io_context_;
|
||||
bool& isHost_;
|
||||
int& localPort_;
|
||||
};
|
||||
106
net/tcp_server.cpp
Normal file
106
net/tcp_server.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#include "tcp_server.h"
|
||||
#include "../steam/steam_networking_manager.h"
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
TCPServer::TCPServer(int port, SteamNetworkingManager* manager) : port_(port), running_(false), acceptor_(io_context_), work_(boost::asio::make_work_guard(io_context_)), manager_(manager) {}
|
||||
|
||||
TCPServer::~TCPServer() { stop(); }
|
||||
|
||||
bool TCPServer::start() {
|
||||
try {
|
||||
tcp::endpoint endpoint(tcp::v4(), port_);
|
||||
acceptor_.open(endpoint.protocol());
|
||||
acceptor_.set_option(tcp::acceptor::reuse_address(true));
|
||||
acceptor_.bind(endpoint);
|
||||
acceptor_.listen();
|
||||
|
||||
multiplexManager_ = std::make_unique<MultiplexManager>(manager_->getInterface(), manager_->getConnection(),
|
||||
io_context_, manager_->getIsHost(), *manager_->getLocalPort());
|
||||
|
||||
running_ = true;
|
||||
serverThread_ = std::thread([this]() {
|
||||
std::cout << "Server thread started" << std::endl;
|
||||
io_context_.run();
|
||||
std::cout << "Server thread stopped" << std::endl;
|
||||
});
|
||||
start_accept();
|
||||
std::cout << "TCP server started on port " << port_ << std::endl;
|
||||
return true;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Failed to start TCP server: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void TCPServer::stop() {
|
||||
running_ = false;
|
||||
io_context_.stop();
|
||||
if (serverThread_.joinable()) {
|
||||
serverThread_.join();
|
||||
}
|
||||
acceptor_.close();
|
||||
multiplexManager_.reset();
|
||||
}
|
||||
|
||||
void TCPServer::sendToAll(const std::string& message, std::shared_ptr<tcp::socket> excludeSocket) {
|
||||
sendToAll(message.c_str(), message.size(), excludeSocket);
|
||||
}
|
||||
|
||||
void TCPServer::sendToAll(const char* data, size_t size, std::shared_ptr<tcp::socket> excludeSocket) {
|
||||
std::lock_guard<std::mutex> lock(clientsMutex_);
|
||||
for (auto& client : clients_) {
|
||||
if (client != excludeSocket) {
|
||||
boost::asio::async_write(*client, boost::asio::buffer(data, size), [](const boost::system::error_code&, std::size_t) {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int TCPServer::getClientCount() {
|
||||
std::lock_guard<std::mutex> lock(clientsMutex_);
|
||||
return clients_.size();
|
||||
}
|
||||
|
||||
void TCPServer::start_accept() {
|
||||
auto socket = std::make_shared<tcp::socket>(io_context_);
|
||||
acceptor_.async_accept(*socket, [this, socket](const boost::system::error_code& error) {
|
||||
if (!error) {
|
||||
std::cout << "New client connected" << std::endl;
|
||||
std::string id = multiplexManager_->addClient(socket);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(clientsMutex_);
|
||||
clients_.push_back(socket);
|
||||
}
|
||||
start_read(socket, id);
|
||||
}
|
||||
if (running_) {
|
||||
start_accept();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void TCPServer::start_read(std::shared_ptr<tcp::socket> socket, std::string id) {
|
||||
auto buffer = std::make_shared<std::vector<char>>(1024);
|
||||
socket->async_read_some(boost::asio::buffer(*buffer), [this, socket, buffer, id](const boost::system::error_code& error, std::size_t bytes_transferred) {
|
||||
if (!error) {
|
||||
std::cout << "Received " << bytes_transferred << " bytes from TCP client " << id << std::endl;
|
||||
if (manager_->isConnected()) {
|
||||
multiplexManager_->sendTunnelPacket(id, buffer->data(), bytes_transferred, 0);
|
||||
} else {
|
||||
std::cout << "Not connected to Steam, skipping forward" << std::endl;
|
||||
}
|
||||
sendToAll(buffer->data(), bytes_transferred, socket);
|
||||
start_read(socket, id);
|
||||
} else {
|
||||
std::cout << "TCP client " << id << " disconnected or error: " << error.message() << std::endl;
|
||||
// Send disconnect packet
|
||||
if (manager_->isConnected()) {
|
||||
multiplexManager_->sendTunnelPacket(id, nullptr, 0, 1);
|
||||
}
|
||||
// Remove client
|
||||
multiplexManager_->removeClient(id);
|
||||
std::lock_guard<std::mutex> lock(clientsMutex_);
|
||||
clients_.erase(std::remove(clients_.begin(), clients_.end(), socket), clients_.end());
|
||||
}
|
||||
});
|
||||
}
|
||||
46
net/tcp_server.h
Normal file
46
net/tcp_server.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
#include <isteamnetworkingsockets.h>
|
||||
#include <isteamnetworkingutils.h>
|
||||
#include <steamnetworkingtypes.h>
|
||||
#include "multiplex_manager.h"
|
||||
|
||||
class SteamNetworkingManager;
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
// TCP Server class
|
||||
class TCPServer {
|
||||
public:
|
||||
TCPServer(int port, SteamNetworkingManager* manager);
|
||||
~TCPServer();
|
||||
|
||||
bool start();
|
||||
void stop();
|
||||
void sendToAll(const std::string& message, std::shared_ptr<tcp::socket> excludeSocket = nullptr);
|
||||
void sendToAll(const char* data, size_t size, std::shared_ptr<tcp::socket> excludeSocket = nullptr);
|
||||
int getClientCount();
|
||||
MultiplexManager* getMultiplexManager() { return multiplexManager_.get(); }
|
||||
|
||||
private:
|
||||
void start_accept();
|
||||
void start_read(std::shared_ptr<tcp::socket> socket, std::string id);
|
||||
|
||||
int port_;
|
||||
bool running_;
|
||||
boost::asio::io_context io_context_;
|
||||
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work_;
|
||||
tcp::acceptor acceptor_;
|
||||
std::vector<std::shared_ptr<tcp::socket>> clients_;
|
||||
std::mutex clientsMutex_;
|
||||
std::thread serverThread_;
|
||||
SteamNetworkingManager* manager_;
|
||||
std::unique_ptr<MultiplexManager> multiplexManager_;
|
||||
};
|
||||
Reference in New Issue
Block a user