about summary refs log tree commit diff stats
path: root/example/src
diff options
context:
space:
mode:
authorFedor Indutny <fedor@indutny.com>2016-05-27 21:40:29 -0400
committerFedor Indutny <fedor@indutny.com>2016-05-27 21:40:29 -0400
commit3523bbda22a89eb0e64cd08958efa6cafd710c09 (patch)
treed57ae82dab951f40cf30a051ac02ab7a79c3b440 /example/src
parent9f6eff69114a493047ffdf1bb6f0e3c86b2119aa (diff)
downloaduv_link_t-3523bbda22a89eb0e64cd08958efa6cafd710c09.tar.gz
src: simplify uv_link_close
Diffstat (limited to 'example/src')
-rw-r--r--example/src/main.c16
-rw-r--r--example/src/middle.c11
2 files changed, 22 insertions, 5 deletions
diff --git a/example/src/main.c b/example/src/main.c
index 01d0447..ac4c4a9 100644
--- a/example/src/main.c
+++ b/example/src/main.c
@@ -20,6 +20,13 @@ struct client_s {
   uv_link_observer_t observer;
 };
 
+static void close_cb(uv_link_t* link) {
+  client_t* client;
+
+  client = link->data;
+  free(client);
+}
+
 static void read_cb(uv_link_observer_t* observer,
                     ssize_t nread,
                     const uv_buf_t* buf) {
@@ -29,9 +36,7 @@ static void read_cb(uv_link_observer_t* observer,
 
   if (nread < 0) {
     fprintf(stderr, "error or close\n");
-    uv_link_read_stop(&observer->link);
-    uv_close((uv_handle_t*) &client->tcp, NULL);
-    free(client);
+    uv_link_close(&observer->link, close_cb);
     return;
   }
 
@@ -61,7 +66,10 @@ static void connection_cb(uv_stream_t* s, int status) {
   err = uv_link_chain(&client->source.link, &client->middle);
   assert(err == 0);
 
-  err = uv_link_observer_init(&client->observer, &client->middle);
+  err = uv_link_observer_init(&client->observer);
+  assert(err == 0);
+
+  err = uv_link_chain(&client->middle, &client->observer.link);
   assert(err == 0);
 
   client->observer.read_cb = read_cb;
diff --git a/example/src/middle.c b/example/src/middle.c
index 447a341..6bd4e85 100644
--- a/example/src/middle.c
+++ b/example/src/middle.c
@@ -14,6 +14,12 @@ static int read_stop_impl(uv_link_t* link) {
 }
 
 
+static void close_impl(uv_link_t* link, uv_link_t* source,
+                       uv_link_close_cb cb) {
+  cb(source);
+}
+
+
 static void alloc_cb_override(uv_link_t* link,
                               size_t suggested_size,
                               uv_buf_t* buf) {
@@ -35,7 +41,9 @@ static void read_cb_override(uv_link_t* link,
     else
       res = "go away";
   } else {
-    res = "error";
+    free(buf->base);
+    uv_link_propagate_read_cb(link, nread, NULL);
+    return;
   }
 
   free(buf->base);
@@ -51,6 +59,7 @@ static void read_cb_override(uv_link_t* link,
 uv_link_methods_t middle_methods = {
   .read_start = read_start_impl,
   .read_stop = read_stop_impl,
+  .close = close_impl,
 
   /* Other doesn't matter in this example */
   .alloc_cb_override = alloc_cb_override,