Realistic 3D camera system
3D camera system components
winrt_ssocket_service_base.hpp
Go to the documentation of this file.
1 //
2 // detail/winrt_ssocket_service_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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 #ifndef ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP
12 #define ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 
20 #if defined(ASIO_WINDOWS_RUNTIME)
21 
22 #include "asio/buffer.hpp"
23 #include "asio/error.hpp"
24 #include "asio/io_service.hpp"
25 #include "asio/socket_base.hpp"
32 
34 
35 namespace asio {
36 namespace detail {
37 
38 class winrt_ssocket_service_base
39 {
40 public:
41  // The native type of a socket.
42  typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type;
43 
44  // The implementation type of the socket.
45  struct base_implementation_type
46  {
47  // Default constructor.
48  base_implementation_type()
49  : socket_(nullptr),
50  next_(0),
51  prev_(0)
52  {
53  }
54 
55  // The underlying native socket.
56  native_handle_type socket_;
57 
58  // Pointers to adjacent socket implementations in linked list.
59  base_implementation_type* next_;
60  base_implementation_type* prev_;
61  };
62 
63  // Constructor.
64  ASIO_DECL winrt_ssocket_service_base(
65  asio::io_service& io_service);
66 
67  // Destroy all user-defined handler objects owned by the service.
68  ASIO_DECL void shutdown_service();
69 
70  // Construct a new socket implementation.
71  ASIO_DECL void construct(base_implementation_type&);
72 
73  // Move-construct a new socket implementation.
74  ASIO_DECL void base_move_construct(base_implementation_type& impl,
75  base_implementation_type& other_impl);
76 
77  // Move-assign from another socket implementation.
78  ASIO_DECL void base_move_assign(base_implementation_type& impl,
79  winrt_ssocket_service_base& other_service,
80  base_implementation_type& other_impl);
81 
82  // Destroy a socket implementation.
83  ASIO_DECL void destroy(base_implementation_type& impl);
84 
85  // Determine whether the socket is open.
86  bool is_open(const base_implementation_type& impl) const
87  {
88  return impl.socket_ != nullptr;
89  }
90 
91  // Destroy a socket implementation.
93  base_implementation_type& impl, asio::error_code& ec);
94 
95  // Get the native socket representation.
96  native_handle_type native_handle(base_implementation_type& impl)
97  {
98  return impl.socket_;
99  }
100 
101  // Cancel all operations associated with the socket.
102  asio::error_code cancel(base_implementation_type&,
103  asio::error_code& ec)
104  {
106  return ec;
107  }
108 
109  // Determine whether the socket is at the out-of-band data mark.
110  bool at_mark(const base_implementation_type&,
111  asio::error_code& ec) const
112  {
114  return false;
115  }
116 
117  // Determine the number of bytes available for reading.
118  std::size_t available(const base_implementation_type&,
119  asio::error_code& ec) const
120  {
122  return 0;
123  }
124 
125  // Perform an IO control command on the socket.
126  template <typename IO_Control_Command>
127  asio::error_code io_control(base_implementation_type&,
128  IO_Control_Command&, asio::error_code& ec)
129  {
131  return ec;
132  }
133 
134  // Gets the non-blocking mode of the socket.
135  bool non_blocking(const base_implementation_type&) const
136  {
137  return false;
138  }
139 
140  // Sets the non-blocking mode of the socket.
141  asio::error_code non_blocking(base_implementation_type&,
142  bool, asio::error_code& ec)
143  {
145  return ec;
146  }
147 
148  // Gets the non-blocking mode of the native socket implementation.
149  bool native_non_blocking(const base_implementation_type&) const
150  {
151  return false;
152  }
153 
154  // Sets the non-blocking mode of the native socket implementation.
155  asio::error_code native_non_blocking(base_implementation_type&,
156  bool, asio::error_code& ec)
157  {
159  return ec;
160  }
161 
162  // Disable sends or receives on the socket.
163  asio::error_code shutdown(base_implementation_type&,
165  {
167  return ec;
168  }
169 
170  // Send the given data to the peer.
171  template <typename ConstBufferSequence>
172  std::size_t send(base_implementation_type& impl,
173  const ConstBufferSequence& buffers,
175  {
176  return do_send(impl,
177  buffer_sequence_adapter<asio::const_buffer,
178  ConstBufferSequence>::first(buffers), flags, ec);
179  }
180 
181  // Wait until data can be sent without blocking.
182  std::size_t send(base_implementation_type&, const null_buffers&,
184  {
186  return 0;
187  }
188 
189  // Start an asynchronous send. The data being sent must be valid for the
190  // lifetime of the asynchronous operation.
191  template <typename ConstBufferSequence, typename Handler>
192  void async_send(base_implementation_type& impl,
193  const ConstBufferSequence& buffers,
194  socket_base::message_flags flags, Handler& handler)
195  {
196  bool is_continuation =
198 
199  // Allocate and construct an operation to wrap the handler.
200  typedef winrt_socket_send_op<ConstBufferSequence, Handler> op;
201  typename op::ptr p = { asio::detail::addressof(handler),
203  sizeof(op), handler), 0 };
204  p.p = new (p.v) op(buffers, handler);
205 
206  ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send"));
207 
208  start_send_op(impl,
209  buffer_sequence_adapter<asio::const_buffer,
210  ConstBufferSequence>::first(buffers),
211  flags, p.p, is_continuation);
212  p.v = p.p = 0;
213  }
214 
215  // Start an asynchronous wait until data can be sent without blocking.
216  template <typename Handler>
217  void async_send(base_implementation_type&, const null_buffers&,
218  socket_base::message_flags, Handler& handler)
219  {
221  const std::size_t bytes_transferred = 0;
222  io_service_.get_io_service().post(
223  detail::bind_handler(handler, ec, bytes_transferred));
224  }
225 
226  // Receive some data from the peer. Returns the number of bytes received.
227  template <typename MutableBufferSequence>
228  std::size_t receive(base_implementation_type& impl,
229  const MutableBufferSequence& buffers,
231  {
232  return do_receive(impl,
233  buffer_sequence_adapter<asio::mutable_buffer,
234  MutableBufferSequence>::first(buffers), flags, ec);
235  }
236 
237  // Wait until data can be received without blocking.
238  std::size_t receive(base_implementation_type&, const null_buffers&,
240  {
242  return 0;
243  }
244 
245  // Start an asynchronous receive. The buffer for the data being received
246  // must be valid for the lifetime of the asynchronous operation.
247  template <typename MutableBufferSequence, typename Handler>
248  void async_receive(base_implementation_type& impl,
249  const MutableBufferSequence& buffers,
250  socket_base::message_flags flags, Handler& handler)
251  {
252  bool is_continuation =
254 
255  // Allocate and construct an operation to wrap the handler.
256  typedef winrt_socket_recv_op<MutableBufferSequence, Handler> op;
257  typename op::ptr p = { asio::detail::addressof(handler),
259  sizeof(op), handler), 0 };
260  p.p = new (p.v) op(buffers, handler);
261 
262  ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive"));
263 
264  start_receive_op(impl,
265  buffer_sequence_adapter<asio::mutable_buffer,
266  MutableBufferSequence>::first(buffers),
267  flags, p.p, is_continuation);
268  p.v = p.p = 0;
269  }
270 
271  // Wait until data can be received without blocking.
272  template <typename Handler>
273  void async_receive(base_implementation_type&, const null_buffers&,
274  socket_base::message_flags, Handler& handler)
275  {
277  const std::size_t bytes_transferred = 0;
278  io_service_.get_io_service().post(
279  detail::bind_handler(handler, ec, bytes_transferred));
280  }
281 
282 protected:
283  // Helper function to obtain endpoints associated with the connection.
284  ASIO_DECL std::size_t do_get_endpoint(
285  const base_implementation_type& impl, bool local,
286  void* addr, std::size_t addr_len, asio::error_code& ec) const;
287 
288  // Helper function to set a socket option.
289  ASIO_DECL asio::error_code do_set_option(
290  base_implementation_type& impl,
291  int level, int optname, const void* optval,
292  std::size_t optlen, asio::error_code& ec);
293 
294  // Helper function to get a socket option.
295  ASIO_DECL void do_get_option(
296  const base_implementation_type& impl,
297  int level, int optname, void* optval,
298  std::size_t* optlen, asio::error_code& ec) const;
299 
300  // Helper function to perform a synchronous connect.
301  ASIO_DECL asio::error_code do_connect(
302  base_implementation_type& impl,
303  const void* addr, asio::error_code& ec);
304 
305  // Helper function to start an asynchronous connect.
306  ASIO_DECL void start_connect_op(
307  base_implementation_type& impl, const void* addr,
308  winrt_async_op<void>* op, bool is_continuation);
309 
310  // Helper function to perform a synchronous send.
311  ASIO_DECL std::size_t do_send(
312  base_implementation_type& impl, const asio::const_buffer& data,
314 
315  // Helper function to start an asynchronous send.
316  ASIO_DECL void start_send_op(base_implementation_type& impl,
318  winrt_async_op<unsigned int>* op, bool is_continuation);
319 
320  // Helper function to perform a synchronous receive.
321  ASIO_DECL std::size_t do_receive(
322  base_implementation_type& impl, const asio::mutable_buffer& data,
324 
325  // Helper function to start an asynchronous receive.
326  ASIO_DECL void start_receive_op(base_implementation_type& impl,
328  winrt_async_op<Windows::Storage::Streams::IBuffer^>* op,
329  bool is_continuation);
330 
331  // The io_service implementation used for delivering completions.
332  io_service_impl& io_service_;
333 
334  // The manager that keeps track of outstanding operations.
335  winrt_async_manager& async_manager_;
336 
337  // Mutex to protect access to the linked list of implementations.
338  asio::detail::mutex mutex_;
339 
340  // The head of a linked list of all implementations.
341  base_implementation_type* impl_list_;
342 };
343 
344 } // namespace detail
345 } // namespace asio
346 
348 
349 #if defined(ASIO_HEADER_ONLY)
351 #endif // defined(ASIO_HEADER_ONLY)
352 
353 #endif // defined(ASIO_WINDOWS_RUNTIME)
354 
355 #endif // ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP
int message_flags
Bitmask type for flags that can be passed to send and receive operations.
Definition: socket_base.hpp:53
Provides core I/O functionality.
Definition: io_service.hpp:184
Holds a buffer that cannot be modified.
Definition: buffer.hpp:211
class task_io_service io_service_impl
Definition: io_service.hpp:48
const MutableBufferSequence & buffers
Definition: read.hpp:521
signed_size_type send(socket_type s, const buf *bufs, size_t count, int flags, asio::error_code &ec)
Holds a buffer that can be modified.
Definition: buffer.hpp:91
shutdown_type
Different ways a socket may be shutdown.
Definition: socket_base.hpp:34
size_t available(socket_type s, asio::error_code &ec)
Definition: socket_ops.ipp:660
bool is_continuation(Context &context)
Class to represent an error code value.
Definition: error_code.hpp:80
#define ASIO_DECL
Definition: config.hpp:43
ASIO_DECL int close(int d, state_type &state, asio::error_code &ec)
void * allocate(std::size_t s, Handler &h)
int shutdown(socket_type s, int what, asio::error_code &ec)
Definition: socket_ops.ipp:452
binder1< Handler, Arg1 > bind_handler(Handler handler, const Arg1 &arg1)
#define ASIO_HANDLER_CREATION(args)
Operation not supported.
Definition: error.hpp:166