1//
2// server.cpp
3// ~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 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
11#include <array>
12#include <cstdlib>
13#include <iostream>
14#include <memory>
15#include <type_traits>
16#include <utility>
17#include <boost/asio.hpp>
18
19using boost::asio::ip::tcp;
20
21// Class to manage the memory to be used for handler-based custom allocation.
22// It contains a single block of memory which may be returned for allocation
23// requests. If the memory is in use when an allocation request is made, the
24// allocator delegates allocation to the global heap.
25class handler_memory
26{
27public:
28 handler_memory()
29 : in_use_(false)
30 {
31 }
32
33 handler_memory(const handler_memory&) = delete;
34 handler_memory& operator=(const handler_memory&) = delete;
35
36 void* allocate(std::size_t size)
37 {
38 if (!in_use_ && size < sizeof(storage_))
39 {
40 in_use_ = true;
41 return &storage_;
42 }
43 else
44 {
45 return ::operator new(size);
46 }
47 }
48
49 void deallocate(void* pointer)
50 {
51 if (pointer == &storage_)
52 {
53 in_use_ = false;
54 }
55 else
56 {
57 ::operator delete(pointer);
58 }
59 }
60
61private:
62 // Storage space used for handler-based custom memory allocation.
63 typename std::aligned_storage<1024>::type storage_;
64
65 // Whether the handler-based custom allocation storage has been used.
66 bool in_use_;
67};
68
69// The allocator to be associated with the handler objects. This allocator only
70// needs to satisfy the C++11 minimal allocator requirements.
71template <typename T>
72class handler_allocator
73{
74public:
75 using value_type = T;
76
77 explicit handler_allocator(handler_memory& mem)
78 : memory_(mem)
79 {
80 }
81
82 template <typename U>
83 handler_allocator(const handler_allocator<U>& other) noexcept
84 : memory_(other.memory_)
85 {
86 }
87
88 bool operator==(const handler_allocator& other) const noexcept
89 {
90 return &memory_ == &other.memory_;
91 }
92
93 bool operator!=(const handler_allocator& other) const noexcept
94 {
95 return &memory_ != &other.memory_;
96 }
97
98 T* allocate(std::size_t n) const
99 {
100 return static_cast<T*>(memory_.allocate(size: sizeof(T) * n));
101 }
102
103 void deallocate(T* p, std::size_t /*n*/) const
104 {
105 return memory_.deallocate(pointer: p);
106 }
107
108private:
109 template <typename> friend class handler_allocator;
110
111 // The underlying memory.
112 handler_memory& memory_;
113};
114
115class session
116 : public std::enable_shared_from_this<session>
117{
118public:
119 session(tcp::socket socket)
120 : socket_(std::move(socket))
121 {
122 }
123
124 void start()
125 {
126 do_read();
127 }
128
129private:
130 void do_read()
131 {
132 auto self(shared_from_this());
133 socket_.async_read_some(buffers: boost::asio::buffer(data&: data_),
134 token: boost::asio::bind_allocator(
135 s: handler_allocator<int>(handler_memory_),
136 t: [this, self](boost::system::error_code ec, std::size_t length)
137 {
138 if (!ec)
139 {
140 do_write(length);
141 }
142 }));
143 }
144
145 void do_write(std::size_t length)
146 {
147 auto self(shared_from_this());
148 boost::asio::async_write(s&: socket_, buffers: boost::asio::buffer(data&: data_, max_size_in_bytes: length),
149 token: boost::asio::bind_allocator(
150 s: handler_allocator<int>(handler_memory_),
151 t: [this, self](boost::system::error_code ec, std::size_t /*length*/)
152 {
153 if (!ec)
154 {
155 do_read();
156 }
157 }));
158 }
159
160 // The socket used to communicate with the client.
161 tcp::socket socket_;
162
163 // Buffer used to store data received from the client.
164 std::array<char, 1024> data_;
165
166 // The memory to use for handler-based custom memory allocation.
167 handler_memory handler_memory_;
168};
169
170class server
171{
172public:
173 server(boost::asio::io_context& io_context, short port)
174 : acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
175 {
176 do_accept();
177 }
178
179private:
180 void do_accept()
181 {
182 acceptor_.async_accept(
183 token: [this](boost::system::error_code ec, tcp::socket socket)
184 {
185 if (!ec)
186 {
187 std::make_shared<session>(args: std::move(socket))->start();
188 }
189
190 do_accept();
191 });
192 }
193
194 tcp::acceptor acceptor_;
195};
196
197int main(int argc, char* argv[])
198{
199 try
200 {
201 if (argc != 2)
202 {
203 std::cerr << "Usage: server <port>\n";
204 return 1;
205 }
206
207 boost::asio::io_context io_context;
208 server s(io_context, std::atoi(nptr: argv[1]));
209 io_context.run();
210 }
211 catch (std::exception& e)
212 {
213 std::cerr << "Exception: " << e.what() << "\n";
214 }
215
216 return 0;
217}
218

source code of boost/libs/asio/example/cpp11/allocation/server.cpp