Git Product home page Git Product logo

nsuv's Introduction

nsuv

C++ wrapper around libuv focused on making callback arg passing safer. The data passed to the callback does not use the data member of the uv_handle_t or uv_req_t. So they can still be used as usual.

You can check out our blog post for additional information.

Example usage:

// Notice that the second argument of the callback can contain any pointer type,
// and it'll automatically be checked from the callsite, and give a compiler
// error if it doesn't match.

#include "nsuv-inl.h"

using namespace nsuv;

ns_tcp client;
ns_tcp incoming;
ns_tcp server;
ns_connect<ns_tcp> connect_req;
ns_write<ns_tcp> write_req;

static void alloc_cb(ns_tcp* handle, size_t, uv_buf_t* buf) {
  static char slab[1024];

  buf->base = slab;
  buf->len = sizeof(slab);
}

static void read_cb(ns_tcp* handle, ssize_t, const uv_buf_t*) {
  handle->close();
  client.close();
  server.close();
}

static void write_cb(ns_write<ns_tcp>* req, int) {
  // Retrieve a reference to the uv_buf_t array and size.
  uv_buf_t* bufs = req->bufs();
  size_t size = req->size();
}

static void connection_cb(ns_tcp* server, int) {
  int r;
  r = incoming.init(server->get_loop());
  r = server->accept(&incoming);
  r = incoming.read_start(alloc_cb, read_cb);
}

static void connect_cb(ns_connect<ns_tcp>* req, int, char* data) {
  static char bye_ctr[] = "BYE";
  uv_buf_t buf1 = uv_buf_init(data, strlen(data));
  uv_buf_t buf2 = uv_buf_init(bye_ctr, strlen(bye_ctr));
  // Write to the handle attached to this request and pass along data.
  int r = req->handle()->write(&write_req, { buf1, buf2 }, write_cb);
}

static void do_listen() {
  static char hello_cstr[] = "HELLO";
  struct sockaddr_in addr_in;
  struct sockaddr* addr;
  int r;

  r = uv_ip4_addr("127.0.0.1", 9999, &addr_in);
  addr = reinterpret_cast<struct sockaddr*>(&addr_in);

  // Server setup.
  r = server.init(uv_default_loop());
  r = server.bind(addr, 0);
  r = server.listen(1, connection_cb);

  // Client connection.
  r = client.init(uv_default_loop());
  r = client.connect(&connect_req, addr, connect_cb, hello_cstr);

  uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}

Additional usage can be seen in test/.

nsuv's People

Contributors

santigimeno avatar trevnorris avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.