-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
pipe_test.c
51 lines (40 loc) · 1.09 KB
/
pipe_test.c
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
/*
* hio_create_pipe test
*
* @build make examples
* @test bin/pipe_test
*
*/
#include "hloop.h"
#include "htime.h"
static hio_t* pipeio[2] = { NULL, NULL };
static void on_read(hio_t* io, void* buf, int readbytes) {
printf("< %.*s\n", readbytes, (char*)buf);
}
static void on_timer_write(htimer_t* timer) {
char str[DATETIME_FMT_BUFLEN] = {0};
datetime_t dt = datetime_now();
datetime_fmt(&dt, str);
hio_write(pipeio[1], str, strlen(str));
}
static void on_timer_stop(htimer_t* timer) {
hio_close(pipeio[0]);
hio_close(pipeio[1]);
hloop_stop(hevent_loop(timer));
}
int main(int argc, char** argv) {
hloop_t* loop = hloop_new(0);
int ret = hio_create_pipe(loop, pipeio);
if (ret != 0) {
printf("hio_create_pipe failed!\n");
return -10;
}
printf("pipefd %d<=>%d\n", hio_fd(pipeio[0]), hio_fd(pipeio[1]));
hio_setcb_read(pipeio[0], on_read);
hio_read(pipeio[0]);
htimer_add(loop, on_timer_write, 1000, INFINITE);
htimer_add(loop, on_timer_stop, 10000, 1);
hloop_run(loop);
hloop_free(&loop);
return 0;
}