2
3
6
7
8
12#include <restinio/impl/ioctx_on_thread_pool.hpp>
14#include <restinio/http_server.hpp>
23
24
25
26
27
37
38
39
40
41
42
43
44
45
46
47
48
56
57
58
59
60
61
62
63
64
65
66
67
78
79
80
81
82
83
84
85template<
typename Traits>
86class run_on_this_thread_settings_t final
88 run_on_this_thread_settings_t<Traits>,
92 run_on_this_thread_settings_t<Traits>, Traits>;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
122run_on_this_thread_settings_t<Traits>
129
130
131
132
133
134
135
136template<
typename Traits>
137class run_on_thread_pool_settings_t final
139 run_on_thread_pool_settings_t<Traits>,
149 std::size_t pool_size )
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
182run_on_thread_pool_settings_t<Traits>
185 std::size_t pool_size )
187 return run_on_thread_pool_settings_t<Traits>( pool_size );
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214template<
typename Traits>
219 asio_ns::io_context & ioctx,
221 run_on_this_thread_settings_t<Traits> && settings )
223 using settings_t = run_on_this_thread_settings_t<Traits>;
228 std::forward<settings_t>(settings) };
230 std::exception_ptr exception_caught;
232 asio_ns::signal_set break_signals{ server.io_context(), SIGINT };
233 break_signals.async_wait(
234 [&](
const asio_ns::error_code & ec,
int ){
242 [&exception_caught]( std::exception_ptr ex ){
245 exception_caught = ex;
252 [&ioctx, &exception_caught]( std::exception_ptr ex ){
257 exception_caught = ex;
263 if( exception_caught )
264 std::rethrow_exception( exception_caught );
269
270
271
272
273
274
275
276
277
278
279
280
281template<
typename Traits>
284 run_on_this_thread_settings_t<Traits> && settings )
286 asio_ns::io_context io_context;
287 run( io_context, std::move(settings) );
293
294
295
296
297
298
299
300
301template<
typename Io_Context_Holder,
typename Traits>
305 run_on_thread_pool_settings_t<Traits> && settings )
307 using settings_t = run_on_thread_pool_settings_t<Traits>;
312 std::forward<settings_t>(settings) };
314 std::exception_ptr exception_caught;
316 asio_ns::signal_set break_signals{ server.io_context(), SIGINT };
317 break_signals.async_wait(
318 [&](
const asio_ns::error_code & ec,
int ){
326 [&exception_caught]( std::exception_ptr ex ){
329 exception_caught = ex;
336 [&pool, &exception_caught]( std::exception_ptr ex ){
341 exception_caught = ex;
348 if( exception_caught )
349 std::rethrow_exception( exception_caught );
356
357
358
359
360
361
362
363
364
365
366
367
368template<
typename Traits>
370run( run_on_thread_pool_settings_t<Traits> && settings )
375 thread_pool_t pool( settings.pool_size() );
377 impl::run( pool, std::move(settings) );
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399template<
typename Traits>
404 asio_ns::io_context & ioctx,
406 run_on_thread_pool_settings_t<Traits> && settings )
411 thread_pool_t pool{ settings.pool_size(), ioctx };
413 impl::run( pool, std::move(settings) );
420
421
422
423
424
425
426
427
428template<
typename Traits>
437
438
445 std::size_t pool_size,
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492template<
typename Traits>
495 std::size_t pool_size,
499 return { pool_size, break_handling, server };
505
506
507
508
509
510
511
512
513
514
515
516
517
518template<
typename Io_Context_Holder,
typename Traits>
524 std::exception_ptr exception_caught;
526 asio_ns::signal_set break_signals{ server.io_context(), SIGINT };
527 break_signals.async_wait(
528 [&](
const asio_ns::error_code & ec,
int ){
536 [&exception_caught]( std::exception_ptr ex ){
539 exception_caught = ex;
546 [&pool, &exception_caught]( std::exception_ptr ex ){
551 exception_caught = ex;
558 if( exception_caught )
559 std::rethrow_exception( exception_caught );
563
564
565
566
567
568
569
570
571
572
573
574
575template<
typename Io_Context_Holder,
typename Traits>
581 std::exception_ptr exception_caught;
585 [&pool, &exception_caught]( std::exception_ptr ex ){
590 exception_caught = ex;
597 if( exception_caught )
598 std::rethrow_exception( exception_caught );
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630template<
typename Traits>
637 thread_pool_t pool{ params.pool_size(), params.server().io_context() };
640 impl::run_with_break_signal_handling( pool, params.server() );
642 impl::run_without_break_signal_handling( pool, params.server() );
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679template<
typename Traits>
683 server.io_context().post( [&server] {
685 server.io_context().stop();
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782template<
typename Http_Server>
799 std::size_t pool_size,
803 Http_Server & server )
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
855 typename On_Ok_Callback,
856 typename On_Error_Callback >
860 On_Ok_Callback && on_ok,
868 On_Error_Callback && on_error )
870 static_assert(
noexcept(on_ok()),
"On_Ok_Callback should be noexcept" );
871 static_assert(
noexcept(on_error(std::declval<std::exception_ptr>())),
872 "On_Error_Callback should be noexcept" );
875 [callback = std::move(on_ok)]()
noexcept { callback(); },
876 [
this, callback = std::move(on_error)]( std::exception_ptr ex )
noexcept {
880 callback( std::move(ex) );
888
889
890
896 []( std::exception_ptr )
noexcept { } );
901 started()
const noexcept {
return m_pool.started(); }
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
965 stop( Error_CB error_cb = Error_CB{} )
noexcept
975 [
this, callback = std::move(error_cb)]( std::exception_ptr ex )
noexcept {
980 callback( std::move(ex) );
986
987
988
990 wait()
noexcept { m_pool.wait(); }
995template<
typename Http_Server >
1002
1003
1004
1005
1006
1007
1008template<
typename Traits >
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027template<
typename Http_Server >
1030 template<
typename Traits >
1034 server_settings_t<Traits> &&,
1035 std::size_t thread_pool_size );
1046 server_settings_t<
typename Http_Server::traits_t > && settings,
1047 std::size_t thread_pool_size )
1048 :
m_server{ std::move(io_context), std::move(settings) }
1055
1056
1057
1058
1059
1060
1064 std::promise<
void> p;
1065 auto f = p.get_future();
1067 [&p]()
noexcept { p.set_value(); },
1068 [&p]( std::exception_ptr ex )
noexcept {
1069 p.set_exception( std::move(ex) );
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1201 server_settings_t< Traits > && settings,
1202 std::size_t thread_pool_size )
1204 running_server_handle_t< Traits > handle{
1206 std::move(io_context),
1207 std::move(settings),
Basic container for http_server settings.
A class for holding a reference to external Asio's io_context.
A class for holding actual instance of Asio's io_context.
Helper class for holding shared pointer to io_context.
Helper class for running an existing HTTP-server on a thread pool without blocking the current thread...
void wait() noexcept
Wait for full stop of the server.
void start()
Start the server.
on_pool_runner_t(std::size_t pool_size, Http_Server &server)
Initializing constructor.
impl::ioctx_on_thread_pool_t< impl::external_io_context_for_thread_pool_t > m_pool
Thread pool for running the server.
on_pool_runner_t(const on_pool_runner_t &)=delete
bool started() const noexcept
Is server started.
void start(On_Ok_Callback &&on_ok, On_Error_Callback &&on_error)
Start the server with callbacks that will be called on success or failure.
void stop(Error_CB error_cb=Error_CB{}) noexcept
Stop the server.
on_pool_runner_t(on_pool_runner_t &&)=delete
Http_Server & m_server
HTTP-server to be run.
Helper type for holding parameters necessary for running HTTP-server on a thread pool.
http_server_t< Traits > * m_server
HTTP-server to be used on a thread pool.
break_signal_handling_t m_break_handling
Should break signal handler be used?
break_signal_handling_t break_handling() const noexcept
run_existing_server_on_thread_pool_t(std::size_t pool_size, break_signal_handling_t break_handling, http_server_t< Traits > &server)
Initializing constructor.
std::size_t pool_size() const noexcept
http_server_t< Traits > & server() const noexcept
std::size_t m_pool_size
Size of thread pool.
basic_server_settings_t< run_on_this_thread_settings_t< Traits >, Traits > base_type_t
std::size_t m_pool_size
Size of the pool.
std::size_t pool_size() const
Get the pool size.
run_on_thread_pool_settings_t(std::size_t pool_size)
Constructor.
A helper class used in an implementation of run_async function.
void start()
Start the HTTP-server.
void wait() noexcept
Wait for the shutdown of HTTP-server.
friend running_server_handle_t< Traits > run_async(io_context_holder_t, server_settings_t< Traits > &&, std::size_t thread_pool_size)
Creates an instance of HTTP-server and launches it on a separate thread or thread pool.
Http_Server m_server
Actual server instance.
running_server_instance_t(io_context_holder_t io_context, server_settings_t< typename Http_Server::traits_t > &&settings, std::size_t thread_pool_size)
Initializing constructor.
on_pool_runner_t< Http_Server > m_runner
The runner of the server.
void run_without_break_signal_handling(ioctx_on_thread_pool_t< Io_Context_Holder > &pool, http_server_t< Traits > &server)
An implementation of run-function for thread pool case with existing http_server instance.
void run(ioctx_on_thread_pool_t< Io_Context_Holder > &pool, run_on_thread_pool_settings_t< Traits > &&settings)
An implementation of run-function for thread pool case.
void run_with_break_signal_handling(ioctx_on_thread_pool_t< Io_Context_Holder > &pool, http_server_t< Traits > &server)
An implementation of run-function for thread pool case with existing http_server instance.
constexpr break_signal_handling_t skip_break_signal_handling() noexcept
Make the indicator for absence of break signal handler.
run_existing_server_on_thread_pool_t< Traits > on_thread_pool(std::size_t pool_size, break_signal_handling_t break_handling, http_server_t< Traits > &server)
Helper function for running an existing HTTP-server on a thread pool.
run_on_thread_pool_settings_t< Traits > on_thread_pool(std::size_t pool_size)
A special marker for the case when http_server must be run on an thread pool.
void run(run_on_this_thread_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
run_on_this_thread_settings_t< Traits > on_this_thread()
A special marker for the case when http_server must be run on the context of the current thread.
io_context_holder_t external_io_context(asio_ns::io_context &ctx)
Function which tells that http_server should use external instance of io_context and should not contr...
running_server_handle_t< Traits > run_async(io_context_holder_t io_context, server_settings_t< Traits > &&settings, std::size_t thread_pool_size)
Creates an instance of HTTP-server and launches it on a separate thread or thread pool.
void run(run_on_thread_pool_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
void run(asio_ns::io_context &ioctx, run_on_thread_pool_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
constexpr break_signal_handling_t use_break_signal_handling() noexcept
Make the indicator for usage of break signal handler.
void run(run_existing_server_on_thread_pool_t< Traits > &¶ms)
Helper function for running an existing HTTP-server on a thread pool.
void run(asio_ns::io_context &ioctx, run_on_this_thread_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
break_signal_handling_t
Indication of usage of break signal handlers for some forms of run functions.
@ used
Signal handler should be used by run() function.
@ skipped
Signal handler should not be used by run() function.
void initiate_shutdown(http_server_t< Traits > &server)
Helper function for initiation of server shutdown.
Type of a function to be used as the default on_error-callback.
void operator()(std::exception_ptr) const noexcept