1 module kaleidic.nanomsg.bindings; 2 3 4 /** 5 Ported to Dlang (2014,2015,2016) by Laeeth Isharc. Caveat emptor. 6 */ 7 8 extern(C) @system nothrow @nogc align(1): 9 10 enum PROTO_SP = 1; 11 enum SP_HDR = 1; 12 enum NN_H_INCLUDED = 1; 13 14 /******************************************************************************/ 15 /* ABI versioning support. */ 16 /******************************************************************************/ 17 18 /* Don't change this unless you know exactly what you're doing and have */ 19 /* read and understand the following documents: */ 20 /* www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html */ 21 /* www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html */ 22 23 /* The current interface version. */ 24 enum 25 { 26 NN_VERSION_CURRENT = 5, 27 NN_VERSION_REVISION = 0, 28 NN_VERSION_AGE = 0, 29 } 30 31 /******************************************************************************/ 32 /* Errors. */ 33 /******************************************************************************/ 34 35 /* A number random enough not to collide with different errno ranges on */ 36 /* different OSes. The assumption is that error_t is at least 32-bit type. */ 37 enum 38 { 39 40 NN_HAUSNUMERO=156384712, 41 42 /* On some platforms some standard POSIX errnos are not defined. */ 43 ENOTSUP=(NN_HAUSNUMERO + 1), 44 EPROTONOSUPPORT =(NN_HAUSNUMERO + 2), 45 ENOBUFS =(NN_HAUSNUMERO + 3), 46 ENETDOWN =(NN_HAUSNUMERO + 4), 47 EADDRINUSE =(NN_HAUSNUMERO + 5), 48 EADDRNOTAVAIL =(NN_HAUSNUMERO + 6), 49 ECONNREFUSED =(NN_HAUSNUMERO + 7), 50 EINPROGRESS =(NN_HAUSNUMERO + 8), 51 ENOTSOCK =(NN_HAUSNUMERO + 9), 52 EAFNOSUPPORT =(NN_HAUSNUMERO + 10), 53 EPROTO =(NN_HAUSNUMERO + 11), 54 EAGAIN =(NN_HAUSNUMERO + 12), 55 EBADF =(NN_HAUSNUMERO + 13), 56 EINVAL= (NN_HAUSNUMERO + 14), 57 EMFILE =(NN_HAUSNUMERO + 15), 58 EFAULT =(NN_HAUSNUMERO + 16), 59 EACCESS =(NN_HAUSNUMERO + 17), 60 ENETRESET =(NN_HAUSNUMERO + 18), 61 ENETUNREACH =(NN_HAUSNUMERO + 19), 62 EHOSTUNREACH= (NN_HAUSNUMERO + 20), 63 ENOTCONN =(NN_HAUSNUMERO + 21), 64 EMSGSIZE= (NN_HAUSNUMERO + 22), 65 ETIMEDOUT= (NN_HAUSNUMERO + 23), 66 ECONNABORTED= (NN_HAUSNUMERO + 24), 67 ECONNRESET =(NN_HAUSNUMERO + 25), 68 ENOPROTOOPT =(NN_HAUSNUMERO + 26), 69 EISCONN =(NN_HAUSNUMERO + 27), 70 NN_EISCONN_DEFINED=1, 71 ESOCKTNOSUPPORT =(NN_HAUSNUMERO + 28), 72 ETERM =(NN_HAUSNUMERO + 53), 73 EFSM =(NN_HAUSNUMERO + 54), 74 } 75 76 /** 77 This function retrieves the errno as it is known to the library. 78 The goal of this function is to make the code 100% portable, including 79 where the library is compiled with certain CRT library (on Windows) and 80 linked to an application that uses different CRT library. 81 82 Returns: the errno as it is known to the library 83 */ 84 extern (C) int nn_errno(); 85 86 /** Resolves system errors and native errors to human-readable string. 87 Returns: const(char)* human-readable string 88 */ 89 extern (C) const(char)* nn_strerror (int errnum); 90 91 92 /** 93 Returns the symbol name (e.g. "NN_REQ") and value at a specified index. 94 If the index is out-of-range, returns 0 and sets errno to EINVAL 95 General usage is to start at i=0 and iterate until 0 is returned. 96 97 Params: 98 i = index 99 v = pointer to value 100 101 Returns: symbol name eg "NN_REQ" and value at a specified index 102 */ 103 extern (C) char *nn_symbol (int i, int *value); 104 105 /* Constants that are returned in `ns` member of nn_symbol_properties */ 106 enum NN_NS 107 { 108 NAMESPACE =0, 109 VERSION =1, 110 DOMAIN =2, 111 TRANSPORT= 3, 112 PROTOCOL =4, 113 OPTION_LEVEL= 5, 114 SOCKET_OPTION= 6, 115 TRANSPORT_OPTION =7, 116 OPTION_TYPE =8, 117 OPTION_UNIT =9, 118 FLAG =10, 119 ERROR =11, 120 LIMIT=12, 121 EVENT=13, 122 } 123 enum NN_NS_NAMESPACE = NN_NS.NAMESPACE; 124 enum NN_NS_VERSION = NN_NS.VERSION; 125 enum NN_NS_DOMAIN = NN_NS.DOMAIN; 126 enum NN_NS_TRANSPORT = NN_NS.TRANSPORT; 127 enum NN_NS_PROTOCOL = NN_NS.PROTOCOL; 128 enum NN_NS_OPTION_LEVEL = NN_NS.OPTION_LEVEL; 129 enum NN_NS_SOCKET_OPTION = NN_NS.SOCKET_OPTION; 130 enum NN_NS_TRANSPORT_OPTION = NN_NS.TRANSPORT_OPTION; 131 enum NN_NS_OPTION_TYPE = NN_NS.OPTION_TYPE; 132 enum NN_NS_OPTION_UNIT = NN_NS.OPTION_UNIT; 133 enum NN_NS_FLAG = NN_NS.FLAG; 134 enum NN_NS_ERROR = NN_NS.ERROR; 135 enum NN_NS_LIMIT = NN_NS.LIMIT; 136 enum NN_NS_EVENT = NN_NS.EVENT; 137 138 139 /** Constants that are returned in `ns` member of nn_symbol_properties */ 140 enum NN_TYPE 141 { 142 NONE=0, 143 INT=1, 144 STR=2, 145 } 146 147 enum NN_TYPE_NONE = NN_TYPE.NONE; 148 enum NN_TYPE_INT = NN_TYPE.INT; 149 enum NN_TYPE_STR = NN_TYPE.STR; 150 151 /** Constants that are returned in `ns` member of nn_symbol_properties */ 152 enum NN_UNIT 153 { 154 NONE =0, 155 BYTES =1, 156 MILLISECONDS =2, 157 PRIORITY =3, 158 BOOLEAN =4, 159 } 160 enum NN_UNIT_NONE = NN_UNIT.NONE; 161 enum NN_UNIT_BYTES = NN_UNIT.BYTES; 162 enum NN_UNIT_MILLISECONDS = NN_UNIT.MILLISECONDS; 163 enum NN_UNIT_PRIORITY = NN_UNIT.PRIORITY; 164 enum NN_UNIT_BOOLEAN = NN_UNIT.BOOLEAN; 165 166 /* Structure that is returned from nn_symbol */ 167 align(1) struct nn_symbol_properties 168 { 169 /* The constant value */ 170 int value; 171 /* The constant name */ 172 const(char)* name; 173 /* The constant namespace, or zero for namespaces themselves */ 174 int ns; 175 /* The option type for socket option constants */ 176 int type; 177 /* The unit for the option value for socket option constants */ 178 int unit; 179 } 180 181 /* Fills in nn_symbol_properties structure and returns it's length */ 182 /* If the index is out-of-range, returns 0 */ 183 /* General usage is to start at i=0 and iterate until zero is returned. */ 184 extern (C) int nn_symbol_info (int i, nn_symbol_properties* buf, int buflen); 185 186 /******************************************************************************/ 187 /* Helper function for shutting down multi-threaded applications. */ 188 /******************************************************************************/ 189 extern (C) void nn_term (); 190 191 192 /******************************************************************************/ 193 /* Zero-copy support. */ 194 /******************************************************************************/ 195 196 enum NN_MSG= cast(size_t)-1; 197 extern (C) void *nn_allocmsg (size_t size, int type); 198 extern (C) void *nn_reallocmsg (void* msg, size_t size); 199 extern (C) int nn_freemsg (void* msg); 200 201 /******************************************************************************/ 202 /* Socket definition. */ 203 /******************************************************************************/ 204 align(1) struct nn_iovec 205 { 206 void* iov_base; 207 size_t iov_len; 208 } 209 210 align(1) struct nn_msghdr 211 { 212 nn_iovec* msg_iov; 213 int msg_iovlen; 214 void* msg_control; 215 size_t msg_controllen; 216 } 217 218 align(1) struct nn_cmsghdr 219 { 220 size_t cmsg_len; 221 int cmsg_level; 222 int cmsg_type; 223 } 224 225 /** Internal function. Not to be used directly. 226 Use NN_CMSG_NEXTHDR macro instead. 227 */ 228 extern (C) nn_cmsghdr* nn_cmsg_nexthdr_ (const(nn_msghdr)* mhdr,const(nn_cmsghdr)* cmsg); 229 alias NN_CMSG_ALIGN_ = (len) => (len + size_t.sizeof - 1) & cast(size_t) ~(size_t.sizeof - 1); 230 231 /* POSIX-defined msghdr manipulation. */ 232 233 alias NN_CMSG_FIRSTHDR = (mhdr) => nn_cmsg_nxthdr_ (cast(nn_msghdr*) mhdr, null); 234 235 alias NN_CMSG_NXTHDR = (mhdr, cmsg) => nn_cmsg_nxthdr_ (cast(nn_msghdr*) mhdr, cast(nn_cmsghdr*) cmsg); 236 237 alias NN_CMSG_DATA = (cmsg) => cast(ubyte*) ((cast(nn_cmsghdr*) cmsg) + 1); 238 239 /* Extensions to POSIX defined by RFC 3542. */ 240 241 alias NN_CMSG_SPACE = (len) => (NN_CMSG_ALIGN_ (len) + NN_CMSG_ALIGN_ (nn_cmsghdr.sizeof)); 242 243 alias NN_CMSG_LEN = (len) => (NN_CMSG_ALIGN_ (nn_cmsghdr.sizeof) + (len)); 244 245 246 /* SP address families. */ 247 enum AF_SP = 1; 248 enum AF_SP_RAW = 2; 249 250 /* Max size of an SP address. */ 251 enum NN_SOCKADDR_MAX =128; 252 253 /* Socket option levels: Negative numbers are reserved for transports, 254 positive for socket types. */ 255 enum NN_SOL_SOCKET =0; 256 257 /* Generic socket options (NN_SOL_SOCKET level). */ 258 enum NN 259 { 260 LINGER=1, 261 SNDBUF =2, 262 RCVBUF =3, 263 SNDTIMEO =4, 264 RCVTIMEO =5, 265 RECONNECT_IVL= 6, 266 RECONNECT_IVL_MAX =7, 267 SNDPRIO =8, 268 RCVPRIO= 9, 269 SNDFD=10, 270 RCVFD =11, 271 DOMAIN =12, 272 PROTOCOL =13, 273 IPV4ONLY =14, 274 SOCKET_NAME=15, 275 RCVMAXSIZE=16, 276 } 277 enum NN_LINGER = NN.LINGER; 278 enum NN_SNDBUF = NN.SNDBUF; 279 enum NN_RCVBUF = NN.RCVBUF; 280 enum NN_SNDTIMEO = NN.SNDTIMEO; 281 enum NN_RCVTIMEO = NN.RCVTIMEO; 282 enum NN_RECONNECT_IVL = NN.RECONNECT_IVL; 283 enum NN_RECONNECT_IVL_MAX = NN.RECONNECT_IVL_MAX; 284 enum NN_SNDPRIO = NN.SNDPRIO; 285 enum NN_RCVPRIO = NN.RCVPRIO; 286 enum NN_SNDFD = NN.SNDFD; 287 enum NN_RCVFD = NN.RCVFD; 288 enum NN_DOMAIN = NN.DOMAIN; 289 enum NN_PROTOCOL = NN.PROTOCOL; 290 enum NN_IPV4ONLY = NN.IPV4ONLY; 291 enum NN_SOCKET_NAME = NN.SOCKET_NAME; 292 enum NN_RCVMAXSIZE = NN.RCVMAXSIZE; 293 294 /* Send/recv options. */ 295 enum NN_DONTWAIT =1; 296 297 298 extern (C) int nn_socket(int domain, int protocol); 299 extern (C) int nn_close(int s); 300 extern (C) int nn_setsockopt(int s, int level, int option, const(void)* optval,size_t optvallen); 301 extern (C) int nn_getsockopt(int s, int level, int option, void* optval,size_t* optvallen); 302 extern (C) int nn_bind(int s, const(char)* addr); 303 extern (C) int nn_connect(int s, const(char)* addr); 304 extern (C) int nn_shutdown(int s, int how); 305 extern (C) int nn_send(int s, const(void)* buf, size_t len, int flags); 306 extern (C) int nn_recv(int s, void* buf, size_t len, int flags); 307 extern (C) int nn_sendmsg(int s, nn_msghdr* msghdr, int flags); 308 extern (C) int nn_sendmsg(int s, const(nn_msghdr)* msghdr, int flags); 309 extern (C) int nn_recvmsg(int s, nn_msghdr* msghdr, int flags); 310 311 312 /******************************************************************************/ 313 /* Socket multiplexing support. */ 314 /******************************************************************************/ 315 316 enum NN_POLLIN = 1; 317 enum NN_POLLOUT = 2; 318 319 align(1) struct nn_pollfd 320 { 321 int fd; 322 short events; 323 short revents; 324 } 325 326 extern(C) int nn_poll(nn_pollfd* fds, int nfds, int timeout); 327 328 /******************************************************************************/ 329 /* Built-in support for devices. */ 330 /******************************************************************************/ 331 332 extern(C) int nn_device(int s1, int s2); 333 334 /******************************************************************************/ 335 /* Built-in support for multiplexers. */ 336 /******************************************************************************/ 337 338 /******************************************************************************/ 339 /* Built-in support for devices. */ 340 /******************************************************************************/ 341 342 extern(C) int nn_tcpmuxd (int port); 343 344 345 enum PAIR_H_INCLUDED=1; 346 347 /** 348 PAIR 349 */ 350 enum NN_PROTO_PAIR=1; 351 enum NN_PAIR=(NN_PROTO_PAIR * 16 + 0); 352 353 enum PIPELINE_H_INCLUDED=1; 354 355 /** 356 PIPELINE 357 */ 358 enum NN_PROTO_PIPELINE=5; 359 enum NN_PUSH=(NN_PROTO_PIPELINE * 16 + 0); 360 enum NN_PULL=(NN_PROTO_PIPELINE * 16 + 1); 361 362 363 364 enum NN_PROTOCOL_INCLUDED=1; 365 struct nn_ctx; 366 enum NN_PIPE 367 { 368 RELEASE=1, 369 PARSED=2, 370 IN=33987, 371 OUT=33988, 372 } 373 struct nn_pipe; 374 struct nn_msg; 375 376 extern(C) 377 { 378 void nn_pipe_setdata ( nn_pipe *self, void *data); 379 void *nn_pipe_getdata (nn_pipe *self); 380 int nn_pipe_send(nn_pipe *self, nn_msg *msg); 381 int nn_pipe_recv ( nn_pipe *self, nn_msg *msg); 382 void nn_pipe_getopt ( nn_pipe *self, int level, int option,void *optval, size_t *optvallen); 383 } 384 385 386 /******************************************************************************/ 387 /* Base class for all socket types. */ 388 /******************************************************************************/ 389 390 /* Any combination of these events can be returned from 'events' virtual 391 function. */ 392 enum NN_SOCKBASE_EVENT_IN=1; 393 enum NN_SOCKBASE_EVENT_OUT=2; 394 395 /* To be implemented by individual socket types. */ 396 align(1) struct nn_sockbase_vfptr 397 { 398 extern(C) void function(nn_sockbase*) stop; 399 extern(C) void function(nn_sockbase*) destroy; 400 extern(C) int function(nn_sockbase*,nn_pipe*) add; 401 extern(C) void function(nn_sockbase*, nn_pipe*) rm; 402 extern(C) void function(nn_sockbase*, nn_pipe*) in_; 403 extern(C) void function(nn_sockbase*, nn_pipe*) out_; 404 extern(C) int function(nn_sockbase*) events; 405 extern(C) int function(nn_sockbase*, nn_msg*) send; 406 extern(C) int function(nn_sockbase*, nn_msg*) recv; 407 extern(C) int function(nn_sockbase*, int level, int option,const void* optval, size_t optvallen) setopt; 408 extern(C) int function(nn_sockbase*, int level, int option,void* optval, size_t *optvallen) getopt; 409 } 410 411 struct nn_sockbase 412 { 413 const(nn_sockbase_vfptr)* vfptr; 414 nn_sock *sock; 415 } 416 417 /* Initialise the socket base class. 'hint' is the opaque value passed to the 418 nn_transport's 'create' function. */ 419 extern(C) void nn_sockbase_init ( nn_sockbase *self,const nn_sockbase_vfptr *vfptr, void *hint); 420 421 /* Terminate the socket base class. */ 422 extern(C) void nn_sockbase_term ( nn_sockbase *self); 423 424 /* Call this function when stopping is done. */ 425 extern(C) void nn_sockbase_stopped ( nn_sockbase *self); 426 427 /* Returns the AIO context associated with the socket. This function is 428 useful when socket type implementation needs to create async objects, 429 such as timers. */ 430 extern(C) nn_ctx *nn_sockbase_getctx ( nn_sockbase *self); 431 432 /* Retrieve a NN_SOL_SOCKET-level option. */ 433 extern(C) int nn_sockbase_getopt (nn_sockbase* self, int option, void* optval, size_t *optvallen); 434 435 /* Add some statitistics for socket */ 436 extern(C) void nn_sockbase_stat_increment (nn_sockbase* self, int name,int increment); 437 438 enum NN_STAT_CURRENT_SND_PRIORITY=401; 439 440 /******************************************************************************/ 441 /* The socktype class. */ 442 /******************************************************************************/ 443 444 /* This structure defines a class factory for individual socket types. */ 445 446 /* Specifies that the socket type can be never used to receive messages. */ 447 enum NN_SOCKTYPE_FLAG_NORECV=1; 448 449 /* Specifies that the socket type can be never used to send messages. */ 450 enum NN_SOCKTYPE_FLAG_NOSEND=2; 451 452 struct nn_socktype { 453 int domain; 454 int protocol; 455 int flags; 456 int function(void *hint, nn_sockbase **sockbase) create; 457 int function(int socktype) ispeer; 458 nn_list_item* item; 459 }; 460 461 462 /** 463 PUBSUB 464 */ 465 enum NN_PROTO_PUBSUB=2; 466 enum NN_PUB=NN_PROTO_PUBSUB * 16 + 0; 467 enum NN_SUB=NN_PROTO_PUBSUB * 16 + 1; 468 enum NN_SUB_SUBSCRIBE =1; 469 enum NN_SUB_UNSUBSCRIBE=2; 470 471 472 /** 473 BUS 474 */ 475 enum NN_PROTO_BUS=7; 476 enum NN_BUS=(NN_PROTO_BUS * 16 + 0); 477 478 /** 479 INPROC 480 */ 481 enum NN_INPROC=-1; 482 483 /** 484 IPC 485 */ 486 487 enum NN_IPC=-2; 488 489 490 /** 491 REQREP 492 */ 493 enum REQREP_H_INCLUDED=1; 494 enum NN_PROTO_REQREP = 3; 495 496 enum NN_REQ=NN_PROTO_REQREP * 16 + 0; 497 enum NN_REP=NN_PROTO_REQREP * 16 + 1; 498 499 enum NN_REQ_RESEND_IVL=1; 500 501 align(1) union nn_req_handle 502 { 503 int i; 504 void *ptr; 505 }; 506 507 extern(C) int nn_req_send (int s, nn_req_handle hndl, const(void)* buf, size_t len, int flags); 508 extern(C) int nn_req_recv (int s, nn_req_handle *hndl, void *buf, size_t len, int flags); 509 510 511 /** 512 TCPMUX 513 */ 514 enum NN_TCPMUX = -5; 515 enum NN_TCPMUX_NODELAY = 1; 516 517 /** 518 WS 519 */ 520 enum NN_WS = -4; 521 522 /* NN_WS level socket/cmsg options. Note that only NN_WSMG_TYPE_TEXT and 523 NN_WS_MSG_TYPE_BINARY messages are supported fully by this implementation. 524 Attempting to set other message types is undefined. */ 525 enum NN_WS_MSG_TYPE = 1; 526 527 /* WebSocket opcode constants as per RFC 6455 5.2 */ 528 enum NN_WS_MSG_TYPE_TEXT = 0x01; 529 enum NN_WS_MSG_TYPE_BINARY = 0x02; 530 531 /** 532 SURVEY 533 */ 534 535 536 enum SURVEY_H_INCLUDED=1; 537 enum NN_PROTO_SURVEY=6; 538 539 enum NN_SURVEYOR=(NN_PROTO_SURVEY * 16 + 2); 540 enum NN_RESPONDENT=(NN_PROTO_SURVEY * 16 + 3); 541 542 enum NN_SURVEYOR_DEADLINE=1; 543 enum TCP_H_INCLUDED=1; 544 enum NN_TCP=-3; 545 enum NN_TCP_NODELAY=1; 546 struct nn_sock; 547 struct nn_cp; 548 struct nn_ep; 549 550 struct nn_optset_vfptr 551 { 552 extern (C) void function(nn_optset *self) destroy; 553 extern (C) int function(nn_optset *self, int option, const(void)* optval,size_t optvallen) setopt; 554 extern (C) int function(nn_optset *self, int option, void *optval,size_t *optvallen) getopt; 555 } 556 557 struct nn_optset 558 { 559 const(nn_optset_vfptr)* vfptr; 560 }; 561 562 563 struct nn_epbase_vfptr 564 { 565 extern (C) void function(nn_epbase *) stop; 566 extern (C) void function(nn_epbase *) destroy; 567 } 568 569 struct nn_epbase 570 { 571 const(nn_epbase_vfptr)* vfptr; 572 nn_ep *ep; 573 } 574 575 /* Creates a new endpoint. 'hint' parameter is an opaque value that 576 was passed to transport's bind or connect function. */ 577 578 extern(C) void nn_epbase_init ( nn_epbase *self,const nn_epbase_vfptr *vfptr, void *hint); 579 extern(C) void nn_epbase_stopped ( nn_epbase *self); 580 extern(C) void nn_epbase_term (nn_epbase *self); 581 extern(C) nn_ctx *nn_epbase_getctx ( nn_epbase *self); 582 extern(C) char *nn_epbase_getaddr ( nn_epbase *self); 583 extern(C) void nn_epbase_getopt ( nn_epbase *self, int level, int option,void *optval, size_t *optvallen); 584 extern(C) int nn_epbase_ispeer ( nn_epbase *self, int socktype); 585 extern(C) void nn_epbase_set_error( nn_epbase *self, int errnum); 586 extern(C) void nn_epbase_clear_error( nn_epbase *self); 587 extern(C) void nn_epbase_stat_increment(nn_epbase *self, int name, int increment); 588 589 590 enum NN_STAT 591 { 592 ESTABLISHED_CONNECTIONS=101, 593 ACCEPTED_CONNECTIONS =102, 594 DROPPED_CONNECTIONS =103, 595 BROKEN_CONNECTIONS =104, 596 CONNECT_ERRORS =105, 597 BIND_ERRORS =106, 598 ACCEPT_ERRORS =107, 599 CURRENT_CONNECTIONS =201, 600 INPROGRESS_CONNECTIONS =202, 601 CURRENT_EP_ERRORS =203, 602 } 603 604 enum NN_PIPE_RELEASE=1; 605 enum NN_PIPE_PARSED=2; 606 enum NN_PIPE_IN=33987; 607 enum NN_PIPE_OUT=33988; 608 609 enum NN_PIPEBASE 610 { 611 RELEASE=1, 612 PARSED=2, 613 } 614 615 align(1) struct nn_pipebase_vfptr 616 { 617 alias _send=extern(C) int function(nn_pipebase* self, nn_msg* msg); 618 alias _recv=extern(C) int function(nn_pipebase* self, nn_msg* msg); 619 _send send; 620 _recv recv; 621 } 622 623 624 align(1) struct nn_ep_options 625 { 626 int sndprio; 627 int rcvprio; 628 int ipv4only; 629 } 630 631 align(1) struct nn_pipebase 632 { 633 nn_fsm fsm; 634 const nn_pipebase_vfptr* vfptr; 635 ubyte state; 636 ubyte instate; 637 ubyte outstate; 638 nn_sock *sock; 639 void *data; 640 nn_fsm_event IN; 641 nn_fsm_event OUT; 642 nn_ep_options options; 643 } 644 645 extern(C) void nn_pipebase_init (nn_pipebase*,const(nn_pipebase_vfptr)* vfptr, nn_epbase* epbase); 646 extern(C) void nn_pipebase_term ( nn_pipebase*); 647 extern(C) int nn_pipebase_start ( nn_pipebase*); 648 extern(C) void nn_pipebase_stop ( nn_pipebase*); 649 extern(C) void nn_pipebase_received ( nn_pipebase*); 650 extern(C) void nn_pipebase_sent ( nn_pipebase*); 651 extern(C) void nn_pipebase_getopt ( nn_pipebase* , int level, int option,void *optval, size_t *optvallen); 652 extern(C) int nn_pipebase_ispeer ( nn_pipebase*, int socktype); 653 extern(C) void nn_pipe_setdata(nn_pipe *self, void *data); 654 extern(C) void *nn_pipe_getdata ( nn_pipe *self); 655 extern(C) int nn_pipe_send ( nn_pipe *self, nn_msg *msg); 656 extern(C) int nn_pipe_recv ( nn_pipe *self, nn_msg *msg); 657 extern(C) void nn_pipe_getopt (nn_pipe *self, int level, int option, void *optval, size_t *optvallen); 658 659 660 align(1) struct nn_transport 661 { 662 const(char*) name; 663 int id; 664 extern(C) void function() init; 665 extern(C) void function() term; 666 extern(C) int function(void *hint, nn_epbase **epbase) bind; 667 extern(C) int function(void *hint, nn_epbase **epbase) connect; 668 extern(C) nn_optset* function() optset; 669 nn_list_item item; 670 } 671 672 enum NN_FSM_INCLUDED=1; 673 674 align(1) struct nn_worker; 675 676 align(1) struct nn_fsm_event 677 { 678 nn_fsm* fsm; 679 int src; 680 void* srcptr; 681 int type; 682 nn_queue_item item; 683 } 684 685 686 extern(C) void nn_fsm_event_init(nn_fsm_event *self); 687 extern(C) void nn_fsm_event_term(nn_fsm_event *self); 688 extern(C) int nn_fsm_event_active(nn_fsm_event *self); 689 extern(C) void nn_fsm_event_process(nn_fsm_event *self); 690 691 692 /* Special source for actions. It's negative not to clash with user-defined 693 sources. */ 694 enum NN_FSM_ACTION =-2; 695 696 /* Actions generated by fsm object. The values are negative not to clash 697 with user-defined actions. */ 698 enum NN_FSM_START=-2; 699 enum NN_FSM_STOP =-3; 700 701 /* Virtual function to be implemented by the derived class to handle the 702 incoming events. */ 703 alias nn_fsm_fn = extern(C) void function(nn_fsm*, int, int, void*); 704 705 align(1) struct nn_fsm_owner 706 { 707 int src; 708 nn_fsm *fsm; 709 } 710 711 align(1) struct nn_fsm 712 { 713 nn_fsm_fn fn; 714 nn_fsm_fn shutdown_fn; 715 int state; 716 int src; 717 void *srcptr; 718 nn_fsm *owner; 719 nn_ctx *ctx; 720 nn_fsm_event stopped; 721 } 722 723 724 extern(C) void nn_fsm_init_root(nn_fsm *self, nn_fsm_fn fn,nn_fsm_fn shutdown_fn, nn_ctx* ctx); 725 extern(C) void nn_fsm_init(nn_fsm *, nn_fsm_fn fn,nn_fsm_fn shutdown_fn,int src, void *srcptr, nn_fsm* owner); 726 extern(C) void nn_fsm_term(nn_fsm *); 727 extern(C) int nn_fsm_isidle(nn_fsm *); 728 extern(C) void nn_fsm_start(nn_fsm *); 729 extern(C) void nn_fsm_stop(nn_fsm *); 730 extern(C) void nn_fsm_stopped(nn_fsm*, int type); 731 extern(C) void nn_fsm_stopped_noevent(nn_fsm*); 732 extern(C) void nn_fsm_swap_owner(nn_fsm*, nn_fsm_owner* owner); 733 extern(C) nn_worker* nn_fsm_choose_worker(nn_fsm*); 734 extern(C) void nn_fsm_action(nn_fsm*, int type); 735 extern(C) void nn_fsm_raise(nn_fsm*, nn_fsm_event* event, int type); 736 extern(C) void nn_fsm_raiseto(nn_fsm*, nn_fsm* dst, nn_fsm_event* event, int src, int type, void *srcptr); 737 extern(C) void nn_fsm_feed(nn_fsm*, int src, int type, void* srcptr); 738 739 740 enum NN_LIST_INCLUDED=1; 741 742 align(1) struct nn_list_item 743 { 744 nn_list_item* next; 745 nn_list_item* prev; 746 } 747 748 align(1) struct nn_list 749 { 750 nn_list_item* first; 751 nn_list_item* last; 752 }; 753 754 /* Undefined value for initializing a list item which is not part of a list. */ 755 enum NN_LIST_NOTINLIST = cast(const(nn_list_item)*)-1; 756 757 /* Use for initializing a list item statically. */ 758 immutable typeof(NN_LIST_NOTINLIST)[2] NN_LIST_ITEM_INITIALIZER=[NN_LIST_NOTINLIST, NN_LIST_NOTINLIST]; 759 760 761 /* Initialise the list. */ 762 763 extern(C) void nn_list_init(nn_list*); 764 extern(C) void nn_list_term(nn_list*); 765 extern(C) int nn_list_empty(nn_list*); 766 extern(C) nn_list_item* nn_list_begin(nn_list*); 767 extern(C) nn_list_item* nn_list_end(nn_list*); 768 extern(C) nn_list_item *nn_list_prev(nn_list*,nn_list_item*); 769 extern(C) nn_list_item *nn_list_next(nn_list*,nn_list_item*); 770 extern(C) void nn_list_insert(nn_list*, nn_list_item*, nn_list_item*); 771 extern(C) nn_list_item* nn_list_erase(nn_list*,nn_list_item*); 772 extern(C) void nn_list_item_init(nn_list_item*); 773 extern(C) void nn_list_item_term(nn_list_item*); 774 extern(C) int nn_list_item_isinlist(nn_list_item*); 775 776 777 enum NN_QUEUE_INCLUDED = 1; 778 779 /* Undefined value for initialising a queue item which is not 780 part of a queue. */ 781 const(nn_queue_item)* NN_QUEUE_NOTINQUEUE=cast(const(nn_queue_item)*) -1; 782 783 784 /+ 785 /* Use for initialising a queue item statically. */ 786 auto NN_QUEUE_ITEM_INITIALIZER() 787 { 788 return [NN_LIST_NOTINQUEUE]; 789 } 790 +/ 791 792 align(1) struct nn_queue_item 793 { 794 nn_queue_item *next; 795 } 796 797 align(1) struct nn_queue 798 { 799 nn_queue_item *head; 800 nn_queue_item *tail; 801 } 802 803 extern(C) void nn_queue_init(nn_queue*); 804 extern(C) void nn_queue_term(nn_queue*); 805 extern(C) int nn_queue_empty(nn_queue*); 806 extern(C) void nn_queue_push(nn_queue*, nn_queue_item*); 807 extern(C) nn_queue_item *nn_queue_pop( nn_queue*); 808 extern(C) void nn_queue_item_init(nn_queue_item*); 809 extern(C) void nn_queue_item_term(nn_queue_item*); 810 extern(C) int nn_queue_item_isinqueue(nn_queue_item*); 811 812 813 814 /* Returns the symbol name (e.g. "NN_REQ") and value at a specified index. */ 815 /* If the index is out-of-range, returns null and sets errno to EINVAL */ 816 /* General usage is to start at i=0 and iterate until null is returned. */ 817 extern(C) const(char)* nn_symbol (int i, int *value); 818 819 820 /* Associates opaque pointer to protocol-specific data with the pipe. */ 821 extern(C) void nn_pipe_setdata (nn_pipe *self, void *data); 822 823 /* Retrieves the opaque pointer associated with the pipe. */ 824 extern(C) void *nn_pipe_getdata (nn_pipe* self); 825 826 /* Send the message to the pipe. If successful, pipe takes ownership of the 827 messages. */ 828 extern(C) int nn_pipe_send (nn_pipe* self, nn_msg* msg); 829 830 /* Receive a message from a pipe. 'msg' should not be initialised prior to 831 the call. It will be initialised when the call succeeds. */ 832 extern(C) int nn_pipe_recv (nn_pipe* self, nn_msg* msg); 833 834 /* Get option for pipe. Mostly useful for endpoint-specific options */ 835 extern(C) void nn_pipe_getopt (nn_pipe* self, int level, int option, void* optval, size_t* optvallen); 836 837 838 /******************************************************************************/ 839 /* Base class for all socket types. */ 840 /******************************************************************************/ 841 842 /* Initialise the socket base class. 'hint' is the opaque value passed to the 843 nn_transport's 'create' function. */ 844 extern(C) void nn_sockbase_init (nn_sockbase* self, const(nn_sockbase_vfptr)* vfptr, void* hint); 845 846 /* Terminate the socket base class. */ 847 extern(C) void nn_sockbase_term (nn_sockbase* self); 848 849 /* Call this function when stopping is done. */ 850 extern(C) void nn_sockbase_stopped (nn_sockbase* self); 851 852 /* Returns the AIO context associated with the socket. This function is 853 useful when socket type implementation needs to create async objects, 854 such as timers. */ 855 extern(C) nn_ctx *nn_sockbase_getctx (nn_sockbase *self); 856 857 /* Retrieve a NN_SOL_SOCKET-level option. */ 858 extern(C) int nn_sockbase_getopt (nn_sockbase *self, int option, void *optval, size_t *optvallen); 859 860 /* Add some statistics for socket */ 861 extern(C) void nn_sockbase_stat_increment (nn_sockbase *self, int name, int increment); 862 863 /* Creates a new endpoint. 'hint' parameter is an opaque value that 864 was passed to transport's bind or connect function. */ 865 extern(C) void nn_epbase_init (nn_epbase* self, const(nn_epbase_vfptr)* vfptr, void *hint); 866 867 /* Notify the user that stopping is done. */ 868 extern(C) void nn_epbase_stopped (nn_epbase* self); 869 870 /* Terminate the epbase object. */ 871 extern(C) void nn_epbase_term (nn_epbase* self); 872 873 /* Returns the AIO context associated with the endpoint. */ 874 extern(C) nn_ctx *nn_epbase_getctx (nn_epbase* self); 875 876 /* Returns the address string associated with this endpoint. */ 877 extern(C) const(char*) nn_epbase_getaddr (nn_epbase* self); 878 879 /* Retrieve value of a socket option. */ 880 extern(C) void nn_epbase_getopt (nn_epbase* self, int level, int option, void *optval, size_t *optvallen); 881 882 /* Returns 1 is the specified socket type is a valid peer for this socket, 883 or 0 otherwise. */ 884 extern(C) int nn_epbase_ispeer (nn_epbase* self, int socktype); 885 886 /* Notifies a monitoring system the error on this endpoint */ 887 extern(C) void nn_epbase_set_error(nn_epbase* self, int errnum); 888 889 /* Notifies a monitoring system that error is gone */ 890 extern(C) void nn_epbase_clear_error(nn_epbase* self); 891 892 /* Increments statistics counters in the socket structure */ 893 extern(C) void nn_epbase_stat_increment(nn_epbase* self, int name, int increment); 894 895 896 enum NN_STAT_ESTABLISHED_CONNECTIONS =101; 897 enum NN_STAT_ACCEPTED_CONNECTIONS =102; 898 enum NN_STAT_DROPPED_CONNECTIONS =103; 899 enum NN_STAT_BROKEN_CONNECTIONS =104; 900 enum NN_STAT_CONNECT_ERRORS =105; 901 enum NN_STAT_BIND_ERRORS =106; 902 enum NN_STAT_ACCEPT_ERRORS =107; 903 904 enum NN_STAT_CURRENT_CONNECTIONS =201; 905 enum NN_STAT_INPROGRESS_CONNECTIONS =202; 906 enum NN_STAT_CURRENT_EP_ERRORS =203; 907 908 909 /******************************************************************************/ 910 /* The base class for pipes. */ 911 /******************************************************************************/ 912 913 /* Pipe represents one "connection", i.e. perfectly ordered uni- or 914 bi-directional stream of messages. One endpoint can create multiple pipes 915 (for example, bound TCP socket is an endpoint, individual accepted TCP 916 connections are represented by pipes. */ 917 918 919 /* This value is returned by pipe's send and recv functions to signalise that 920 more sends/recvs are not possible at the moment. From that moment on, 921 the core will stop invoking the function. To re-establish the message 922 flow nn_pipebase_received (respectively nn_pipebase_sent) should 923 be called. */ 924 enum NN_PIPEBASE_RELEASE=1; 925 926 /* Specifies that received message is already split into header and body. 927 This flag is used only by inproc transport to avoid merging and re-splitting 928 the messages passed with a single process. */ 929 enum NN_PIPEBASE_PARSED=2; 930 931 /* Initialise the pipe. */ 932 extern(C) void nn_pipebase_init (nn_pipebase *self, const(nn_pipebase_vfptr)* vfptr, nn_epbase *epbase); 933 934 /* Terminate the pipe. */ 935 extern(C) void nn_pipebase_term (nn_pipebase *self); 936 937 /* Call this function once the connection is established. */ 938 extern(C) int nn_pipebase_start (nn_pipebase *self); 939 940 /* Call this function once the connection is broken. */ 941 extern(C) void nn_pipebase_stop (nn_pipebase *self); 942 943 /* Call this function when new message was fully received. */ 944 extern(C) void nn_pipebase_received (nn_pipebase *self); 945 946 /* Call this function when current outgoing message was fully sent. */ 947 extern(C) void nn_pipebase_sent (nn_pipebase *self); 948 949 /* Retrieve value of a socket option. */ 950 extern(C) void nn_pipebase_getopt (nn_pipebase *self, int level, int option, void *optval, size_t *optvallen); 951 952 /* Returns 1 is the specified socket type is a valid peer for this socket, 953 or 0 otherwise. */ 954 extern(C) int nn_pipebase_ispeer (nn_pipebase *self, int socktype); 955 956 957 /* 958 Copyright (c) 2012-2014 250bpm s.r.o. All rights reserved. 959 Copyright (c) 2013 GoPivotal, Inc. All rights reserved. 960 961 Permission is hereby granted, free of charge, to any person obtaining a copy 962 of this software and associated documentation files (the "Software"), 963 to deal in the Software without restriction, including without limitation 964 the rights to use, copy, modify, merge, publish, distribute, sublicense, 965 and/or sell copies of the Software, and to permit persons to whom 966 the Software is furnished to do so, subject to the following conditions: 967 968 The above copyright notice and this permission notice shall be included 969 in all copies or substantial portions of the Software. 970 971 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 972 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 973 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 974 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 975 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 976 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 977 IN THE SOFTWARE. 978 */