xref: /libuv/docs/code/multi-echo-server/worker.c (revision d59d6e6f)
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <uv.h>
7 
8 uv_loop_t *loop;
9 uv_pipe_t queue;
10 
11 typedef struct {
12     uv_write_t req;
13     uv_buf_t buf;
14 } write_req_t;
15 
free_write_req(uv_write_t * req)16 void free_write_req(uv_write_t *req) {
17     write_req_t *wr = (write_req_t*) req;
18     free(wr->buf.base);
19     free(wr);
20 }
21 
alloc_buffer(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)22 void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
23   buf->base = malloc(suggested_size);
24   buf->len = suggested_size;
25 }
26 
echo_write(uv_write_t * req,int status)27 void echo_write(uv_write_t *req, int status) {
28     if (status) {
29         fprintf(stderr, "Write error %s\n", uv_err_name(status));
30     }
31     free_write_req(req);
32 }
33 
echo_read(uv_stream_t * client,ssize_t nread,const uv_buf_t * buf)34 void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
35     if (nread > 0) {
36         write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
37         req->buf = uv_buf_init(buf->base, nread);
38         uv_write((uv_write_t*) req, client, &req->buf, 1, echo_write);
39         return;
40     }
41 
42     if (nread < 0) {
43         if (nread != UV_EOF)
44             fprintf(stderr, "Read error %s\n", uv_err_name(nread));
45         uv_close((uv_handle_t*) client, NULL);
46     }
47 
48     free(buf->base);
49 }
50 
on_new_connection(uv_stream_t * q,ssize_t nread,const uv_buf_t * buf)51 void on_new_connection(uv_stream_t *q, ssize_t nread, const uv_buf_t *buf) {
52     if (nread < 0) {
53         if (nread != UV_EOF)
54             fprintf(stderr, "Read error %s\n", uv_err_name(nread));
55         uv_close((uv_handle_t*) q, NULL);
56         return;
57     }
58 
59     uv_pipe_t *pipe = (uv_pipe_t*) q;
60     if (!uv_pipe_pending_count(pipe)) {
61         fprintf(stderr, "No pending count\n");
62         return;
63     }
64 
65     uv_handle_type pending = uv_pipe_pending_type(pipe);
66     assert(pending == UV_TCP);
67 
68     uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
69     uv_tcp_init(loop, client);
70     if (uv_accept(q, (uv_stream_t*) client) == 0) {
71         uv_os_fd_t fd;
72         uv_fileno((const uv_handle_t*) client, &fd);
73         fprintf(stderr, "Worker %d: Accepted fd %d\n", getpid(), fd);
74         uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read);
75     }
76     else {
77         uv_close((uv_handle_t*) client, NULL);
78     }
79 }
80 
main()81 int main() {
82     loop = uv_default_loop();
83 
84     uv_pipe_init(loop, &queue, 1 /* ipc */);
85     uv_pipe_open(&queue, 0);
86     uv_read_start((uv_stream_t*)&queue, alloc_buffer, on_new_connection);
87     return uv_run(loop, UV_RUN_DEFAULT);
88 }
89