51 lines
1.6 KiB
C++
51 lines
1.6 KiB
C++
#include "ProxyTcpSession.h"
|
|
#include "BoostLog.h"
|
|
#include <boost/asio.hpp>
|
|
#include <sstream>
|
|
|
|
ProxyTcpSession::ProxyTcpSession(boost::asio::ip::tcp::socket &&client, boost::asio::ip::tcp::socket &&server)
|
|
: m_client(std::move(client)), m_server(std::move(server)) {
|
|
}
|
|
|
|
ProxyTcpSession::~ProxyTcpSession() {
|
|
}
|
|
|
|
void ProxyTcpSession::run() {
|
|
clientRead();
|
|
serverRead();
|
|
}
|
|
|
|
void ProxyTcpSession::clientRead() {
|
|
m_client.async_read_some(
|
|
boost::asio::buffer(m_clientBuffer),
|
|
[self = shared_from_this()](boost::system::error_code error, std::size_t bytes_transferred) {
|
|
if (error) {
|
|
LOG(error) << error.message();
|
|
self->m_server.close();
|
|
return;
|
|
}
|
|
boost::asio::write(self->m_server, boost::asio::buffer(self->m_clientBuffer, bytes_transferred), error);
|
|
if (error) {
|
|
LOG(info) << error.message();
|
|
}
|
|
self->clientRead();
|
|
});
|
|
}
|
|
|
|
void ProxyTcpSession::serverRead() {
|
|
m_server.async_read_some(
|
|
boost::asio::buffer(m_serverBuffer),
|
|
[self = shared_from_this()](boost::system::error_code error, std::size_t bytes_transferred) {
|
|
if (error) {
|
|
self->m_client.close();
|
|
LOG(error) << error.message();
|
|
return;
|
|
}
|
|
boost::asio::write(self->m_client, boost::asio::buffer(self->m_serverBuffer, bytes_transferred), error);
|
|
if (error) {
|
|
LOG(info) << error.message();
|
|
}
|
|
self->serverRead();
|
|
});
|
|
}
|