2
// raw_socket_service.hpp
3
// ~~~~~~~~~~~~~~~~~~~~~~
5
// Copyright (c) 2003-2008 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)
11
#ifndef ASIO_RAW_SOCKET_SERVICE_HPP
12
#define ASIO_RAW_SOCKET_SERVICE_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
22
#include <boost/config.hpp>
23
#include "asio/detail/pop_options.hpp"
25
#include "asio/error.hpp"
26
#include "asio/io_service.hpp"
27
#include "asio/detail/epoll_reactor.hpp"
28
#include "asio/detail/kqueue_reactor.hpp"
29
#include "asio/detail/select_reactor.hpp"
30
#include "asio/detail/service_base.hpp"
31
#include "asio/detail/reactive_socket_service.hpp"
32
#include "asio/detail/win_iocp_socket_service.hpp"
36
/// Default service implementation for a raw socket.
37
template <typename Protocol>
38
class raw_socket_service
39
#if defined(GENERATING_DOCUMENTATION)
40
: public asio::io_service::service
42
: public asio::detail::service_base<raw_socket_service<Protocol> >
46
#if defined(GENERATING_DOCUMENTATION)
47
/// The unique service identifier.
48
static asio::io_service::id id;
51
/// The protocol type.
52
typedef Protocol protocol_type;
54
/// The endpoint type.
55
typedef typename Protocol::endpoint endpoint_type;
58
// The type of the platform-specific implementation.
59
#if defined(ASIO_HAS_IOCP)
60
typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
61
#elif defined(ASIO_HAS_EPOLL)
62
typedef detail::reactive_socket_service<
63
Protocol, detail::epoll_reactor<false> > service_impl_type;
64
#elif defined(ASIO_HAS_KQUEUE)
65
typedef detail::reactive_socket_service<
66
Protocol, detail::kqueue_reactor<false> > service_impl_type;
67
#elif defined(ASIO_HAS_DEV_POLL)
68
typedef detail::reactive_socket_service<
69
Protocol, detail::dev_poll_reactor<false> > service_impl_type;
71
typedef detail::reactive_socket_service<
72
Protocol, detail::select_reactor<false> > service_impl_type;
76
/// The type of a raw socket.
77
#if defined(GENERATING_DOCUMENTATION)
78
typedef implementation_defined implementation_type;
80
typedef typename service_impl_type::implementation_type implementation_type;
83
/// The native socket type.
84
#if defined(GENERATING_DOCUMENTATION)
85
typedef implementation_defined native_type;
87
typedef typename service_impl_type::native_type native_type;
90
/// Construct a new raw socket service for the specified io_service.
91
explicit raw_socket_service(asio::io_service& io_service)
92
: asio::detail::service_base<
93
raw_socket_service<Protocol> >(io_service),
94
service_impl_(asio::use_service<service_impl_type>(io_service))
98
/// Destroy all user-defined handler objects owned by the service.
99
void shutdown_service()
103
/// Construct a new raw socket implementation.
104
void construct(implementation_type& impl)
106
service_impl_.construct(impl);
109
/// Destroy a raw socket implementation.
110
void destroy(implementation_type& impl)
112
service_impl_.destroy(impl);
115
// Open a new raw socket implementation.
116
asio::error_code open(implementation_type& impl,
117
const protocol_type& protocol, asio::error_code& ec)
119
if (protocol.type() == SOCK_RAW)
120
service_impl_.open(impl, protocol, ec);
122
ec = asio::error::invalid_argument;
126
/// Assign an existing native socket to a raw socket.
127
asio::error_code assign(implementation_type& impl,
128
const protocol_type& protocol, const native_type& native_socket,
129
asio::error_code& ec)
131
return service_impl_.assign(impl, protocol, native_socket, ec);
134
/// Determine whether the socket is open.
135
bool is_open(const implementation_type& impl) const
137
return service_impl_.is_open(impl);
140
/// Close a raw socket implementation.
141
asio::error_code close(implementation_type& impl,
142
asio::error_code& ec)
144
return service_impl_.close(impl, ec);
147
/// Get the native socket implementation.
148
native_type native(implementation_type& impl)
150
return service_impl_.native(impl);
153
/// Cancel all asynchronous operations associated with the socket.
154
asio::error_code cancel(implementation_type& impl,
155
asio::error_code& ec)
157
return service_impl_.cancel(impl, ec);
160
/// Determine whether the socket is at the out-of-band data mark.
161
bool at_mark(const implementation_type& impl,
162
asio::error_code& ec) const
164
return service_impl_.at_mark(impl, ec);
167
/// Determine the number of bytes available for reading.
168
std::size_t available(const implementation_type& impl,
169
asio::error_code& ec) const
171
return service_impl_.available(impl, ec);
174
// Bind the raw socket to the specified local endpoint.
175
asio::error_code bind(implementation_type& impl,
176
const endpoint_type& endpoint, asio::error_code& ec)
178
return service_impl_.bind(impl, endpoint, ec);
181
/// Connect the raw socket to the specified endpoint.
182
asio::error_code connect(implementation_type& impl,
183
const endpoint_type& peer_endpoint, asio::error_code& ec)
185
return service_impl_.connect(impl, peer_endpoint, ec);
188
/// Start an asynchronous connect.
189
template <typename ConnectHandler>
190
void async_connect(implementation_type& impl,
191
const endpoint_type& peer_endpoint, ConnectHandler handler)
193
service_impl_.async_connect(impl, peer_endpoint, handler);
196
/// Set a socket option.
197
template <typename SettableSocketOption>
198
asio::error_code set_option(implementation_type& impl,
199
const SettableSocketOption& option, asio::error_code& ec)
201
return service_impl_.set_option(impl, option, ec);
204
/// Get a socket option.
205
template <typename GettableSocketOption>
206
asio::error_code get_option(const implementation_type& impl,
207
GettableSocketOption& option, asio::error_code& ec) const
209
return service_impl_.get_option(impl, option, ec);
212
/// Perform an IO control command on the socket.
213
template <typename IoControlCommand>
214
asio::error_code io_control(implementation_type& impl,
215
IoControlCommand& command, asio::error_code& ec)
217
return service_impl_.io_control(impl, command, ec);
220
/// Get the local endpoint.
221
endpoint_type local_endpoint(const implementation_type& impl,
222
asio::error_code& ec) const
224
return service_impl_.local_endpoint(impl, ec);
227
/// Get the remote endpoint.
228
endpoint_type remote_endpoint(const implementation_type& impl,
229
asio::error_code& ec) const
231
return service_impl_.remote_endpoint(impl, ec);
234
/// Disable sends or receives on the socket.
235
asio::error_code shutdown(implementation_type& impl,
236
socket_base::shutdown_type what, asio::error_code& ec)
238
return service_impl_.shutdown(impl, what, ec);
241
/// Send the given data to the peer.
242
template <typename ConstBufferSequence>
243
std::size_t send(implementation_type& impl,
244
const ConstBufferSequence& buffers,
245
socket_base::message_flags flags, asio::error_code& ec)
247
return service_impl_.send(impl, buffers, flags, ec);
250
/// Start an asynchronous send.
251
template <typename ConstBufferSequence, typename WriteHandler>
252
void async_send(implementation_type& impl, const ConstBufferSequence& buffers,
253
socket_base::message_flags flags, WriteHandler handler)
255
service_impl_.async_send(impl, buffers, flags, handler);
258
/// Send raw data to the specified endpoint.
259
template <typename ConstBufferSequence>
260
std::size_t send_to(implementation_type& impl,
261
const ConstBufferSequence& buffers, const endpoint_type& destination,
262
socket_base::message_flags flags, asio::error_code& ec)
264
return service_impl_.send_to(impl, buffers, destination, flags, ec);
267
/// Start an asynchronous send.
268
template <typename ConstBufferSequence, typename WriteHandler>
269
void async_send_to(implementation_type& impl,
270
const ConstBufferSequence& buffers, const endpoint_type& destination,
271
socket_base::message_flags flags, WriteHandler handler)
273
service_impl_.async_send_to(impl, buffers, destination, flags, handler);
276
/// Receive some data from the peer.
277
template <typename MutableBufferSequence>
278
std::size_t receive(implementation_type& impl,
279
const MutableBufferSequence& buffers,
280
socket_base::message_flags flags, asio::error_code& ec)
282
return service_impl_.receive(impl, buffers, flags, ec);
285
/// Start an asynchronous receive.
286
template <typename MutableBufferSequence, typename ReadHandler>
287
void async_receive(implementation_type& impl,
288
const MutableBufferSequence& buffers,
289
socket_base::message_flags flags, ReadHandler handler)
291
service_impl_.async_receive(impl, buffers, flags, handler);
294
/// Receive raw data with the endpoint of the sender.
295
template <typename MutableBufferSequence>
296
std::size_t receive_from(implementation_type& impl,
297
const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
298
socket_base::message_flags flags, asio::error_code& ec)
300
return service_impl_.receive_from(impl, buffers, sender_endpoint, flags,
304
/// Start an asynchronous receive that will get the endpoint of the sender.
305
template <typename MutableBufferSequence, typename ReadHandler>
306
void async_receive_from(implementation_type& impl,
307
const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
308
socket_base::message_flags flags, ReadHandler handler)
310
service_impl_.async_receive_from(impl, buffers, sender_endpoint, flags,
315
// The service that provides the platform-specific implementation.
316
service_impl_type& service_impl_;
321
#include "asio/detail/pop_options.hpp"
323
#endif // ASIO_RAW_SOCKET_SERVICE_HPP