Migrating from libnanomsg

Previous version of NNG offered direct API compatibility with libnanomsg, but that support is no longer offered in this version.

If your application is still using legacy libnanomsg APIs, you will need to update it for this version of NNG.

Header Files

Most applications can replace all #include <nn/*.h> statements with #include <nng/nng.h>.

Replace -lnanomsg with -lnng. It may be necessary to include additional system libraries, such as -lpthread, depending on your system.

Initialization

It is necessary to explicitly initialize the library, using the nng_init function. This must be called before any other function.

Types

Sockets, dialers, and listeners in libnanomsg are simple integers. In NNG, these are struct types.

Messages are quite different in NNG, with the absence of the POSIX message control headers.

The struct nn_msghdr structure has no equivalent. See nng_msg for the NNG approach to messages. Likewise there is no struct nn_cmsghdr equivalent.

API Conversions

Nanomsg APINNG EquivalentNotes
nn_strerrornng_strerror
nn_errnoNoneErrors are returned directly rather than through errno.
nn_socketVariousUse the appropriate protocol constructor, such as nng_req0_open.
nn_closenng_close
nn_bindnng_listen, nng_listener_createAllocating a listener with nng_lister_create and configuring it offers more capabilities.
nn_connectnng_dial, nng_dialer_createAllocating a dialer with nng_dialer_create and configuring it offers more capabilities.
nn_shutdownnng_lister_close, nng_dialer_close
nn_allocmsgnng_msg_allocThere are significant semantic differences.
nn_freemsgnng_msg_free
nn_reallocmsgnng_msg_realloc
nn_sendnng_send
nn_recvnng_recv
nn_sendmsgnng_sendmsg
nn_getsockoptnng_socket_getNNG has typed accessors for options, and also separate functions for dialers and listeners.
nn_setsockoptnng_socket_set
nn_devicenng_device
nn_pollNoneCan be constructed using nng_aio. Few if any applications ever used this API.
nn_termNoneThe nng_fini API can do this, but is not recommended except when debugging memory leaks.
nn_get_statisticnng_stats_getThe statistics in NNG are completely different, with different semantics and no stability guarantees.
NN_POLLINNoneUsed only with nn_poll.
NN_POLLOUTNoneUsed only with nn_poll.
NN_MSGNNG_FLAG_ALLOCSee nng_send and nng_recv for details.
NN_CMSG_ALIGNNone
NN_CMSG_FIRSTHDRNone
NN_CMSG_NXTHDRNone
NN_CMSG_DATANone
NN_CMSG_LENNone
NN_CMSG_SPACENone
struct nn_iovecnng_iov
struct nn_msghdrnng_msg
struct nn_cmsghdrnng_msg and nng_msg_header

Options

The following options are changed.

Nanomsg OptionNNG EqvaivalentNotes
NN_LINGERNoneNNG does not support tuning this.
NN_SNDBUFNNG_OPT_SENDBUFNNG value is given in messages, not bytes.
NN_RCVBUFNNG_OPT_RECVBUFNNG value is given in messages, not bytes.
NN_SNDTIMEONNG_OPT_SENDTIMEO
NN_RCVTIMEONNG_OPT_RECVTIMEO
NN_RECONNECT_IVLNNG_OPT_RECONNMINT
NN_RECONNECT_IVL_MAXNNG_OPT_RECONNMAXT
NN_SNDPRIONoneNot supported in NNG yet.
NN_RCVPRIONoneNot supported in NNG yet.
NN_RCVFDnng_socket_get_recv_poll_fdNo longer an option, use a function call.
NN_SNDFDnng_socket_get_send_poll_fdNo longer an option, use a function call.
NN_DOMAINNoneNNG options are not divided by domain or protocol.
NN_PROTOCOLnng_socket_proto_idNo longer an option. See also nng_socket_proto_name.
NN_IPV4ONLYNoneUse URL such as tcp4:// to obtain this functionality.
NN_SOCKET_NAMENoneRemoved from NNG.
NN_MAXTTLNNG_OPT_MAXTTL
NN_SUB_SUBSCRIBEnng_sub0_socket_subscribeNo longer an option, use a function call.
NN_SUB_UNSUBSCRIBEnng_sub0_socket_unsubscribeNo longer an option, use a function call.

Error Codes

Most of the error codes have similar names in NNG, just prefixed with NNG_. There are some exceptions. Be aware that the numeric values are not the same.

Nanomsg ErrorNNG ErrorNotes
EINTRNNG_EINTR
ENOMEMNNG_ENOMEM
EINVALNNG_EINVAL, NNG_EADDRINVAL, NNG_EBADTYPE, NNG_EAMBIGUOUSNNG discrimates between different types of errors.
EBUSYNNG_EBUSY
ETIMEDOUTNNG_ETIMEDOUT
ECONNREFUSEDNNG_ECONNREFUSED
EBADFNNG_ECLOSED, NNG_ECANCELEDCanceling an operation returns differently than using an invalid or closed object.
EAGAINNNG_EAGAIN
ENOTSUPNNG_ENOTSUP
EADDRINUSENNG_EADDRINUSE
EFSMNNG_ESTATENot a legal POSIX errno value.
ENOENTNNG_ENOENT
EPROTONNG_EPROTO
EHOSTUNREACHNNG_EUNREACHABLE
EACCCESNNG_EPERM, NNG_EWRITEONLY, NNG_EREADONLY, NNG_ECRYPTO, NNG_EPEERAUTHNNG has more fine grained reasons for access failures.
EMSGSIZENNG_EMSGSIZE
ECONNABORTEDNNG_ECONNABORTED
ECONNRESETNNG_ECONNRESET
EEXISTNNG_EEXIST
EMFILENNG_ENOFILES
ENOSPCNNG_ENOSPC

Local Addresses for Dialing

The ability to specify the source address in the UROL,to use when using nn_dial inside the URL is not present in NNG. The correct way to specify the local address is using the NNG_OPT_LOCADDR option on the dialer before starting to dial.