-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
81 lines (70 loc) · 1.8 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <boost/asio.hpp>
class ProxySession
{
public:
ProxySession(std::shared_ptr<boost::asio::ip::tcp::socket> socketPtr)
{
}
void start()
{
}
};
class ProxyServer
{
public:
ProxyServer(boost::asio::io_context& ioContext, std::uint16_t portNumber)
: _acceptor{ioContext}, _ioContext{ioContext}
{
initialize(portNumber);
startAccept();
}
private:
void initialize(std::uint16_t portNumber)
{
boost::asio::ip::tcp::endpoint endpoint {
boost::asio::ip::tcp::v4(),
portNumber
};
_acceptor.open(endpoint.protocol());
_acceptor.bind(endpoint);
_acceptor.listen();
}
void startAccept()
{
auto socketPtr = std::make_shared<boost::asio::ip::tcp::socket>(_ioContext);
_acceptor.async_accept(*socketPtr.get(),
[this, socketPtr](const boost::system::error_code& errCode)
{
if (!errCode)
std::make_shared<ProxySession>(socketPtr)->start();
else
std::cerr << "Failed to accept: " << errCode.value()
<< ". Message: " << errCode.message();
startAccept();
});
}
private:
boost::asio::ip::tcp::acceptor _acceptor;
boost::asio::io_context& _ioContext;
};
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cerr << "Usage: proxy_server <port>\n";
return -1;
}
std::uint16_t portNumber = std::atoi(argv[1]);
try
{
boost::asio::io_context ioContext{};
ProxyServer server{ioContext, portNumber};
ioContext.run();
}
catch (const std::exception& e)
{
std::cerr << "Exception: " << e.what() << '\n';
return -1;
}
}