5 // Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
13 #include <boost/aligned_storage.hpp>
14 #include <boost/array.hpp>
15 #include <boost/bind.hpp>
16 #include <boost/enable_shared_from_this.hpp>
17 #include <boost/noncopyable.hpp>
18 #include <boost/shared_ptr.hpp>
19 #include <boost/asio.hpp>
21 using boost::asio::ip::tcp;
23 // Class to manage the memory to be used for handler-based custom allocation.
24 // It contains a single block of memory which may be returned for allocation
25 // requests. If the memory is in use when an allocation request is made, the
26 // allocator delegates allocation to the global heap.
27 class handler_allocator
28 : private boost::noncopyable
36 void* allocate(std::size_t size)
38 if (!in_use_ && size < storage_.size)
41 return storage_.address();
45 return ::operator new(size);
49 void deallocate(void* pointer)
51 if (pointer == storage_.address())
57 ::operator delete(pointer);
62 // Storage space used for handler-based custom memory allocation.
63 boost::aligned_storage<1024> storage_;
65 // Whether the handler-based custom allocation storage has been used.
69 // Wrapper class template for handler objects to allow handler memory
70 // allocation to be customised. Calls to operator() are forwarded to the
71 // encapsulated handler.
72 template <typename Handler>
73 class custom_alloc_handler
76 custom_alloc_handler(handler_allocator& a, Handler h)
82 template <typename Arg1>
83 void operator()(Arg1 arg1)
88 template <typename Arg1, typename Arg2>
89 void operator()(Arg1 arg1, Arg2 arg2)
94 friend void* asio_handler_allocate(std::size_t size,
95 custom_alloc_handler<Handler>* this_handler)
97 return this_handler->allocator_.allocate(size);
100 friend void asio_handler_deallocate(void* pointer, std::size_t /*size*/,
101 custom_alloc_handler<Handler>* this_handler)
103 this_handler->allocator_.deallocate(pointer);
107 handler_allocator& allocator_;
111 // Helper function to wrap a handler object to add custom allocation.
112 template <typename Handler>
113 inline custom_alloc_handler<Handler> make_custom_alloc_handler(
114 handler_allocator& a, Handler h)
116 return custom_alloc_handler<Handler>(a, h);
120 : public boost::enable_shared_from_this<session>
123 session(boost::asio::io_service& io_service)
124 : socket_(io_service)
128 tcp::socket& socket()
135 socket_.async_read_some(boost::asio::buffer(data_),
136 make_custom_alloc_handler(allocator_,
137 boost::bind(&session::handle_read,
139 boost::asio::placeholders::error,
140 boost::asio::placeholders::bytes_transferred)));
143 void handle_read(const boost::system::error_code& error,
144 size_t bytes_transferred)
148 boost::asio::async_write(socket_,
149 boost::asio::buffer(data_, bytes_transferred),
150 make_custom_alloc_handler(allocator_,
151 boost::bind(&session::handle_write,
153 boost::asio::placeholders::error)));
157 void handle_write(const boost::system::error_code& error)
161 socket_.async_read_some(boost::asio::buffer(data_),
162 make_custom_alloc_handler(allocator_,
163 boost::bind(&session::handle_read,
165 boost::asio::placeholders::error,
166 boost::asio::placeholders::bytes_transferred)));
171 // The socket used to communicate with the client.
174 // Buffer used to store data received from the client.
175 boost::array<char, 1024> data_;
177 // The allocator to use for handler-based custom memory allocation.
178 handler_allocator allocator_;
181 typedef boost::shared_ptr<session> session_ptr;
186 server(boost::asio::io_service& io_service, short port)
187 : io_service_(io_service),
188 acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
190 session_ptr new_session(new session(io_service_));
191 acceptor_.async_accept(new_session->socket(),
192 boost::bind(&server::handle_accept, this, new_session,
193 boost::asio::placeholders::error));
196 void handle_accept(session_ptr new_session,
197 const boost::system::error_code& error)
201 new_session->start();
204 new_session.reset(new session(io_service_));
205 acceptor_.async_accept(new_session->socket(),
206 boost::bind(&server::handle_accept, this, new_session,
207 boost::asio::placeholders::error));
211 boost::asio::io_service& io_service_;
212 tcp::acceptor acceptor_;
215 int main(int argc, char* argv[])
221 std::cerr << "Usage: server <port>\n";
225 boost::asio::io_service io_service;
227 using namespace std; // For atoi.
228 server s(io_service, atoi(argv[1]));
232 catch (std::exception& e)
234 std::cerr << "Exception: " << e.what() << "\n";