-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbridge.hpp
67 lines (59 loc) · 2.14 KB
/
bridge.hpp
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
#ifndef BRIDGE_HPP_
#define BRIDGE_HPP_
namespace pika
{
class bridge : public std::enable_shared_from_this<bridge>
{
public:
lib::tcp::socket first_socket_;
lib::tcp::socket second_socket_;
bridge (lib::tcp::socket && f, lib::tcp::socket && s) :
first_socket_{std::move(f)},
second_socket_{std::move(s)} {}
lib::awaitable<void> start_transport()
{
auto self = shared_from_this();
auto executor = co_await lib::this_coro::executor();
lib::co_spawn(executor,
[self]() mutable {
return self->redir(self->first_socket_,
self->second_socket_);
}, lib::detached);
lib::co_spawn(executor,
[self]() mutable {
return self->redir(self->second_socket_,
self->first_socket_);
}, lib::detached);
}
private:
lib::awaitable<void> redir(lib::tcp::socket &from, lib::tcp::socket &to)
{
auto executor = co_await lib::this_coro::executor();
auto token = co_await lib::this_coro::token();
auto self = shared_from_this();
try
{
std::array<char, def::bufsize> raw_buf;
for (;;)
{
std::size_t read_n = co_await from.async_read_some(boost::asio::buffer(raw_buf), token);
std::ignore = co_await boost::asio::async_write(to, boost::asio::buffer(raw_buf, read_n), token);
}
}
catch (boost::system::system_error const & e)
{
if (e.code() != boost::asio::error::eof)
std::cerr << "bridge::redir() exception: " << e.what() << std::endl;
}
catch (std::exception const &e)
{
std::cerr << "bridge::redir() std exception: " << e.what() << std::endl;
}
boost::system::error_code ec;
from.shutdown(lib::tcp::socket::shutdown_both, ec);
to.shutdown(lib::tcp::socket::shutdown_both, ec);
co_return;
}
};
} // namespace pika
#endif // BRIDGE_HPP_