重构代码,删除不必要的文件,添加控制数据包处理和Steam消息处理功能
This commit is contained in:
@@ -43,9 +43,10 @@ void TCPClient::send(const std::string& message) {
|
||||
void TCPClient::send(const char* data, size_t size) {
|
||||
if (!connected_) return;
|
||||
// std::cout << "Sending " << size << " bytes" << std::endl;
|
||||
boost::asio::async_write(*socket_, boost::asio::buffer(data, size), [](const boost::system::error_code& error, std::size_t) {
|
||||
boost::asio::async_write(*socket_, boost::asio::buffer(data, size), [this](const boost::system::error_code& error, std::size_t) {
|
||||
if (error) {
|
||||
std::cerr << "Send failed: " << error.message() << std::endl;
|
||||
disconnect();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -75,7 +76,11 @@ void TCPClient::handle_read(const boost::system::error_code& error, std::size_t
|
||||
}
|
||||
start_read();
|
||||
} else {
|
||||
std::cerr << "Read failed: " << error.message() << std::endl;
|
||||
if (error == boost::asio::error::eof) {
|
||||
std::cout << "Connection closed by peer" << std::endl;
|
||||
} else {
|
||||
std::cerr << "Read failed: " << error.message() << std::endl;
|
||||
}
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,13 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
TCPServer::TCPServer(int port) : port_(port), running_(false), acceptor_(io_context_), work_(boost::asio::make_work_guard(io_context_)) {}
|
||||
TCPServer::TCPServer(int port) : port_(port), running_(false), acceptor_(io_context_), work_(boost::asio::make_work_guard(io_context_)), hasAcceptedConnection_(false) {}
|
||||
|
||||
TCPServer::~TCPServer() { stop(); }
|
||||
|
||||
bool TCPServer::start() {
|
||||
try {
|
||||
hasAcceptedConnection_ = false;
|
||||
tcp::endpoint endpoint(tcp::v4(), port_);
|
||||
acceptor_.open(endpoint.protocol());
|
||||
acceptor_.set_option(tcp::acceptor::reuse_address(true));
|
||||
@@ -31,6 +32,7 @@ bool TCPServer::start() {
|
||||
|
||||
void TCPServer::stop() {
|
||||
running_ = false;
|
||||
hasAcceptedConnection_ = false;
|
||||
io_context_.stop();
|
||||
if (serverThread_.joinable()) {
|
||||
serverThread_.join();
|
||||
@@ -65,9 +67,10 @@ void TCPServer::start_accept() {
|
||||
std::lock_guard<std::mutex> lock(clientsMutex_);
|
||||
clients_.push_back(socket);
|
||||
}
|
||||
hasAcceptedConnection_ = true;
|
||||
start_read(socket);
|
||||
}
|
||||
if (running_) {
|
||||
if (running_ && !hasAcceptedConnection_) {
|
||||
start_accept();
|
||||
}
|
||||
});
|
||||
@@ -92,6 +95,11 @@ void TCPServer::start_read(std::shared_ptr<tcp::socket> socket) {
|
||||
// Remove client
|
||||
std::lock_guard<std::mutex> lock(clientsMutex_);
|
||||
clients_.erase(std::remove(clients_.begin(), clients_.end(), socket), clients_.end());
|
||||
// Reset to allow new connection
|
||||
hasAcceptedConnection_ = false;
|
||||
if (running_) {
|
||||
start_accept();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -42,4 +42,5 @@ private:
|
||||
std::vector<std::shared_ptr<tcp::socket>> clients_;
|
||||
std::mutex clientsMutex_;
|
||||
std::thread serverThread_;
|
||||
bool hasAcceptedConnection_;
|
||||
};
|
||||
Reference in New Issue
Block a user