Skip to content

Commit

Permalink
support pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Apr 23, 2024
1 parent 8e7339a commit 3564a4f
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ default: all
all: libhv examples
@echo "make all done, please enjoy libhv."

examples: hmain_test htimer_test hloop_test \
examples: hmain_test htimer_test hloop_test pipe_test \
nc nmap tinyhttpd tinyproxyd httpd curl wget wrk consul \
tcp_client_test \
tcp_echo_server \
Expand Down Expand Up @@ -111,6 +111,9 @@ htimer_test: prepare
hloop_test: prepare
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/hloop_test.c"

pipe_test: prepare
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/pipe_test.c"

tcp_client_test: prepare
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/tcp_client_test.c"

Expand Down
3 changes: 2 additions & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ int main(int argc, char** argv) {
### c版本
- 事件循环: [examples/hloop_test.c](examples/hloop_test.c)
- 定时器: [examples/htimer_test.c](examples/htimer_test.c)
- pipe示例: [examples/pipe_test.c](examples/pipe_test.c)
- TCP回显服务: [examples/tcp_echo_server.c](examples/tcp_echo_server.c)
- TCP聊天服务: [examples/tcp_chat_server.c](examples/tcp_chat_server.c)
- TCP代理服务: [examples/tcp_proxy_server.c](examples/tcp_proxy_server.c)
Expand Down Expand Up @@ -429,7 +430,6 @@ int main(int argc, char** argv) {
- HTTP客户端: [examples/http_client_test.cpp](examples/http_client_test.cpp)
- WebSocket服务端: [examples/websocket_server_test.cpp](examples/websocket_server_test.cpp)
- WebSocket客户端: [examples/websocket_client_test.cpp](examples/websocket_client_test.cpp)
- kcptun隧道: [examples/kcptun](examples/kcptun)
- protobufRPC示例: [examples/protorpc](examples/protorpc)
- Qt中使用libhv示例: [hv-projects/QtDemo](https://github.com/hv-projects/QtDemo)

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

## 🥇 性能测试

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ int main(int argc, char** argv) {
### c version
- [examples/hloop_test.c](examples/hloop_test.c)
- [examples/htimer_test.c](examples/htimer_test.c)
- [examples/pipe_test.c](examples/pipe_test.c)
- [examples/tcp_echo_server.c](examples/tcp_echo_server.c)
- [examples/tcp_chat_server.c](examples/tcp_chat_server.c)
- [examples/tcp_proxy_server.c](examples/tcp_proxy_server.c)
Expand Down Expand Up @@ -369,7 +370,6 @@ int main(int argc, char** argv) {
- [examples/http_client_test.cpp](examples/http_client_test.cpp)
- [examples/websocket_server_test.cpp](examples/websocket_server_test.cpp)
- [examples/websocket_client_test.cpp](examples/websocket_client_test.cpp)
- [examples/kcptun](examples/kcptun)
- [examples/protorpc](examples/protorpc)
- [hv-projects/QtDemo](https://github.com/hv-projects/QtDemo)

Expand All @@ -381,6 +381,7 @@ int main(int argc, char** argv) {
- [examples/curl](examples/curl.cpp)
- [examples/wget](examples/wget.cpp)
- [examples/consul](examples/consul)
- [examples/kcptun](examples/kcptun)

## 🥇 Benchmark
### `pingpong echo-servers`
Expand Down
1 change: 1 addition & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@
- hio_setup_ssl_upstream
- hio_setup_udp_upstream
- hio_create_socket
- hio_create_pipe
- hio_context
- hio_set_context
- htimer_add
Expand Down
4 changes: 4 additions & 0 deletions docs/cn/hloop.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ hio_t* hloop_create_udp_server (hloop_t* loop, const char* host, int port);
// 创建UDP客户端,示例代码见 examples/nc.c
hio_t* hloop_create_udp_client (hloop_t* loop, const char* host, int port);

//-----------------pipe---------------------------------------------
// 创建pipe,示例代码见 examples/pipe_test.c
int hio_create_pipe(hloop_t* loop, hio_t* pipeio[2]);

//-----------------转发---------------------------------------------
// hio_read(io)
// hio_read(io->upstream_io)
Expand Down
22 changes: 22 additions & 0 deletions event/hloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -1025,3 +1025,25 @@ hio_t* hloop_create_udp_server(hloop_t* loop, const char* host, int port) {
hio_t* hloop_create_udp_client(hloop_t* loop, const char* host, int port) {
return hio_create_socket(loop, host, port, HIO_TYPE_UDP, HIO_CLIENT_SIDE);
}

int hio_create_pipe(hloop_t* loop, hio_t* pipeio[2]) {
int pipefd[2];
hio_type_e type = HIO_TYPE_PIPE;
#if defined(OS_UNIX) && HAVE_PIPE
if (pipe(pipefd) != 0) {
hloge("pipe create failed!");
return -1;
}
#else
if (Socketpair(AF_INET, SOCK_STREAM, 0, pipefd) != 0) {
hloge("socketpair create failed!");
return -1;
}
type = HIO_TYPE_TCP;
#endif
pipeio[0] = hio_get(loop, pipefd[0]);
pipeio[1] = hio_get(loop, pipefd[1]);
pipeio[0]->io_type = type;
pipeio[1]->io_type = type;
return 0;
}
5 changes: 5 additions & 0 deletions event/hloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ typedef enum {
HIO_TYPE_STDIO = 0x0000000F,

HIO_TYPE_FILE = 0x00000010,
HIO_TYPE_PIPE = 0x00000020,

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

//-----------------pipe---------------------------------------------
// @see examples/pipe_test.c
HV_EXPORT int hio_create_pipe(hloop_t* loop, hio_t* pipeio[2]);

//-----------------upstream---------------------------------------------
// hio_read(io)
// hio_read(io->upstream_io)
Expand Down
2 changes: 2 additions & 0 deletions event/nio.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@ int hio_close (hio_t* io) {
SAFE_FREE(io->hostname);
if (io->io_type & HIO_TYPE_SOCKET) {
closesocket(io->fd);
} else if (io->io_type == HIO_TYPE_PIPE) {
close(io->fd);
}
return 0;
}
Expand Down
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
list(APPEND EXAMPLES
hloop_test
htimer_test
pipe_test
nc
tinyhttpd
tinyproxyd
Expand All @@ -23,6 +24,9 @@ target_link_libraries(hloop_test ${HV_LIBRARIES})
add_executable(htimer_test htimer_test.c)
target_link_libraries(htimer_test ${HV_LIBRARIES})

add_executable(pipe_test pipe_test.c)
target_link_libraries(pipe_test ${HV_LIBRARIES})

add_executable(nc nc.c)
target_link_libraries(nc ${HV_LIBRARIES})

Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
├── consul/ consul服务注册与发现
├── httpd/ HTTP服务端
├── jsonrpc/ json RPC示例
├── kcptun/ kcp隧道
├── mqtt/ MQTT发布订阅示例
├── multi-thread/ 多线程网络编程示例
├── nmap/ 网络扫描工具
Expand All @@ -16,6 +17,7 @@
├── http_client_test.c HTTP客户端测试代码
├── http_server_test.c HTTP服务端测试代码
├── nc.c 网络连接工具
├── pipe_test.c pipe示例代码
├── socks5_proxy_server.c SOCKS5代理服务
├── tcp_chat_server.c TCP聊天服务
├── tcp_echo_server.c TCP回显服务
Expand Down
51 changes: 51 additions & 0 deletions examples/pipe_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}

0 comments on commit 3564a4f

Please sign in to comment.