~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to portable/libtorrent/include/libtorrent/asio/basic_socket.hpp

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// basic_socket.hpp
3
 
// ~~~~~~~~~~~~~~~~
4
 
//
5
 
// Copyright (c) 2003-2008 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_BASIC_SOCKET_HPP
12
 
#define ASIO_BASIC_SOCKET_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/push_options.hpp"
19
 
 
20
 
#include "asio/detail/push_options.hpp"
21
 
#include <boost/config.hpp>
22
 
#include "asio/detail/pop_options.hpp"
23
 
 
24
 
#include "asio/basic_io_object.hpp"
25
 
#include "asio/error.hpp"
26
 
#include "asio/socket_base.hpp"
27
 
#include "asio/detail/throw_error.hpp"
28
 
 
29
 
namespace asio {
30
 
 
31
 
/// Provides socket functionality.
32
 
/**
33
 
 * The basic_socket class template provides functionality that is common to both
34
 
 * stream-oriented and datagram-oriented sockets.
35
 
 *
36
 
 * @par Thread Safety
37
 
 * @e Distinct @e objects: Safe.@n
38
 
 * @e Shared @e objects: Unsafe.
39
 
 */
40
 
template <typename Protocol, typename SocketService>
41
 
class basic_socket
42
 
  : public basic_io_object<SocketService>,
43
 
    public socket_base
44
 
{
45
 
public:
46
 
  /// The native representation of a socket.
47
 
  typedef typename SocketService::native_type native_type;
48
 
 
49
 
  /// The protocol type.
50
 
  typedef Protocol protocol_type;
51
 
 
52
 
  /// The endpoint type.
53
 
  typedef typename Protocol::endpoint endpoint_type;
54
 
 
55
 
  /// A basic_socket is always the lowest layer.
56
 
  typedef basic_socket<Protocol, SocketService> lowest_layer_type;
57
 
 
58
 
  /// Construct a basic_socket without opening it.
59
 
  /**
60
 
   * This constructor creates a socket without opening it.
61
 
   *
62
 
   * @param io_service The io_service object that the socket will use to
63
 
   * dispatch handlers for any asynchronous operations performed on the socket.
64
 
   */
65
 
  explicit basic_socket(asio::io_service& io_service)
66
 
    : basic_io_object<SocketService>(io_service)
67
 
  {
68
 
  }
69
 
 
70
 
  /// Construct and open a basic_socket.
71
 
  /**
72
 
   * This constructor creates and opens a socket.
73
 
   *
74
 
   * @param io_service The io_service object that the socket will use to
75
 
   * dispatch handlers for any asynchronous operations performed on the socket.
76
 
   *
77
 
   * @param protocol An object specifying protocol parameters to be used.
78
 
   *
79
 
   * @throws asio::system_error Thrown on failure.
80
 
   */
81
 
  basic_socket(asio::io_service& io_service,
82
 
      const protocol_type& protocol)
83
 
    : basic_io_object<SocketService>(io_service)
84
 
  {
85
 
    asio::error_code ec;
86
 
    this->service.open(this->implementation, protocol, ec);
87
 
    asio::detail::throw_error(ec);
88
 
  }
89
 
 
90
 
  /// Construct a basic_socket, opening it and binding it to the given local
91
 
  /// endpoint.
92
 
  /**
93
 
   * This constructor creates a socket and automatically opens it bound to the
94
 
   * specified endpoint on the local machine. The protocol used is the protocol
95
 
   * associated with the given endpoint.
96
 
   *
97
 
   * @param io_service The io_service object that the socket will use to
98
 
   * dispatch handlers for any asynchronous operations performed on the socket.
99
 
   *
100
 
   * @param endpoint An endpoint on the local machine to which the socket will
101
 
   * be bound.
102
 
   *
103
 
   * @throws asio::system_error Thrown on failure.
104
 
   */
105
 
  basic_socket(asio::io_service& io_service,
106
 
      const endpoint_type& endpoint)
107
 
    : basic_io_object<SocketService>(io_service)
108
 
  {
109
 
    asio::error_code ec;
110
 
    this->service.open(this->implementation, endpoint.protocol(), ec);
111
 
    asio::detail::throw_error(ec);
112
 
    this->service.bind(this->implementation, endpoint, ec);
113
 
    asio::detail::throw_error(ec);
114
 
  }
115
 
 
116
 
  /// Construct a basic_socket on an existing native socket.
117
 
  /**
118
 
   * This constructor creates a socket object to hold an existing native socket.
119
 
   *
120
 
   * @param io_service The io_service object that the socket will use to
121
 
   * dispatch handlers for any asynchronous operations performed on the socket.
122
 
   *
123
 
   * @param protocol An object specifying protocol parameters to be used.
124
 
   *
125
 
   * @param native_socket A native socket.
126
 
   *
127
 
   * @throws asio::system_error Thrown on failure.
128
 
   */
129
 
  basic_socket(asio::io_service& io_service,
130
 
      const protocol_type& protocol, const native_type& native_socket)
131
 
    : basic_io_object<SocketService>(io_service)
132
 
  {
133
 
    asio::error_code ec;
134
 
    this->service.assign(this->implementation, protocol, native_socket, ec);
135
 
    asio::detail::throw_error(ec);
136
 
  }
137
 
 
138
 
  /// Get a reference to the lowest layer.
139
 
  /**
140
 
   * This function returns a reference to the lowest layer in a stack of
141
 
   * layers. Since a basic_socket cannot contain any further layers, it simply
142
 
   * returns a reference to itself.
143
 
   *
144
 
   * @return A reference to the lowest layer in the stack of layers. Ownership
145
 
   * is not transferred to the caller.
146
 
   */
147
 
  lowest_layer_type& lowest_layer()
148
 
  {
149
 
    return *this;
150
 
  }
151
 
 
152
 
  /// Open the socket using the specified protocol.
153
 
  /**
154
 
   * This function opens the socket so that it will use the specified protocol.
155
 
   *
156
 
   * @param protocol An object specifying protocol parameters to be used.
157
 
   *
158
 
   * @throws asio::system_error Thrown on failure.
159
 
   *
160
 
   * @par Example
161
 
   * @code
162
 
   * asio::ip::tcp::socket socket(io_service);
163
 
   * socket.open(asio::ip::tcp::v4());
164
 
   * @endcode
165
 
   */
166
 
  void open(const protocol_type& protocol = protocol_type())
167
 
  {
168
 
    asio::error_code ec;
169
 
    this->service.open(this->implementation, protocol, ec);
170
 
    asio::detail::throw_error(ec);
171
 
  }
172
 
 
173
 
  /// Open the socket using the specified protocol.
174
 
  /**
175
 
   * This function opens the socket so that it will use the specified protocol.
176
 
   *
177
 
   * @param protocol An object specifying which protocol is to be used.
178
 
   *
179
 
   * @param ec Set to indicate what error occurred, if any.
180
 
   *
181
 
   * @par Example
182
 
   * @code
183
 
   * asio::ip::tcp::socket socket(io_service);
184
 
   * asio::error_code ec;
185
 
   * socket.open(asio::ip::tcp::v4(), ec);
186
 
   * if (ec)
187
 
   * {
188
 
   *   // An error occurred.
189
 
   * }
190
 
   * @endcode
191
 
   */
192
 
  asio::error_code open(const protocol_type& protocol,
193
 
      asio::error_code& ec)
194
 
  {
195
 
    return this->service.open(this->implementation, protocol, ec);
196
 
  }
197
 
 
198
 
  /// Assign an existing native socket to the socket.
199
 
  /*
200
 
   * This function opens the socket to hold an existing native socket.
201
 
   *
202
 
   * @param protocol An object specifying which protocol is to be used.
203
 
   *
204
 
   * @param native_socket A native socket.
205
 
   *
206
 
   * @throws asio::system_error Thrown on failure.
207
 
   */
208
 
  void assign(const protocol_type& protocol, const native_type& native_socket)
209
 
  {
210
 
    asio::error_code ec;
211
 
    this->service.assign(this->implementation, protocol, native_socket, ec);
212
 
    asio::detail::throw_error(ec);
213
 
  }
214
 
 
215
 
  /// Assign an existing native socket to the socket.
216
 
  /*
217
 
   * This function opens the socket to hold an existing native socket.
218
 
   *
219
 
   * @param protocol An object specifying which protocol is to be used.
220
 
   *
221
 
   * @param native_socket A native socket.
222
 
   *
223
 
   * @param ec Set to indicate what error occurred, if any.
224
 
   */
225
 
  asio::error_code assign(const protocol_type& protocol,
226
 
      const native_type& native_socket, asio::error_code& ec)
227
 
  {
228
 
    return this->service.assign(this->implementation,
229
 
        protocol, native_socket, ec);
230
 
  }
231
 
 
232
 
  /// Determine whether the socket is open.
233
 
  bool is_open() const
234
 
  {
235
 
    return this->service.is_open(this->implementation);
236
 
  }
237
 
 
238
 
  /// Close the socket.
239
 
  /**
240
 
   * This function is used to close the socket. Any asynchronous send, receive
241
 
   * or connect operations will be cancelled immediately, and will complete
242
 
   * with the asio::error::operation_aborted error.
243
 
   *
244
 
   * @throws asio::system_error Thrown on failure.
245
 
   *
246
 
   * @note For portable behaviour with respect to graceful closure of a
247
 
   * connected socket, call shutdown() before closing the socket.
248
 
   */
249
 
  void close()
250
 
  {
251
 
    asio::error_code ec;
252
 
    this->service.close(this->implementation, ec);
253
 
    asio::detail::throw_error(ec);
254
 
  }
255
 
 
256
 
  /// Close the socket.
257
 
  /**
258
 
   * This function is used to close the socket. Any asynchronous send, receive
259
 
   * or connect operations will be cancelled immediately, and will complete
260
 
   * with the asio::error::operation_aborted error.
261
 
   *
262
 
   * @param ec Set to indicate what error occurred, if any.
263
 
   *
264
 
   * @par Example
265
 
   * @code
266
 
   * asio::ip::tcp::socket socket(io_service);
267
 
   * ...
268
 
   * asio::error_code ec;
269
 
   * socket.close(ec);
270
 
   * if (ec)
271
 
   * {
272
 
   *   // An error occurred.
273
 
   * }
274
 
   * @endcode
275
 
   *
276
 
   * @note For portable behaviour with respect to graceful closure of a
277
 
   * connected socket, call shutdown() before closing the socket.
278
 
   */
279
 
  asio::error_code close(asio::error_code& ec)
280
 
  {
281
 
    return this->service.close(this->implementation, ec);
282
 
  }
283
 
 
284
 
  /// Get the native socket representation.
285
 
  /**
286
 
   * This function may be used to obtain the underlying representation of the
287
 
   * socket. This is intended to allow access to native socket functionality
288
 
   * that is not otherwise provided.
289
 
   */
290
 
  native_type native()
291
 
  {
292
 
    return this->service.native(this->implementation);
293
 
  }
294
 
 
295
 
  /// Cancel all asynchronous operations associated with the socket.
296
 
  /**
297
 
   * This function causes all outstanding asynchronous connect, send and receive
298
 
   * operations to finish immediately, and the handlers for cancelled operations
299
 
   * will be passed the asio::error::operation_aborted error.
300
 
   *
301
 
   * @throws asio::system_error Thrown on failure.
302
 
   *
303
 
   * @note Calls to cancel() will always fail with
304
 
   * asio::error::operation_not_supported when run on Windows XP, Windows
305
 
   * Server 2003, and earlier versions of Windows, unless
306
 
   * ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
307
 
   * two issues that should be considered before enabling its use:
308
 
   *
309
 
   * @li It will only cancel asynchronous operations that were initiated in the
310
 
   * current thread.
311
 
   *
312
 
   * @li It can appear to complete without error, but the request to cancel the
313
 
   * unfinished operations may be silently ignored by the operating system.
314
 
   * Whether it works or not seems to depend on the drivers that are installed.
315
 
   *
316
 
   * For portable cancellation, consider using one of the following
317
 
   * alternatives:
318
 
   *
319
 
   * @li Disable asio's I/O completion port backend by defining
320
 
   * ASIO_DISABLE_IOCP.
321
 
   *
322
 
   * @li Use the close() function to simultaneously cancel the outstanding
323
 
   * operations and close the socket.
324
 
   *
325
 
   * When running on Windows Vista, Windows Server 2008, and later, the
326
 
   * CancelIoEx function is always used. This function does not have the
327
 
   * problems described above.
328
 
   */
329
 
#if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) \
330
 
  && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
331
 
  && !defined(ASIO_ENABLE_CANCELIO)
332
 
  __declspec(deprecated("By default, this function always fails with "
333
 
        "operation_not_supported when used on Windows XP, Windows Server 2003, "
334
 
        "or earlier. Consult documentation for details."))
335
 
#endif
336
 
  void cancel()
337
 
  {
338
 
    asio::error_code ec;
339
 
    this->service.cancel(this->implementation, ec);
340
 
    asio::detail::throw_error(ec);
341
 
  }
342
 
 
343
 
  /// Cancel all asynchronous operations associated with the socket.
344
 
  /**
345
 
   * This function causes all outstanding asynchronous connect, send and receive
346
 
   * operations to finish immediately, and the handlers for cancelled operations
347
 
   * will be passed the asio::error::operation_aborted error.
348
 
   *
349
 
   * @param ec Set to indicate what error occurred, if any.
350
 
   *
351
 
   * @note Calls to cancel() will always fail with
352
 
   * asio::error::operation_not_supported when run on Windows XP, Windows
353
 
   * Server 2003, and earlier versions of Windows, unless
354
 
   * ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has
355
 
   * two issues that should be considered before enabling its use:
356
 
   *
357
 
   * @li It will only cancel asynchronous operations that were initiated in the
358
 
   * current thread.
359
 
   *
360
 
   * @li It can appear to complete without error, but the request to cancel the
361
 
   * unfinished operations may be silently ignored by the operating system.
362
 
   * Whether it works or not seems to depend on the drivers that are installed.
363
 
   *
364
 
   * For portable cancellation, consider using one of the following
365
 
   * alternatives:
366
 
   *
367
 
   * @li Disable asio's I/O completion port backend by defining
368
 
   * ASIO_DISABLE_IOCP.
369
 
   *
370
 
   * @li Use the close() function to simultaneously cancel the outstanding
371
 
   * operations and close the socket.
372
 
   *
373
 
   * When running on Windows Vista, Windows Server 2008, and later, the
374
 
   * CancelIoEx function is always used. This function does not have the
375
 
   * problems described above.
376
 
   */
377
 
#if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) \
378
 
  && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
379
 
  && !defined(ASIO_ENABLE_CANCELIO)
380
 
  __declspec(deprecated("By default, this function always fails with "
381
 
        "operation_not_supported when used on Windows XP, Windows Server 2003, "
382
 
        "or earlier. Consult documentation for details."))
383
 
#endif
384
 
  asio::error_code cancel(asio::error_code& ec)
385
 
  {
386
 
    return this->service.cancel(this->implementation, ec);
387
 
  }
388
 
 
389
 
  /// Determine whether the socket is at the out-of-band data mark.
390
 
  /**
391
 
   * This function is used to check whether the socket input is currently
392
 
   * positioned at the out-of-band data mark.
393
 
   *
394
 
   * @return A bool indicating whether the socket is at the out-of-band data
395
 
   * mark.
396
 
   *
397
 
   * @throws asio::system_error Thrown on failure.
398
 
   */
399
 
  bool at_mark() const
400
 
  {
401
 
    asio::error_code ec;
402
 
    bool b = this->service.at_mark(this->implementation, ec);
403
 
    asio::detail::throw_error(ec);
404
 
    return b;
405
 
  }
406
 
 
407
 
  /// Determine whether the socket is at the out-of-band data mark.
408
 
  /**
409
 
   * This function is used to check whether the socket input is currently
410
 
   * positioned at the out-of-band data mark.
411
 
   *
412
 
   * @param ec Set to indicate what error occurred, if any.
413
 
   *
414
 
   * @return A bool indicating whether the socket is at the out-of-band data
415
 
   * mark.
416
 
   */
417
 
  bool at_mark(asio::error_code& ec) const
418
 
  {
419
 
    return this->service.at_mark(this->implementation, ec);
420
 
  }
421
 
 
422
 
  /// Determine the number of bytes available for reading.
423
 
  /**
424
 
   * This function is used to determine the number of bytes that may be read
425
 
   * without blocking.
426
 
   *
427
 
   * @return The number of bytes that may be read without blocking, or 0 if an
428
 
   * error occurs.
429
 
   *
430
 
   * @throws asio::system_error Thrown on failure.
431
 
   */
432
 
  std::size_t available() const
433
 
  {
434
 
    asio::error_code ec;
435
 
    std::size_t s = this->service.available(this->implementation, ec);
436
 
    asio::detail::throw_error(ec);
437
 
    return s;
438
 
  }
439
 
 
440
 
  /// Determine the number of bytes available for reading.
441
 
  /**
442
 
   * This function is used to determine the number of bytes that may be read
443
 
   * without blocking.
444
 
   *
445
 
   * @param ec Set to indicate what error occurred, if any.
446
 
   *
447
 
   * @return The number of bytes that may be read without blocking, or 0 if an
448
 
   * error occurs.
449
 
   */
450
 
  std::size_t available(asio::error_code& ec) const
451
 
  {
452
 
    return this->service.available(this->implementation, ec);
453
 
  }
454
 
 
455
 
  /// Bind the socket to the given local endpoint.
456
 
  /**
457
 
   * This function binds the socket to the specified endpoint on the local
458
 
   * machine.
459
 
   *
460
 
   * @param endpoint An endpoint on the local machine to which the socket will
461
 
   * be bound.
462
 
   *
463
 
   * @throws asio::system_error Thrown on failure.
464
 
   *
465
 
   * @par Example
466
 
   * @code
467
 
   * asio::ip::tcp::socket socket(io_service);
468
 
   * socket.open(asio::ip::tcp::v4());
469
 
   * socket.bind(asio::ip::tcp::endpoint(
470
 
   *       asio::ip::tcp::v4(), 12345));
471
 
   * @endcode
472
 
   */
473
 
  void bind(const endpoint_type& endpoint)
474
 
  {
475
 
    asio::error_code ec;
476
 
    this->service.bind(this->implementation, endpoint, ec);
477
 
    asio::detail::throw_error(ec);
478
 
  }
479
 
 
480
 
  /// Bind the socket to the given local endpoint.
481
 
  /**
482
 
   * This function binds the socket to the specified endpoint on the local
483
 
   * machine.
484
 
   *
485
 
   * @param endpoint An endpoint on the local machine to which the socket will
486
 
   * be bound.
487
 
   *
488
 
   * @param ec Set to indicate what error occurred, if any.
489
 
   *
490
 
   * @par Example
491
 
   * @code
492
 
   * asio::ip::tcp::socket socket(io_service);
493
 
   * socket.open(asio::ip::tcp::v4());
494
 
   * asio::error_code ec;
495
 
   * socket.bind(asio::ip::tcp::endpoint(
496
 
   *       asio::ip::tcp::v4(), 12345), ec);
497
 
   * if (ec)
498
 
   * {
499
 
   *   // An error occurred.
500
 
   * }
501
 
   * @endcode
502
 
   */
503
 
  asio::error_code bind(const endpoint_type& endpoint,
504
 
      asio::error_code& ec)
505
 
  {
506
 
    return this->service.bind(this->implementation, endpoint, ec);
507
 
  }
508
 
 
509
 
  /// Connect the socket to the specified endpoint.
510
 
  /**
511
 
   * This function is used to connect a socket to the specified remote endpoint.
512
 
   * The function call will block until the connection is successfully made or
513
 
   * an error occurs.
514
 
   *
515
 
   * The socket is automatically opened if it is not already open. If the
516
 
   * connect fails, and the socket was automatically opened, the socket is
517
 
   * not returned to the closed state.
518
 
   *
519
 
   * @param peer_endpoint The remote endpoint to which the socket will be
520
 
   * connected.
521
 
   *
522
 
   * @throws asio::system_error Thrown on failure.
523
 
   *
524
 
   * @par Example
525
 
   * @code
526
 
   * asio::ip::tcp::socket socket(io_service);
527
 
   * asio::ip::tcp::endpoint endpoint(
528
 
   *     asio::ip::address::from_string("1.2.3.4"), 12345);
529
 
   * socket.connect(endpoint);
530
 
   * @endcode
531
 
   */
532
 
  void connect(const endpoint_type& peer_endpoint)
533
 
  {
534
 
    asio::error_code ec;
535
 
    if (!is_open())
536
 
    {
537
 
      this->service.open(this->implementation, peer_endpoint.protocol(), ec);
538
 
      asio::detail::throw_error(ec);
539
 
    }
540
 
    this->service.connect(this->implementation, peer_endpoint, ec);
541
 
    asio::detail::throw_error(ec);
542
 
  }
543
 
 
544
 
  /// Connect the socket to the specified endpoint.
545
 
  /**
546
 
   * This function is used to connect a socket to the specified remote endpoint.
547
 
   * The function call will block until the connection is successfully made or
548
 
   * an error occurs.
549
 
   *
550
 
   * The socket is automatically opened if it is not already open. If the
551
 
   * connect fails, and the socket was automatically opened, the socket is
552
 
   * not returned to the closed state.
553
 
   *
554
 
   * @param peer_endpoint The remote endpoint to which the socket will be
555
 
   * connected.
556
 
   *
557
 
   * @param ec Set to indicate what error occurred, if any.
558
 
   *
559
 
   * @par Example
560
 
   * @code
561
 
   * asio::ip::tcp::socket socket(io_service);
562
 
   * asio::ip::tcp::endpoint endpoint(
563
 
   *     asio::ip::address::from_string("1.2.3.4"), 12345);
564
 
   * asio::error_code ec;
565
 
   * socket.connect(endpoint, ec);
566
 
   * if (ec)
567
 
   * {
568
 
   *   // An error occurred.
569
 
   * }
570
 
   * @endcode
571
 
   */
572
 
  asio::error_code connect(const endpoint_type& peer_endpoint,
573
 
      asio::error_code& ec)
574
 
  {
575
 
    if (!is_open())
576
 
    {
577
 
      if (this->service.open(this->implementation,
578
 
            peer_endpoint.protocol(), ec))
579
 
      {
580
 
        return ec;
581
 
      }
582
 
    }
583
 
 
584
 
    return this->service.connect(this->implementation, peer_endpoint, ec);
585
 
  }
586
 
 
587
 
  /// Start an asynchronous connect.
588
 
  /**
589
 
   * This function is used to asynchronously connect a socket to the specified
590
 
   * remote endpoint. The function call always returns immediately.
591
 
   *
592
 
   * The socket is automatically opened if it is not already open. If the
593
 
   * connect fails, and the socket was automatically opened, the socket is
594
 
   * not returned to the closed state.
595
 
   *
596
 
   * @param peer_endpoint The remote endpoint to which the socket will be
597
 
   * connected. Copies will be made of the endpoint object as required.
598
 
   *
599
 
   * @param handler The handler to be called when the connection operation
600
 
   * completes. Copies will be made of the handler as required. The function
601
 
   * signature of the handler must be:
602
 
   * @code void handler(
603
 
   *   const asio::error_code& error // Result of operation
604
 
   * ); @endcode
605
 
   * Regardless of whether the asynchronous operation completes immediately or
606
 
   * not, the handler will not be invoked from within this function. Invocation
607
 
   * of the handler will be performed in a manner equivalent to using
608
 
   * asio::io_service::post().
609
 
   *
610
 
   * @par Example
611
 
   * @code
612
 
   * void connect_handler(const asio::error_code& error)
613
 
   * {
614
 
   *   if (!error)
615
 
   *   {
616
 
   *     // Connect succeeded.
617
 
   *   }
618
 
   * }
619
 
   *
620
 
   * ...
621
 
   *
622
 
   * asio::ip::tcp::socket socket(io_service);
623
 
   * asio::ip::tcp::endpoint endpoint(
624
 
   *     asio::ip::address::from_string("1.2.3.4"), 12345);
625
 
   * socket.async_connect(endpoint, connect_handler);
626
 
   * @endcode
627
 
   */
628
 
  template <typename ConnectHandler>
629
 
  void async_connect(const endpoint_type& peer_endpoint, ConnectHandler handler)
630
 
  {
631
 
    if (!is_open())
632
 
    {
633
 
      asio::error_code ec;
634
 
      if (this->service.open(this->implementation,
635
 
            peer_endpoint.protocol(), ec))
636
 
      {
637
 
        this->get_io_service().post(
638
 
            asio::detail::bind_handler(handler, ec));
639
 
        return;
640
 
      }
641
 
    }
642
 
 
643
 
    this->service.async_connect(this->implementation, peer_endpoint, handler);
644
 
  }
645
 
 
646
 
  /// Set an option on the socket.
647
 
  /**
648
 
   * This function is used to set an option on the socket.
649
 
   *
650
 
   * @param option The new option value to be set on the socket.
651
 
   *
652
 
   * @throws asio::system_error Thrown on failure.
653
 
   *
654
 
   * @sa SettableSocketOption @n
655
 
   * asio::socket_base::broadcast @n
656
 
   * asio::socket_base::do_not_route @n
657
 
   * asio::socket_base::keep_alive @n
658
 
   * asio::socket_base::linger @n
659
 
   * asio::socket_base::receive_buffer_size @n
660
 
   * asio::socket_base::receive_low_watermark @n
661
 
   * asio::socket_base::reuse_address @n
662
 
   * asio::socket_base::send_buffer_size @n
663
 
   * asio::socket_base::send_low_watermark @n
664
 
   * asio::ip::multicast::join_group @n
665
 
   * asio::ip::multicast::leave_group @n
666
 
   * asio::ip::multicast::enable_loopback @n
667
 
   * asio::ip::multicast::outbound_interface @n
668
 
   * asio::ip::multicast::hops @n
669
 
   * asio::ip::tcp::no_delay
670
 
   *
671
 
   * @par Example
672
 
   * Setting the IPPROTO_TCP/TCP_NODELAY option:
673
 
   * @code
674
 
   * asio::ip::tcp::socket socket(io_service);
675
 
   * ...
676
 
   * asio::ip::tcp::no_delay option(true);
677
 
   * socket.set_option(option);
678
 
   * @endcode
679
 
   */
680
 
  template <typename SettableSocketOption>
681
 
  void set_option(const SettableSocketOption& option)
682
 
  {
683
 
    asio::error_code ec;
684
 
    this->service.set_option(this->implementation, option, ec);
685
 
    asio::detail::throw_error(ec);
686
 
  }
687
 
 
688
 
  /// Set an option on the socket.
689
 
  /**
690
 
   * This function is used to set an option on the socket.
691
 
   *
692
 
   * @param option The new option value to be set on the socket.
693
 
   *
694
 
   * @param ec Set to indicate what error occurred, if any.
695
 
   *
696
 
   * @sa SettableSocketOption @n
697
 
   * asio::socket_base::broadcast @n
698
 
   * asio::socket_base::do_not_route @n
699
 
   * asio::socket_base::keep_alive @n
700
 
   * asio::socket_base::linger @n
701
 
   * asio::socket_base::receive_buffer_size @n
702
 
   * asio::socket_base::receive_low_watermark @n
703
 
   * asio::socket_base::reuse_address @n
704
 
   * asio::socket_base::send_buffer_size @n
705
 
   * asio::socket_base::send_low_watermark @n
706
 
   * asio::ip::multicast::join_group @n
707
 
   * asio::ip::multicast::leave_group @n
708
 
   * asio::ip::multicast::enable_loopback @n
709
 
   * asio::ip::multicast::outbound_interface @n
710
 
   * asio::ip::multicast::hops @n
711
 
   * asio::ip::tcp::no_delay
712
 
   *
713
 
   * @par Example
714
 
   * Setting the IPPROTO_TCP/TCP_NODELAY option:
715
 
   * @code
716
 
   * asio::ip::tcp::socket socket(io_service);
717
 
   * ...
718
 
   * asio::ip::tcp::no_delay option(true);
719
 
   * asio::error_code ec;
720
 
   * socket.set_option(option, ec);
721
 
   * if (ec)
722
 
   * {
723
 
   *   // An error occurred.
724
 
   * }
725
 
   * @endcode
726
 
   */
727
 
  template <typename SettableSocketOption>
728
 
  asio::error_code set_option(const SettableSocketOption& option,
729
 
      asio::error_code& ec)
730
 
  {
731
 
    return this->service.set_option(this->implementation, option, ec);
732
 
  }
733
 
 
734
 
  /// Get an option from the socket.
735
 
  /**
736
 
   * This function is used to get the current value of an option on the socket.
737
 
   *
738
 
   * @param option The option value to be obtained from the socket.
739
 
   *
740
 
   * @throws asio::system_error Thrown on failure.
741
 
   *
742
 
   * @sa GettableSocketOption @n
743
 
   * asio::socket_base::broadcast @n
744
 
   * asio::socket_base::do_not_route @n
745
 
   * asio::socket_base::keep_alive @n
746
 
   * asio::socket_base::linger @n
747
 
   * asio::socket_base::receive_buffer_size @n
748
 
   * asio::socket_base::receive_low_watermark @n
749
 
   * asio::socket_base::reuse_address @n
750
 
   * asio::socket_base::send_buffer_size @n
751
 
   * asio::socket_base::send_low_watermark @n
752
 
   * asio::ip::multicast::join_group @n
753
 
   * asio::ip::multicast::leave_group @n
754
 
   * asio::ip::multicast::enable_loopback @n
755
 
   * asio::ip::multicast::outbound_interface @n
756
 
   * asio::ip::multicast::hops @n
757
 
   * asio::ip::tcp::no_delay
758
 
   *
759
 
   * @par Example
760
 
   * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
761
 
   * @code
762
 
   * asio::ip::tcp::socket socket(io_service);
763
 
   * ...
764
 
   * asio::ip::tcp::socket::keep_alive option;
765
 
   * socket.get_option(option);
766
 
   * bool is_set = option.get();
767
 
   * @endcode
768
 
   */
769
 
  template <typename GettableSocketOption>
770
 
  void get_option(GettableSocketOption& option) const
771
 
  {
772
 
    asio::error_code ec;
773
 
    this->service.get_option(this->implementation, option, ec);
774
 
    asio::detail::throw_error(ec);
775
 
  }
776
 
 
777
 
  /// Get an option from the socket.
778
 
  /**
779
 
   * This function is used to get the current value of an option on the socket.
780
 
   *
781
 
   * @param option The option value to be obtained from the socket.
782
 
   *
783
 
   * @param ec Set to indicate what error occurred, if any.
784
 
   *
785
 
   * @sa GettableSocketOption @n
786
 
   * asio::socket_base::broadcast @n
787
 
   * asio::socket_base::do_not_route @n
788
 
   * asio::socket_base::keep_alive @n
789
 
   * asio::socket_base::linger @n
790
 
   * asio::socket_base::receive_buffer_size @n
791
 
   * asio::socket_base::receive_low_watermark @n
792
 
   * asio::socket_base::reuse_address @n
793
 
   * asio::socket_base::send_buffer_size @n
794
 
   * asio::socket_base::send_low_watermark @n
795
 
   * asio::ip::multicast::join_group @n
796
 
   * asio::ip::multicast::leave_group @n
797
 
   * asio::ip::multicast::enable_loopback @n
798
 
   * asio::ip::multicast::outbound_interface @n
799
 
   * asio::ip::multicast::hops @n
800
 
   * asio::ip::tcp::no_delay
801
 
   *
802
 
   * @par Example
803
 
   * Getting the value of the SOL_SOCKET/SO_KEEPALIVE option:
804
 
   * @code
805
 
   * asio::ip::tcp::socket socket(io_service);
806
 
   * ...
807
 
   * asio::ip::tcp::socket::keep_alive option;
808
 
   * asio::error_code ec;
809
 
   * socket.get_option(option, ec);
810
 
   * if (ec)
811
 
   * {
812
 
   *   // An error occurred.
813
 
   * }
814
 
   * bool is_set = option.get();
815
 
   * @endcode
816
 
   */
817
 
  template <typename GettableSocketOption>
818
 
  asio::error_code get_option(GettableSocketOption& option,
819
 
      asio::error_code& ec) const
820
 
  {
821
 
    return this->service.get_option(this->implementation, option, ec);
822
 
  }
823
 
 
824
 
  /// Perform an IO control command on the socket.
825
 
  /**
826
 
   * This function is used to execute an IO control command on the socket.
827
 
   *
828
 
   * @param command The IO control command to be performed on the socket.
829
 
   *
830
 
   * @throws asio::system_error Thrown on failure.
831
 
   *
832
 
   * @sa IoControlCommand @n
833
 
   * asio::socket_base::bytes_readable @n
834
 
   * asio::socket_base::non_blocking_io
835
 
   *
836
 
   * @par Example
837
 
   * Getting the number of bytes ready to read:
838
 
   * @code
839
 
   * asio::ip::tcp::socket socket(io_service);
840
 
   * ...
841
 
   * asio::ip::tcp::socket::bytes_readable command;
842
 
   * socket.io_control(command);
843
 
   * std::size_t bytes_readable = command.get();
844
 
   * @endcode
845
 
   */
846
 
  template <typename IoControlCommand>
847
 
  void io_control(IoControlCommand& command)
848
 
  {
849
 
    asio::error_code ec;
850
 
    this->service.io_control(this->implementation, command, ec);
851
 
    asio::detail::throw_error(ec);
852
 
  }
853
 
 
854
 
  /// Perform an IO control command on the socket.
855
 
  /**
856
 
   * This function is used to execute an IO control command on the socket.
857
 
   *
858
 
   * @param command The IO control command to be performed on the socket.
859
 
   *
860
 
   * @param ec Set to indicate what error occurred, if any.
861
 
   *
862
 
   * @sa IoControlCommand @n
863
 
   * asio::socket_base::bytes_readable @n
864
 
   * asio::socket_base::non_blocking_io
865
 
   *
866
 
   * @par Example
867
 
   * Getting the number of bytes ready to read:
868
 
   * @code
869
 
   * asio::ip::tcp::socket socket(io_service);
870
 
   * ...
871
 
   * asio::ip::tcp::socket::bytes_readable command;
872
 
   * asio::error_code ec;
873
 
   * socket.io_control(command, ec);
874
 
   * if (ec)
875
 
   * {
876
 
   *   // An error occurred.
877
 
   * }
878
 
   * std::size_t bytes_readable = command.get();
879
 
   * @endcode
880
 
   */
881
 
  template <typename IoControlCommand>
882
 
  asio::error_code io_control(IoControlCommand& command,
883
 
      asio::error_code& ec)
884
 
  {
885
 
    return this->service.io_control(this->implementation, command, ec);
886
 
  }
887
 
 
888
 
  /// Get the local endpoint of the socket.
889
 
  /**
890
 
   * This function is used to obtain the locally bound endpoint of the socket.
891
 
   *
892
 
   * @returns An object that represents the local endpoint of the socket.
893
 
   *
894
 
   * @throws asio::system_error Thrown on failure.
895
 
   *
896
 
   * @par Example
897
 
   * @code
898
 
   * asio::ip::tcp::socket socket(io_service);
899
 
   * ...
900
 
   * asio::ip::tcp::endpoint endpoint = socket.local_endpoint();
901
 
   * @endcode
902
 
   */
903
 
  endpoint_type local_endpoint() const
904
 
  {
905
 
    asio::error_code ec;
906
 
    endpoint_type ep = this->service.local_endpoint(this->implementation, ec);
907
 
    asio::detail::throw_error(ec);
908
 
    return ep;
909
 
  }
910
 
 
911
 
  /// Get the local endpoint of the socket.
912
 
  /**
913
 
   * This function is used to obtain the locally bound endpoint of the socket.
914
 
   *
915
 
   * @param ec Set to indicate what error occurred, if any.
916
 
   *
917
 
   * @returns An object that represents the local endpoint of the socket.
918
 
   * Returns a default-constructed endpoint object if an error occurred.
919
 
   *
920
 
   * @par Example
921
 
   * @code
922
 
   * asio::ip::tcp::socket socket(io_service);
923
 
   * ...
924
 
   * asio::error_code ec;
925
 
   * asio::ip::tcp::endpoint endpoint = socket.local_endpoint(ec);
926
 
   * if (ec)
927
 
   * {
928
 
   *   // An error occurred.
929
 
   * }
930
 
   * @endcode
931
 
   */
932
 
  endpoint_type local_endpoint(asio::error_code& ec) const
933
 
  {
934
 
    return this->service.local_endpoint(this->implementation, ec);
935
 
  }
936
 
 
937
 
  /// Get the remote endpoint of the socket.
938
 
  /**
939
 
   * This function is used to obtain the remote endpoint of the socket.
940
 
   *
941
 
   * @returns An object that represents the remote endpoint of the socket.
942
 
   *
943
 
   * @throws asio::system_error Thrown on failure.
944
 
   *
945
 
   * @par Example
946
 
   * @code
947
 
   * asio::ip::tcp::socket socket(io_service);
948
 
   * ...
949
 
   * asio::ip::tcp::endpoint endpoint = socket.remote_endpoint();
950
 
   * @endcode
951
 
   */
952
 
  endpoint_type remote_endpoint() const
953
 
  {
954
 
    asio::error_code ec;
955
 
    endpoint_type ep = this->service.remote_endpoint(this->implementation, ec);
956
 
    asio::detail::throw_error(ec);
957
 
    return ep;
958
 
  }
959
 
 
960
 
  /// Get the remote endpoint of the socket.
961
 
  /**
962
 
   * This function is used to obtain the remote endpoint of the socket.
963
 
   *
964
 
   * @param ec Set to indicate what error occurred, if any.
965
 
   *
966
 
   * @returns An object that represents the remote endpoint of the socket.
967
 
   * Returns a default-constructed endpoint object if an error occurred.
968
 
   *
969
 
   * @par Example
970
 
   * @code
971
 
   * asio::ip::tcp::socket socket(io_service);
972
 
   * ...
973
 
   * asio::error_code ec;
974
 
   * asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec);
975
 
   * if (ec)
976
 
   * {
977
 
   *   // An error occurred.
978
 
   * }
979
 
   * @endcode
980
 
   */
981
 
  endpoint_type remote_endpoint(asio::error_code& ec) const
982
 
  {
983
 
    return this->service.remote_endpoint(this->implementation, ec);
984
 
  }
985
 
 
986
 
  /// Disable sends or receives on the socket.
987
 
  /**
988
 
   * This function is used to disable send operations, receive operations, or
989
 
   * both.
990
 
   *
991
 
   * @param what Determines what types of operation will no longer be allowed.
992
 
   *
993
 
   * @throws asio::system_error Thrown on failure.
994
 
   *
995
 
   * @par Example
996
 
   * Shutting down the send side of the socket:
997
 
   * @code
998
 
   * asio::ip::tcp::socket socket(io_service);
999
 
   * ...
1000
 
   * socket.shutdown(asio::ip::tcp::socket::shutdown_send);
1001
 
   * @endcode
1002
 
   */
1003
 
  void shutdown(shutdown_type what)
1004
 
  {
1005
 
    asio::error_code ec;
1006
 
    this->service.shutdown(this->implementation, what, ec);
1007
 
    asio::detail::throw_error(ec);
1008
 
  }
1009
 
 
1010
 
  /// Disable sends or receives on the socket.
1011
 
  /**
1012
 
   * This function is used to disable send operations, receive operations, or
1013
 
   * both.
1014
 
   *
1015
 
   * @param what Determines what types of operation will no longer be allowed.
1016
 
   *
1017
 
   * @param ec Set to indicate what error occurred, if any.
1018
 
   *
1019
 
   * @par Example
1020
 
   * Shutting down the send side of the socket:
1021
 
   * @code
1022
 
   * asio::ip::tcp::socket socket(io_service);
1023
 
   * ...
1024
 
   * asio::error_code ec;
1025
 
   * socket.shutdown(asio::ip::tcp::socket::shutdown_send, ec);
1026
 
   * if (ec)
1027
 
   * {
1028
 
   *   // An error occurred.
1029
 
   * }
1030
 
   * @endcode
1031
 
   */
1032
 
  asio::error_code shutdown(shutdown_type what,
1033
 
      asio::error_code& ec)
1034
 
  {
1035
 
    return this->service.shutdown(this->implementation, what, ec);
1036
 
  }
1037
 
 
1038
 
protected:
1039
 
  /// Protected destructor to prevent deletion through this type.
1040
 
  ~basic_socket()
1041
 
  {
1042
 
  }
1043
 
};
1044
 
 
1045
 
} // namespace asio
1046
 
 
1047
 
#include "asio/detail/pop_options.hpp"
1048
 
 
1049
 
#endif // ASIO_BASIC_SOCKET_HPP