1 | //
|
---|
2 | // basic_dccp_socket.hpp
|
---|
3 | // ~~~~~~~~~~~~~~~~~~~~~
|
---|
4 | //
|
---|
5 | // Copyright (c) 2009 Alexander Dubov (oakad at yahoo 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 | #ifndef BOOST_ASIO_BASIC_DCCP_SOCKET_HPP
|
---|
12 | #define BOOST_ASIO_BASIC_DCCP_SOCKET_HPP
|
---|
13 |
|
---|
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
---|
15 | # pragma once
|
---|
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
---|
17 |
|
---|
18 | #include <boost/asio/detail/push_options.hpp>
|
---|
19 |
|
---|
20 | #include <boost/asio/detail/push_options.hpp>
|
---|
21 | #include <cstddef>
|
---|
22 | #include <boost/config.hpp>
|
---|
23 | #include <boost/asio/detail/pop_options.hpp>
|
---|
24 |
|
---|
25 | #include <boost/asio/basic_socket.hpp>
|
---|
26 | #include "dccp_socket_service.hpp"
|
---|
27 | #include <boost/asio/error.hpp>
|
---|
28 | #include <boost/asio/detail/throw_error.hpp>
|
---|
29 |
|
---|
30 | namespace boost {
|
---|
31 | namespace asio {
|
---|
32 |
|
---|
33 | /// Provides dccp-oriented socket functionality.
|
---|
34 | /**
|
---|
35 | * The basic_dccp_socket class template provides asynchronous and blocking
|
---|
36 | * dccp-oriented socket functionality.
|
---|
37 | *
|
---|
38 | * @par Thread Safety
|
---|
39 | * @e Distinct @e objects: Safe.@n
|
---|
40 | * @e Shared @e objects: Unsafe.
|
---|
41 | */
|
---|
42 | template <typename Protocol,
|
---|
43 | typename DccpSocketService = dccp_socket_service<Protocol> >
|
---|
44 | class basic_dccp_socket
|
---|
45 | : public basic_socket<Protocol, DccpSocketService>
|
---|
46 | {
|
---|
47 | public:
|
---|
48 | /// The native representation of a socket.
|
---|
49 | typedef typename DccpSocketService::native_type native_type;
|
---|
50 |
|
---|
51 | /// The protocol type.
|
---|
52 | typedef Protocol protocol_type;
|
---|
53 |
|
---|
54 | /// The endpoint type.
|
---|
55 | typedef typename Protocol::endpoint endpoint_type;
|
---|
56 |
|
---|
57 | /// Construct a basic_dccp_socket without opening it.
|
---|
58 | /**
|
---|
59 | * This constructor creates a dccp socket without opening it. The open()
|
---|
60 | * function must be called before data can be sent or received on the socket.
|
---|
61 | *
|
---|
62 | * @param io_service The io_service object that the dccp socket will use
|
---|
63 | * to dispatch handlers for any asynchronous operations performed on the
|
---|
64 | * socket.
|
---|
65 | */
|
---|
66 | explicit basic_dccp_socket(boost::asio::io_service& io_service)
|
---|
67 | : basic_socket<Protocol, DccpSocketService>(io_service)
|
---|
68 | {
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// Construct and open a basic_dccp_socket.
|
---|
72 | /**
|
---|
73 | * This constructor creates and opens a dccp socket.
|
---|
74 | *
|
---|
75 | * @param io_service The io_service object that the dccp socket will use
|
---|
76 | * to dispatch handlers for any asynchronous operations performed on the
|
---|
77 | * socket.
|
---|
78 | *
|
---|
79 | * @param protocol An object specifying protocol parameters to be used.
|
---|
80 | *
|
---|
81 | * @throws boost::system::system_error Thrown on failure.
|
---|
82 | */
|
---|
83 | basic_dccp_socket(boost::asio::io_service& io_service,
|
---|
84 | const protocol_type& protocol)
|
---|
85 | : basic_socket<Protocol, DccpSocketService>(io_service, protocol)
|
---|
86 | {
|
---|
87 | }
|
---|
88 |
|
---|
89 | /// Construct a basic_dccp_socket, opening it and binding it to the given
|
---|
90 | /// local endpoint.
|
---|
91 | /**
|
---|
92 | * This constructor creates a dccp socket and automatically opens it bound
|
---|
93 | * to the specified endpoint on the local machine. The protocol used is the
|
---|
94 | * protocol associated with the given endpoint.
|
---|
95 | *
|
---|
96 | * @param io_service The io_service object that the dccp socket will use
|
---|
97 | * to dispatch handlers for any asynchronous operations performed on the
|
---|
98 | * socket.
|
---|
99 | *
|
---|
100 | * @param endpoint An endpoint on the local machine to which the dccp
|
---|
101 | * socket will be bound.
|
---|
102 | *
|
---|
103 | * @throws boost::system::system_error Thrown on failure.
|
---|
104 | */
|
---|
105 | basic_dccp_socket(boost::asio::io_service& io_service,
|
---|
106 | const endpoint_type& endpoint)
|
---|
107 | : basic_socket<Protocol, DccpSocketService>(io_service, endpoint)
|
---|
108 | {
|
---|
109 | }
|
---|
110 |
|
---|
111 | /// Construct a basic_dccp_socket on an existing native socket.
|
---|
112 | /**
|
---|
113 | * This constructor creates a dccp socket object to hold an existing
|
---|
114 | * native socket.
|
---|
115 | *
|
---|
116 | * @param io_service The io_service object that the dccp socket will use
|
---|
117 | * to dispatch handlers for any asynchronous operations performed on the
|
---|
118 | * socket.
|
---|
119 | *
|
---|
120 | * @param protocol An object specifying protocol parameters to be used.
|
---|
121 | *
|
---|
122 | * @param native_socket The new underlying socket implementation.
|
---|
123 | *
|
---|
124 | * @throws boost::system::system_error Thrown on failure.
|
---|
125 | */
|
---|
126 | basic_dccp_socket(boost::asio::io_service& io_service,
|
---|
127 | const protocol_type& protocol, const native_type& native_socket)
|
---|
128 | : basic_socket<Protocol, DccpSocketService>(
|
---|
129 | io_service, protocol, native_socket)
|
---|
130 | {
|
---|
131 | }
|
---|
132 |
|
---|
133 | /// Send some data on a connected socket.
|
---|
134 | /**
|
---|
135 | * This function is used to send data on the dccp socket. The function
|
---|
136 | * call will block until the data has been sent successfully or an error
|
---|
137 | * occurs.
|
---|
138 | *
|
---|
139 | * @param buffers One ore more data buffers to be sent on the socket.
|
---|
140 | *
|
---|
141 | * @returns The number of bytes sent.
|
---|
142 | *
|
---|
143 | * @throws boost::system::system_error Thrown on failure.
|
---|
144 | *
|
---|
145 | * @par Example
|
---|
146 | * To send a single data buffer use the @ref buffer function as follows:
|
---|
147 | * @code socket.send(boost::asio::buffer(data, size)); @endcode
|
---|
148 | * See the @ref buffer documentation for information on sending multiple
|
---|
149 | * buffers in one go, and how to use it with arrays, boost::array or
|
---|
150 | * std::vector.
|
---|
151 | */
|
---|
152 | template <typename ConstBufferSequence>
|
---|
153 | std::size_t send(const ConstBufferSequence& buffers)
|
---|
154 | {
|
---|
155 | boost::system::error_code ec;
|
---|
156 | std::size_t s = this->service.send(this->implementation, buffers, 0, ec);
|
---|
157 | boost::asio::detail::throw_error(ec);
|
---|
158 | return s;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /// Send some data on a connected socket.
|
---|
162 | /**
|
---|
163 | * This function is used to send data on the dccp socket. The function
|
---|
164 | * call will block until the data has been sent successfully or an error
|
---|
165 | * occurs.
|
---|
166 | *
|
---|
167 | * @param buffers One ore more data buffers to be sent on the socket.
|
---|
168 | *
|
---|
169 | * @param flags Flags specifying how the send call is to be made.
|
---|
170 | *
|
---|
171 | * @returns The number of bytes sent.
|
---|
172 | *
|
---|
173 | * @throws boost::system::system_error Thrown on failure.
|
---|
174 | *
|
---|
175 | */
|
---|
176 | template <typename ConstBufferSequence>
|
---|
177 | std::size_t send(const ConstBufferSequence& buffers,
|
---|
178 | socket_base::message_flags flags)
|
---|
179 | {
|
---|
180 | boost::system::error_code ec;
|
---|
181 | std::size_t s = this->service.send(
|
---|
182 | this->implementation, buffers, flags, ec);
|
---|
183 | boost::asio::detail::throw_error(ec);
|
---|
184 | return s;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /// Send some data on a connected socket.
|
---|
188 | /**
|
---|
189 | * This function is used to send data on the dccp socket. The function
|
---|
190 | * call will block until the data has been sent successfully or an error
|
---|
191 | * occurs.
|
---|
192 | *
|
---|
193 | * @param buffers One or more data buffers to be sent on the socket.
|
---|
194 | *
|
---|
195 | * @param flags Flags specifying how the send call is to be made.
|
---|
196 | *
|
---|
197 | * @param ec Set to indicate what error occurred, if any.
|
---|
198 | *
|
---|
199 | * @returns The number of bytes sent.
|
---|
200 | *
|
---|
201 | */
|
---|
202 | template <typename ConstBufferSequence>
|
---|
203 | std::size_t send(const ConstBufferSequence& buffers,
|
---|
204 | socket_base::message_flags flags, boost::system::error_code& ec)
|
---|
205 | {
|
---|
206 | return this->service.send(this->implementation, buffers, flags, ec);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /// Start an asynchronous send on a connected socket.
|
---|
210 | /**
|
---|
211 | * This function is used to send data on the dccp socket. The function
|
---|
212 | * call will block until the data has been sent successfully or an error
|
---|
213 | * occurs.
|
---|
214 | *
|
---|
215 | * @param buffers One or more data buffers to be sent on the socket. Although
|
---|
216 | * the buffers object may be copied as necessary, ownership of the underlying
|
---|
217 | * memory blocks is retained by the caller, which must guarantee that they
|
---|
218 | * remain valid until the handler is called.
|
---|
219 | *
|
---|
220 | * @param handler The handler to be called when the send operation completes.
|
---|
221 | * Copies will be made of the handler as required. The function signature of
|
---|
222 | * the handler must be:
|
---|
223 | * @code void handler(
|
---|
224 | * const boost::system::error_code& error, // Result of operation.
|
---|
225 | * std::size_t bytes_transferred // Number of bytes sent.
|
---|
226 | * ); @endcode
|
---|
227 | * Regardless of whether the asynchronous operation completes immediately or
|
---|
228 | * not, the handler will not be invoked from within this function. Invocation
|
---|
229 | * of the handler will be performed in a manner equivalent to using
|
---|
230 | * boost::asio::io_service::post().
|
---|
231 | *
|
---|
232 | * @par Example
|
---|
233 | * To send a single data buffer use the @ref buffer function as follows:
|
---|
234 | * @code
|
---|
235 | * socket.async_send(boost::asio::buffer(data, size), handler);
|
---|
236 | * @endcode
|
---|
237 | * See the @ref buffer documentation for information on sending multiple
|
---|
238 | * buffers in one go, and how to use it with arrays, boost::array or
|
---|
239 | * std::vector.
|
---|
240 | */
|
---|
241 | template <typename ConstBufferSequence, typename WriteHandler>
|
---|
242 | void async_send(const ConstBufferSequence& buffers, WriteHandler handler)
|
---|
243 | {
|
---|
244 | this->service.async_send(this->implementation, buffers, 0, handler);
|
---|
245 | }
|
---|
246 |
|
---|
247 | /// Start an asynchronous send on a connected socket.
|
---|
248 | /**
|
---|
249 | * This function is used to send data on the dccp socket. The function
|
---|
250 | * call will block until the data has been sent successfully or an error
|
---|
251 | * occurs.
|
---|
252 | *
|
---|
253 | * @param buffers One or more data buffers to be sent on the socket. Although
|
---|
254 | * the buffers object may be copied as necessary, ownership of the underlying
|
---|
255 | * memory blocks is retained by the caller, which must guarantee that they
|
---|
256 | * remain valid until the handler is called.
|
---|
257 | *
|
---|
258 | * @param flags Flags specifying how the send call is to be made.
|
---|
259 | *
|
---|
260 | * @param handler The handler to be called when the send operation completes.
|
---|
261 | * Copies will be made of the handler as required. The function signature of
|
---|
262 | * the handler must be:
|
---|
263 | * @code void handler(
|
---|
264 | * const boost::system::error_code& error, // Result of operation.
|
---|
265 | * std::size_t bytes_transferred // Number of bytes sent.
|
---|
266 | * ); @endcode
|
---|
267 | * Regardless of whether the asynchronous operation completes immediately or
|
---|
268 | * not, the handler will not be invoked from within this function. Invocation
|
---|
269 | * of the handler will be performed in a manner equivalent to using
|
---|
270 | * boost::asio::io_service::post().
|
---|
271 | *
|
---|
272 | */
|
---|
273 | template <typename ConstBufferSequence, typename WriteHandler>
|
---|
274 | void async_send(const ConstBufferSequence& buffers,
|
---|
275 | socket_base::message_flags flags, WriteHandler handler)
|
---|
276 | {
|
---|
277 | this->service.async_send(this->implementation, buffers, flags, handler);
|
---|
278 | }
|
---|
279 |
|
---|
280 | /// Receive some data on a connected socket.
|
---|
281 | /**
|
---|
282 | * This function is used to receive data on the dccp socket. The function
|
---|
283 | * call will block until data has been received successfully or an error
|
---|
284 | * occurs.
|
---|
285 | *
|
---|
286 | * @param buffers One or more buffers into which the data will be received.
|
---|
287 | *
|
---|
288 | * @returns The number of bytes received.
|
---|
289 | *
|
---|
290 | * @throws boost::system::system_error Thrown on failure.
|
---|
291 | *
|
---|
292 | * @par Example
|
---|
293 | * To receive into a single data buffer use the @ref buffer function as
|
---|
294 | * follows:
|
---|
295 | * @code socket.receive(boost::asio::buffer(data, size)); @endcode
|
---|
296 | * See the @ref buffer documentation for information on receiving into
|
---|
297 | * multiple buffers in one go, and how to use it with arrays, boost::array or
|
---|
298 | * std::vector.
|
---|
299 | */
|
---|
300 | template <typename MutableBufferSequence>
|
---|
301 | std::size_t receive(const MutableBufferSequence& buffers)
|
---|
302 | {
|
---|
303 | boost::system::error_code ec;
|
---|
304 | std::size_t s = this->service.receive(
|
---|
305 | this->implementation, buffers, 0, ec);
|
---|
306 | boost::asio::detail::throw_error(ec);
|
---|
307 | return s;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /// Receive some data on a connected socket.
|
---|
311 | /**
|
---|
312 | * This function is used to receive data on the dccp socket. The function
|
---|
313 | * call will block until data has been received successfully or an error
|
---|
314 | * occurs.
|
---|
315 | *
|
---|
316 | * @param buffers One or more buffers into which the data will be received.
|
---|
317 | *
|
---|
318 | * @param flags Flags specifying how the receive call is to be made.
|
---|
319 | *
|
---|
320 | * @returns The number of bytes received.
|
---|
321 | *
|
---|
322 | * @throws boost::system::system_error Thrown on failure.
|
---|
323 | *
|
---|
324 | */
|
---|
325 | template <typename MutableBufferSequence>
|
---|
326 | std::size_t receive(const MutableBufferSequence& buffers,
|
---|
327 | socket_base::message_flags flags)
|
---|
328 | {
|
---|
329 | boost::system::error_code ec;
|
---|
330 | std::size_t s = this->service.receive(
|
---|
331 | this->implementation, buffers, flags, ec);
|
---|
332 | boost::asio::detail::throw_error(ec);
|
---|
333 | return s;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /// Receive some data on a connected socket.
|
---|
337 | /**
|
---|
338 | * This function is used to receive data on the dccp socket. The function
|
---|
339 | * call will block until data has been received successfully or an error
|
---|
340 | * occurs.
|
---|
341 | *
|
---|
342 | * @param buffers One or more buffers into which the data will be received.
|
---|
343 | *
|
---|
344 | * @param flags Flags specifying how the receive call is to be made.
|
---|
345 | *
|
---|
346 | * @param ec Set to indicate what error occurred, if any.
|
---|
347 | *
|
---|
348 | * @returns The number of bytes received.
|
---|
349 | *
|
---|
350 | */
|
---|
351 | template <typename MutableBufferSequence>
|
---|
352 | std::size_t receive(const MutableBufferSequence& buffers,
|
---|
353 | socket_base::message_flags flags, boost::system::error_code& ec)
|
---|
354 | {
|
---|
355 | return this->service.receive(this->implementation, buffers, flags, ec);
|
---|
356 | }
|
---|
357 |
|
---|
358 | /// Start an asynchronous receive on a connected socket.
|
---|
359 | /**
|
---|
360 | * This function is used to asynchronously receive data from the dccp
|
---|
361 | * socket. The function call always returns immediately.
|
---|
362 | *
|
---|
363 | * @param buffers One or more buffers into which the data will be received.
|
---|
364 | * Although the buffers object may be copied as necessary, ownership of the
|
---|
365 | * underlying memory blocks is retained by the caller, which must guarantee
|
---|
366 | * that they remain valid until the handler is called.
|
---|
367 | *
|
---|
368 | * @param handler The handler to be called when the receive operation
|
---|
369 | * completes. Copies will be made of the handler as required. The function
|
---|
370 | * signature of the handler must be:
|
---|
371 | * @code void handler(
|
---|
372 | * const boost::system::error_code& error, // Result of operation.
|
---|
373 | * std::size_t bytes_transferred // Number of bytes received.
|
---|
374 | * ); @endcode
|
---|
375 | * Regardless of whether the asynchronous operation completes immediately or
|
---|
376 | * not, the handler will not be invoked from within this function. Invocation
|
---|
377 | * of the handler will be performed in a manner equivalent to using
|
---|
378 | * boost::asio::io_service::post().
|
---|
379 | *
|
---|
380 | * @par Example
|
---|
381 | * To receive into a single data buffer use the @ref buffer function as
|
---|
382 | * follows:
|
---|
383 | * @code
|
---|
384 | * socket.async_receive(boost::asio::buffer(data, size), handler);
|
---|
385 | * @endcode
|
---|
386 | * See the @ref buffer documentation for information on receiving into
|
---|
387 | * multiple buffers in one go, and how to use it with arrays, boost::array or
|
---|
388 | * std::vector.
|
---|
389 | */
|
---|
390 | template <typename MutableBufferSequence, typename ReadHandler>
|
---|
391 | void async_receive(const MutableBufferSequence& buffers, ReadHandler handler)
|
---|
392 | {
|
---|
393 | this->service.async_receive(this->implementation, buffers, 0, handler);
|
---|
394 | }
|
---|
395 |
|
---|
396 | /// Start an asynchronous receive on a connected socket.
|
---|
397 | /**
|
---|
398 | * This function is used to asynchronously receive data from the dccp
|
---|
399 | * socket. The function call always returns immediately.
|
---|
400 | *
|
---|
401 | * @param buffers One or more buffers into which the data will be received.
|
---|
402 | * Although the buffers object may be copied as necessary, ownership of the
|
---|
403 | * underlying memory blocks is retained by the caller, which must guarantee
|
---|
404 | * that they remain valid until the handler is called.
|
---|
405 | *
|
---|
406 | * @param flags Flags specifying how the receive call is to be made.
|
---|
407 | *
|
---|
408 | * @param handler The handler to be called when the receive operation
|
---|
409 | * completes. Copies will be made of the handler as required. The function
|
---|
410 | * signature of the handler must be:
|
---|
411 | * @code void handler(
|
---|
412 | * const boost::system::error_code& error, // Result of operation.
|
---|
413 | * std::size_t bytes_transferred // Number of bytes received.
|
---|
414 | * ); @endcode
|
---|
415 | * Regardless of whether the asynchronous operation completes immediately or
|
---|
416 | * not, the handler will not be invoked from within this function. Invocation
|
---|
417 | * of the handler will be performed in a manner equivalent to using
|
---|
418 | * boost::asio::io_service::post().
|
---|
419 | *
|
---|
420 | */
|
---|
421 | template <typename MutableBufferSequence, typename ReadHandler>
|
---|
422 | void async_receive(const MutableBufferSequence& buffers,
|
---|
423 | socket_base::message_flags flags, ReadHandler handler)
|
---|
424 | {
|
---|
425 | this->service.async_receive(this->implementation, buffers, flags, handler);
|
---|
426 | }
|
---|
427 | };
|
---|
428 |
|
---|
429 | } // namespace asio
|
---|
430 | } // namespace boost
|
---|
431 |
|
---|
432 | #include <boost/asio/detail/pop_options.hpp>
|
---|
433 |
|
---|
434 | #endif // BOOST_ASIO_BASIC_DCCP_SOCKET_HPP
|
---|