Chris@16
|
1 //
|
Chris@16
|
2 // detail/win_iocp_socket_service.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #if defined(BOOST_ASIO_HAS_IOCP)
|
Chris@16
|
21
|
Chris@16
|
22 #include <cstring>
|
Chris@16
|
23 #include <boost/asio/error.hpp>
|
Chris@16
|
24 #include <boost/asio/io_service.hpp>
|
Chris@16
|
25 #include <boost/asio/socket_base.hpp>
|
Chris@16
|
26 #include <boost/asio/detail/addressof.hpp>
|
Chris@16
|
27 #include <boost/asio/detail/bind_handler.hpp>
|
Chris@16
|
28 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
|
Chris@16
|
29 #include <boost/asio/detail/fenced_block.hpp>
|
Chris@16
|
30 #include <boost/asio/detail/handler_alloc_helpers.hpp>
|
Chris@16
|
31 #include <boost/asio/detail/handler_invoke_helpers.hpp>
|
Chris@16
|
32 #include <boost/asio/detail/mutex.hpp>
|
Chris@16
|
33 #include <boost/asio/detail/operation.hpp>
|
Chris@16
|
34 #include <boost/asio/detail/reactor.hpp>
|
Chris@16
|
35 #include <boost/asio/detail/reactor_op.hpp>
|
Chris@16
|
36 #include <boost/asio/detail/socket_holder.hpp>
|
Chris@16
|
37 #include <boost/asio/detail/socket_ops.hpp>
|
Chris@16
|
38 #include <boost/asio/detail/socket_types.hpp>
|
Chris@16
|
39 #include <boost/asio/detail/win_iocp_io_service.hpp>
|
Chris@16
|
40 #include <boost/asio/detail/win_iocp_null_buffers_op.hpp>
|
Chris@16
|
41 #include <boost/asio/detail/win_iocp_socket_accept_op.hpp>
|
Chris@101
|
42 #include <boost/asio/detail/win_iocp_socket_connect_op.hpp>
|
Chris@16
|
43 #include <boost/asio/detail/win_iocp_socket_recvfrom_op.hpp>
|
Chris@16
|
44 #include <boost/asio/detail/win_iocp_socket_send_op.hpp>
|
Chris@16
|
45 #include <boost/asio/detail/win_iocp_socket_service_base.hpp>
|
Chris@16
|
46
|
Chris@16
|
47 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
48
|
Chris@16
|
49 namespace boost {
|
Chris@16
|
50 namespace asio {
|
Chris@16
|
51 namespace detail {
|
Chris@16
|
52
|
Chris@16
|
53 template <typename Protocol>
|
Chris@16
|
54 class win_iocp_socket_service : public win_iocp_socket_service_base
|
Chris@16
|
55 {
|
Chris@16
|
56 public:
|
Chris@16
|
57 // The protocol type.
|
Chris@16
|
58 typedef Protocol protocol_type;
|
Chris@16
|
59
|
Chris@16
|
60 // The endpoint type.
|
Chris@16
|
61 typedef typename Protocol::endpoint endpoint_type;
|
Chris@16
|
62
|
Chris@16
|
63 // The native type of a socket.
|
Chris@16
|
64 class native_handle_type
|
Chris@16
|
65 {
|
Chris@16
|
66 public:
|
Chris@16
|
67 native_handle_type(socket_type s)
|
Chris@16
|
68 : socket_(s),
|
Chris@16
|
69 have_remote_endpoint_(false)
|
Chris@16
|
70 {
|
Chris@16
|
71 }
|
Chris@16
|
72
|
Chris@16
|
73 native_handle_type(socket_type s, const endpoint_type& ep)
|
Chris@16
|
74 : socket_(s),
|
Chris@16
|
75 have_remote_endpoint_(true),
|
Chris@16
|
76 remote_endpoint_(ep)
|
Chris@16
|
77 {
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 void operator=(socket_type s)
|
Chris@16
|
81 {
|
Chris@16
|
82 socket_ = s;
|
Chris@16
|
83 have_remote_endpoint_ = false;
|
Chris@16
|
84 remote_endpoint_ = endpoint_type();
|
Chris@16
|
85 }
|
Chris@16
|
86
|
Chris@16
|
87 operator socket_type() const
|
Chris@16
|
88 {
|
Chris@16
|
89 return socket_;
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 bool have_remote_endpoint() const
|
Chris@16
|
93 {
|
Chris@16
|
94 return have_remote_endpoint_;
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 endpoint_type remote_endpoint() const
|
Chris@16
|
98 {
|
Chris@16
|
99 return remote_endpoint_;
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 private:
|
Chris@16
|
103 socket_type socket_;
|
Chris@16
|
104 bool have_remote_endpoint_;
|
Chris@16
|
105 endpoint_type remote_endpoint_;
|
Chris@16
|
106 };
|
Chris@16
|
107
|
Chris@16
|
108 // The implementation type of the socket.
|
Chris@16
|
109 struct implementation_type :
|
Chris@16
|
110 win_iocp_socket_service_base::base_implementation_type
|
Chris@16
|
111 {
|
Chris@16
|
112 // Default constructor.
|
Chris@16
|
113 implementation_type()
|
Chris@16
|
114 : protocol_(endpoint_type().protocol()),
|
Chris@16
|
115 have_remote_endpoint_(false),
|
Chris@16
|
116 remote_endpoint_()
|
Chris@16
|
117 {
|
Chris@16
|
118 }
|
Chris@16
|
119
|
Chris@16
|
120 // The protocol associated with the socket.
|
Chris@16
|
121 protocol_type protocol_;
|
Chris@16
|
122
|
Chris@16
|
123 // Whether we have a cached remote endpoint.
|
Chris@16
|
124 bool have_remote_endpoint_;
|
Chris@16
|
125
|
Chris@16
|
126 // A cached remote endpoint.
|
Chris@16
|
127 endpoint_type remote_endpoint_;
|
Chris@16
|
128 };
|
Chris@16
|
129
|
Chris@16
|
130 // Constructor.
|
Chris@16
|
131 win_iocp_socket_service(boost::asio::io_service& io_service)
|
Chris@16
|
132 : win_iocp_socket_service_base(io_service)
|
Chris@16
|
133 {
|
Chris@16
|
134 }
|
Chris@16
|
135
|
Chris@16
|
136 // Move-construct a new socket implementation.
|
Chris@16
|
137 void move_construct(implementation_type& impl,
|
Chris@16
|
138 implementation_type& other_impl)
|
Chris@16
|
139 {
|
Chris@16
|
140 this->base_move_construct(impl, other_impl);
|
Chris@16
|
141
|
Chris@16
|
142 impl.protocol_ = other_impl.protocol_;
|
Chris@16
|
143 other_impl.protocol_ = endpoint_type().protocol();
|
Chris@16
|
144
|
Chris@16
|
145 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
|
Chris@16
|
146 other_impl.have_remote_endpoint_ = false;
|
Chris@16
|
147
|
Chris@16
|
148 impl.remote_endpoint_ = other_impl.remote_endpoint_;
|
Chris@16
|
149 other_impl.remote_endpoint_ = endpoint_type();
|
Chris@16
|
150 }
|
Chris@16
|
151
|
Chris@16
|
152 // Move-assign from another socket implementation.
|
Chris@16
|
153 void move_assign(implementation_type& impl,
|
Chris@16
|
154 win_iocp_socket_service_base& other_service,
|
Chris@16
|
155 implementation_type& other_impl)
|
Chris@16
|
156 {
|
Chris@16
|
157 this->base_move_assign(impl, other_service, other_impl);
|
Chris@16
|
158
|
Chris@16
|
159 impl.protocol_ = other_impl.protocol_;
|
Chris@16
|
160 other_impl.protocol_ = endpoint_type().protocol();
|
Chris@16
|
161
|
Chris@16
|
162 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
|
Chris@16
|
163 other_impl.have_remote_endpoint_ = false;
|
Chris@16
|
164
|
Chris@16
|
165 impl.remote_endpoint_ = other_impl.remote_endpoint_;
|
Chris@16
|
166 other_impl.remote_endpoint_ = endpoint_type();
|
Chris@16
|
167 }
|
Chris@16
|
168
|
Chris@16
|
169 // Move-construct a new socket implementation from another protocol type.
|
Chris@16
|
170 template <typename Protocol1>
|
Chris@16
|
171 void converting_move_construct(implementation_type& impl,
|
Chris@16
|
172 typename win_iocp_socket_service<
|
Chris@16
|
173 Protocol1>::implementation_type& other_impl)
|
Chris@16
|
174 {
|
Chris@16
|
175 this->base_move_construct(impl, other_impl);
|
Chris@16
|
176
|
Chris@16
|
177 impl.protocol_ = protocol_type(other_impl.protocol_);
|
Chris@16
|
178 other_impl.protocol_ = typename Protocol1::endpoint().protocol();
|
Chris@16
|
179
|
Chris@16
|
180 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
|
Chris@16
|
181 other_impl.have_remote_endpoint_ = false;
|
Chris@16
|
182
|
Chris@16
|
183 impl.remote_endpoint_ = other_impl.remote_endpoint_;
|
Chris@16
|
184 other_impl.remote_endpoint_ = typename Protocol1::endpoint();
|
Chris@16
|
185 }
|
Chris@16
|
186
|
Chris@16
|
187 // Open a new socket implementation.
|
Chris@16
|
188 boost::system::error_code open(implementation_type& impl,
|
Chris@16
|
189 const protocol_type& protocol, boost::system::error_code& ec)
|
Chris@16
|
190 {
|
Chris@16
|
191 if (!do_open(impl, protocol.family(),
|
Chris@16
|
192 protocol.type(), protocol.protocol(), ec))
|
Chris@16
|
193 {
|
Chris@16
|
194 impl.protocol_ = protocol;
|
Chris@16
|
195 impl.have_remote_endpoint_ = false;
|
Chris@16
|
196 impl.remote_endpoint_ = endpoint_type();
|
Chris@16
|
197 }
|
Chris@16
|
198 return ec;
|
Chris@16
|
199 }
|
Chris@16
|
200
|
Chris@16
|
201 // Assign a native socket to a socket implementation.
|
Chris@16
|
202 boost::system::error_code assign(implementation_type& impl,
|
Chris@16
|
203 const protocol_type& protocol, const native_handle_type& native_socket,
|
Chris@16
|
204 boost::system::error_code& ec)
|
Chris@16
|
205 {
|
Chris@16
|
206 if (!do_assign(impl, protocol.type(), native_socket, ec))
|
Chris@16
|
207 {
|
Chris@16
|
208 impl.protocol_ = protocol;
|
Chris@16
|
209 impl.have_remote_endpoint_ = native_socket.have_remote_endpoint();
|
Chris@16
|
210 impl.remote_endpoint_ = native_socket.remote_endpoint();
|
Chris@16
|
211 }
|
Chris@16
|
212 return ec;
|
Chris@16
|
213 }
|
Chris@16
|
214
|
Chris@16
|
215 // Get the native socket representation.
|
Chris@16
|
216 native_handle_type native_handle(implementation_type& impl)
|
Chris@16
|
217 {
|
Chris@16
|
218 if (impl.have_remote_endpoint_)
|
Chris@16
|
219 return native_handle_type(impl.socket_, impl.remote_endpoint_);
|
Chris@16
|
220 return native_handle_type(impl.socket_);
|
Chris@16
|
221 }
|
Chris@16
|
222
|
Chris@16
|
223 // Bind the socket to the specified local endpoint.
|
Chris@16
|
224 boost::system::error_code bind(implementation_type& impl,
|
Chris@16
|
225 const endpoint_type& endpoint, boost::system::error_code& ec)
|
Chris@16
|
226 {
|
Chris@16
|
227 socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
|
Chris@16
|
228 return ec;
|
Chris@16
|
229 }
|
Chris@16
|
230
|
Chris@16
|
231 // Set a socket option.
|
Chris@16
|
232 template <typename Option>
|
Chris@16
|
233 boost::system::error_code set_option(implementation_type& impl,
|
Chris@16
|
234 const Option& option, boost::system::error_code& ec)
|
Chris@16
|
235 {
|
Chris@16
|
236 socket_ops::setsockopt(impl.socket_, impl.state_,
|
Chris@16
|
237 option.level(impl.protocol_), option.name(impl.protocol_),
|
Chris@16
|
238 option.data(impl.protocol_), option.size(impl.protocol_), ec);
|
Chris@16
|
239 return ec;
|
Chris@16
|
240 }
|
Chris@16
|
241
|
Chris@16
|
242 // Set a socket option.
|
Chris@16
|
243 template <typename Option>
|
Chris@16
|
244 boost::system::error_code get_option(const implementation_type& impl,
|
Chris@16
|
245 Option& option, boost::system::error_code& ec) const
|
Chris@16
|
246 {
|
Chris@16
|
247 std::size_t size = option.size(impl.protocol_);
|
Chris@16
|
248 socket_ops::getsockopt(impl.socket_, impl.state_,
|
Chris@16
|
249 option.level(impl.protocol_), option.name(impl.protocol_),
|
Chris@16
|
250 option.data(impl.protocol_), &size, ec);
|
Chris@16
|
251 if (!ec)
|
Chris@16
|
252 option.resize(impl.protocol_, size);
|
Chris@16
|
253 return ec;
|
Chris@16
|
254 }
|
Chris@16
|
255
|
Chris@16
|
256 // Get the local endpoint.
|
Chris@16
|
257 endpoint_type local_endpoint(const implementation_type& impl,
|
Chris@16
|
258 boost::system::error_code& ec) const
|
Chris@16
|
259 {
|
Chris@16
|
260 endpoint_type endpoint;
|
Chris@16
|
261 std::size_t addr_len = endpoint.capacity();
|
Chris@16
|
262 if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
|
Chris@16
|
263 return endpoint_type();
|
Chris@16
|
264 endpoint.resize(addr_len);
|
Chris@16
|
265 return endpoint;
|
Chris@16
|
266 }
|
Chris@16
|
267
|
Chris@16
|
268 // Get the remote endpoint.
|
Chris@16
|
269 endpoint_type remote_endpoint(const implementation_type& impl,
|
Chris@16
|
270 boost::system::error_code& ec) const
|
Chris@16
|
271 {
|
Chris@16
|
272 endpoint_type endpoint = impl.remote_endpoint_;
|
Chris@16
|
273 std::size_t addr_len = endpoint.capacity();
|
Chris@16
|
274 if (socket_ops::getpeername(impl.socket_, endpoint.data(),
|
Chris@16
|
275 &addr_len, impl.have_remote_endpoint_, ec))
|
Chris@16
|
276 return endpoint_type();
|
Chris@16
|
277 endpoint.resize(addr_len);
|
Chris@16
|
278 return endpoint;
|
Chris@16
|
279 }
|
Chris@16
|
280
|
Chris@16
|
281 // Send a datagram to the specified endpoint. Returns the number of bytes
|
Chris@16
|
282 // sent.
|
Chris@16
|
283 template <typename ConstBufferSequence>
|
Chris@16
|
284 size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
|
Chris@16
|
285 const endpoint_type& destination, socket_base::message_flags flags,
|
Chris@16
|
286 boost::system::error_code& ec)
|
Chris@16
|
287 {
|
Chris@16
|
288 buffer_sequence_adapter<boost::asio::const_buffer,
|
Chris@16
|
289 ConstBufferSequence> bufs(buffers);
|
Chris@16
|
290
|
Chris@16
|
291 return socket_ops::sync_sendto(impl.socket_, impl.state_,
|
Chris@16
|
292 bufs.buffers(), bufs.count(), flags,
|
Chris@16
|
293 destination.data(), destination.size(), ec);
|
Chris@16
|
294 }
|
Chris@16
|
295
|
Chris@16
|
296 // Wait until data can be sent without blocking.
|
Chris@16
|
297 size_t send_to(implementation_type& impl, const null_buffers&,
|
Chris@16
|
298 const endpoint_type&, socket_base::message_flags,
|
Chris@16
|
299 boost::system::error_code& ec)
|
Chris@16
|
300 {
|
Chris@16
|
301 // Wait for socket to become ready.
|
Chris@16
|
302 socket_ops::poll_write(impl.socket_, impl.state_, ec);
|
Chris@16
|
303
|
Chris@16
|
304 return 0;
|
Chris@16
|
305 }
|
Chris@16
|
306
|
Chris@16
|
307 // Start an asynchronous send. The data being sent must be valid for the
|
Chris@16
|
308 // lifetime of the asynchronous operation.
|
Chris@16
|
309 template <typename ConstBufferSequence, typename Handler>
|
Chris@16
|
310 void async_send_to(implementation_type& impl,
|
Chris@16
|
311 const ConstBufferSequence& buffers, const endpoint_type& destination,
|
Chris@16
|
312 socket_base::message_flags flags, Handler& handler)
|
Chris@16
|
313 {
|
Chris@16
|
314 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
315 typedef win_iocp_socket_send_op<ConstBufferSequence, Handler> op;
|
Chris@16
|
316 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
317 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
318 sizeof(op), handler), 0 };
|
Chris@16
|
319 p.p = new (p.v) op(impl.cancel_token_, buffers, handler);
|
Chris@16
|
320
|
Chris@16
|
321 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send_to"));
|
Chris@16
|
322
|
Chris@16
|
323 buffer_sequence_adapter<boost::asio::const_buffer,
|
Chris@16
|
324 ConstBufferSequence> bufs(buffers);
|
Chris@16
|
325
|
Chris@16
|
326 start_send_to_op(impl, bufs.buffers(), bufs.count(),
|
Chris@16
|
327 destination.data(), static_cast<int>(destination.size()),
|
Chris@16
|
328 flags, p.p);
|
Chris@16
|
329 p.v = p.p = 0;
|
Chris@16
|
330 }
|
Chris@16
|
331
|
Chris@16
|
332 // Start an asynchronous wait until data can be sent without blocking.
|
Chris@16
|
333 template <typename Handler>
|
Chris@16
|
334 void async_send_to(implementation_type& impl, const null_buffers&,
|
Chris@16
|
335 const endpoint_type&, socket_base::message_flags, Handler& handler)
|
Chris@16
|
336 {
|
Chris@16
|
337 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
338 typedef win_iocp_null_buffers_op<Handler> op;
|
Chris@16
|
339 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
340 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
341 sizeof(op), handler), 0 };
|
Chris@16
|
342 p.p = new (p.v) op(impl.cancel_token_, handler);
|
Chris@16
|
343
|
Chris@16
|
344 BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
|
Chris@16
|
345 &impl, "async_send_to(null_buffers)"));
|
Chris@16
|
346
|
Chris@16
|
347 start_reactor_op(impl, reactor::write_op, p.p);
|
Chris@16
|
348 p.v = p.p = 0;
|
Chris@16
|
349 }
|
Chris@16
|
350
|
Chris@16
|
351 // Receive a datagram with the endpoint of the sender. Returns the number of
|
Chris@16
|
352 // bytes received.
|
Chris@16
|
353 template <typename MutableBufferSequence>
|
Chris@16
|
354 size_t receive_from(implementation_type& impl,
|
Chris@16
|
355 const MutableBufferSequence& buffers,
|
Chris@16
|
356 endpoint_type& sender_endpoint, socket_base::message_flags flags,
|
Chris@16
|
357 boost::system::error_code& ec)
|
Chris@16
|
358 {
|
Chris@16
|
359 buffer_sequence_adapter<boost::asio::mutable_buffer,
|
Chris@16
|
360 MutableBufferSequence> bufs(buffers);
|
Chris@16
|
361
|
Chris@16
|
362 std::size_t addr_len = sender_endpoint.capacity();
|
Chris@16
|
363 std::size_t bytes_recvd = socket_ops::sync_recvfrom(
|
Chris@16
|
364 impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
|
Chris@16
|
365 flags, sender_endpoint.data(), &addr_len, ec);
|
Chris@16
|
366
|
Chris@16
|
367 if (!ec)
|
Chris@16
|
368 sender_endpoint.resize(addr_len);
|
Chris@16
|
369
|
Chris@16
|
370 return bytes_recvd;
|
Chris@16
|
371 }
|
Chris@16
|
372
|
Chris@16
|
373 // Wait until data can be received without blocking.
|
Chris@16
|
374 size_t receive_from(implementation_type& impl,
|
Chris@16
|
375 const null_buffers&, endpoint_type& sender_endpoint,
|
Chris@16
|
376 socket_base::message_flags, boost::system::error_code& ec)
|
Chris@16
|
377 {
|
Chris@16
|
378 // Wait for socket to become ready.
|
Chris@16
|
379 socket_ops::poll_read(impl.socket_, impl.state_, ec);
|
Chris@16
|
380
|
Chris@16
|
381 // Reset endpoint since it can be given no sensible value at this time.
|
Chris@16
|
382 sender_endpoint = endpoint_type();
|
Chris@16
|
383
|
Chris@16
|
384 return 0;
|
Chris@16
|
385 }
|
Chris@16
|
386
|
Chris@16
|
387 // Start an asynchronous receive. The buffer for the data being received and
|
Chris@16
|
388 // the sender_endpoint object must both be valid for the lifetime of the
|
Chris@16
|
389 // asynchronous operation.
|
Chris@16
|
390 template <typename MutableBufferSequence, typename Handler>
|
Chris@16
|
391 void async_receive_from(implementation_type& impl,
|
Chris@16
|
392 const MutableBufferSequence& buffers, endpoint_type& sender_endp,
|
Chris@16
|
393 socket_base::message_flags flags, Handler& handler)
|
Chris@16
|
394 {
|
Chris@16
|
395 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
396 typedef win_iocp_socket_recvfrom_op<
|
Chris@16
|
397 MutableBufferSequence, endpoint_type, Handler> op;
|
Chris@16
|
398 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
399 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
400 sizeof(op), handler), 0 };
|
Chris@16
|
401 p.p = new (p.v) op(sender_endp, impl.cancel_token_, buffers, handler);
|
Chris@16
|
402
|
Chris@16
|
403 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive_from"));
|
Chris@16
|
404
|
Chris@16
|
405 buffer_sequence_adapter<boost::asio::mutable_buffer,
|
Chris@16
|
406 MutableBufferSequence> bufs(buffers);
|
Chris@16
|
407
|
Chris@16
|
408 start_receive_from_op(impl, bufs.buffers(), bufs.count(),
|
Chris@16
|
409 sender_endp.data(), flags, &p.p->endpoint_size(), p.p);
|
Chris@16
|
410 p.v = p.p = 0;
|
Chris@16
|
411 }
|
Chris@16
|
412
|
Chris@16
|
413 // Wait until data can be received without blocking.
|
Chris@16
|
414 template <typename Handler>
|
Chris@16
|
415 void async_receive_from(implementation_type& impl,
|
Chris@16
|
416 const null_buffers&, endpoint_type& sender_endpoint,
|
Chris@16
|
417 socket_base::message_flags flags, Handler& handler)
|
Chris@16
|
418 {
|
Chris@16
|
419 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
420 typedef win_iocp_null_buffers_op<Handler> op;
|
Chris@16
|
421 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
422 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
423 sizeof(op), handler), 0 };
|
Chris@16
|
424 p.p = new (p.v) op(impl.cancel_token_, handler);
|
Chris@16
|
425
|
Chris@16
|
426 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl,
|
Chris@16
|
427 "async_receive_from(null_buffers)"));
|
Chris@16
|
428
|
Chris@16
|
429 // Reset endpoint since it can be given no sensible value at this time.
|
Chris@16
|
430 sender_endpoint = endpoint_type();
|
Chris@16
|
431
|
Chris@16
|
432 start_null_buffers_receive_op(impl, flags, p.p);
|
Chris@16
|
433 p.v = p.p = 0;
|
Chris@16
|
434 }
|
Chris@16
|
435
|
Chris@16
|
436 // Accept a new connection.
|
Chris@16
|
437 template <typename Socket>
|
Chris@16
|
438 boost::system::error_code accept(implementation_type& impl, Socket& peer,
|
Chris@16
|
439 endpoint_type* peer_endpoint, boost::system::error_code& ec)
|
Chris@16
|
440 {
|
Chris@16
|
441 // We cannot accept a socket that is already open.
|
Chris@16
|
442 if (peer.is_open())
|
Chris@16
|
443 {
|
Chris@16
|
444 ec = boost::asio::error::already_open;
|
Chris@16
|
445 return ec;
|
Chris@16
|
446 }
|
Chris@16
|
447
|
Chris@16
|
448 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
|
Chris@16
|
449 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
|
Chris@16
|
450 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
|
Chris@16
|
451 peer_endpoint ? &addr_len : 0, ec));
|
Chris@16
|
452
|
Chris@16
|
453 // On success, assign new connection to peer socket object.
|
Chris@16
|
454 if (new_socket.get() != invalid_socket)
|
Chris@16
|
455 {
|
Chris@16
|
456 if (peer_endpoint)
|
Chris@16
|
457 peer_endpoint->resize(addr_len);
|
Chris@16
|
458 if (!peer.assign(impl.protocol_, new_socket.get(), ec))
|
Chris@16
|
459 new_socket.release();
|
Chris@16
|
460 }
|
Chris@16
|
461
|
Chris@16
|
462 return ec;
|
Chris@16
|
463 }
|
Chris@16
|
464
|
Chris@16
|
465 // Start an asynchronous accept. The peer and peer_endpoint objects
|
Chris@16
|
466 // must be valid until the accept's handler is invoked.
|
Chris@16
|
467 template <typename Socket, typename Handler>
|
Chris@16
|
468 void async_accept(implementation_type& impl, Socket& peer,
|
Chris@16
|
469 endpoint_type* peer_endpoint, Handler& handler)
|
Chris@16
|
470 {
|
Chris@16
|
471 // Allocate and construct an operation to wrap the handler.
|
Chris@16
|
472 typedef win_iocp_socket_accept_op<Socket, protocol_type, Handler> op;
|
Chris@16
|
473 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
474 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
475 sizeof(op), handler), 0 };
|
Chris@16
|
476 bool enable_connection_aborted =
|
Chris@16
|
477 (impl.state_ & socket_ops::enable_connection_aborted) != 0;
|
Chris@16
|
478 p.p = new (p.v) op(*this, impl.socket_, peer, impl.protocol_,
|
Chris@16
|
479 peer_endpoint, enable_connection_aborted, handler);
|
Chris@16
|
480
|
Chris@16
|
481 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_accept"));
|
Chris@16
|
482
|
Chris@16
|
483 start_accept_op(impl, peer.is_open(), p.p->new_socket(),
|
Chris@16
|
484 impl.protocol_.family(), impl.protocol_.type(),
|
Chris@16
|
485 impl.protocol_.protocol(), p.p->output_buffer(),
|
Chris@16
|
486 p.p->address_length(), p.p);
|
Chris@16
|
487 p.v = p.p = 0;
|
Chris@16
|
488 }
|
Chris@16
|
489
|
Chris@16
|
490 // Connect the socket to the specified endpoint.
|
Chris@16
|
491 boost::system::error_code connect(implementation_type& impl,
|
Chris@16
|
492 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
|
Chris@16
|
493 {
|
Chris@16
|
494 socket_ops::sync_connect(impl.socket_,
|
Chris@16
|
495 peer_endpoint.data(), peer_endpoint.size(), ec);
|
Chris@16
|
496 return ec;
|
Chris@16
|
497 }
|
Chris@16
|
498
|
Chris@16
|
499 // Start an asynchronous connect.
|
Chris@16
|
500 template <typename Handler>
|
Chris@16
|
501 void async_connect(implementation_type& impl,
|
Chris@16
|
502 const endpoint_type& peer_endpoint, Handler& handler)
|
Chris@16
|
503 {
|
Chris@16
|
504 // Allocate and construct an operation to wrap the handler.
|
Chris@101
|
505 typedef win_iocp_socket_connect_op<Handler> op;
|
Chris@16
|
506 typename op::ptr p = { boost::asio::detail::addressof(handler),
|
Chris@16
|
507 boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
508 sizeof(op), handler), 0 };
|
Chris@16
|
509 p.p = new (p.v) op(impl.socket_, handler);
|
Chris@16
|
510
|
Chris@16
|
511 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_connect"));
|
Chris@16
|
512
|
Chris@101
|
513 start_connect_op(impl, impl.protocol_.family(), impl.protocol_.type(),
|
Chris@101
|
514 peer_endpoint.data(), static_cast<int>(peer_endpoint.size()), p.p);
|
Chris@16
|
515 p.v = p.p = 0;
|
Chris@16
|
516 }
|
Chris@16
|
517 };
|
Chris@16
|
518
|
Chris@16
|
519 } // namespace detail
|
Chris@16
|
520 } // namespace asio
|
Chris@16
|
521 } // namespace boost
|
Chris@16
|
522
|
Chris@16
|
523 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
524
|
Chris@16
|
525 #endif // defined(BOOST_ASIO_HAS_IOCP)
|
Chris@16
|
526
|
Chris@16
|
527 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP
|