Realistic 3D camera system
3D camera system components
stream_socket_service.hpp
Go to the documentation of this file.
1 //
2 // stream_socket_service.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 SERVICES_STREAM_SOCKET_SERVICE_HPP
12 #define SERVICES_STREAM_SOCKET_SERVICE_HPP
13 
14 #include <asio.hpp>
15 #include <boost/noncopyable.hpp>
16 #include <boost/lexical_cast.hpp>
17 #include "logger.hpp"
18 
19 namespace services {
20 
22 template <typename Protocol>
25 {
26 private:
29 
30 public:
33 
35  typedef Protocol protocol_type;
36 
38  typedef typename Protocol::endpoint endpoint_type;
39 
42 
45 
48  : asio::io_service::service(io_service),
49  service_impl_(asio::use_service<service_impl_type>(io_service)),
50  logger_(io_service, "stream_socket")
51  {
52  }
53 
56  {
57  }
58 
60  void construct(implementation_type& impl)
61  {
62  service_impl_.construct(impl);
63  }
64 
66  void destroy(implementation_type& impl)
67  {
68  service_impl_.destroy(impl);
69  }
70 
72  asio::error_code open(implementation_type& impl,
73  const protocol_type& protocol, asio::error_code& ec)
74  {
75  logger_.log("Opening new socket");
76  return service_impl_.open(impl, protocol, ec);
77  }
78 
80  asio::error_code assign(implementation_type& impl,
81  const protocol_type& protocol, const native_handle_type& native_socket,
82  asio::error_code& ec)
83  {
84  logger_.log("Assigning from a native socket");
85  return service_impl_.assign(impl, protocol, native_socket, ec);
86  }
87 
89  bool is_open(const implementation_type& impl) const
90  {
91  logger_.log("Checking if socket is open");
92  return service_impl_.is_open(impl);
93  }
94 
96  asio::error_code close(implementation_type& impl,
97  asio::error_code& ec)
98  {
99  logger_.log("Closing socket");
100  return service_impl_.close(impl, ec);
101  }
102 
104  bool at_mark(const implementation_type& impl,
105  asio::error_code& ec) const
106  {
107  logger_.log("Checking if socket is at out-of-band data mark");
108  return service_impl_.at_mark(impl, ec);
109  }
110 
112  std::size_t available(const implementation_type& impl,
113  asio::error_code& ec) const
114  {
115  logger_.log("Determining number of bytes available for reading");
116  return service_impl_.available(impl, ec);
117  }
118 
120  asio::error_code bind(implementation_type& impl,
121  const endpoint_type& endpoint, asio::error_code& ec)
122  {
123  logger_.log("Binding socket");
124  return service_impl_.bind(impl, endpoint, ec);
125  }
126 
128  asio::error_code connect(implementation_type& impl,
129  const endpoint_type& peer_endpoint, asio::error_code& ec)
130  {
131  logger_.log("Connecting socket to " +
132  boost::lexical_cast<std::string>(peer_endpoint));
133  return service_impl_.connect(impl, peer_endpoint, ec);
134  }
135 
137  template <typename Handler>
139  {
140  public:
141  connect_handler(Handler h, logger& l)
142  : handler_(h),
143  logger_(l)
144  {
145  }
146 
148  {
149  if (e)
150  {
151  std::string msg = "Asynchronous connect failed: ";
152  msg += e.message();
153  logger_.log(msg);
154  }
155  else
156  {
157  logger_.log("Asynchronous connect succeeded");
158  }
159 
160  handler_(e);
161  }
162 
163  private:
164  Handler handler_;
165  logger& logger_;
166  };
167 
169  template <typename Handler>
170  void async_connect(implementation_type& impl,
171  const endpoint_type& peer_endpoint, Handler handler)
172  {
173  logger_.log("Starting asynchronous connect to " +
174  boost::lexical_cast<std::string>(peer_endpoint));
175  service_impl_.async_connect(impl, peer_endpoint,
176  connect_handler<Handler>(handler, logger_));
177  }
178 
180  template <typename Option>
181  asio::error_code set_option(implementation_type& impl,
182  const Option& option, asio::error_code& ec)
183  {
184  logger_.log("Setting socket option");
185  return service_impl_.set_option(impl, option, ec);
186  }
187 
189  template <typename Option>
190  asio::error_code get_option(const implementation_type& impl,
191  Option& option, asio::error_code& ec) const
192  {
193  logger_.log("Getting socket option");
194  return service_impl_.get_option(impl, option, ec);
195  }
196 
198  template <typename IO_Control_Command>
199  asio::error_code io_control(implementation_type& impl,
200  IO_Control_Command& command, asio::error_code& ec)
201  {
202  logger_.log("Performing IO control command on socket");
203  return service_impl_.io_control(impl, command, ec);
204  }
205 
207  endpoint_type local_endpoint(const implementation_type& impl,
208  asio::error_code& ec) const
209  {
210  logger_.log("Getting socket's local endpoint");
211  return service_impl_.local_endpoint(impl, ec);
212  }
213 
215  endpoint_type remote_endpoint(const implementation_type& impl,
216  asio::error_code& ec) const
217  {
218  logger_.log("Getting socket's remote endpoint");
219  return service_impl_.remote_endpoint(impl, ec);
220  }
221 
223  asio::error_code shutdown(implementation_type& impl,
225  asio::error_code& ec)
226  {
227  logger_.log("Shutting down socket");
228  return service_impl_.shutdown(impl, what, ec);
229  }
230 
232  template <typename Const_Buffers>
233  std::size_t send(implementation_type& impl, const Const_Buffers& buffers,
235  asio::error_code& ec)
236  {
237  logger_.log("Sending data on socket");
238  return service_impl_.send(impl, buffers, flags, ec);
239  }
240 
242  template <typename Handler>
244  {
245  public:
246  send_handler(Handler h, logger& l)
247  : handler_(h),
248  logger_(l)
249  {
250  }
251 
253  std::size_t bytes_transferred)
254  {
255  if (e)
256  {
257  std::string msg = "Asynchronous send failed: ";
258  msg += e.message();
259  logger_.log(msg);
260  }
261  else
262  {
263  logger_.log("Asynchronous send succeeded");
264  }
265 
266  handler_(e, bytes_transferred);
267  }
268 
269  private:
270  Handler handler_;
271  logger& logger_;
272  };
273 
275  template <typename Const_Buffers, typename Handler>
276  void async_send(implementation_type& impl, const Const_Buffers& buffers,
277  asio::socket_base::message_flags flags, Handler handler)
278  {
279  logger_.log("Starting asynchronous send");
280  service_impl_.async_send(impl, buffers, flags,
281  send_handler<Handler>(handler, logger_));
282  }
283 
285  template <typename Mutable_Buffers>
286  std::size_t receive(implementation_type& impl,
287  const Mutable_Buffers& buffers,
289  asio::error_code& ec)
290  {
291  logger_.log("Receiving data on socket");
292  return service_impl_.receive(impl, buffers, flags, ec);
293  }
294 
296  template <typename Handler>
298  {
299  public:
300  receive_handler(Handler h, logger& l)
301  : handler_(h),
302  logger_(l)
303  {
304  }
305 
307  std::size_t bytes_transferred)
308  {
309  if (e)
310  {
311  std::string msg = "Asynchronous receive failed: ";
312  msg += e.message();
313  logger_.log(msg);
314  }
315  else
316  {
317  logger_.log("Asynchronous receive succeeded");
318  }
319 
320  handler_(e, bytes_transferred);
321  }
322 
323  private:
324  Handler handler_;
325  logger& logger_;
326  };
327 
329  template <typename Mutable_Buffers, typename Handler>
330  void async_receive(implementation_type& impl, const Mutable_Buffers& buffers,
331  asio::socket_base::message_flags flags, Handler handler)
332  {
333  logger_.log("Starting asynchronous receive");
334  service_impl_.async_receive(impl, buffers, flags,
335  receive_handler<Handler>(handler, logger_));
336  }
337 
338 private:
340  service_impl_type& service_impl_;
341 
343  mutable logger logger_;
344 };
345 
346 template <typename Protocol>
348 
349 } // namespace services
350 
351 #endif // SERVICES_STREAM_SOCKET_SERVICE_HPP
int message_flags
Bitmask type for flags that can be passed to send and receive operations.
Definition: socket_base.hpp:53
void construct(implementation_type &impl)
Construct a new stream socket implementation.
void async_receive(implementation_type &impl, const Mutable_Buffers &buffers, asio::socket_base::message_flags flags, Handler handler)
Start an asynchronous receive.
asio::error_code open(implementation_type &impl, const protocol_type &protocol, asio::error_code &ec)
Open a new stream socket implementation.
bool at_mark(const implementation_type &impl, asio::error_code &ec) const
Determine whether the socket is at the out-of-band data mark.
endpoint_type local_endpoint(const implementation_type &impl, asio::error_code &ec) const
Get the local endpoint.
Class used to uniquely identify a service.
Definition: io_service.hpp:665
Provides core I/O functionality.
Definition: io_service.hpp:184
asio::error_code bind(implementation_type &impl, const endpoint_type &endpoint, asio::error_code &ec)
Bind the stream socket to the specified local endpoint.
std::size_t available(const implementation_type &impl, asio::error_code &ec) const
Determine the number of bytes available for reading.
stream_socket_service(asio::io_service &io_service)
Construct a new stream socket service for the specified io_service.
bool is_open(const implementation_type &impl) const
Determine whether the socket is open.
asio::error_code assign(implementation_type &impl, const protocol_type &protocol, const native_handle_type &native_socket, asio::error_code &ec)
Open a stream socket from an existing native socket.
Protocol::endpoint endpoint_type
The endpoint type.
pylon Camera Software Suite for Linux for Use with Basler Gigabit the pylon Viewer and the IP Configurator applications might not be available System I340 and I350 series Although the pylon software will work with any GigE network we would recommend to use one of these adapters USB For U3V devices a USB3 capable USB controller is necessary For best performance and stability we highly recommend a kernel the following settings should be i e
asio::error_code assign(implementation_type &impl, const protocol_type &protocol, const native_handle_type &native_socket, asio::error_code &ec)
Assign an existing native socket to a stream socket.
Default service implementation for a stream socket.
bool at_mark(const implementation_type &impl, asio::error_code &ec) const
Determine whether the socket is at the out-of-band data mark.
void destroy(implementation_type &impl)
Destroy a stream socket implementation.
Service & use_service(io_service &ios)
Definition: io_service.hpp:26
ASIO_DECL service(asio::io_service &owner)
Constructor.
Definition: io_service.ipp:127
endpoint_type remote_endpoint(const implementation_type &impl, asio::error_code &ec) const
Get the remote endpoint.
std::size_t receive(implementation_type &impl, const Mutable_Buffers &buffers, asio::socket_base::message_flags flags, asio::error_code &ec)
Receive some data from the peer.
bool is_open(const implementation_type &impl) const
Determine whether the socket is open.
const MutableBufferSequence & buffers
Definition: read.hpp:521
std::size_t send(implementation_type &impl, const Const_Buffers &buffers, asio::socket_base::message_flags flags, asio::error_code &ec)
Send the given data to the peer.
asio::error_code close(implementation_type &impl, asio::error_code &ec)
Close a stream socket implementation.
void destroy(implementation_type &impl)
Destroy a stream socket implementation.
asio::error_code connect(implementation_type &impl, const endpoint_type &peer_endpoint, asio::error_code &ec)
Connect the stream socket to the specified endpoint.
service_impl_type::native_handle_type native_handle_type
The native type of a stream socket.
asio::error_code connect(implementation_type &impl, const endpoint_type &peer_endpoint, asio::error_code &ec)
Connect the stream socket to the specified endpoint.
static asio::io_service::id id
The unique service identifier.
asio::error_code shutdown(implementation_type &impl, asio::socket_base::shutdown_type what, asio::error_code &ec)
Disable sends or receives on the socket.
shutdown_type
Different ways a socket may be shutdown.
Definition: socket_base.hpp:34
asio::error_code open(implementation_type &impl, const protocol_type &protocol, asio::error_code &ec)
Open a stream socket.
void log(const std::string &message)
Log a message.
asio::error_code close(implementation_type &impl, asio::error_code &ec)
Close a stream socket implementation.
Class to represent an error code value.
Definition: error_code.hpp:80
asio::error_code get_option(const implementation_type &impl, Option &option, asio::error_code &ec) const
Get a socket option.
asio::error_code set_option(implementation_type &impl, const Option &option, asio::error_code &ec)
Set a socket option.
service_impl_type::implementation_type implementation_type
The implementation type of a stream socket.
service_impl_type::native_handle_type native_handle_type
The native socket type.
void construct(implementation_type &impl)
Construct a new stream socket implementation.
Debugging stream socket service that wraps the normal stream socket service.
Handler to wrap asynchronous connect completion.
asio::error_code bind(implementation_type &impl, const endpoint_type &endpoint, asio::error_code &ec)
Bind the stream socket to the specified local endpoint.
Handler to wrap asynchronous send completion.
std::string message() const
Get the message associated with the error.
Definition: error_code.hpp:117
void operator()(const asio::error_code &e, std::size_t bytes_transferred)
void async_send(implementation_type &impl, const Const_Buffers &buffers, asio::socket_base::message_flags flags, Handler handler)
Start an asynchronous send.
Handler to wrap asynchronous receive completion.
asio::error_code io_control(implementation_type &impl, IO_Control_Command &command, asio::error_code &ec)
Perform an IO control command on the socket.
std::size_t available(const implementation_type &impl, asio::error_code &ec) const
Determine the number of bytes available for reading.
Base class for all io_service services.
Definition: io_service.hpp:674
Protocol protocol_type
The protocol type.
void shutdown_service()
Destroy all user-defined handler objects owned by the service.
void async_connect(implementation_type &impl, const endpoint_type &peer_endpoint, Handler handler)
Start an asynchronous connect.
void operator()(const asio::error_code &e, std::size_t bytes_transferred)