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