Chainable libuv streams
Find a file
2023-02-06 17:31:43 +01:00
docs docs: minor fix 2017-04-18 16:58:58 -04:00
example/src gyp: remove extra files 2016-06-07 12:25:27 -04:00
include inlucde: add extern "C" 2016-08-06 18:46:37 +02:00
src fix crashes when close() is called without stream set 2023-01-02 10:59:58 -05:00
test/src test: fix compilation issues on Windows platform 2017-06-14 14:16:12 -04:00
.gitignore Replace stupid gypkg build system with simple Makefile 2023-01-31 23:52:48 +01:00
.travis.yml src: port to gypkg 2016-06-06 17:13:16 -04:00
binding.gyp npm: add binding.gyp to fix travis 2016-06-06 17:16:44 -04:00
common.gypi src: move methods to separate read-only structure 2016-05-26 02:50:14 -04:00
config.mk make install more configurable 2023-02-06 17:31:43 +01:00
Makefile make install more configurable 2023-02-06 17:31:43 +01:00
options.gypi src: port to gypkg 2016-06-06 17:13:16 -04:00
package.json 1.0.5 2017-06-14 14:16:43 -04:00
README.md readme: png => svg 2016-06-07 02:23:06 -04:00
uv_link_t.gyp gyp: specify mini-test.c semver 2017-04-16 16:03:15 -04:00

uv_link_t

GitHub version Build Status

HIGHLY UNSTABLE

Chainable libuv streams.

Why?

It is quite easy to write a TCP server/client in libuv. Writing HTTP server/client is a bit harder. Writing HTTP server/client on top of TLS server could be unwieldy.

uv_link_t aims to solve complexity problem that quickly escalates once using multiple layers of protocols in libuv by providing a way to implement protocols separately and chain them together in an easy and high-performant way using very narrow interfaces.

How?

(NOTE: chain is built from links)

(NOTE: many of these API methods have return values, check them!)

First, a uv_stream_t* instance needs to be picked. It will act as a source link in a chain:

uv_stream_t* stream = ...;

uv_link_source_t source;

uv_link_source_init(uv_default_loop(), &source, stream);

A chain can be formed with &source and any other uv_link_t instance:

uv_link_t link;

static uv_link_methods_t methods = {
  .read_start = read_start_impl,
  .read_stop = read_stop_impl,
  .write = write_impl,
  .try_write = try_write_impl,
  .shutdown = shutdown_impl,
  .close = close_impl,

  /* These will be used only when chaining two links together */

  .alloc_cb_override = alloc_cb_impl,
  .read_cb_override = read_cb_impl
};

uv_link_init(&link, &methods);

/* Just like in libuv */
link.alloc_cb = my_alloc_cb;
link.read_cb = my_read_cb;

/* Creating a chain here */
uv_link_chain(&source, &link);

uv_link_read_start(&link);

Now comes a funny part, any of these method implementations may hook up into the parent link in a chain to perform their actions:

static int shutdown_impl(uv_link_t* link,
                         uv_link_t* source,
                         uv_link_shutdown_cb cb,
                         void* arg) {
  fprintf(stderr, "this will be printed\n");
  return uv_link_propagate_shutdown(link->parent, source, cb, arg);
}

Technical details

API Docs Implementation Guide

LICENSE

This software is licensed under the MIT License.

Copyright Fedor Indutny, 2016.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.