diff options
author | Fedor Indutny <fedor@indutny.com> | 2016-05-27 22:07:58 -0400 |
---|---|---|
committer | Fedor Indutny <fedor@indutny.com> | 2016-05-27 22:07:58 -0400 |
commit | 6a1f2335d96dfaf95a4423b94c36df389e3d4ec2 (patch) | |
tree | 159608e72e6a3fe93593946a4a1c4977aaa9abf4 /example/src/main.c | |
parent | 3523bbda22a89eb0e64cd08958efa6cafd710c09 (diff) | |
download | uv_link_t-6a1f2335d96dfaf95a4423b94c36df389e3d4ec2.tar.gz |
example: simplify
Diffstat (limited to 'example/src/main.c')
-rw-r--r-- | example/src/main.c | 55 |
1 files changed, 17 insertions, 38 deletions
diff --git a/example/src/main.c b/example/src/main.c index ac4c4a9..5c847ec 100644 --- a/example/src/main.c +++ b/example/src/main.c @@ -1,4 +1,3 @@ -#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -9,6 +8,9 @@ /* Declaration of `middle_methods` */ #include "middle.h" +#define CHECK(V) if ((V) != 0) abort() +#define CHECK_ALLOC(V) if ((V) == NULL) abort() + typedef struct client_s client_t; static uv_tcp_t server; @@ -45,65 +47,42 @@ static void read_cb(uv_link_observer_t* observer, static void connection_cb(uv_stream_t* s, int status) { - int err; client_t* client; - client = malloc(sizeof(*client)); - assert(client != NULL); - - err = uv_tcp_init(uv_default_loop(), &client->tcp); - assert(err == 0); - - err = uv_accept(s, (uv_stream_t*) &client->tcp); - assert(err == 0); - - err = uv_link_source_init(&client->source, (uv_stream_t*) &client->tcp); - assert(err == 0); - - err = uv_link_init(&client->middle, &middle_methods); - assert(err == 0); + CHECK_ALLOC(client = malloc(sizeof(*client))); - err = uv_link_chain(&client->source.link, &client->middle); - assert(err == 0); + CHECK(uv_tcp_init(uv_default_loop(), &client->tcp)); + CHECK(uv_accept(s, (uv_stream_t*) &client->tcp)); - err = uv_link_observer_init(&client->observer); - assert(err == 0); - - err = uv_link_chain(&client->middle, &client->observer.link); - assert(err == 0); + CHECK(uv_link_source_init(&client->source, (uv_stream_t*) &client->tcp)); + CHECK(uv_link_init(&client->middle, &middle_methods)); + CHECK(uv_link_chain(&client->source.link, &client->middle)); + CHECK(uv_link_observer_init(&client->observer)); + CHECK(uv_link_chain(&client->middle, &client->observer.link)); client->observer.read_cb = read_cb; client->observer.link.data = client; - err = uv_link_read_start(&client->observer.link); - assert(err == 0); + CHECK(uv_link_read_start(&client->observer.link)); } int main() { static const int kBacklog = 128; - int err; uv_loop_t* loop; struct sockaddr_in addr; loop = uv_default_loop(); - err = uv_tcp_init(loop, &server); - assert(err == 0); - - err = uv_ip4_addr("0.0.0.0", 9000, &addr); - assert(err == 0); - - err = uv_tcp_bind(&server, (struct sockaddr*) &addr, 0); - assert(err == 0); + CHECK(uv_tcp_init(loop, &server)); + CHECK(uv_ip4_addr("0.0.0.0", 9000, &addr)); + CHECK(uv_tcp_bind(&server, (struct sockaddr*) &addr, 0)); fprintf(stderr, "Listening on 0.0.0.0:9000\n"); - err = uv_listen((uv_stream_t*) &server, kBacklog, connection_cb); - - err = uv_run(loop, UV_RUN_DEFAULT); - assert(err == 0); + CHECK(uv_listen((uv_stream_t*) &server, kBacklog, connection_cb)); + CHECK(uv_run(loop, UV_RUN_DEFAULT)); return 0; } |