-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (59 loc) · 1.79 KB
/
Copy pathmain.cpp
File metadata and controls
86 lines (59 loc) · 1.79 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#define NODEPP_EVENT_SCHEDULER NODEPP_SCHEDULER_EPOLL
#include <nodepp/nodepp.h>
#include <nodepp/worker.h>
#include <nodepp/timer.h>
#include <nodepp/udp.h>
#include <nodepp/fs.h>
using namespace nodepp;
void server(){
auto server = udp::server();
server.onConnect([=]( socket_t cli ){
cli.onData([=]( string_t data ){
cli.set_write_address( cli.get_read_address() );
cli.write( "<: received" );
console::log( data );
});
cli.onClose([=](){
console::log("closed");
});
console::log( "connected" );
stream::pipe( cli );
});
server.onError([=]( except_t err ){
console::log( ">>", err.what() );
});
server.listen( "localhost", 8000, []( socket_t srv ){
console::log("-> udb://localhost:8000");
});
}
void client(){
auto client = udp::client();
client.onConnect([=]( socket_t cli ){
cli.onData([=]( string_t data ){
console::log( "server read>>", data );
});
cli.onClose([=](){
console::log("closed");
});
stream::pipe( cli );
timer ::add ([=](){
auto msg = regex::format( "hello world! ${0}", process::now() );
return cli.write( msg ) <= 0 ? -1 : 1 ;
},1000);
console::log( "connected" );
});
client.onError([=]( except_t err ){
console::log( ">>", err.what() );
});
client.connect( "localhost", 8000, []( socket_t cli ){
console::log("-> udp://localhost:8000");
});
}
void onMain() {
worker::add( coroutine::add( COROUTINE(){
coBegin /*--*/ ; server();
process::wait(); coFinish }));
worker::add( coroutine::add( COROUTINE(){
coBegin /*--*/ ; client();
process::wait(); coFinish }));
}