@@ -286,7 +286,7 @@ namespace SimpleWeb {
286
286
Config (unsigned short port) noexcept : port(port) {}
287
287
288
288
public:
289
- // / Port number to use. Defaults to 80 for HTTP and 443 for HTTPS.
289
+ // / Port number to use. Defaults to 80 for HTTP and 443 for HTTPS. Set to 0 get an assigned port.
290
290
unsigned short port;
291
291
// / If io_service is not set, number of threads that the server will use when start() is called.
292
292
// / Defaults to 1 thread.
@@ -332,35 +332,43 @@ namespace SimpleWeb {
332
332
// / If you have your own asio::io_service, store its pointer here before running start().
333
333
std::shared_ptr<asio::io_service> io_service;
334
334
335
- virtual unsigned short bindAndPrepare () {
336
- if (!io_service) {
337
- io_service = std::make_shared<asio::io_service>();
338
- internal_io_service = true ;
339
- }
340
-
341
- if (io_service->stopped ())
342
- io_service->reset ();
343
-
335
+ // / If you know the server port in advance, use start() instead.
336
+ // / Returns assigned port. If io_service is not set, an internal io_service is created instead.
337
+ // / Call before accept_and_run().
338
+ unsigned short bind () {
344
339
asio::ip::tcp::endpoint endpoint;
345
340
if (config.address .size () > 0 )
346
341
endpoint = asio::ip::tcp::endpoint (asio::ip::address::from_string (config.address ), config.port );
347
342
else
348
343
endpoint = asio::ip::tcp::endpoint (asio::ip::tcp::v4 (), config.port );
349
344
345
+ if (!io_service) {
346
+ io_service = std::make_shared<asio::io_service>();
347
+ internal_io_service = true ;
348
+ }
349
+
350
350
if (!acceptor)
351
351
acceptor = std::unique_ptr<asio::ip::tcp::acceptor>(new asio::ip::tcp::acceptor (*io_service));
352
352
acceptor->open (endpoint.protocol ());
353
353
acceptor->set_option (asio::socket_base::reuse_address (config.reuse_address ));
354
354
acceptor->bind (endpoint);
355
- acceptor->listen ();
356
355
357
- accept ();
356
+ after_bind ();
358
357
359
358
return acceptor->local_endpoint ().port ();
360
359
}
361
360
362
- virtual void runServer () {
361
+ // / If you know the server port in advance, use start() instead.
362
+ // / Accept requests, and if io_service was not set before calling bind(), run the internal io_service instead.
363
+ // / Call after bind().
364
+ void accept_and_run () {
365
+ acceptor->listen ();
366
+ accept ();
367
+
363
368
if (internal_io_service) {
369
+ if (io_service->stopped ())
370
+ io_service->reset ();
371
+
364
372
// If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling
365
373
threads.clear ();
366
374
for (std::size_t c = 1 ; c < config.thread_pool_size ; c++) {
@@ -379,9 +387,10 @@ namespace SimpleWeb {
379
387
}
380
388
}
381
389
382
- virtual void start () {
383
- bindAndPrepare ();
384
- runServer ();
390
+ // / Start the server by calling bind() and accept_and_run()
391
+ void start () {
392
+ bind ();
393
+ accept_and_run ();
385
394
}
386
395
387
396
// / Stop accepting new requests, and close current connections.
@@ -420,6 +429,7 @@ namespace SimpleWeb {
420
429
421
430
ServerBase (unsigned short port) noexcept : config(port), connections(new std::unordered_set<Connection *>()), connections_mutex(new std::mutex()), handler_runner(new ScopeRunner()) {}
422
431
432
+ virtual void after_bind () {}
423
433
virtual void accept () = 0;
424
434
425
435
template <typename ... Args>
0 commit comments