Skip to content

Commit 3e6e488

Browse files
committed
📌 Nodepp | worker.h update | V1.4.2 📌
1 parent 89982e2 commit 3e6e488

9 files changed

Lines changed: 269 additions & 308 deletions

File tree

examples/62-invoker.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <nodepp/nodepp.h>
2+
3+
using namespace nodepp;
4+
5+
void onMain(){
6+
7+
string_t addr1 = process::invoke([=]( any_t raw ){
8+
auto data = raw.as<ptr_t<ulong>>();
9+
console::log( "<-->", data, *data, process::now() );
10+
return 1; });
11+
12+
string_t addr2 = process::invoke([=]( any_t raw ){
13+
static ulong x=1; static ulong y=1; ulong z=0;
14+
auto data = raw.as<ptr_t<ulong>>();
15+
z = x; x = z + y; y = z; *data = x;
16+
// console::log( "<-->", data, *data, process::now(), x );
17+
return 1; });
18+
19+
process::add( coroutine::add( COROUTINE(){
20+
coBegin
21+
22+
while( true ){ do {
23+
auto x = ptr_t<ulong>( 0UL, 0 );
24+
process::call( addr2, x );
25+
process::call( addr1, x );
26+
} while(0); coDelay(1000); }
27+
28+
coFinish
29+
}));
30+
31+
}

include/nodepp/evloop.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@
1616

1717
namespace nodepp { namespace process {
1818

19-
kernel_t& NODEPP_EV_LOOP(){ thread_local static kernel_t evloop; return evloop; }
19+
kernel_t& NODEPP_EV_LOOP(){ thread_local static kernel_t out; return out; }
20+
invoke_t& NODEPP_INVOKE (){ thread_local static invoke_t out; return out; }
2021

2122
/*─······································································─*/
2223

2324
template< class... T >
24-
void await( const T&... args ){ while(NODEPP_EV_LOOP().await( args... )==1){/*unused*/} }
25+
void revoke( const T&... args ){ NODEPP_INVOKE().off( args... ); }
26+
27+
template< class... T >
28+
int call( const T&... args ){ return NODEPP_INVOKE().emit( args... ); }
29+
30+
template< class... T >
31+
string_t invoke( const T&... args ){ return NODEPP_INVOKE().add( args... ); }
32+
33+
/*─······································································─*/
2534

2635
template< class... T >
27-
ptr_t<task_t> foop( const T&... args ){ return NODEPP_EV_LOOP().loop_add( args... ); }
36+
void await( const T&... args ){ while(NODEPP_EV_LOOP().await( args... )==1){/*unused*/} }
2837

2938
template< class... T >
3039
ptr_t<task_t> loop( const T&... args ){ return NODEPP_EV_LOOP().loop_add( args... ); }

include/nodepp/import.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#include "os.h"
6565
#include "query.h"
6666
#include "kernel.h"
67+
#include "invoke.h"
6768

6869
/*────────────────────────────────────────────────────────────────────────────*/
6970

include/nodepp/invoke.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2023 The Nodepp Project Authors. All Rights Reserved.
3+
*
4+
* Licensed under the MIT (the "License"). You may not use
5+
* this file except in compliance with the License. You can obtain a copy
6+
* in the file LICENSE in the source distribution or at
7+
* https://github.com/NodeppOfficial/nodepp/blob/main/LICENSE
8+
*/
9+
10+
/*────────────────────────────────────────────────────────────────────────────*/
11+
12+
#ifndef NODEPP_DMA_INVOKE
13+
#define NODEPP_DMA_INVOKE
14+
15+
/*────────────────────────────────────────────────────────────────────────────*/
16+
17+
namespace nodepp { class invoke_t {
18+
protected:
19+
20+
using NODE_CLB = function_t<int,any_t>;
21+
struct NODE {
22+
queue_t<NODE_CLB> queue;
23+
}; ptr_t<NODE> obj;
24+
25+
public:
26+
27+
~invoke_t() { if( obj.count() > 1 ){ return; } free(); }
28+
invoke_t() : obj( new NODE() ){}
29+
30+
/*─······································································─*/
31+
32+
ulong size () const noexcept { return obj->queue.size (); }
33+
void clear() const noexcept { /*--*/ obj->queue.clear(); }
34+
void free () const noexcept { /*--*/ obj->queue.clear(); }
35+
36+
/*─······································································─*/
37+
38+
int emit( string_t address, any_t value ) const noexcept {
39+
if( address.empty() ) /*-------------*/ { return -1; }
40+
auto mem = obj->queue.as( string::to_addr( address ) );
41+
if( mem == nullptr ) /*--------------*/ { return -1; }
42+
int c = mem->data.emit( value );
43+
if( c==-1 ){ off( address ); }
44+
return c; }
45+
46+
int off( string_t address ) const noexcept {
47+
if( address.empty() ) /*-------------*/ { return -1; }
48+
auto mem = obj->queue.as( string::to_addr( address ) );
49+
if( mem == nullptr ) /*--------------*/ { return -1; }
50+
memset( address.get(), 0, address.size() );
51+
obj->queue.erase(mem); return 1;
52+
}
53+
54+
string_t add( NODE_CLB callback ) const noexcept {
55+
obj->queue.push ( callback );
56+
void* ID = obj->queue.last();
57+
return string::to_string( ID ); }
58+
59+
}; }
60+
61+
/*────────────────────────────────────────────────────────────────────────────*/
62+
63+
#endif
64+
65+
/*────────────────────────────────────────────────────────────────────────────*/

include/nodepp/posix/dns.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,38 @@ namespace nodepp { namespace dns {
4444

4545
if( host == "broadcast" || host == "255.255.255.255" ){
4646
dns_t tmp ({ "255.255.255.255", host, AF_INET });
47-
return ptr_t<dns_t>( 0UL, tmp );
47+
return ptr_t<dns_t>( 1UL, tmp );
4848
}
4949
elif( host == "localhost" || host == "127.0.0.1" ){
5050
dns_t tmp ({ "127.0.0.1", host, AF_INET });
51-
return ptr_t<dns_t>( 0UL, tmp );
51+
return ptr_t<dns_t>( 1UL, tmp );
5252
}
5353
elif( host == "global" || host == "0.0.0.0" ){
5454
dns_t tmp ({ "0.0.0.0", host, AF_INET });
55-
return ptr_t<dns_t>( 0UL, tmp );
55+
return ptr_t<dns_t>( 1UL, tmp );
5656
}
5757
elif( host == "loopback" || host == "1.1.1.1" ){
5858
dns_t tmp ({ "1.1.1.1", host, AF_INET });
59-
return ptr_t<dns_t>( 0UL, tmp );
59+
return ptr_t<dns_t>( 1UL, tmp );
6060
}
6161

6262
} elif( family == AF_INET6 ) {
6363

6464
if( host == "broadcast" || host == "::2" ){
6565
dns_t tmp ({ "::2", host, AF_INET6 });
66-
return ptr_t<dns_t>( 0UL, tmp );
66+
return ptr_t<dns_t>( 1UL, tmp );
6767
}
6868
elif( host == "localhost" || host == "::1" ){
6969
dns_t tmp ({ "::1", host, AF_INET6 });
70-
return ptr_t<dns_t>( 0UL, tmp );
70+
return ptr_t<dns_t>( 1UL, tmp );
7171
}
7272
elif( host == "global" || host == "::0" ){
7373
dns_t tmp ({ "::0", host, AF_INET6 });
74-
return ptr_t<dns_t>( 0UL, tmp );
74+
return ptr_t<dns_t>( 1UL, tmp );
7575
}
7676
elif( host == "loopback" || host == "::3" ){
7777
dns_t tmp ({ "::3", host, AF_INET6 });
78-
return ptr_t<dns_t>( 0UL, tmp );
78+
return ptr_t<dns_t>( 1UL, tmp );
7979
}
8080

8181
}

0 commit comments

Comments
 (0)