Skip to content

reqshark/mill

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Sep 29, 2016
50f3893 Â· Sep 29, 2016

History

97 Commits
Sep 25, 2015
Sep 29, 2016
Mar 29, 2016
Sep 10, 2015
Jul 14, 2015
Sep 29, 2016
Jul 18, 2015
Jul 18, 2015
Sep 9, 2015
Sep 29, 2016
Sep 29, 2016
Jan 9, 2016
Mar 29, 2016
Sep 29, 2016
Apr 4, 2016
Jan 14, 2016
Sep 21, 2015
Jan 12, 2016

Repository files navigation

node.js function access to libmill

Build Status

go style concurrency for node.

an experiment

$ npm i libmill

# or
$ git clone https://github.com/reqshark/mill.git && cd mill
$ git submodule update --init
$ npm i && npm t

# or build with:
$ make

tcp library

for msgs over tcp/ip, a server can use tcplisten(). once a client connects, the server can use tcpaccept() to establish bidirectional tcp streams.

tcplisten() and tcpaccept()

var lib = require('libmill');

/* create an ipaddr */
var ipaddr = lib.iplocal(5555);

/* listen on the ipaddr */
var ls = lib.tcplisten(ipaddr);
console.log('listening on port %s', lib.tcpport(ls));

while(1){
  /* accept and establish tcp connection */
  var as = lib.tcpaccept(ls);
  console.log('\nnew connection\nmsg recvd: ' + lib.tcprecvuntil(as));

  /* close tcp connection */
  lib.tcpclose(as);
}

tcpconnect()

var lib = require('libmill');

/* create an ipaddr */
var ipaddr = lib.iplocal(5555);

/* connect over the ipaddr */
var cs = lib.tcpconnect(ipaddr);
var str = 'go style concurrency for node', num = 1;

/* send a few msg before tcpflush */
/* delimiter is set to '\r', tells tcprecvuntil when to finish */
send('msg # ', str);
send('msg # ', str);
send('msg #', str+'\r');
function send(n, msg){
  n+= num+++msg+'\n';
  lib.tcpsend(cs, new Buffer(n));
}

/* tcpsend() stores data in user space and tcpflush() pushes it to kernel */
lib.tcpflush(cs);

udp library

udplisten() and udprecv()

var lib = require('libmill');

/* create an ipaddr */
var ipaddr = lib.iplocal(4444);

/* listen on ipaddr */
var ls = lib.udplisten(ipaddr);
process.stdout.write('udp socket listening on port: ' + lib.udpport(ls) + '\n');

/* next, to get udp msgs, use udprecv
   if you pass a cb function as the 3rd param, the call will use libuv async
   the callback's param will contain your msg

   otherwise w/out a cb, it will block and udprecv's return value is your msg */


/* the non-blocking way (a for async) */
lib.udprecv(ls, 255, function (msg) {
  var buf = String(msg.buf) /* msg.buf is a node buffer of the packet body */
  var addr = msg.addr  /* string address of packet origin */
});

/* the blocking way  */
while (1) {
  var sz = 13;
  var deadline = 10;

  var msg = lib.udprecv(ls, sz, deadline); /* deadline is optional param */
  process.stdout.write(msg.buf + '\n');
}

udpsend()

var s = lib.udplisten(ipaddr);
var buf = new Buffer('Hello, world!');

lib.udpsend(s, ipaddr, buf);

test

see test directory

license

MIT

tested on travis linux and osx.