Skip to content

Commit 2feb3be

Browse files
committed
V1.4.3_3 | generator.h stream::until rewrite - stream::split added
1 parent 3d0c38f commit 2feb3be

12 files changed

Lines changed: 187 additions & 116 deletions

File tree

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: 128 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -62,87 +62,120 @@ namespace nodepp { namespace generator { namespace file {
6262

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

65-
GENERATOR( until ){
66-
protected: ulong pos ; file::read _read;
67-
public: ulong state; string_t data ;
65+
GENERATOR( split ){
66+
protected: string_t borrow; ulong pos ; file::read _read;
67+
public: string_t data ; ulong state;
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 ( 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:
102+
103+
file::read _read; string_t borrow;
104+
105+
public:
106+
107+
ulong state;
108+
string_t data;
111109

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);
110+
template< class T > coEmit( T* fd, string_t ch ){
111+
coBegin data.clear(); state = 0UL;
112+
113+
do{ /*----------------*/ auto &bff = fd->get_borrow();
114+
if( !bff.empty() ){ do { auto pos = bff.find ( ch );
115+
116+
if( pos.null() ){
117+
if( bff.size() > NODEPP_UNBFF_SIZE ){
118+
data = type::move( bff );
119+
state= data.size();
120+
return 1; } break; }
121+
122+
data = bff.slice( 0, pos[0] );
123+
state= data.size();
124+
125+
fd->get_borrow().ptr().slice ( pos[1], (ulong) -1 );
126+
127+
return -1; } while(0); }
128+
129+
borrow = type::move( fd->get_borrow() );
117130

118-
if( data[ data.size()-1 ] == ch ){ coEnd; }
131+
} while(0); coNext;
119132

120-
coGoto(1) ; coFinish }};
133+
coWait( _read( fd )==1 );
134+
if( _read.state<=0 ){ state=0; coEnd; }
135+
fd->set_borrow ( borrow+_read.data ); coGoto(0);
136+
137+
coFinish }};
121138

122139
/*─······································································─*/
123140

124141
GENERATOR( line ){
125-
protected: file::read _read;
126-
public: ulong state; string_t data;
142+
protected:
143+
144+
file::read _read; string_t borrow;
145+
146+
public:
147+
148+
ulong state;
149+
string_t data;
127150

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

131-
coWait( _read( fd )==1 );
132-
if ( _read.state<=0 ){ state = data.size(); coEnd; }
154+
do{ /*----------------*/ auto &bff = fd->get_borrow();
155+
if( !bff.empty() ){ do { auto pos = bff.find ('\n');
156+
157+
if( pos.null() ){
158+
if( bff.size() > NODEPP_UNBFF_SIZE ){
159+
data = type::move( bff );
160+
state= data.size();
161+
return 1; } break; }
133162

134-
fd->set_borrow(_read.data);
163+
data = bff.slice( 0, pos[0] );
164+
state= data.size();
135165

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

140-
data +=fd->get_borrow().splice( 0, state );
141-
state =data.size();
142-
143-
if( data[data.size()-1] == '\n' ){ coEnd; }
168+
return -1; } while(0); }
169+
170+
borrow = type::move( fd->get_borrow() );
171+
172+
} while(0); coNext;
144173

145-
coGoto(1) ; coFinish }};
174+
coWait( _read( fd )==1 );
175+
if( _read.state<=0 ){ state=0; coEnd; }
176+
fd->set_borrow ( borrow+_read.data ); coGoto(0);
177+
178+
coFinish }};
146179

147180
}}}
148181
#undef NODEPP_GENERATOR
@@ -234,6 +267,50 @@ namespace nodepp { namespace generator { namespace stream {
234267

235268
/*─······································································─*/
236269

270+
GENERATOR( split ){
271+
protected:
272+
273+
file::write _write;
274+
file::split _read ;
275+
276+
public:
277+
278+
template< class T, class U >
279+
coEmit( const T& inp, const U& val ){
280+
coBegin
281+
282+
inp.onPipe.emit(); inp.resume();
283+
284+
while ( inp.is_available() ){
285+
coWait( _read(&inp,val)==1 ); if( _read.state<=0 ){ break; }
286+
inp.onData.emit(_read.data);
287+
}
288+
289+
inp.close();
290+
291+
coFinish }
292+
293+
template< class T, class V, class U >
294+
coEmit( const T& inp, const V& out, const U& val ){
295+
coBegin
296+
297+
inp.onPipe.emit(); inp.resume();
298+
out.onPipe.emit(); out.resume();
299+
300+
while( inp.is_available() && out.is_available() ){
301+
coWait( _read (&inp,val)==1 ); /*--*/ if( _read .state<=0 ){ break; }
302+
coWait( _write(&out,_read.data)==1 ); if( _write.state<=0 ){ break; }
303+
inp.onData.emit(_read.data);
304+
}
305+
306+
inp.close(); out.close();
307+
308+
coFinish }
309+
310+
};
311+
312+
/*─······································································─*/
313+
237314
GENERATOR( until ){
238315
protected:
239316

include/nodepp/http.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ namespace nodepp { class http_t : public socket_t, public generator_t {
124124

125125
struct DONE { len_t size; uchar state; };
126126
struct NODE {
127-
generator::file::line line ; DONE mode[2];
127+
generator::file::until line ; DONE mode[2];
128128
generator::http::read read ;
129129
generator::http::write write;
130130
}; ptr_t<NODE> http;
@@ -185,11 +185,11 @@ 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

192-
if( !is_available() ) /*--------------*/ { coEnd; } coWait( http->line( this )==1 );
192+
if( !is_available() ) /*--------------*/ { coEnd; } coWait( http->line( this, "\r\n" )==1 );
193193
if( http->line.state <= 0 ) /*--------*/ { coEnd; }
194194
if( http->line.data.find("HTTP").null() ){ coEnd; }
195195

@@ -204,11 +204,12 @@ 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; }
207+
do{ coWait( http->line( this, "\r\n" )==1 );
208+
if( http->line.state <= 0 ) /**/ { break; }
208209
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);
210+
if( y.null() ){ break; }
211+
headers[ x.slice( 0, y[0] ).to_capital_case() ] = x.slice( y[1] );
212+
} while(0); } while(true);
212213

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

include/nodepp/https.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace nodepp { class https_t : public ssocket_t, public generator_t {
2424

2525
struct DONE { len_t size; uchar state; };
2626
struct NODE {
27-
generator::file::line line ; DONE mode[2];
27+
generator::file::until line ; DONE mode[2];
2828
generator::http::read read ;
2929
generator::http::write write;
3030
}; ptr_t<NODE> http;
@@ -85,11 +85,11 @@ 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

92-
if( !is_available() ) /*--------------*/ { coEnd; } coWait( http->line( this )==1 );
92+
if( !is_available() ) /*--------------*/ { coEnd; } coWait( http->line( this, "\r\n" )==1 );
9393
if( http->line.state <= 0 ) /*--------*/ { coEnd; }
9494
if( http->line.data.find("HTTP").null() ){ coEnd; }
9595

@@ -104,11 +104,12 @@ 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; }
107+
do{ coWait( http->line( this, "\r\n" )==1 );
108+
if( http->line.state <= 0 ) /**/ { break; }
108109
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);
110+
if( y.null() ){ break; }
111+
headers[ x.slice( 0, y[0] ).to_capital_case() ] = x.slice( y[1] );
112+
} while(0); } while(true);
112113

113114
http->read.borrow = type::move( get_borrow( ) );
114115
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
}));

include/nodepp/posix/socket.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -670,12 +670,6 @@ class socket_t {
670670
return obj->_until.data;
671671
}
672672

673-
string_t read_until( char ch ) const noexcept {
674-
while( obj->_until( this, ch ) == 1 )
675-
{ process::next(); }
676-
return obj->_until.data;
677-
}
678-
679673
string_t read_line() const noexcept {
680674
while( obj->_line( this ) == 1 )
681675
{ process::next(); }

include/nodepp/stream.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ namespace nodepp { namespace stream {
3333

3434
/*─······································································─*/
3535

36+
template< class T, class V, class U >
37+
ptr_t<task_t> split( const T& fa, const V& fb, const U& val ){ generator::stream::split arg;
38+
return process::poll( fa, POLL_STATE::READ | POLL_STATE::EDGE, arg, 0UL, fa, fb, val ); }
39+
40+
template< class T, class U >
41+
ptr_t<task_t> split( const T& fa, const U& val ){ generator::stream::split arg;
42+
return process::poll( fa, POLL_STATE::READ | POLL_STATE::EDGE, arg, 0UL, fa, val ); }
43+
44+
/*─······································································─*/
45+
3646
template< class T, class V >
3747
ptr_t<task_t> duplex( const T& fa, const V& fb ){ generator::stream::pipe arg;
3848
process::poll( arg, fb, POLL_STATE::READ | POLL_STATE::EDGE, arg, 0UL, fb, fa );

include/nodepp/windows/file.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,6 @@ namespace nodepp { class file_t {
247247
return obj->_until.data;
248248
}
249249

250-
string_t read_until( char ch ) const noexcept {
251-
while( obj->_until( this, ch ) == 1 )
252-
{ process::next(); }
253-
return obj->_until.data;
254-
}
255-
256250
string_t read_line() const noexcept {
257251
while( obj->_line( this ) == 1 )
258252
{ process::next(); }

0 commit comments

Comments
 (0)