增强MultiplexManager,添加异步读取功能,管理每个客户端的读取缓冲区
This commit is contained in:
@@ -21,6 +21,8 @@ std::string MultiplexManager::addClient(std::shared_ptr<tcp::socket> socket) {
|
|||||||
std::lock_guard<std::mutex> lock(mapMutex_);
|
std::lock_guard<std::mutex> lock(mapMutex_);
|
||||||
std::string id = nanoid::generate(6);
|
std::string id = nanoid::generate(6);
|
||||||
clientMap_[id] = socket;
|
clientMap_[id] = socket;
|
||||||
|
readBuffers_[id].resize(1024);
|
||||||
|
startAsyncRead(id);
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,6 +33,7 @@ void MultiplexManager::removeClient(const std::string& id) {
|
|||||||
it->second->close();
|
it->second->close();
|
||||||
clientMap_.erase(it);
|
clientMap_.erase(it);
|
||||||
}
|
}
|
||||||
|
readBuffers_.erase(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<tcp::socket> MultiplexManager::getClient(const std::string& id) {
|
std::shared_ptr<tcp::socket> MultiplexManager::getClient(const std::string& id) {
|
||||||
@@ -80,8 +83,10 @@ void MultiplexManager::handleTunnelPacket(const char* data, size_t len) {
|
|||||||
|
|
||||||
std::lock_guard<std::mutex> lock(mapMutex_);
|
std::lock_guard<std::mutex> lock(mapMutex_);
|
||||||
clientMap_[id] = newSocket;
|
clientMap_[id] = newSocket;
|
||||||
|
readBuffers_[id].resize(1024);
|
||||||
socket = newSocket;
|
socket = newSocket;
|
||||||
std::cout << "Successfully created TCP client for id " << id << std::endl;
|
std::cout << "Successfully created TCP client for id " << id << std::endl;
|
||||||
|
startAsyncRead(id);
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
std::cerr << "Failed to create TCP client for id " << id << ": " << e.what() << std::endl;
|
std::cerr << "Failed to create TCP client for id " << id << ": " << e.what() << std::endl;
|
||||||
return;
|
return;
|
||||||
@@ -99,4 +104,18 @@ void MultiplexManager::handleTunnelPacket(const char* data, size_t len) {
|
|||||||
} else {
|
} else {
|
||||||
std::cerr << "Unknown packet type " << type << std::endl;
|
std::cerr << "Unknown packet type " << type << std::endl;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MultiplexManager::startAsyncRead(const std::string& id) {
|
||||||
|
auto socket = getClient(id);
|
||||||
|
if (!socket || readBuffers_.find(id) == readBuffers_.end()) return;
|
||||||
|
socket->async_read_some(boost::asio::buffer(readBuffers_[id]),
|
||||||
|
[this, id](const boost::system::error_code& ec, std::size_t bytes_transferred) {
|
||||||
|
if (!ec && bytes_transferred > 0) {
|
||||||
|
sendTunnelPacket(id, readBuffers_[id].data(), bytes_transferred, 0);
|
||||||
|
startAsyncRead(id);
|
||||||
|
} else {
|
||||||
|
removeClient(id);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
@@ -34,4 +34,7 @@ private:
|
|||||||
boost::asio::io_context& io_context_;
|
boost::asio::io_context& io_context_;
|
||||||
bool& isHost_;
|
bool& isHost_;
|
||||||
int& localPort_;
|
int& localPort_;
|
||||||
|
std::unordered_map<std::string, std::vector<char>> readBuffers_;
|
||||||
|
|
||||||
|
void startAsyncRead(const std::string& id);
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user