Ticket #3131: dccp_socket_service.hpp

File dccp_socket_service.hpp, 8.9 KB (added by oakad@…, 13 years ago)

"connected-only" datagram socket service

Line 
1//
2// dccp_socket_service.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_DCCP_SOCKET_SERVICE_HPP
12#define BOOST_ASIO_DCCP_SOCKET_SERVICE_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/error.hpp>
26#include <boost/asio/io_service.hpp>
27#include <boost/asio/detail/epoll_reactor.hpp>
28#include <boost/asio/detail/kqueue_reactor.hpp>
29#include <boost/asio/detail/select_reactor.hpp>
30#include <boost/asio/detail/service_base.hpp>
31
32#include <boost/asio/detail/reactive_socket_service.hpp>
33#include <boost/asio/detail/win_iocp_socket_service.hpp>
34
35namespace boost {
36namespace asio {
37
38/// Default service implementation for a datagram socket.
39template <typename Protocol>
40class dccp_socket_service
41#if defined(GENERATING_DOCUMENTATION)
42 : public boost::asio::io_service::service
43#else
44 : public boost::asio::detail::service_base<dccp_socket_service<Protocol> >
45#endif
46{
47public:
48#if defined(GENERATING_DOCUMENTATION)
49 /// The unique service identifier.
50 static boost::asio::io_service::id id;
51#endif
52
53 /// The protocol type.
54 typedef Protocol protocol_type;
55
56 /// The endpoint type.
57 typedef typename Protocol::endpoint endpoint_type;
58
59private:
60 // The type of the platform-specific implementation.
61#if defined(BOOST_ASIO_HAS_IOCP)
62 typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
63#elif defined(BOOST_ASIO_HAS_EPOLL)
64 typedef detail::reactive_socket_service<
65 Protocol, detail::epoll_reactor<false> > service_impl_type;
66#elif defined(BOOST_ASIO_HAS_KQUEUE)
67 typedef detail::reactive_socket_service<
68 Protocol, detail::kqueue_reactor<false> > service_impl_type;
69#elif defined(BOOST_ASIO_HAS_DEV_POLL)
70 typedef detail::reactive_socket_service<
71 Protocol, detail::dev_poll_reactor<false> > service_impl_type;
72#else
73 typedef detail::reactive_socket_service<
74 Protocol, detail::select_reactor<false> > service_impl_type;
75#endif
76
77public:
78 /// The type of a dccp socket.
79 /// The type of a datagram socket.
80#if defined(GENERATING_DOCUMENTATION)
81 typedef implementation_defined implementation_type;
82#else
83 typedef typename service_impl_type::implementation_type implementation_type;
84#endif
85
86 /// The native socket type.
87#if defined(GENERATING_DOCUMENTATION)
88 typedef implementation_defined native_type;
89#else
90 typedef typename service_impl_type::native_type native_type;
91#endif
92
93 /// Construct a new dccp socket service for the specified io_service.
94 explicit dccp_socket_service(boost::asio::io_service& io_service)
95 : boost::asio::detail::service_base<
96 dccp_socket_service<Protocol> >(io_service),
97 service_impl_(boost::asio::use_service<service_impl_type>(io_service))
98 {
99 }
100
101 /// Destroy all user-defined handler objects owned by the service.
102 void shutdown_service()
103 {
104 }
105
106 /// Construct a new dccp socket implementation.
107 void construct(implementation_type& impl)
108 {
109 service_impl_.construct(impl);
110 }
111
112 /// Destroy a dccp socket implementation.
113 void destroy(implementation_type& impl)
114 {
115 service_impl_.destroy(impl);
116 }
117
118 /// Open a new datagram socket implementation.
119 boost::system::error_code open(implementation_type& impl,
120 const protocol_type& protocol, boost::system::error_code& ec)
121 {
122 if (protocol.type() == SOCK_DCCP)
123 service_impl_.open(impl, protocol, ec);
124 else
125 ec = boost::asio::error::invalid_argument;
126 return ec;
127 }
128
129 /// Assign an existing native socket to a dccp socket.
130 boost::system::error_code assign(implementation_type& impl,
131 const protocol_type& protocol, const native_type& native_socket,
132 boost::system::error_code& ec)
133 {
134 return service_impl_.assign(impl, protocol, native_socket, ec);
135 }
136
137 /// Determine whether the socket is open.
138 bool is_open(const implementation_type& impl) const
139 {
140 return service_impl_.is_open(impl);
141 }
142
143 /// Close a datagram socket implementation.
144 boost::system::error_code close(implementation_type& impl,
145 boost::system::error_code& ec)
146 {
147 return service_impl_.close(impl, ec);
148 }
149
150 /// Get the native socket implementation.
151 native_type native(implementation_type& impl)
152 {
153 return service_impl_.native(impl);
154 }
155
156 /// Cancel all asynchronous operations associated with the socket.
157 boost::system::error_code cancel(implementation_type& impl,
158 boost::system::error_code& ec)
159 {
160 return service_impl_.cancel(impl, ec);
161 }
162
163 /// Determine whether the socket is at the out-of-band data mark.
164 bool at_mark(const implementation_type& impl,
165 boost::system::error_code& ec) const
166 {
167 return service_impl_.at_mark(impl, ec);
168 }
169
170 /// Determine the number of bytes available for reading.
171 std::size_t available(const implementation_type& impl,
172 boost::system::error_code& ec) const
173 {
174 return service_impl_.available(impl, ec);
175 }
176
177 /// Bind the datagram socket to the specified local endpoint.
178 boost::system::error_code bind(implementation_type& impl,
179 const endpoint_type& endpoint, boost::system::error_code& ec)
180 {
181 return service_impl_.bind(impl, endpoint, ec);
182 }
183
184 /// Connect the datagram socket to the specified endpoint.
185 boost::system::error_code connect(implementation_type& impl,
186 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
187 {
188 return service_impl_.connect(impl, peer_endpoint, ec);
189 }
190
191 /// Start an asynchronous connect.
192 template <typename ConnectHandler>
193 void async_connect(implementation_type& impl,
194 const endpoint_type& peer_endpoint, ConnectHandler handler)
195 {
196 service_impl_.async_connect(impl, peer_endpoint, handler);
197 }
198
199 /// Set a socket option.
200 template <typename SettableSocketOption>
201 boost::system::error_code set_option(implementation_type& impl,
202 const SettableSocketOption& option, boost::system::error_code& ec)
203 {
204 return service_impl_.set_option(impl, option, ec);
205 }
206
207 /// Get a socket option.
208 template <typename GettableSocketOption>
209 boost::system::error_code get_option(const implementation_type& impl,
210 GettableSocketOption& option, boost::system::error_code& ec) const
211 {
212 return service_impl_.get_option(impl, option, ec);
213 }
214
215 /// Perform an IO control command on the socket.
216 template <typename IoControlCommand>
217 boost::system::error_code io_control(implementation_type& impl,
218 IoControlCommand& command, boost::system::error_code& ec)
219 {
220 return service_impl_.io_control(impl, command, ec);
221 }
222
223 /// Get the local endpoint.
224 endpoint_type local_endpoint(const implementation_type& impl,
225 boost::system::error_code& ec) const
226 {
227 return service_impl_.local_endpoint(impl, ec);
228 }
229
230 /// Get the remote endpoint.
231 endpoint_type remote_endpoint(const implementation_type& impl,
232 boost::system::error_code& ec) const
233 {
234 return service_impl_.remote_endpoint(impl, ec);
235 }
236
237 /// Disable sends or receives on the socket.
238 boost::system::error_code shutdown(implementation_type& impl,
239 socket_base::shutdown_type what, boost::system::error_code& ec)
240 {
241 return service_impl_.shutdown(impl, what, ec);
242 }
243
244 /// Send the given data to the peer.
245 template <typename ConstBufferSequence>
246 std::size_t send(implementation_type& impl,
247 const ConstBufferSequence& buffers,
248 socket_base::message_flags flags, boost::system::error_code& ec)
249 {
250 return service_impl_.send(impl, buffers, flags, ec);
251 }
252
253 /// Start an asynchronous send.
254 template <typename ConstBufferSequence, typename WriteHandler>
255 void async_send(implementation_type& impl, const ConstBufferSequence& buffers,
256 socket_base::message_flags flags, WriteHandler handler)
257 {
258 service_impl_.async_send(impl, buffers, flags, handler);
259 }
260
261 /// Receive some data from the peer.
262 template <typename MutableBufferSequence>
263 std::size_t receive(implementation_type& impl,
264 const MutableBufferSequence& buffers,
265 socket_base::message_flags flags, boost::system::error_code& ec)
266 {
267 return service_impl_.receive(impl, buffers, flags, ec);
268 }
269
270 /// Start an asynchronous receive.
271 template <typename MutableBufferSequence, typename ReadHandler>
272 void async_receive(implementation_type& impl,
273 const MutableBufferSequence& buffers,
274 socket_base::message_flags flags, ReadHandler handler)
275 {
276 service_impl_.async_receive(impl, buffers, flags, handler);
277 }
278
279private:
280 // The service that provides the platform-specific implementation.
281 service_impl_type& service_impl_;
282};
283
284} // namespace asio
285} // namespace boost
286
287#include <boost/asio/detail/pop_options.hpp>
288
289#endif // BOOST_ASIO_DCCP_SOCKET_SERVICE_HPP