-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathserver.hpp
More file actions
94 lines (73 loc) · 2.38 KB
/
server.hpp
File metadata and controls
94 lines (73 loc) · 2.38 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// server.hpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef AWESOME_SERVER_HPP
#define AWESOME_SERVER_HPP
#include <boost/asio/ip/tcp.hpp>
#include <memory>
#include <string>
#include "allocator.hpp"
#include "coroutine.hpp"
namespace awesome {
using boost::asio::ip::tcp;
// The top-level class of the proxy.
class server : coroutine
{
public:
// Construct the server to listen on the specified port.
server(const std::string& listen_address, const std::string& listen_port,
const std::string& target_address, const std::string& target_port);
// Copy constructor to print when called.
server(const server& other);
// Use default move constructor.
server(server&&) = default;
// Run the operations associated with the server.
void operator()(boost::system::error_code ec = boost::system::error_code(),
std::size_t length = 0);
private:
// The io_service used to perform asynchronous operations.
std::shared_ptr<boost::asio::io_service> io_service_;
// Resolver used to turn host and service names into TCP endpoints.
std::shared_ptr<tcp::resolver> resolver_;
// Acceptor used to listen for incoming connections.
std::shared_ptr<tcp::acceptor> acceptor_;
// The target endpoint for all forwarded connections.
tcp::endpoint up_endpoint_;
// The pair of sockets that are being connected together by the proxy.
std::shared_ptr<tcp::socket> socket1_, socket2_;
// Buffer for forwarding data.
std::shared_ptr<std::array<unsigned char, 1024>> buffer_;
// Custom memory allocator.
std::shared_ptr<allocator> allocator_;
// Custom allocation hook.
friend void* asio_handler_allocate(std::size_t n, server* s)
{
return s->allocator_->allocate(n);
}
// Custom deallocation hook.
friend void asio_handler_deallocate(void* p, std::size_t, server* s)
{
s->allocator_->deallocate(p);
}
// Custom invocation hook.
template <class Function>
friend void asio_handler_invoke(const Function& f, server*)
{
Function tmp;
tmp();
}
// Custom invocation hook.
template <class Function>
friend void asio_handler_invoke(Function& f, server*)
{
f();
}
};
} // namespace awesome
#endif // AWESOME_SERVER_HPP