~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/asio/basic_socket_acceptor.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// basic_socket_acceptor.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_ACCEPTOR_HPP
 
12
#define ASIO_BASIC_SOCKET_ACCEPTOR_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/basic_socket.hpp"
 
22
#include "asio/error.hpp"
 
23
#include "asio/socket_acceptor_service.hpp"
 
24
#include "asio/socket_base.hpp"
 
25
#include "asio/detail/throw_error.hpp"
 
26
 
 
27
namespace asio {
 
28
 
 
29
/// Provides the ability to accept new connections.
 
30
/**
 
31
 * The basic_socket_acceptor class template is used for accepting new socket
 
32
 * connections.
 
33
 *
 
34
 * @par Thread Safety
 
35
 * @e Distinct @e objects: Safe.@n
 
36
 * @e Shared @e objects: Unsafe.
 
37
 *
 
38
 * @par Example
 
39
 * Opening a socket acceptor with the SO_REUSEADDR option enabled:
 
40
 * @code
 
41
 * asio::ip::tcp::acceptor acceptor(io_service);
 
42
 * asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port);
 
43
 * acceptor.open(endpoint.protocol());
 
44
 * acceptor.set_option(asio::ip::tcp::acceptor::reuse_address(true));
 
45
 * acceptor.bind(endpoint);
 
46
 * acceptor.listen();
 
47
 * @endcode
 
48
 */
 
49
template <typename Protocol,
 
50
    typename SocketAcceptorService = socket_acceptor_service<Protocol> >
 
51
class basic_socket_acceptor
 
52
  : public basic_io_object<SocketAcceptorService>,
 
53
    public socket_base
 
54
{
 
55
public:
 
56
  /// The native representation of an acceptor.
 
57
  typedef typename SocketAcceptorService::native_type native_type;
 
58
 
 
59
  /// The protocol type.
 
60
  typedef Protocol protocol_type;
 
61
 
 
62
  /// The endpoint type.
 
63
  typedef typename Protocol::endpoint endpoint_type;
 
64
 
 
65
  /// Construct an acceptor without opening it.
 
66
  /**
 
67
   * This constructor creates an acceptor without opening it to listen for new
 
68
   * connections. The open() function must be called before the acceptor can
 
69
   * accept new socket connections.
 
70
   *
 
71
   * @param io_service The io_service object that the acceptor will use to
 
72
   * dispatch handlers for any asynchronous operations performed on the
 
73
   * acceptor.
 
74
   */
 
75
  explicit basic_socket_acceptor(asio::io_service& io_service)
 
76
    : basic_io_object<SocketAcceptorService>(io_service)
 
77
  {
 
78
  }
 
79
 
 
80
  /// Construct an open acceptor.
 
81
  /**
 
82
   * This constructor creates an acceptor and automatically opens it.
 
83
   *
 
84
   * @param io_service The io_service object that the acceptor will use to
 
85
   * dispatch handlers for any asynchronous operations performed on the
 
86
   * acceptor.
 
87
   *
 
88
   * @param protocol An object specifying protocol parameters to be used.
 
89
   *
 
90
   * @throws asio::system_error Thrown on failure.
 
91
   */
 
92
  basic_socket_acceptor(asio::io_service& io_service,
 
93
      const protocol_type& protocol)
 
94
    : basic_io_object<SocketAcceptorService>(io_service)
 
95
  {
 
96
    asio::error_code ec;
 
97
    this->service.open(this->implementation, protocol, ec);
 
98
    asio::detail::throw_error(ec);
 
99
  }
 
100
 
 
101
  /// Construct an acceptor opened on the given endpoint.
 
102
  /**
 
103
   * This constructor creates an acceptor and automatically opens it to listen
 
104
   * for new connections on the specified endpoint.
 
105
   *
 
106
   * @param io_service The io_service object that the acceptor will use to
 
107
   * dispatch handlers for any asynchronous operations performed on the
 
108
   * acceptor.
 
109
   *
 
110
   * @param endpoint An endpoint on the local machine on which the acceptor
 
111
   * will listen for new connections.
 
112
   *
 
113
   * @param reuse_addr Whether the constructor should set the socket option
 
114
   * socket_base::reuse_address.
 
115
   *
 
116
   * @throws asio::system_error Thrown on failure.
 
117
   *
 
118
   * @note This constructor is equivalent to the following code:
 
119
   * @code
 
120
   * basic_socket_acceptor<Protocol> acceptor(io_service);
 
121
   * acceptor.open(endpoint.protocol());
 
122
   * if (reuse_addr)
 
123
   *   acceptor.set_option(socket_base::reuse_address(true));
 
124
   * acceptor.bind(endpoint);
 
125
   * acceptor.listen(listen_backlog);
 
126
   * @endcode
 
127
   */
 
128
  basic_socket_acceptor(asio::io_service& io_service,
 
129
      const endpoint_type& endpoint, bool reuse_addr = true)
 
130
    : basic_io_object<SocketAcceptorService>(io_service)
 
131
  {
 
132
    asio::error_code ec;
 
133
    this->service.open(this->implementation, endpoint.protocol(), ec);
 
134
    asio::detail::throw_error(ec);
 
135
    if (reuse_addr)
 
136
    {
 
137
      this->service.set_option(this->implementation,
 
138
          socket_base::reuse_address(true), ec);
 
139
      asio::detail::throw_error(ec);
 
140
    }
 
141
    this->service.bind(this->implementation, endpoint, ec);
 
142
    asio::detail::throw_error(ec);
 
143
    this->service.listen(this->implementation,
 
144
        socket_base::max_connections, ec);
 
145
    asio::detail::throw_error(ec);
 
146
  }
 
147
 
 
148
  /// Construct a basic_socket_acceptor on an existing native acceptor.
 
149
  /**
 
150
   * This constructor creates an acceptor object to hold an existing native
 
151
   * acceptor.
 
152
   *
 
153
   * @param io_service The io_service object that the acceptor will use to
 
154
   * dispatch handlers for any asynchronous operations performed on the
 
155
   * acceptor.
 
156
   *
 
157
   * @param protocol An object specifying protocol parameters to be used.
 
158
   *
 
159
   * @param native_acceptor A native acceptor.
 
160
   *
 
161
   * @throws asio::system_error Thrown on failure.
 
162
   */
 
163
  basic_socket_acceptor(asio::io_service& io_service,
 
164
      const protocol_type& protocol, const native_type& native_acceptor)
 
165
    : basic_io_object<SocketAcceptorService>(io_service)
 
166
  {
 
167
    asio::error_code ec;
 
168
    this->service.assign(this->implementation, protocol, native_acceptor, ec);
 
169
    asio::detail::throw_error(ec);
 
170
  }
 
171
 
 
172
  /// Open the acceptor using the specified protocol.
 
173
  /**
 
174
   * This function opens the socket acceptor so that it will use the specified
 
175
   * protocol.
 
176
   *
 
177
   * @param protocol An object specifying which protocol is to be used.
 
178
   *
 
179
   * @throws asio::system_error Thrown on failure.
 
180
   *
 
181
   * @par Example
 
182
   * @code
 
183
   * asio::ip::tcp::acceptor acceptor(io_service);
 
184
   * acceptor.open(asio::ip::tcp::v4());
 
185
   * @endcode
 
186
   */
 
187
  void open(const protocol_type& protocol = protocol_type())
 
188
  {
 
189
    asio::error_code ec;
 
190
    this->service.open(this->implementation, protocol, ec);
 
191
    asio::detail::throw_error(ec);
 
192
  }
 
193
 
 
194
  /// Open the acceptor using the specified protocol.
 
195
  /**
 
196
   * This function opens the socket acceptor so that it will use the specified
 
197
   * protocol.
 
198
   *
 
199
   * @param protocol An object specifying which protocol is to be used.
 
200
   *
 
201
   * @param ec Set to indicate what error occurred, if any.
 
202
   *
 
203
   * @par Example
 
204
   * @code
 
205
   * asio::ip::tcp::acceptor acceptor(io_service);
 
206
   * asio::error_code ec;
 
207
   * acceptor.open(asio::ip::tcp::v4(), ec);
 
208
   * if (ec)
 
209
   * {
 
210
   *   // An error occurred.
 
211
   * }
 
212
   * @endcode
 
213
   */
 
214
  asio::error_code open(const protocol_type& protocol,
 
215
      asio::error_code& ec)
 
216
  {
 
217
    return this->service.open(this->implementation, protocol, ec);
 
218
  }
 
219
 
 
220
  /// Assigns an existing native acceptor to the acceptor.
 
221
  /*
 
222
   * This function opens the acceptor to hold an existing native acceptor.
 
223
   *
 
224
   * @param protocol An object specifying which protocol is to be used.
 
225
   *
 
226
   * @param native_acceptor A native acceptor.
 
227
   *
 
228
   * @throws asio::system_error Thrown on failure.
 
229
   */
 
230
  void assign(const protocol_type& protocol, const native_type& native_acceptor)
 
231
  {
 
232
    asio::error_code ec;
 
233
    this->service.assign(this->implementation, protocol, native_acceptor, ec);
 
234
    asio::detail::throw_error(ec);
 
235
  }
 
236
 
 
237
  /// Assigns an existing native acceptor to the acceptor.
 
238
  /*
 
239
   * This function opens the acceptor to hold an existing native acceptor.
 
240
   *
 
241
   * @param protocol An object specifying which protocol is to be used.
 
242
   *
 
243
   * @param native_acceptor A native acceptor.
 
244
   *
 
245
   * @param ec Set to indicate what error occurred, if any.
 
246
   */
 
247
  asio::error_code assign(const protocol_type& protocol,
 
248
      const native_type& native_acceptor, asio::error_code& ec)
 
249
  {
 
250
    return this->service.assign(this->implementation,
 
251
        protocol, native_acceptor, ec);
 
252
  }
 
253
 
 
254
  /// Determine whether the acceptor is open.
 
255
  bool is_open() const
 
256
  {
 
257
    return this->service.is_open(this->implementation);
 
258
  }
 
259
 
 
260
  /// Bind the acceptor to the given local endpoint.
 
261
  /**
 
262
   * This function binds the socket acceptor to the specified endpoint on the
 
263
   * local machine.
 
264
   *
 
265
   * @param endpoint An endpoint on the local machine to which the socket
 
266
   * acceptor will be bound.
 
267
   *
 
268
   * @throws asio::system_error Thrown on failure.
 
269
   *
 
270
   * @par Example
 
271
   * @code
 
272
   * asio::ip::tcp::acceptor acceptor(io_service);
 
273
   * acceptor.open(asio::ip::tcp::v4());
 
274
   * acceptor.bind(asio::ip::tcp::endpoint(12345));
 
275
   * @endcode
 
276
   */
 
277
  void bind(const endpoint_type& endpoint)
 
278
  {
 
279
    asio::error_code ec;
 
280
    this->service.bind(this->implementation, endpoint, ec);
 
281
    asio::detail::throw_error(ec);
 
282
  }
 
283
 
 
284
  /// Bind the acceptor to the given local endpoint.
 
285
  /**
 
286
   * This function binds the socket acceptor to the specified endpoint on the
 
287
   * local machine.
 
288
   *
 
289
   * @param endpoint An endpoint on the local machine to which the socket
 
290
   * acceptor will be bound.
 
291
   *
 
292
   * @param ec Set to indicate what error occurred, if any.
 
293
   *
 
294
   * @par Example
 
295
   * @code
 
296
   * asio::ip::tcp::acceptor acceptor(io_service);
 
297
   * acceptor.open(asio::ip::tcp::v4());
 
298
   * asio::error_code ec;
 
299
   * acceptor.bind(asio::ip::tcp::endpoint(12345), ec);
 
300
   * if (ec)
 
301
   * {
 
302
   *   // An error occurred.
 
303
   * }
 
304
   * @endcode
 
305
   */
 
306
  asio::error_code bind(const endpoint_type& endpoint,
 
307
      asio::error_code& ec)
 
308
  {
 
309
    return this->service.bind(this->implementation, endpoint, ec);
 
310
  }
 
311
 
 
312
  /// Place the acceptor into the state where it will listen for new
 
313
  /// connections.
 
314
  /**
 
315
   * This function puts the socket acceptor into the state where it may accept
 
316
   * new connections.
 
317
   *
 
318
   * @param backlog The maximum length of the queue of pending connections.
 
319
   *
 
320
   * @throws asio::system_error Thrown on failure.
 
321
   */
 
322
  void listen(int backlog = socket_base::max_connections)
 
323
  {
 
324
    asio::error_code ec;
 
325
    this->service.listen(this->implementation, backlog, ec);
 
326
    asio::detail::throw_error(ec);
 
327
  }
 
328
 
 
329
  /// Place the acceptor into the state where it will listen for new
 
330
  /// connections.
 
331
  /**
 
332
   * This function puts the socket acceptor into the state where it may accept
 
333
   * new connections.
 
334
   *
 
335
   * @param backlog The maximum length of the queue of pending connections.
 
336
   *
 
337
   * @param ec Set to indicate what error occurred, if any.
 
338
   *
 
339
   * @par Example
 
340
   * @code
 
341
   * asio::ip::tcp::acceptor acceptor(io_service);
 
342
   * ...
 
343
   * asio::error_code ec;
 
344
   * acceptor.listen(asio::socket_base::max_connections, ec);
 
345
   * if (ec)
 
346
   * {
 
347
   *   // An error occurred.
 
348
   * }
 
349
   * @endcode
 
350
   */
 
351
  asio::error_code listen(int backlog, asio::error_code& ec)
 
352
  {
 
353
    return this->service.listen(this->implementation, backlog, ec);
 
354
  }
 
355
 
 
356
  /// Close the acceptor.
 
357
  /**
 
358
   * This function is used to close the acceptor. Any asynchronous accept
 
359
   * operations will be cancelled immediately.
 
360
   *
 
361
   * A subsequent call to open() is required before the acceptor can again be
 
362
   * used to again perform socket accept operations.
 
363
   *
 
364
   * @throws asio::system_error Thrown on failure.
 
365
   */
 
366
  void close()
 
367
  {
 
368
    asio::error_code ec;
 
369
    this->service.close(this->implementation, ec);
 
370
    asio::detail::throw_error(ec);
 
371
  }
 
372
 
 
373
  /// Close the acceptor.
 
374
  /**
 
375
   * This function is used to close the acceptor. Any asynchronous accept
 
376
   * operations will be cancelled immediately.
 
377
   *
 
378
   * A subsequent call to open() is required before the acceptor can again be
 
379
   * used to again perform socket accept operations.
 
380
   *
 
381
   * @param ec Set to indicate what error occurred, if any.
 
382
   *
 
383
   * @par Example
 
384
   * @code
 
385
   * asio::ip::tcp::acceptor acceptor(io_service);
 
386
   * ...
 
387
   * asio::error_code ec;
 
388
   * acceptor.close(ec);
 
389
   * if (ec)
 
390
   * {
 
391
   *   // An error occurred.
 
392
   * }
 
393
   * @endcode
 
394
   */
 
395
  asio::error_code close(asio::error_code& ec)
 
396
  {
 
397
    return this->service.close(this->implementation, ec);
 
398
  }
 
399
 
 
400
  /// Get the native acceptor representation.
 
401
  /**
 
402
   * This function may be used to obtain the underlying representation of the
 
403
   * acceptor. This is intended to allow access to native acceptor functionality
 
404
   * that is not otherwise provided.
 
405
   */
 
406
  native_type native()
 
407
  {
 
408
    return this->service.native(this->implementation);
 
409
  }
 
410
 
 
411
  /// Cancel all asynchronous operations associated with the acceptor.
 
412
  /**
 
413
   * This function causes all outstanding asynchronous connect, send and receive
 
414
   * operations to finish immediately, and the handlers for cancelled operations
 
415
   * will be passed the asio::error::operation_aborted error.
 
416
   *
 
417
   * @throws asio::system_error Thrown on failure.
 
418
   */
 
419
  void cancel()
 
420
  {
 
421
    asio::error_code ec;
 
422
    this->service.cancel(this->implementation, ec);
 
423
    asio::detail::throw_error(ec);
 
424
  }
 
425
 
 
426
  /// Cancel all asynchronous operations associated with the acceptor.
 
427
  /**
 
428
   * This function causes all outstanding asynchronous connect, send and receive
 
429
   * operations to finish immediately, and the handlers for cancelled operations
 
430
   * will be passed the asio::error::operation_aborted error.
 
431
   *
 
432
   * @param ec Set to indicate what error occurred, if any.
 
433
   */
 
434
  asio::error_code cancel(asio::error_code& ec)
 
435
  {
 
436
    return this->service.cancel(this->implementation, ec);
 
437
  }
 
438
 
 
439
  /// Set an option on the acceptor.
 
440
  /**
 
441
   * This function is used to set an option on the acceptor.
 
442
   *
 
443
   * @param option The new option value to be set on the acceptor.
 
444
   *
 
445
   * @throws asio::system_error Thrown on failure.
 
446
   *
 
447
   * @sa SettableSocketOption @n
 
448
   * asio::socket_base::reuse_address
 
449
   * asio::socket_base::enable_connection_aborted
 
450
   *
 
451
   * @par Example
 
452
   * Setting the SOL_SOCKET/SO_REUSEADDR option:
 
453
   * @code
 
454
   * asio::ip::tcp::acceptor acceptor(io_service);
 
455
   * ...
 
456
   * asio::ip::tcp::acceptor::reuse_address option(true);
 
457
   * acceptor.set_option(option);
 
458
   * @endcode
 
459
   */
 
460
  template <typename SettableSocketOption>
 
461
  void set_option(const SettableSocketOption& option)
 
462
  {
 
463
    asio::error_code ec;
 
464
    this->service.set_option(this->implementation, option, ec);
 
465
    asio::detail::throw_error(ec);
 
466
  }
 
467
 
 
468
  /// Set an option on the acceptor.
 
469
  /**
 
470
   * This function is used to set an option on the acceptor.
 
471
   *
 
472
   * @param option The new option value to be set on the acceptor.
 
473
   *
 
474
   * @param ec Set to indicate what error occurred, if any.
 
475
   *
 
476
   * @sa SettableSocketOption @n
 
477
   * asio::socket_base::reuse_address
 
478
   * asio::socket_base::enable_connection_aborted
 
479
   *
 
480
   * @par Example
 
481
   * Setting the SOL_SOCKET/SO_REUSEADDR option:
 
482
   * @code
 
483
   * asio::ip::tcp::acceptor acceptor(io_service);
 
484
   * ...
 
485
   * asio::ip::tcp::acceptor::reuse_address option(true);
 
486
   * asio::error_code ec;
 
487
   * acceptor.set_option(option, ec);
 
488
   * if (ec)
 
489
   * {
 
490
   *   // An error occurred.
 
491
   * }
 
492
   * @endcode
 
493
   */
 
494
  template <typename SettableSocketOption>
 
495
  asio::error_code set_option(const SettableSocketOption& option,
 
496
      asio::error_code& ec)
 
497
  {
 
498
    return this->service.set_option(this->implementation, option, ec);
 
499
  }
 
500
 
 
501
  /// Get an option from the acceptor.
 
502
  /**
 
503
   * This function is used to get the current value of an option on the
 
504
   * acceptor.
 
505
   *
 
506
   * @param option The option value to be obtained from the acceptor.
 
507
   *
 
508
   * @throws asio::system_error Thrown on failure.
 
509
   *
 
510
   * @sa GettableSocketOption @n
 
511
   * asio::socket_base::reuse_address
 
512
   *
 
513
   * @par Example
 
514
   * Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
 
515
   * @code
 
516
   * asio::ip::tcp::acceptor acceptor(io_service);
 
517
   * ...
 
518
   * asio::ip::tcp::acceptor::reuse_address option;
 
519
   * acceptor.get_option(option);
 
520
   * bool is_set = option.get();
 
521
   * @endcode
 
522
   */
 
523
  template <typename GettableSocketOption>
 
524
  void get_option(GettableSocketOption& option)
 
525
  {
 
526
    asio::error_code ec;
 
527
    this->service.get_option(this->implementation, option, ec);
 
528
    asio::detail::throw_error(ec);
 
529
  }
 
530
 
 
531
  /// Get an option from the acceptor.
 
532
  /**
 
533
   * This function is used to get the current value of an option on the
 
534
   * acceptor.
 
535
   *
 
536
   * @param option The option value to be obtained from the acceptor.
 
537
   *
 
538
   * @param ec Set to indicate what error occurred, if any.
 
539
   *
 
540
   * @sa GettableSocketOption @n
 
541
   * asio::socket_base::reuse_address
 
542
   *
 
543
   * @par Example
 
544
   * Getting the value of the SOL_SOCKET/SO_REUSEADDR option:
 
545
   * @code
 
546
   * asio::ip::tcp::acceptor acceptor(io_service);
 
547
   * ...
 
548
   * asio::ip::tcp::acceptor::reuse_address option;
 
549
   * asio::error_code ec;
 
550
   * acceptor.get_option(option, ec);
 
551
   * if (ec)
 
552
   * {
 
553
   *   // An error occurred.
 
554
   * }
 
555
   * bool is_set = option.get();
 
556
   * @endcode
 
557
   */
 
558
  template <typename GettableSocketOption>
 
559
  asio::error_code get_option(GettableSocketOption& option,
 
560
      asio::error_code& ec)
 
561
  {
 
562
    return this->service.get_option(this->implementation, option, ec);
 
563
  }
 
564
 
 
565
  /// Get the local endpoint of the acceptor.
 
566
  /**
 
567
   * This function is used to obtain the locally bound endpoint of the acceptor.
 
568
   *
 
569
   * @returns An object that represents the local endpoint of the acceptor.
 
570
   *
 
571
   * @throws asio::system_error Thrown on failure.
 
572
   *
 
573
   * @par Example
 
574
   * @code
 
575
   * asio::ip::tcp::acceptor acceptor(io_service);
 
576
   * ...
 
577
   * asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint();
 
578
   * @endcode
 
579
   */
 
580
  endpoint_type local_endpoint() const
 
581
  {
 
582
    asio::error_code ec;
 
583
    endpoint_type ep = this->service.local_endpoint(this->implementation, ec);
 
584
    asio::detail::throw_error(ec);
 
585
    return ep;
 
586
  }
 
587
 
 
588
  /// Get the local endpoint of the acceptor.
 
589
  /**
 
590
   * This function is used to obtain the locally bound endpoint of the acceptor.
 
591
   *
 
592
   * @param ec Set to indicate what error occurred, if any.
 
593
   *
 
594
   * @returns An object that represents the local endpoint of the acceptor.
 
595
   * Returns a default-constructed endpoint object if an error occurred and the
 
596
   * error handler did not throw an exception.
 
597
   *
 
598
   * @par Example
 
599
   * @code
 
600
   * asio::ip::tcp::acceptor acceptor(io_service);
 
601
   * ...
 
602
   * asio::error_code ec;
 
603
   * asio::ip::tcp::endpoint endpoint = acceptor.local_endpoint(ec);
 
604
   * if (ec)
 
605
   * {
 
606
   *   // An error occurred.
 
607
   * }
 
608
   * @endcode
 
609
   */
 
610
  endpoint_type local_endpoint(asio::error_code& ec) const
 
611
  {
 
612
    return this->service.local_endpoint(this->implementation, ec);
 
613
  }
 
614
 
 
615
  /// Accept a new connection.
 
616
  /**
 
617
   * This function is used to accept a new connection from a peer into the
 
618
   * given socket. The function call will block until a new connection has been
 
619
   * accepted successfully or an error occurs.
 
620
   *
 
621
   * @param peer The socket into which the new connection will be accepted.
 
622
   *
 
623
   * @throws asio::system_error Thrown on failure.
 
624
   *
 
625
   * @par Example
 
626
   * @code
 
627
   * asio::ip::tcp::acceptor acceptor(io_service);
 
628
   * ...
 
629
   * asio::ip::tcp::socket socket(io_service);
 
630
   * acceptor.accept(socket);
 
631
   * @endcode
 
632
   */
 
633
  template <typename SocketService>
 
634
  void accept(basic_socket<protocol_type, SocketService>& peer)
 
635
  {
 
636
    asio::error_code ec;
 
637
    this->service.accept(this->implementation, peer, 0, ec);
 
638
    asio::detail::throw_error(ec);
 
639
  }
 
640
 
 
641
  /// Accept a new connection.
 
642
  /**
 
643
   * This function is used to accept a new connection from a peer into the
 
644
   * given socket. The function call will block until a new connection has been
 
645
   * accepted successfully or an error occurs.
 
646
   *
 
647
   * @param peer The socket into which the new connection will be accepted.
 
648
   *
 
649
   * @param ec Set to indicate what error occurred, if any.
 
650
   *
 
651
   * @par Example
 
652
   * @code
 
653
   * asio::ip::tcp::acceptor acceptor(io_service);
 
654
   * ...
 
655
   * asio::ip::tcp::soocket socket(io_service);
 
656
   * asio::error_code ec;
 
657
   * acceptor.accept(socket, ec);
 
658
   * if (ec)
 
659
   * {
 
660
   *   // An error occurred.
 
661
   * }
 
662
   * @endcode
 
663
   */
 
664
  template <typename SocketService>
 
665
  asio::error_code accept(
 
666
      basic_socket<protocol_type, SocketService>& peer,
 
667
      asio::error_code& ec)
 
668
  {
 
669
    return this->service.accept(this->implementation, peer, 0, ec);
 
670
  }
 
671
 
 
672
  /// Start an asynchronous accept.
 
673
  /**
 
674
   * This function is used to asynchronously accept a new connection into a
 
675
   * socket. The function call always returns immediately.
 
676
   *
 
677
   * @param peer The socket into which the new connection will be accepted.
 
678
   * Ownership of the peer object is retained by the caller, which must
 
679
   * guarantee that it is valid until the handler is called.
 
680
   *
 
681
   * @param handler The handler to be called when the accept operation
 
682
   * completes. Copies will be made of the handler as required. The function
 
683
   * signature of the handler must be:
 
684
   * @code void handler(
 
685
   *   const asio::error_code& error // Result of operation.
 
686
   * ); @endcode
 
687
   * Regardless of whether the asynchronous operation completes immediately or
 
688
   * not, the handler will not be invoked from within this function. Invocation
 
689
   * of the handler will be performed in a manner equivalent to using
 
690
   * asio::io_service::post().
 
691
   *
 
692
   * @par Example
 
693
   * @code
 
694
   * void accept_handler(const asio::error_code& error)
 
695
   * {
 
696
   *   if (!error)
 
697
   *   {
 
698
   *     // Accept succeeded.
 
699
   *   }
 
700
   * }
 
701
   *
 
702
   * ...
 
703
   *
 
704
   * asio::ip::tcp::acceptor acceptor(io_service);
 
705
   * ...
 
706
   * asio::ip::tcp::socket socket(io_service);
 
707
   * acceptor.async_accept(socket, accept_handler);
 
708
   * @endcode
 
709
   */
 
710
  template <typename SocketService, typename AcceptHandler>
 
711
  void async_accept(basic_socket<protocol_type, SocketService>& peer,
 
712
      AcceptHandler handler)
 
713
  {
 
714
    this->service.async_accept(this->implementation, peer, 0, handler);
 
715
  }
 
716
 
 
717
  /// Accept a new connection and obtain the endpoint of the peer
 
718
  /**
 
719
   * This function is used to accept a new connection from a peer into the
 
720
   * given socket, and additionally provide the endpoint of the remote peer.
 
721
   * The function call will block until a new connection has been accepted
 
722
   * successfully or an error occurs.
 
723
   *
 
724
   * @param peer The socket into which the new connection will be accepted.
 
725
   *
 
726
   * @param peer_endpoint An endpoint object which will receive the endpoint of
 
727
   * the remote peer.
 
728
   *
 
729
   * @throws asio::system_error Thrown on failure.
 
730
   *
 
731
   * @par Example
 
732
   * @code
 
733
   * asio::ip::tcp::acceptor acceptor(io_service);
 
734
   * ...
 
735
   * asio::ip::tcp::socket socket(io_service);
 
736
   * asio::ip::tcp::endpoint endpoint;
 
737
   * acceptor.accept(socket, endpoint);
 
738
   * @endcode
 
739
   */
 
740
  template <typename SocketService>
 
741
  void accept(basic_socket<protocol_type, SocketService>& peer,
 
742
      endpoint_type& peer_endpoint)
 
743
  {
 
744
    asio::error_code ec;
 
745
    this->service.accept(this->implementation, peer, &peer_endpoint, ec);
 
746
    asio::detail::throw_error(ec);
 
747
  }
 
748
 
 
749
  /// Accept a new connection and obtain the endpoint of the peer
 
750
  /**
 
751
   * This function is used to accept a new connection from a peer into the
 
752
   * given socket, and additionally provide the endpoint of the remote peer.
 
753
   * The function call will block until a new connection has been accepted
 
754
   * successfully or an error occurs.
 
755
   *
 
756
   * @param peer The socket into which the new connection will be accepted.
 
757
   *
 
758
   * @param peer_endpoint An endpoint object which will receive the endpoint of
 
759
   * the remote peer.
 
760
   *
 
761
   * @param ec Set to indicate what error occurred, if any.
 
762
   *
 
763
   * @par Example
 
764
   * @code
 
765
   * asio::ip::tcp::acceptor acceptor(io_service);
 
766
   * ...
 
767
   * asio::ip::tcp::socket socket(io_service);
 
768
   * asio::ip::tcp::endpoint endpoint;
 
769
   * asio::error_code ec;
 
770
   * acceptor.accept(socket, endpoint, ec);
 
771
   * if (ec)
 
772
   * {
 
773
   *   // An error occurred.
 
774
   * }
 
775
   * @endcode
 
776
   */
 
777
  template <typename SocketService>
 
778
  asio::error_code accept(
 
779
      basic_socket<protocol_type, SocketService>& peer,
 
780
      endpoint_type& peer_endpoint, asio::error_code& ec)
 
781
  {
 
782
    return this->service.accept(this->implementation, peer, &peer_endpoint, ec);
 
783
  }
 
784
 
 
785
  /// Start an asynchronous accept.
 
786
  /**
 
787
   * This function is used to asynchronously accept a new connection into a
 
788
   * socket, and additionally obtain the endpoint of the remote peer. The
 
789
   * function call always returns immediately.
 
790
   *
 
791
   * @param peer The socket into which the new connection will be accepted.
 
792
   * Ownership of the peer object is retained by the caller, which must
 
793
   * guarantee that it is valid until the handler is called.
 
794
   *
 
795
   * @param peer_endpoint An endpoint object into which the endpoint of the
 
796
   * remote peer will be written. Ownership of the peer_endpoint object is
 
797
   * retained by the caller, which must guarantee that it is valid until the
 
798
   * handler is called.
 
799
   *
 
800
   * @param handler The handler to be called when the accept operation
 
801
   * completes. Copies will be made of the handler as required. The function
 
802
   * signature of the handler must be:
 
803
   * @code void handler(
 
804
   *   const asio::error_code& error // Result of operation.
 
805
   * ); @endcode
 
806
   * Regardless of whether the asynchronous operation completes immediately or
 
807
   * not, the handler will not be invoked from within this function. Invocation
 
808
   * of the handler will be performed in a manner equivalent to using
 
809
   * asio::io_service::post().
 
810
   */
 
811
  template <typename SocketService, typename AcceptHandler>
 
812
  void async_accept(basic_socket<protocol_type, SocketService>& peer,
 
813
      endpoint_type& peer_endpoint, AcceptHandler handler)
 
814
  {
 
815
    this->service.async_accept(this->implementation,
 
816
        peer, &peer_endpoint, handler);
 
817
  }
 
818
};
 
819
 
 
820
} // namespace asio
 
821
 
 
822
#include "asio/detail/pop_options.hpp"
 
823
 
 
824
#endif // ASIO_BASIC_SOCKET_ACCEPTOR_HPP