about summary refs log tree commit diff stats
path: root/example/src/middle.c
diff options
context:
space:
mode:
authorFedor Indutny <fedor@indutny.com>2016-05-26 03:42:56 -0400
committerFedor Indutny <fedor@indutny.com>2016-05-26 03:42:56 -0400
commitf3be4240e5da100f7615e6805648ed50485c2ebb (patch)
tree1ab9b525bdf3f47a53d49602a0383548861906b6 /example/src/middle.c
parente6061e7dbcc1ac16222f2c44c467cf5e7a3f31ad (diff)
downloaduv_link_t-f3be4240e5da100f7615e6805648ed50485c2ebb.tar.gz
example: initial
Diffstat (limited to 'example/src/middle.c')
-rw-r--r--example/src/middle.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/example/src/middle.c b/example/src/middle.c
new file mode 100644
index 0000000..9e6d158
--- /dev/null
+++ b/example/src/middle.c
@@ -0,0 +1,58 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "uv_link_t.h"
+
+static int read_start_impl(uv_link_t* link) {
+  return uv_link_read_start(link->parent);
+}
+
+
+static int read_stop_impl(uv_link_t* link) {
+  return uv_link_read_stop(link->parent);
+}
+
+
+static void alloc_cb_override(uv_link_t* link,
+                              size_t suggested_size,
+                              uv_buf_t* buf) {
+  buf->base = malloc(suggested_size);
+  assert(buf->base != NULL);
+  buf->len = suggested_size;
+}
+
+
+static void read_cb_override(uv_link_t* link,
+                             ssize_t nread,
+                             const uv_buf_t* buf) {
+  const char* res;
+  uv_buf_t tmp;
+
+  if (nread >= 0) {
+    if (nread == 9 && strncmp(buf->base, "password\n", 9) == 0)
+      res = "welcome";
+    else
+      res = "go away";
+  } else {
+    res = "error";
+  }
+
+  free(buf->base);
+
+  uv_link_invoke_alloc_cb(link, strlen(res), &tmp);
+  assert(tmp.len >= strlen(res));
+
+  memcpy(tmp.base, res, strlen(res));
+  uv_link_invoke_read_cb(link, strlen(res), &tmp);
+}
+
+
+uv_link_methods_t middle_methods = {
+  .read_start = read_start_impl,
+  .read_stop = read_stop_impl,
+
+  /* Other doesn't matter in this example */
+  .alloc_cb_override = alloc_cb_override,
+  .read_cb_override = read_cb_override
+};