2
3
6
7
8
12#include <restinio/impl/ioctx_on_thread_pool.hpp>
14#include <restinio/http_server.hpp>
16#if defined(RESTINIO_DEFAULT_BREAK_SIGNALS_LIST)
17#error "RESTINIO_DEFAULT_BREAK_SIGNALS_LIST symbol should not be defined by user"
19#define RESTINIO_DEFAULT_BREAK_SIGNALS_LIST SIGINT
,SIGTERM
29
30
31
32
33
43
44
45
46
47
48
49
50
51
52
53
54
62
63
64
65
66
67
68
69
70
71
72
73
84
85
86
87
88
89
90
91template<
typename Traits>
92class run_on_this_thread_settings_t final
94 run_on_this_thread_settings_t<Traits>,
98 run_on_this_thread_settings_t<Traits>, Traits>;
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
128run_on_this_thread_settings_t<Traits>
135
136
137
138
139
140
141
142template<
typename Traits>
143class run_on_thread_pool_settings_t final
145 run_on_thread_pool_settings_t<Traits>,
155 std::size_t pool_size )
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
188run_on_thread_pool_settings_t<Traits>
191 std::size_t pool_size )
193 return run_on_thread_pool_settings_t<Traits>( pool_size );
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220template<
typename Traits>
225 asio_ns::io_context & ioctx,
227 run_on_this_thread_settings_t<Traits> && settings )
229 using settings_t = run_on_this_thread_settings_t<Traits>;
234 std::forward<settings_t>(settings) };
236 std::exception_ptr exception_caught;
238 asio_ns::signal_set break_signals{
242 break_signals.async_wait(
243 [&](
const asio_ns::error_code & ec,
int ){
251 [&exception_caught]( std::exception_ptr ex ){
254 exception_caught = ex;
261 [&ioctx, &exception_caught]( std::exception_ptr ex ){
266 exception_caught = ex;
272 if( exception_caught )
273 std::rethrow_exception( exception_caught );
278
279
280
281
282
283
284
285
286
287
288
289
290template<
typename Traits>
293 run_on_this_thread_settings_t<Traits> && settings )
295 asio_ns::io_context io_context;
296 run( io_context, std::move(settings) );
302
303
304
305
306
307
308
309
310template<
typename Io_Context_Holder,
typename Traits>
314 run_on_thread_pool_settings_t<Traits> && settings )
316 using settings_t = run_on_thread_pool_settings_t<Traits>;
321 std::forward<settings_t>(settings) };
323 std::exception_ptr exception_caught;
325 asio_ns::signal_set break_signals{
329 break_signals.async_wait(
330 [&](
const asio_ns::error_code & ec,
int ){
338 [&exception_caught]( std::exception_ptr ex ){
341 exception_caught = ex;
348 [&pool, &exception_caught]( std::exception_ptr ex ){
353 exception_caught = ex;
360 if( exception_caught )
361 std::rethrow_exception( exception_caught );
368
369
370
371
372
373
374
375
376
377
378
379
380template<
typename Traits>
382run( run_on_thread_pool_settings_t<Traits> && settings )
387 thread_pool_t pool( settings.pool_size() );
389 impl::run( pool, std::move(settings) );
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411template<
typename Traits>
416 asio_ns::io_context & ioctx,
418 run_on_thread_pool_settings_t<Traits> && settings )
423 thread_pool_t pool{ settings.pool_size(), ioctx };
425 impl::run( pool, std::move(settings) );
432
433
434
435
436
437
438
439
440template<
typename Traits>
449
450
457 std::size_t pool_size,
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504template<
typename Traits>
507 std::size_t pool_size,
511 return { pool_size, break_handling, server };
517
518
519
520
521
522
523
524
525
526
527
528
529
530template<
typename Io_Context_Holder,
typename Traits>
536 std::exception_ptr exception_caught;
538 asio_ns::signal_set break_signals{
542 break_signals.async_wait(
543 [&](
const asio_ns::error_code & ec,
int ){
551 [&exception_caught]( std::exception_ptr ex ){
554 exception_caught = ex;
561 [&pool, &exception_caught]( std::exception_ptr ex ){
566 exception_caught = ex;
573 if( exception_caught )
574 std::rethrow_exception( exception_caught );
578
579
580
581
582
583
584
585
586
587
588
589
590template<
typename Io_Context_Holder,
typename Traits>
596 std::exception_ptr exception_caught;
600 [&pool, &exception_caught]( std::exception_ptr ex ){
605 exception_caught = ex;
612 if( exception_caught )
613 std::rethrow_exception( exception_caught );
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645template<
typename Traits>
652 thread_pool_t pool{ params.pool_size(), params.server().io_context() };
655 impl::run_with_break_signal_handling( pool, params.server() );
657 impl::run_without_break_signal_handling( pool, params.server() );
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694template<
typename Traits>
702 server.io_context().stop();
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799template<
typename Http_Server>
816 std::size_t pool_size,
820 Http_Server & server )
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
872 typename On_Ok_Callback,
873 typename On_Error_Callback >
877 On_Ok_Callback && on_ok,
885 On_Error_Callback && on_error )
887 static_assert(
noexcept(on_ok()),
"On_Ok_Callback should be noexcept" );
888 static_assert(
noexcept(on_error(std::declval<std::exception_ptr>())),
889 "On_Error_Callback should be noexcept" );
892 [callback = std::move(on_ok)]()
noexcept { callback(); },
893 [
this, callback = std::move(on_error)]( std::exception_ptr ex )
noexcept {
897 callback( std::move(ex) );
905
906
907
913 []( std::exception_ptr )
noexcept { } );
918 started()
const noexcept {
return m_pool.started(); }
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
982 stop( Error_CB error_cb = Error_CB{} )
noexcept
992 [
this, callback = std::move(error_cb)]( std::exception_ptr ex )
noexcept {
997 callback( std::move(ex) );
1003
1004
1005
1012template<
typename Http_Server >
1019
1020
1021
1022
1023
1024
1025template<
typename Traits >
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044template<
typename Http_Server >
1047 template<
typename Traits >
1051 server_settings_t<Traits> &&,
1052 std::size_t thread_pool_size );
1063 server_settings_t<
typename Http_Server::traits_t > && settings,
1064 std::size_t thread_pool_size )
1065 :
m_server{ std::move(io_context), std::move(settings) }
1072
1073
1074
1075
1076
1077
1081 std::promise<
void> p;
1082 auto f = p.get_future();
1084 [&p]()
noexcept { p.set_value(); },
1085 [&p]( std::exception_ptr ex )
noexcept {
1086 p.set_exception( std::move(ex) );
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
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
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1218 server_settings_t< Traits > && settings,
1219 std::size_t thread_pool_size )
1221 running_server_handle_t< Traits > handle{
1223 std::move(io_context),
1224 std::move(settings),
1235#undef RESTINIO_DEFAULT_BREAK_SIGNALS_LIST
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.
#define RESTINIO_DEFAULT_BREAK_SIGNALS_LIST
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