~ubuntu-branches/ubuntu/intrepid/miro/intrepid

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2008-02-09 13:37:10 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080209133710-9rs90q6gckvp1b6i
Tags: 1.1.2-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

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