Skip to content

Commit 3564a4f

Browse files
committed
support pipe
1 parent 8e7339a commit 3564a4f

File tree

11 files changed

+99
-3
lines changed

11 files changed

+99
-3
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ default: all
5252
all: libhv examples
5353
@echo "make all done, please enjoy libhv."
5454

55-
examples: hmain_test htimer_test hloop_test \
55+
examples: hmain_test htimer_test hloop_test pipe_test \
5656
nc nmap tinyhttpd tinyproxyd httpd curl wget wrk consul \
5757
tcp_client_test \
5858
tcp_echo_server \
@@ -111,6 +111,9 @@ htimer_test: prepare
111111
hloop_test: prepare
112112
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/hloop_test.c"
113113

114+
pipe_test: prepare
115+
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/pipe_test.c"
116+
114117
tcp_client_test: prepare
115118
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/tcp_client_test.c"
116119

README-CN.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ int main(int argc, char** argv) {
402402
### c版本
403403
- 事件循环: [examples/hloop_test.c](examples/hloop_test.c)
404404
- 定时器: [examples/htimer_test.c](examples/htimer_test.c)
405+
- pipe示例: [examples/pipe_test.c](examples/pipe_test.c)
405406
- TCP回显服务: [examples/tcp_echo_server.c](examples/tcp_echo_server.c)
406407
- TCP聊天服务: [examples/tcp_chat_server.c](examples/tcp_chat_server.c)
407408
- TCP代理服务: [examples/tcp_proxy_server.c](examples/tcp_proxy_server.c)
@@ -429,7 +430,6 @@ int main(int argc, char** argv) {
429430
- HTTP客户端: [examples/http_client_test.cpp](examples/http_client_test.cpp)
430431
- WebSocket服务端: [examples/websocket_server_test.cpp](examples/websocket_server_test.cpp)
431432
- WebSocket客户端: [examples/websocket_client_test.cpp](examples/websocket_client_test.cpp)
432-
- kcptun隧道: [examples/kcptun](examples/kcptun)
433433
- protobufRPC示例: [examples/protorpc](examples/protorpc)
434434
- Qt中使用libhv示例: [hv-projects/QtDemo](https://github.com/hv-projects/QtDemo)
435435

@@ -441,6 +441,7 @@ int main(int argc, char** argv) {
441441
- URL请求工具: [examples/curl](examples/curl.cpp)
442442
- 文件下载工具: [examples/wget](examples/wget.cpp)
443443
- 服务注册与发现: [examples/consul](examples/consul)
444+
- kcptun隧道: [examples/kcptun](examples/kcptun)
444445

445446
## 🥇 性能测试
446447

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ int main(int argc, char** argv) {
342342
### c version
343343
- [examples/hloop_test.c](examples/hloop_test.c)
344344
- [examples/htimer_test.c](examples/htimer_test.c)
345+
- [examples/pipe_test.c](examples/pipe_test.c)
345346
- [examples/tcp_echo_server.c](examples/tcp_echo_server.c)
346347
- [examples/tcp_chat_server.c](examples/tcp_chat_server.c)
347348
- [examples/tcp_proxy_server.c](examples/tcp_proxy_server.c)
@@ -369,7 +370,6 @@ int main(int argc, char** argv) {
369370
- [examples/http_client_test.cpp](examples/http_client_test.cpp)
370371
- [examples/websocket_server_test.cpp](examples/websocket_server_test.cpp)
371372
- [examples/websocket_client_test.cpp](examples/websocket_client_test.cpp)
372-
- [examples/kcptun](examples/kcptun)
373373
- [examples/protorpc](examples/protorpc)
374374
- [hv-projects/QtDemo](https://github.com/hv-projects/QtDemo)
375375

@@ -381,6 +381,7 @@ int main(int argc, char** argv) {
381381
- [examples/curl](examples/curl.cpp)
382382
- [examples/wget](examples/wget.cpp)
383383
- [examples/consul](examples/consul)
384+
- [examples/kcptun](examples/kcptun)
384385

385386
## 🥇 Benchmark
386387
### `pingpong echo-servers`

docs/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@
470470
- hio_setup_ssl_upstream
471471
- hio_setup_udp_upstream
472472
- hio_create_socket
473+
- hio_create_pipe
473474
- hio_context
474475
- hio_set_context
475476
- htimer_add

docs/cn/hloop.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ hio_t* hloop_create_udp_server (hloop_t* loop, const char* host, int port);
416416
// 创建UDP客户端,示例代码见 examples/nc.c
417417
hio_t* hloop_create_udp_client (hloop_t* loop, const char* host, int port);
418418

419+
//-----------------pipe---------------------------------------------
420+
// 创建pipe,示例代码见 examples/pipe_test.c
421+
int hio_create_pipe(hloop_t* loop, hio_t* pipeio[2]);
422+
419423
//-----------------转发---------------------------------------------
420424
// hio_read(io)
421425
// hio_read(io->upstream_io)

event/hloop.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,3 +1025,25 @@ hio_t* hloop_create_udp_server(hloop_t* loop, const char* host, int port) {
10251025
hio_t* hloop_create_udp_client(hloop_t* loop, const char* host, int port) {
10261026
return hio_create_socket(loop, host, port, HIO_TYPE_UDP, HIO_CLIENT_SIDE);
10271027
}
1028+
1029+
int hio_create_pipe(hloop_t* loop, hio_t* pipeio[2]) {
1030+
int pipefd[2];
1031+
hio_type_e type = HIO_TYPE_PIPE;
1032+
#if defined(OS_UNIX) && HAVE_PIPE
1033+
if (pipe(pipefd) != 0) {
1034+
hloge("pipe create failed!");
1035+
return -1;
1036+
}
1037+
#else
1038+
if (Socketpair(AF_INET, SOCK_STREAM, 0, pipefd) != 0) {
1039+
hloge("socketpair create failed!");
1040+
return -1;
1041+
}
1042+
type = HIO_TYPE_TCP;
1043+
#endif
1044+
pipeio[0] = hio_get(loop, pipefd[0]);
1045+
pipeio[1] = hio_get(loop, pipefd[1]);
1046+
pipeio[0]->io_type = type;
1047+
pipeio[1]->io_type = type;
1048+
return 0;
1049+
}

event/hloop.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ typedef enum {
9494
HIO_TYPE_STDIO = 0x0000000F,
9595

9696
HIO_TYPE_FILE = 0x00000010,
97+
HIO_TYPE_PIPE = 0x00000020,
9798

9899
HIO_TYPE_IP = 0x00000100,
99100
HIO_TYPE_SOCK_RAW = 0x00000F00,
@@ -436,6 +437,10 @@ HV_EXPORT hio_t* hloop_create_udp_server (hloop_t* loop, const char* host, int p
436437
// @see examples/nc.c
437438
HV_EXPORT hio_t* hloop_create_udp_client (hloop_t* loop, const char* host, int port);
438439

440+
//-----------------pipe---------------------------------------------
441+
// @see examples/pipe_test.c
442+
HV_EXPORT int hio_create_pipe(hloop_t* loop, hio_t* pipeio[2]);
443+
439444
//-----------------upstream---------------------------------------------
440445
// hio_read(io)
441446
// hio_read(io->upstream_io)

event/nio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,8 @@ int hio_close (hio_t* io) {
600600
SAFE_FREE(io->hostname);
601601
if (io->io_type & HIO_TYPE_SOCKET) {
602602
closesocket(io->fd);
603+
} else if (io->io_type == HIO_TYPE_PIPE) {
604+
close(io->fd);
603605
}
604606
return 0;
605607
}

examples/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
list(APPEND EXAMPLES
22
hloop_test
33
htimer_test
4+
pipe_test
45
nc
56
tinyhttpd
67
tinyproxyd
@@ -23,6 +24,9 @@ target_link_libraries(hloop_test ${HV_LIBRARIES})
2324
add_executable(htimer_test htimer_test.c)
2425
target_link_libraries(htimer_test ${HV_LIBRARIES})
2526

27+
add_executable(pipe_test pipe_test.c)
28+
target_link_libraries(pipe_test ${HV_LIBRARIES})
29+
2630
add_executable(nc nc.c)
2731
target_link_libraries(nc ${HV_LIBRARIES})
2832

examples/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
├── consul/ consul服务注册与发现
66
├── httpd/ HTTP服务端
77
├── jsonrpc/ json RPC示例
8+
├── kcptun/ kcp隧道
89
├── mqtt/ MQTT发布订阅示例
910
├── multi-thread/ 多线程网络编程示例
1011
├── nmap/ 网络扫描工具
@@ -16,6 +17,7 @@
1617
├── http_client_test.c HTTP客户端测试代码
1718
├── http_server_test.c HTTP服务端测试代码
1819
├── nc.c 网络连接工具
20+
├── pipe_test.c pipe示例代码
1921
├── socks5_proxy_server.c SOCKS5代理服务
2022
├── tcp_chat_server.c TCP聊天服务
2123
├── tcp_echo_server.c TCP回显服务

examples/pipe_test.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* hio_create_pipe test
3+
*
4+
* @build make examples
5+
* @test bin/pipe_test
6+
*
7+
*/
8+
9+
#include "hloop.h"
10+
#include "htime.h"
11+
12+
static hio_t* pipeio[2] = { NULL, NULL };
13+
14+
static void on_read(hio_t* io, void* buf, int readbytes) {
15+
printf("< %.*s\n", readbytes, (char*)buf);
16+
}
17+
18+
static void on_timer_write(htimer_t* timer) {
19+
char str[DATETIME_FMT_BUFLEN] = {0};
20+
datetime_t dt = datetime_now();
21+
datetime_fmt(&dt, str);
22+
hio_write(pipeio[1], str, strlen(str));
23+
}
24+
25+
static void on_timer_stop(htimer_t* timer) {
26+
hio_close(pipeio[0]);
27+
hio_close(pipeio[1]);
28+
hloop_stop(hevent_loop(timer));
29+
}
30+
31+
int main(int argc, char** argv) {
32+
hloop_t* loop = hloop_new(0);
33+
34+
int ret = hio_create_pipe(loop, pipeio);
35+
if (ret != 0) {
36+
printf("hio_create_pipe failed!\n");
37+
return -10;
38+
}
39+
printf("pipefd %d<=>%d\n", hio_fd(pipeio[0]), hio_fd(pipeio[1]));
40+
41+
hio_setcb_read(pipeio[0], on_read);
42+
hio_read(pipeio[0]);
43+
44+
htimer_add(loop, on_timer_write, 1000, INFINITE);
45+
46+
htimer_add(loop, on_timer_stop, 10000, 1);
47+
48+
hloop_run(loop);
49+
hloop_free(&loop);
50+
return 0;
51+
}

0 commit comments

Comments
 (0)