1*0e552da7Schristos #include <stdio.h>
2*0e552da7Schristos #include <stdlib.h>
3*0e552da7Schristos #include <string.h>
4*0e552da7Schristos #include <uv.h>
5*0e552da7Schristos
6*0e552da7Schristos #define DEFAULT_PORT 7000
7*0e552da7Schristos #define DEFAULT_BACKLOG 128
8*0e552da7Schristos
9*0e552da7Schristos uv_loop_t *loop;
10*0e552da7Schristos struct sockaddr_in addr;
11*0e552da7Schristos
12*0e552da7Schristos typedef struct {
13*0e552da7Schristos uv_write_t req;
14*0e552da7Schristos uv_buf_t buf;
15*0e552da7Schristos } write_req_t;
16*0e552da7Schristos
free_write_req(uv_write_t * req)17*0e552da7Schristos void free_write_req(uv_write_t *req) {
18*0e552da7Schristos write_req_t *wr = (write_req_t*) req;
19*0e552da7Schristos free(wr->buf.base);
20*0e552da7Schristos free(wr);
21*0e552da7Schristos }
22*0e552da7Schristos
alloc_buffer(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)23*0e552da7Schristos void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
24*0e552da7Schristos buf->base = (char*) malloc(suggested_size);
25*0e552da7Schristos buf->len = suggested_size;
26*0e552da7Schristos }
27*0e552da7Schristos
on_close(uv_handle_t * handle)28*0e552da7Schristos void on_close(uv_handle_t* handle) {
29*0e552da7Schristos free(handle);
30*0e552da7Schristos }
31*0e552da7Schristos
echo_write(uv_write_t * req,int status)32*0e552da7Schristos void echo_write(uv_write_t *req, int status) {
33*0e552da7Schristos if (status) {
34*0e552da7Schristos fprintf(stderr, "Write error %s\n", uv_strerror(status));
35*0e552da7Schristos }
36*0e552da7Schristos free_write_req(req);
37*0e552da7Schristos }
38*0e552da7Schristos
echo_read(uv_stream_t * client,ssize_t nread,const uv_buf_t * buf)39*0e552da7Schristos void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
40*0e552da7Schristos if (nread > 0) {
41*0e552da7Schristos write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
42*0e552da7Schristos req->buf = uv_buf_init(buf->base, nread);
43*0e552da7Schristos uv_write((uv_write_t*) req, client, &req->buf, 1, echo_write);
44*0e552da7Schristos return;
45*0e552da7Schristos }
46*0e552da7Schristos if (nread < 0) {
47*0e552da7Schristos if (nread != UV_EOF)
48*0e552da7Schristos fprintf(stderr, "Read error %s\n", uv_err_name(nread));
49*0e552da7Schristos uv_close((uv_handle_t*) client, on_close);
50*0e552da7Schristos }
51*0e552da7Schristos
52*0e552da7Schristos free(buf->base);
53*0e552da7Schristos }
54*0e552da7Schristos
on_new_connection(uv_stream_t * server,int status)55*0e552da7Schristos void on_new_connection(uv_stream_t *server, int status) {
56*0e552da7Schristos if (status < 0) {
57*0e552da7Schristos fprintf(stderr, "New connection error %s\n", uv_strerror(status));
58*0e552da7Schristos // error!
59*0e552da7Schristos return;
60*0e552da7Schristos }
61*0e552da7Schristos
62*0e552da7Schristos uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
63*0e552da7Schristos uv_tcp_init(loop, client);
64*0e552da7Schristos if (uv_accept(server, (uv_stream_t*) client) == 0) {
65*0e552da7Schristos uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read);
66*0e552da7Schristos }
67*0e552da7Schristos else {
68*0e552da7Schristos uv_close((uv_handle_t*) client, on_close);
69*0e552da7Schristos }
70*0e552da7Schristos }
71*0e552da7Schristos
main()72*0e552da7Schristos int main() {
73*0e552da7Schristos loop = uv_default_loop();
74*0e552da7Schristos
75*0e552da7Schristos uv_tcp_t server;
76*0e552da7Schristos uv_tcp_init(loop, &server);
77*0e552da7Schristos
78*0e552da7Schristos uv_ip4_addr("0.0.0.0", DEFAULT_PORT, &addr);
79*0e552da7Schristos
80*0e552da7Schristos uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0);
81*0e552da7Schristos int r = uv_listen((uv_stream_t*) &server, DEFAULT_BACKLOG, on_new_connection);
82*0e552da7Schristos if (r) {
83*0e552da7Schristos fprintf(stderr, "Listen error %s\n", uv_strerror(r));
84*0e552da7Schristos return 1;
85*0e552da7Schristos }
86*0e552da7Schristos return uv_run(loop, UV_RUN_DEFAULT);
87*0e552da7Schristos }
88