xref: /netbsd-src/external/mit/libuv/dist/docs/code/pipe-echo-server/main.c (revision 0e552da7216834a96e91ad098e59272b41087480)
1*0e552da7Schristos #include <stdio.h>
2*0e552da7Schristos #include <stdlib.h>
3*0e552da7Schristos #include <string.h>
4*0e552da7Schristos #include <uv.h>
5*0e552da7Schristos 
6*0e552da7Schristos #ifdef _WIN32
7*0e552da7Schristos #define PIPENAME "\\\\?\\pipe\\echo.sock"
8*0e552da7Schristos #else
9*0e552da7Schristos #define PIPENAME "/tmp/echo.sock"
10*0e552da7Schristos #endif
11*0e552da7Schristos 
12*0e552da7Schristos uv_loop_t *loop;
13*0e552da7Schristos 
14*0e552da7Schristos typedef struct {
15*0e552da7Schristos     uv_write_t req;
16*0e552da7Schristos     uv_buf_t buf;
17*0e552da7Schristos } write_req_t;
18*0e552da7Schristos 
free_write_req(uv_write_t * req)19*0e552da7Schristos void free_write_req(uv_write_t *req) {
20*0e552da7Schristos     write_req_t *wr = (write_req_t*) req;
21*0e552da7Schristos     free(wr->buf.base);
22*0e552da7Schristos     free(wr);
23*0e552da7Schristos }
24*0e552da7Schristos 
alloc_buffer(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)25*0e552da7Schristos void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
26*0e552da7Schristos   buf->base = malloc(suggested_size);
27*0e552da7Schristos   buf->len = suggested_size;
28*0e552da7Schristos }
29*0e552da7Schristos 
echo_write(uv_write_t * req,int status)30*0e552da7Schristos void echo_write(uv_write_t *req, int status) {
31*0e552da7Schristos     if (status < 0) {
32*0e552da7Schristos         fprintf(stderr, "Write error %s\n", uv_err_name(status));
33*0e552da7Schristos     }
34*0e552da7Schristos     free_write_req(req);
35*0e552da7Schristos }
36*0e552da7Schristos 
echo_read(uv_stream_t * client,ssize_t nread,const uv_buf_t * buf)37*0e552da7Schristos void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
38*0e552da7Schristos     if (nread > 0) {
39*0e552da7Schristos         write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
40*0e552da7Schristos         req->buf = uv_buf_init(buf->base, nread);
41*0e552da7Schristos         uv_write((uv_write_t*) req, client, &req->buf, 1, echo_write);
42*0e552da7Schristos         return;
43*0e552da7Schristos     }
44*0e552da7Schristos 
45*0e552da7Schristos     if (nread < 0) {
46*0e552da7Schristos         if (nread != UV_EOF)
47*0e552da7Schristos             fprintf(stderr, "Read error %s\n", uv_err_name(nread));
48*0e552da7Schristos         uv_close((uv_handle_t*) client, NULL);
49*0e552da7Schristos     }
50*0e552da7Schristos 
51*0e552da7Schristos     free(buf->base);
52*0e552da7Schristos }
53*0e552da7Schristos 
on_new_connection(uv_stream_t * server,int status)54*0e552da7Schristos void on_new_connection(uv_stream_t *server, int status) {
55*0e552da7Schristos     if (status == -1) {
56*0e552da7Schristos         // error!
57*0e552da7Schristos         return;
58*0e552da7Schristos     }
59*0e552da7Schristos 
60*0e552da7Schristos     uv_pipe_t *client = (uv_pipe_t*) malloc(sizeof(uv_pipe_t));
61*0e552da7Schristos     uv_pipe_init(loop, client, 0);
62*0e552da7Schristos     if (uv_accept(server, (uv_stream_t*) client) == 0) {
63*0e552da7Schristos         uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read);
64*0e552da7Schristos     }
65*0e552da7Schristos     else {
66*0e552da7Schristos         uv_close((uv_handle_t*) client, NULL);
67*0e552da7Schristos     }
68*0e552da7Schristos }
69*0e552da7Schristos 
remove_sock(int sig)70*0e552da7Schristos void remove_sock(int sig) {
71*0e552da7Schristos     uv_fs_t req;
72*0e552da7Schristos     uv_fs_unlink(loop, &req, PIPENAME, NULL);
73*0e552da7Schristos     exit(0);
74*0e552da7Schristos }
75*0e552da7Schristos 
main()76*0e552da7Schristos int main() {
77*0e552da7Schristos     loop = uv_default_loop();
78*0e552da7Schristos 
79*0e552da7Schristos     uv_pipe_t server;
80*0e552da7Schristos     uv_pipe_init(loop, &server, 0);
81*0e552da7Schristos 
82*0e552da7Schristos     signal(SIGINT, remove_sock);
83*0e552da7Schristos 
84*0e552da7Schristos     int r;
85*0e552da7Schristos     if ((r = uv_pipe_bind(&server, PIPENAME))) {
86*0e552da7Schristos         fprintf(stderr, "Bind error %s\n", uv_err_name(r));
87*0e552da7Schristos         return 1;
88*0e552da7Schristos     }
89*0e552da7Schristos     if ((r = uv_listen((uv_stream_t*) &server, 128, on_new_connection))) {
90*0e552da7Schristos         fprintf(stderr, "Listen error %s\n", uv_err_name(r));
91*0e552da7Schristos         return 2;
92*0e552da7Schristos     }
93*0e552da7Schristos     return uv_run(loop, UV_RUN_DEFAULT);
94*0e552da7Schristos }
95