-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.cpp
More file actions
63 lines (42 loc) · 2.1 KB
/
Copy pathmain.cpp
File metadata and controls
63 lines (42 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <nodepp/nodepp.h>
#include <nodepp/worker.h>
#include <nodepp/date.h>
#include <nodepp/http.h>
#include <nodepp/cluster.h>
using namespace nodepp;
/*────────────────────────────────────────────────────────────────────────────*/
void server( int x ){
auto server = http::server([=]( http_t cli ){
cli.write_header( 200, header_t({
{ "content-type", "text/html" }
}));
cli.write( date::fulltime() );
cli.close();
});
server.listen( "localhost", 8000, [=]( socket_t server ){
console::log("server started at http://localhost:8000");
});
}
/*────────────────────────────────────────────────────────────────────────────*/
void spawn( int x ) {
auto cid = regex::format( "?CPU=${0}", x );
auto pid = cluster::add( ptr_t<string_t>({ cid }) );
if( pid.has_value() ){
pid.value().onClose.once([=](){ spawn(x); });
pid.value().onData([=]( string_t msg ){
conio::log( msg );
});
}
}
/*────────────────────────────────────────────────────────────────────────────*/
void onMain(){
os::set_process_priority( os::PRIORITY::HIGH_PRIORITY );
os::set_hard_fileno/*-*/( (uint)-1 );
os::set_soft_fileno/*-*/( (uint)-1 );
int y = string::to_uint( process::env::get( "CPU" ) );
for ( auto x=os::cpus(); x-->0; ){
if ( cluster::is_child() )
{ server(y); break; }
else { spawn (x); } }
}
/*────────────────────────────────────────────────────────────────────────────*/