[nanomsg] Re: async aio framework exposed to apps in nng

  • From: Michael Powell <mwpowellhtx@xxxxxxxxx>
  • To: "nanomsg@xxxxxxxxxxxxx" <nanomsg@xxxxxxxxxxxxx>
  • Date: Thu, 26 Oct 2017 18:29:19 -0400

On Wed, Oct 25, 2017 at 9:43 PM, Garrett D'Amore <garrett@xxxxxxxxxx> wrote:

(This is only for nng, not available for legacy nanomsg.)

I've added async support to the C++ wrappers.

https://github.com/mwpowellhtx/nngcpp/releases/tag/0.12.0

I just pushed nng bug #45.  This changes the API.  The old event callback
scheme is removed (it was pretty horrible actually), and I’m exposing an
“aio” framework.

The way aio’s work is that you allocate one (it’s like a handle) with a
completion callback and a user supplied argument for the callback.

Then you can submit operations and get notified when they are complete
later.  You can also wait on them to be complete.

E.g:

```
nng_aio *a;
void mycallback(void *);
void *myarg;

nng_aio_alloc(&a, mycallback, myarg);
nng_aio_set_timeout (a, 100);
nng_aio_set_message (a, msg);

nng_send_aio(s, a);

… asynchronously the message is sent, and the mycallback is executed.
… it can read the aio result via nng_aio_result (a);

Operations can also be canceled with nng_aio_cancel().

You can then do nng_aio_wait(a).

The details are for now in the nng.h header file.

Note that this is largely the same stuff I’m using *internally* in nng.  I
figured that it was so useful to me that folks building other kinds of async
apps could use it.  For example, this could be used to integrate into an
event loop or drive state machine transitions, etc.

Anyway, not sure how many of you might be poking at nng these days, but this
was the major API change that I wanted to get through before declaring a
broader beta release.  I’d appreciate folks bang away at this.

I’m still going to try to get websocket and TLS support knocked out before
*formal* beta. Oh, and I need to write a lot more docs. :-)

Enjoy!

 - Garrett

Cheers,

Michael

Other related posts: