Skip to content

Commit 933d8b3

Browse files
committed
📌 Nodepp | V1.4.4 📌
1 parent 3d0c38f commit 933d8b3

13 files changed

Lines changed: 215 additions & 118 deletions

File tree

benchmark/stress-test/10-POST.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@ void server() {
1313
cli.write( "not allowed" ); return;
1414
}
1515

16-
cli.onDrain([=](){ console::log( "closed from server" ); });
17-
16+
cli.onClose.once([=](){ console::log( "closed from server" ); });
1817
cli.read_body()
1918

2019
.then([=]( http_t cli ){
21-
console::log( "->", cli.body );
20+
21+
string_t msg = "received";
22+
2223
cli.write_header( 200, header_t({
23-
{ "Transfer-Encoding", "chunked" }
24+
{ "Transfer-Encoding", "chunked" },
25+
{ "Content-Length", string::to_string( msg.size() ) }
2426
}) );
25-
cli.write( "received" );
27+
28+
console::log( "AAA>>", cli.body );
29+
cli.write( msg );
30+
2631
})
2732

2833
.fail([=]( except_t err ){
@@ -44,20 +49,18 @@ void client() {
4449
args.method = "POST";
4550
args.url = "http://localhost:8000/";
4651
args.headers = header_t({
47-
{ "Host", url::host(args.url) }
48-
{ "Transfer-Encoding", "chunked" }
52+
{ "Host", url::host(args.url) },
4953
});
5054

5155
http::fetch( args )
5256

5357
.then([]( http_t cli ){
5458

55-
cli.onDrain([=](){ console::log( "closed from client" ); });
56-
59+
cli.onClose.once([=](){ console::log( "closed from client" ); });
5760
cli.read_body( 1000UL )
5861

5962
.then([=]( http_t cli ){
60-
console::log( ">>", cli.body );
63+
console::log( "BBB>>", cli.body );
6164
});
6265

6366
})

examples/8-Observer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ void onMain(){
1111
{ "var3", 69.f }
1212
});
1313

14-
obj.on( "var1", [=]( observer_t self, any_t before, any_t after ){
14+
obj.on( "var1", [=]( ptr_t<observer_t> self, any_t before, any_t after ){
1515
console::log( "var1:", after.as<string_t>() );
1616
});
1717

18-
obj.on( "var2", [=]( observer_t self, any_t before, any_t after ){
18+
obj.on( "var2", [=]( ptr_t<observer_t> self, any_t before, any_t after ){
1919
console::log( "var2:", after.as<int>() );
2020
});
2121

22-
obj.on( "var3", [=]( observer_t self, any_t before, any_t after ){
22+
obj.on( "var3", [=]( ptr_t<observer_t> self, any_t before, any_t after ){
2323
console::log( "var3:", after.as<float>() );
2424
});
2525

include/nodepp/generator.h

Lines changed: 118 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ namespace nodepp { namespace generator { namespace file {
2727
if ( !fd->is_available() ) { coEnd; } r=fd->get_range();
2828
if ( r[1] != 0 ){ auto pos=fd->pos(); d=min( r[1]-r[0], (len_t)size );
2929
if ( pos < r[0] ){ fd->del_borrow(); fd->pos( r[0] ); }
30-
elif( pos >=r[1] ){ fd->close(); coEnd; }} else {
30+
elif( pos >=r[1] ){ coEnd; }} else {
3131
d = (len_t) min( fd->get_buffer_size(), size );
3232
}
3333

3434
if( fd->get_borrow().empty() ){
3535
coWait((state=fd->_read( fd->get_buffer_data(), fd->get_buffer_size() ))==-2);
36-
if( state <= 0 ) { fd->close(); coEnd; } else {
36+
if( state <= 0 ) { coEnd; } else {
3737
fd->set_borrow( string_t( fd->get_buffer_data(), state ) );
3838
}}
3939

@@ -54,95 +54,116 @@ namespace nodepp { namespace generator { namespace file {
5454
if(!fd->is_available() || msg.empty() ){ coEnd; }
5555

5656
do{ coWait((state=fd->_write( msg.data()+data, msg.size()-data ))==-2 );
57-
if( state<=0 ){ fd->close() ; coEnd; } else {
57+
if( state<=0 ){ coEnd; } else {
5858
data = min( data + state, msg.size() );
5959
} } while( data < msg.size() );
6060

6161
coFinish }};
6262

6363
/*─······································································─*/
6464

65-
GENERATOR( until ){
65+
GENERATOR( split ){
6666
protected: ulong pos ; file::read _read;
67-
public: ulong state; string_t data ;
67+
public: ulong state; string_t data;
6868

6969
template< class T > coEmit( T* fd, string_t ch ){
7070
coBegin; state=0; pos=0; data.clear();
7171

72-
coWait( _read(fd) ==1 );
73-
if( _read.state<=0 )
74-
{ state = data.size(); coEnd; }
75-
fd->set_borrow( _read.data );
72+
do{ auto &bff = fd->get_borrow(); if( !bff.empty() ){
7673

77-
do{ for( auto x: _read.data ){ ++state;
74+
do{ for( auto &x:bff ){ ++state;
7875
if ( ch[pos] ==x ){ ++pos; } else { pos=0; }
7976
if ( ch.size()==pos ){ break; } }
8077
} while(0);
8178

82-
if( memcmp( _read.data.get(), ch.get(), ch.size() )==0 ){
83-
auto &x = fd->get_borrow();
84-
data= x.slice( 0, ch.size() );
85-
/*-*/ x.ptr().slice( ch.size(), (ulong) -1 );
79+
if( memcmp( bff.get(), ch.get(), ch.size() )==0 ){
80+
data= bff.slice( 0, ch.size() );
8681
} elif( state > pos ) {
87-
auto &x = fd->get_borrow();
88-
data= x.slice( 0, state - pos );
89-
/*-*/ x.ptr().slice( state - pos, (ulong) -1 );
82+
data= bff.slice( 0, state - pos );
9083
} else {
91-
auto &x = fd->get_borrow();
92-
data= x.slice( 0, state );
93-
/*-*/ x.ptr().slice( state, (ulong) -1 );
94-
}
84+
data= bff.slice( 0, state );
85+
}
9586

96-
state = data.size();
87+
fd->get_borrow().ptr().slice( data.size(), (ulong) -1 );
88+
state = data.size();
89+
90+
return -1; }} while(0); coNext;
9791

98-
coFinish }
92+
coWait( _read( fd )==1 );
93+
if( _read.state<=0 ){ state=0; coEnd; }
94+
fd->set_borrow ( _read.data ); coGoto(0);
9995

100-
template< class T > coEmit( T* fd, char ch ){
101-
coBegin; data.clear(); coYield(1); state=0;
96+
coFinish }};
10297

103-
coWait( _read(fd) ==1 );
104-
if( _read.state<=0 )
105-
{ state = data.size(); coEnd; }
106-
fd->set_borrow( _read.data );
98+
/*─······································································─*/
10799

108-
do{ for( auto x: _read.data ){ ++state;
109-
if ( ch ==x ){ break; } continue; }
110-
} while(0);
100+
GENERATOR( until ){
101+
protected: string_t borrow; file::read _read;
102+
public: string_t data ; ulong state;
111103

112-
do{ auto &x = fd->get_borrow();
113-
data += x.slice( 0, state );
114-
/*---*/ x.ptr().slice( state, (ulong) -1 );
115-
state = data.size();
116-
} while(0);
104+
template< class T > coEmit( T* fd, string_t ch ){
105+
coBegin data.clear(); state = 0UL;
106+
107+
do{ /*----------------*/ auto &bff = fd->get_borrow();
108+
if( !bff.empty() ){ do { auto pos = bff.find ( ch );
109+
110+
if( pos.null() ){
111+
if( bff.size() > NODEPP_UNBFF_SIZE ){
112+
data = type::move( fd->get_borrow() );
113+
state= data.size();
114+
return -1; } break; }
115+
116+
data = bff.slice( 0, pos[0] );
117+
state= data.size();
118+
119+
fd->get_borrow().ptr().slice ( pos[1], (ulong) -1 );
120+
121+
return state==0 ? 1 : -1; } while(0); }
122+
123+
borrow = type::move( fd->get_borrow() );
124+
125+
} while(0); coNext;
117126

118-
if( data[ data.size()-1 ] == ch ){ coEnd; }
127+
coWait( _read( fd )==1 ); if( _read.state<=0 ){
128+
data = borrow; state = data.size(); coEnd;
129+
} fd->set_borrow ( borrow+_read.data ); coGoto(0);
119130

120-
coGoto(1) ; coFinish }};
131+
coFinish }};
121132

122133
/*─······································································─*/
123134

124135
GENERATOR( line ){
125-
protected: file::read _read;
126-
public: ulong state; string_t data;
136+
protected: string_t borrow; file::read _read;
137+
public: string_t data ; ulong state;
127138

128139
template< class T > coEmit( T* fd ){
129-
coBegin data.clear(); coYield(1); state=0;
140+
coBegin data.clear(); state = 0UL;
130141

131-
coWait( _read( fd )==1 );
132-
if ( _read.state<=0 ){ state = data.size(); coEnd; }
142+
do{ /*----------------*/ auto &bff = fd->get_borrow();
143+
if( !bff.empty() ){ do { auto pos = bff.find ('\n');
144+
145+
if( pos.null() ){
146+
if( bff.size() > NODEPP_UNBFF_SIZE ){
147+
data = type::move( fd->get_borrow() );
148+
state= data.size();
149+
return -1; } break; }
133150

134-
fd->set_borrow(_read.data);
151+
data = bff.slice( 0, pos[0] );
152+
state= data.size();
135153

136-
do{ for( auto x: _read.data ){ ++state;
137-
if ('\n'==x ){ break; } continue; }
138-
} while(0);
154+
fd->get_borrow().ptr().slice ( pos[1], (ulong) -1 );
139155

140-
data +=fd->get_borrow().splice( 0, state );
141-
state =data.size();
142-
143-
if( data[data.size()-1] == '\n' ){ coEnd; }
156+
return state==0 ? 1 : -1; } while(0); }
157+
158+
borrow = type::move( fd->get_borrow() );
159+
160+
} while(0); coNext;
144161

145-
coGoto(1) ; coFinish }};
162+
coWait( _read( fd )==1 ); if( _read.state<=0 ){
163+
data = borrow; state = data.size(); coEnd;
164+
} fd->set_borrow ( borrow+_read.data ); coGoto(0);
165+
166+
coFinish }};
146167

147168
}}}
148169
#undef NODEPP_GENERATOR
@@ -234,6 +255,50 @@ namespace nodepp { namespace generator { namespace stream {
234255

235256
/*─······································································─*/
236257

258+
GENERATOR( split ){
259+
protected:
260+
261+
file::write _write;
262+
file::split _read ;
263+
264+
public:
265+
266+
template< class T, class U >
267+
coEmit( const T& inp, const U& val ){
268+
coBegin
269+
270+
inp.onPipe.emit(); inp.resume();
271+
272+
while ( inp.is_available() ){
273+
coWait( _read(&inp,val)==1 ); if( _read.state<=0 ){ break; }
274+
inp.onData.emit(_read.data);
275+
}
276+
277+
inp.close();
278+
279+
coFinish }
280+
281+
template< class T, class V, class U >
282+
coEmit( const T& inp, const V& out, const U& val ){
283+
coBegin
284+
285+
inp.onPipe.emit(); inp.resume();
286+
out.onPipe.emit(); out.resume();
287+
288+
while( inp.is_available() && out.is_available() ){
289+
coWait( _read (&inp,val)==1 ); /*--*/ if( _read .state<=0 ){ break; }
290+
coWait( _write(&out,_read.data)==1 ); if( _write.state<=0 ){ break; }
291+
inp.onData.emit(_read.data);
292+
}
293+
294+
inp.close(); out.close();
295+
296+
coFinish }
297+
298+
};
299+
300+
/*─······································································─*/
301+
237302
GENERATOR( until ){
238303
protected:
239304

include/nodepp/http.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ namespace nodepp { class http_t : public socket_t, public generator_t {
185185
regex_t( "?[^#]+" )
186186
});
187187

188-
bool b=1; coBegin
188+
coBegin
189189

190190
set_recv_mode( nullptr ); if( is_server() ) { set_send_mode( nullptr ); }
191191

@@ -204,11 +204,11 @@ namespace nodepp { class http_t : public socket_t, public generator_t {
204204
} else { version = base[0]; status = string::to_uint( base[1] ); }
205205
} while(0);
206206

207-
do{ coWait( http->line( this )==1 ); if( http->line.state<=0 ){ coEnd; }
208-
do{ auto x= http->line.data; auto y = x.find( ": " );
209-
if( y.null() ){ b=0; break; }
210-
headers[ x.slice( 0, y[0] ).to_capital_case() ] = x.slice( y[1], -2 );
211-
} while(0); } while(b);
207+
do{ coWait( http->line( this )==1 ); if( http->line.state>0 ) {
208+
auto x= http->line.data; auto y = x.find( ": " );
209+
if( y.null() ){ break; }
210+
headers[ x.slice( 0, y[0] ).to_capital_case() ] = x.slice( y[1], -1 );
211+
} else { break; } } while(true);
212212

213213
http->read.borrow = type::move( get_borrow( ) );
214214
set_recv_mode( headers ); /*-----*/ coStay(0);

include/nodepp/https.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ namespace nodepp { class https_t : public ssocket_t, public generator_t {
8585
regex_t( "?[^#]+" )
8686
});
8787

88-
bool b=1; coBegin
88+
coBegin
8989

9090
set_recv_mode( nullptr ); if( is_server() ) { set_send_mode( nullptr ); }
9191

@@ -104,11 +104,11 @@ namespace nodepp { class https_t : public ssocket_t, public generator_t {
104104
} else { version = base[0]; status = string::to_uint( base[1] ); }
105105
} while(0);
106106

107-
do{ coWait( http->line( this )==1 ); if( http->line.state<=0 ){ coEnd; }
108-
do{ auto x= http->line.data; auto y = x.find( ": " );
109-
if( y.null() ){ b=0; break; }
110-
headers[ x.slice( 0, y[0] ).to_capital_case() ] = x.slice( y[1], -2 );
111-
} while(0); } while(b);
107+
do{ coWait( http->line( this )==1 ); if( http->line.state>0 ) {
108+
auto x= http->line.data; auto y = x.find( ": " );
109+
if( y.null() ){ break; }
110+
headers[ x.slice( 0, y[0] ).to_capital_case() ] = x.slice( y[1], -1 );
111+
} else { break; } } while(true);
112112

113113
http->read.borrow = type::move( get_borrow( ) );
114114
set_recv_mode( headers ); /*-----*/ coStay(0);

include/nodepp/posix/file.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,6 @@ namespace nodepp { class file_t {
250250
return obj->_until.data;
251251
}
252252

253-
string_t read_until( char ch ) const noexcept {
254-
while( obj->_until( this, ch ) == 1 )
255-
{ process::next(); }
256-
return obj->_until.data;
257-
}
258-
259253
string_t read_line() const noexcept {
260254
while( obj->_line( this ) == 1 )
261255
{ process::next(); }

include/nodepp/posix/kernel.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,15 +1021,17 @@ namespace nodepp { class kernel_t {
10211021
template< class T, class U, class... W >
10221022
ptr_t<task_t> poll_add( T& inp, uchar flag, U cb, ulong timeout=0, const W&... args ) const noexcept {
10231023

1024-
function_t<int,W...> clb ( cb ); if ( inp.is_closed() ) { return nullptr; }
10251024
function_t<int,W...> clb ( cb ); if ( inp.is_closed() ) { return nullptr; }
10261025
auto time = timeout>0 ? timeout + process::now() : timeout;
10271026

10281027
return loop_add( coroutine::add( COROUTINE(){
10291028
coBegin
10301029

1031-
if( time > 0 && time < process::now() ){ coEnd; }
1032-
coSet(0); return clb( args... ) >= 0 ? 1 : -1;
1030+
while( clb( args... )>=0 ){
1031+
if ( time > 0 && time < process::now() ) { break; }
1032+
// if ( is_std( fd ) ) { coDelay(100); }
1033+
if ( inp.is_waiting() ){ coDelay(100); coGoto(0); }
1034+
coNext; }
10331035

10341036
coFinish
10351037
}));

0 commit comments

Comments
 (0)