11#include < nodepp/nodepp.h>
22#include < nodepp/worker.h>
3- #include < nodepp/http.h>
4- #include < nodepp/date.h>
5-
6- /* ────────────────────────────────────────────────────────────────────────────*/
3+ #include < nodepp/timer.h>
4+ #include < nodepp/atomic.h>
75
86using namespace nodepp ;
97
10- /* ────────────────────────────────────────────────────────────────────────────*/
11-
12- inline tcp_t start_http_server (){
13-
14- return http::server ([=]( http_t cli ){
15-
16- cli.write_header ( 200 , header_t ({
17- { " content-type" , " text/html" }
18- }) );
19-
20- if ( cli.method == " POST" && cli.headers .has (" Content-Length" ) ){
21- ulong len = string::to_ulong ( cli.headers [" Content-Length" ] );
22-
23- ptr_t <ulong> tmp ( 0UL );
24- ptr_t <generator::file::read> _read_ ( 0UL );
25-
26- process::add ( coroutine::add ( COROUTINE (){
27- coBegin
28-
29- while ( !cli.is_closed () && *tmp < len ){
30- if ( (*_read_)( &cli, len - *tmp )==1 ){ coNext; }
31- console::log ( " stamp" , _read_->data .size (), _read_->data );
32- *tmp += _read_->data .size (); }
33-
34- cli.write ( string::format ( " message-size: %d" , len ) );
35- cli.close ();
36-
37- coFinish
38- }));
8+ void onWorker (){
399
40- } else {
10+ worker::add ([=]() {
4111
42- cli.write ( " hello world" );
43-
44- }
45-
46- });
47-
48- }
49-
50- /* ────────────────────────────────────────────────────────────────────────────*/
51-
52- inline void start_http_client (){
53-
54- file_t file ( " LICENSE" , " r" );
55- fetch_t arg;
56- arg.url = rand ()%2 ==0 ? " http://localhost:8000" : " http://[localhost]:8000" ;
57- arg.method = rand ()%2 ==0 ? " POST" : " GET" ;
58- arg.headers = header_t ({
59- { " content-length" , string::format ( " %lu" , file.size () ) }
60- });
12+ process::add ( coroutine::add ( COROUTINE (){
13+ coBegin
6114
62- http::fetch ( arg, nullptr , [=]( http_t cli ){
63- if ( arg.method == " GET" ){ return ; }
64- file.onData ([=]( string_t data ){
65- cli.write ( data );
66- }); stream::pipe ( file );
67- })
15+ while ( true ){
16+ console::log ( " hello world" , (void *) worker::pid () );
17+ coDelay ( rand () % 1000 ); process::clear (); }
6818
69- .then ([=]( http_t cli ){
70- console::log ( " <<" , cli.read () );
71- })
19+ coFinish
20+ }));
7221
73- .fail ([=]( except_t err ){
74- console::error ( err.what () );
75- });
22+ process::wait (); return -1 ; });
7623
7724}
7825
79- /* ────────────────────────────────────────────────────────────────────────────*/
80-
8126void onMain (){
8227
83- promise_t <null_t ,except_t >([=](
84- res_t <null_t > res, rej_t <except_t > rej
85- ){
86-
87- worker::add ([=](){
88-
89- tcp_t server = start_http_server ();
90-
91- server.onError .once ([=]( except_t err ){
92- rej ( err );
93- });
94-
95- server.listen ( " [::0]" , 8000 , [=]( socket_t ){
96- res ( nullptr );
97- });
98-
99- process::wait (); return -1 ; });
100-
101- })
102-
103- .then ([=]( null_t /* unused*/ ){
104- console::done ( " server started successfuly" );
28+ process::add ( coroutine::add ( COROUTINE (){
29+ coBegin
10530
106- worker::add ( coroutine::add ( COROUTINE (){
107- coBegin
108-
109- while ( true ){
110- start_http_client ();
111- process::wait (); }
31+ while ( true ){
32+ while ( process::size () > MAX_BATCH ){ coDelay (100 ); }
33+ console::log ( " --->" , process::size () );
34+ onWorker ();
35+ coNext; }
11236
113- coFinish
114- }));
115-
116- })
117-
118- .fail ([=]( except_t err ){
119- console::error ( err.what () );
120- });
121-
122- }
37+ coFinish
38+ }));
12339
124- /* ──────────────────────────────────────────────────────────────────────────── */
40+ }
0 commit comments