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/channel.h>
74
85using namespace nodepp ;
96
10- /* ────────────────────────────────────────────────────────────────────────────*/
7+ channel_t <coroutine_t > ch1, ch2;
8+
9+ int worker_1 (){
10+ coStart
1111
12- string_t format ( string_t method, string_t path, string_t data ) {
13- return regex::format ( " .${0}.${1}.${2}." ,
14- !method.empty () ? encoder::base64::atob (method) : " " ,
15- !path .empty () ? encoder::base64::atob (path) : " " ,
16- !data .empty () ? encoder::base64::atob (data) : " "
17- );
18- }
12+ while ( true ){
13+
14+ ch2.write ( coroutine_t ( COROUTINE (){
15+ console::log ( " worker1 -> worker2" , process::now () );
16+ return -1 ; }) );
1917
20- /* ────────────────────────────────────────────────────────────────────────────*/
18+ if ( !ch1.empty () ){
19+ for ( auto x: ch1.read () ){
20+ process::add ( x );
21+ }}
2122
22- void next ( string_t message ) {
23-
24- queue_t <ulong> raw;
23+ coDelay (1000 ); }
2524
26- for ( ulong x= 0 ; x<message. size (); x++ ){
27- if ( message[x] != ' . ' ){ continue ; } raw. push ( x ); }
25+ coStop
26+ }
2827
29- auto idx = array_t <ulong>( raw.data () );
28+ int worker_2 (){
29+ coStart
3030
31- while ( !idx.empty () ){ auto tmp = idx.splice ( 0 , 4 );
32- if ( tmp.size ()<4 ){ break ; }
31+ while ( true ){
3332
34- auto method = encoder::base64::btoa ( message. slice_view ( tmp[ 0 ]+ 1 , tmp[ 1 ] ) );
35- auto path = encoder::base64::btoa ( message. slice_view ( tmp[ 1 ]+ 1 , tmp[ 2 ] ) );
36- auto msg = encoder::base64::btoa ( message. slice_view ( tmp[ 2 ]+ 1 , tmp[ 3 ] ) );
33+ ch1. write ( coroutine_t ( COROUTINE (){
34+ console::log ( " worker2 -> worker1 " , process::now ( ) );
35+ return - 1 ; } ) );
3736
38- console::log (
39- tmp[0 ], tmp[3 ],
40- " mth:" , method,
41- " pth:" , path ,
42- " msg:" , msg
43- );
37+ if ( !ch2.empty () ){
38+ for ( auto x: ch2.read () ){
39+ process::add ( x );
40+ }}
4441
45- }
42+ coDelay ( 1000 ); }
4643
44+ coStop
4745}
4846
49- /* ────────────────────────────────────────────────────────────────────────────*/
50-
5147void onMain (){
5248
53- next (
54- format ( " aa" , " bb" , " cc" )+
55- format ( " aa" , " bb" , " cc" )+
56- format ( " aa" , " bb" , " cc" )
57- );
49+ worker::add ([=](){
50+ process::add ([=](){ return worker_1 (); });
51+ process::wait ();
52+ return -1 ; });
5853
59- }
54+ worker::add ([=](){
55+ process::add ([=](){ return worker_2 (); });
56+ process::wait ();
57+ return -1 ; });
6058
61- /* ──────────────────────────────────────────────────────────────────────────── */
59+ }
0 commit comments