server.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // server.cpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <cstdlib>
  11. #include <iostream>
  12. #include <boost/aligned_storage.hpp>
  13. #include <boost/array.hpp>
  14. #include <boost/bind.hpp>
  15. #include <boost/enable_shared_from_this.hpp>
  16. #include <boost/noncopyable.hpp>
  17. #include <boost/shared_ptr.hpp>
  18. #include <boost/asio.hpp>
  19. using boost::asio::ip::tcp;
  20. // Class to manage the memory to be used for handler-based custom allocation.
  21. // It contains a single block of memory which may be returned for allocation
  22. // requests. If the memory is in use when an allocation request is made, the
  23. // allocator delegates allocation to the global heap.
  24. class handler_memory
  25. : private boost::noncopyable
  26. {
  27. public:
  28. handler_memory()
  29. : in_use_(false)
  30. {
  31. }
  32. void* allocate(std::size_t size)
  33. {
  34. if (!in_use_ && size < storage_.size)
  35. {
  36. in_use_ = true;
  37. return storage_.address();
  38. }
  39. else
  40. {
  41. return ::operator new(size);
  42. }
  43. }
  44. void deallocate(void* pointer)
  45. {
  46. if (pointer == storage_.address())
  47. {
  48. in_use_ = false;
  49. }
  50. else
  51. {
  52. ::operator delete(pointer);
  53. }
  54. }
  55. private:
  56. // Storage space used for handler-based custom memory allocation.
  57. boost::aligned_storage<1024> storage_;
  58. // Whether the handler-based custom allocation storage has been used.
  59. bool in_use_;
  60. };
  61. // The allocator to be associated with the handler objects. This allocator only
  62. // needs to satisfy the C++11 minimal allocator requirements, plus rebind when
  63. // targeting C++03.
  64. template <typename T>
  65. class handler_allocator
  66. {
  67. public:
  68. typedef T value_type;
  69. explicit handler_allocator(handler_memory& mem)
  70. : memory_(mem)
  71. {
  72. }
  73. template <typename U>
  74. handler_allocator(const handler_allocator<U>& other)
  75. : memory_(other.memory_)
  76. {
  77. }
  78. template <typename U>
  79. struct rebind
  80. {
  81. typedef handler_allocator<U> other;
  82. };
  83. bool operator==(const handler_allocator& other) const
  84. {
  85. return &memory_ == &other.memory_;
  86. }
  87. bool operator!=(const handler_allocator& other) const
  88. {
  89. return &memory_ != &other.memory_;
  90. }
  91. T* allocate(std::size_t n) const
  92. {
  93. return static_cast<T*>(memory_.allocate(sizeof(T) * n));
  94. }
  95. void deallocate(T* p, std::size_t /*n*/) const
  96. {
  97. return memory_.deallocate(p);
  98. }
  99. //private:
  100. // The underlying memory.
  101. handler_memory& memory_;
  102. };
  103. // Wrapper class template for handler objects to allow handler memory
  104. // allocation to be customised. The allocator_type typedef and get_allocator()
  105. // member function are used by the asynchronous operations to obtain the
  106. // allocator. Calls to operator() are forwarded to the encapsulated handler.
  107. template <typename Handler>
  108. class custom_alloc_handler
  109. {
  110. public:
  111. typedef handler_allocator<Handler> allocator_type;
  112. custom_alloc_handler(handler_memory& m, Handler h)
  113. : memory_(m),
  114. handler_(h)
  115. {
  116. }
  117. allocator_type get_allocator() const
  118. {
  119. return allocator_type(memory_);
  120. }
  121. template <typename Arg1>
  122. void operator()(Arg1 arg1)
  123. {
  124. handler_(arg1);
  125. }
  126. template <typename Arg1, typename Arg2>
  127. void operator()(Arg1 arg1, Arg2 arg2)
  128. {
  129. handler_(arg1, arg2);
  130. }
  131. private:
  132. handler_memory& memory_;
  133. Handler handler_;
  134. };
  135. // Helper function to wrap a handler object to add custom allocation.
  136. template <typename Handler>
  137. inline custom_alloc_handler<Handler> make_custom_alloc_handler(
  138. handler_memory& m, Handler h)
  139. {
  140. return custom_alloc_handler<Handler>(m, h);
  141. }
  142. class session
  143. : public boost::enable_shared_from_this<session>
  144. {
  145. public:
  146. session(boost::asio::io_context& io_context)
  147. : socket_(io_context)
  148. {
  149. }
  150. tcp::socket& socket()
  151. {
  152. return socket_;
  153. }
  154. void start()
  155. {
  156. socket_.async_read_some(boost::asio::buffer(data_),
  157. make_custom_alloc_handler(handler_memory_,
  158. boost::bind(&session::handle_read,
  159. shared_from_this(),
  160. boost::asio::placeholders::error,
  161. boost::asio::placeholders::bytes_transferred)));
  162. }
  163. void handle_read(const boost::system::error_code& error,
  164. size_t bytes_transferred)
  165. {
  166. if (!error)
  167. {
  168. boost::asio::async_write(socket_,
  169. boost::asio::buffer(data_, bytes_transferred),
  170. make_custom_alloc_handler(handler_memory_,
  171. boost::bind(&session::handle_write,
  172. shared_from_this(),
  173. boost::asio::placeholders::error)));
  174. }
  175. }
  176. void handle_write(const boost::system::error_code& error)
  177. {
  178. if (!error)
  179. {
  180. socket_.async_read_some(boost::asio::buffer(data_),
  181. make_custom_alloc_handler(handler_memory_,
  182. boost::bind(&session::handle_read,
  183. shared_from_this(),
  184. boost::asio::placeholders::error,
  185. boost::asio::placeholders::bytes_transferred)));
  186. }
  187. }
  188. private:
  189. // The socket used to communicate with the client.
  190. tcp::socket socket_;
  191. // Buffer used to store data received from the client.
  192. boost::array<char, 1024> data_;
  193. // The memory to use for handler-based custom memory allocation.
  194. handler_memory handler_memory_;
  195. };
  196. typedef boost::shared_ptr<session> session_ptr;
  197. class server
  198. {
  199. public:
  200. server(boost::asio::io_context& io_context, short port)
  201. : io_context_(io_context),
  202. acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
  203. {
  204. session_ptr new_session(new session(io_context_));
  205. acceptor_.async_accept(new_session->socket(),
  206. boost::bind(&server::handle_accept, this, new_session,
  207. boost::asio::placeholders::error));
  208. }
  209. void handle_accept(session_ptr new_session,
  210. const boost::system::error_code& error)
  211. {
  212. if (!error)
  213. {
  214. new_session->start();
  215. }
  216. new_session.reset(new session(io_context_));
  217. acceptor_.async_accept(new_session->socket(),
  218. boost::bind(&server::handle_accept, this, new_session,
  219. boost::asio::placeholders::error));
  220. }
  221. private:
  222. boost::asio::io_context& io_context_;
  223. tcp::acceptor acceptor_;
  224. };
  225. int main(int argc, char* argv[])
  226. {
  227. try
  228. {
  229. if (argc != 2)
  230. {
  231. std::cerr << "Usage: server <port>\n";
  232. return 1;
  233. }
  234. boost::asio::io_context io_context;
  235. using namespace std; // For atoi.
  236. server s(io_context, atoi(argv[1]));
  237. io_context.run();
  238. }
  239. catch (std::exception& e)
  240. {
  241. std::cerr << "Exception: " << e.what() << "\n";
  242. }
  243. return 0;
  244. }