protocol.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright (c) 2012-2013 Martin Sustrik All rights reserved.
  3. Copyright 2016 Garrett D'Amore <garrett@damore.org>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"),
  6. to deal in the Software without restriction, including without limitation
  7. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom
  9. the Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  18. IN THE SOFTWARE.
  19. */
  20. #ifndef NN_PROTOCOL_INCLUDED
  21. #define NN_PROTOCOL_INCLUDED
  22. #include "utils/msg.h"
  23. #include "utils/list.h"
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. struct nn_ctx;
  27. /******************************************************************************/
  28. /* Pipe class. */
  29. /******************************************************************************/
  30. /* Any combination of following flags can be returned from successful call
  31. to nn_pipe_send or nn_pipe_recv. */
  32. /* This flag means that the pipe can't be used for receiving (when returned
  33. from nn_pipe_recv()) or sending (when returned from nn_pipe_send()).
  34. Protocol implementation should not send/recv messages from the pipe until
  35. the pipe is revived by in()/out() function. */
  36. #define NN_PIPE_RELEASE 1
  37. /* Specifies that received message is already split into header and body.
  38. This flag is used only by inproc transport to avoid merging and re-splitting
  39. the messages passed with a single process. */
  40. #define NN_PIPE_PARSED 2
  41. /* Events generated by the pipe. */
  42. #define NN_PIPE_IN 33987
  43. #define NN_PIPE_OUT 33988
  44. struct nn_pipe;
  45. /* Associates opaque pointer to protocol-specific data with the pipe. */
  46. void nn_pipe_setdata (struct nn_pipe *self, void *data);
  47. /* Retrieves the opaque pointer associated with the pipe. */
  48. void *nn_pipe_getdata (struct nn_pipe *self);
  49. /* Send the message to the pipe. If successful, pipe takes ownership of the
  50. messages. */
  51. int nn_pipe_send (struct nn_pipe *self, struct nn_msg *msg);
  52. /* Receive a message from a pipe. 'msg' should not be initialised prior to
  53. the call. It will be initialised when the call succeeds. */
  54. int nn_pipe_recv (struct nn_pipe *self, struct nn_msg *msg);
  55. /* Get option for pipe. Mostly useful for endpoint-specific options */
  56. void nn_pipe_getopt (struct nn_pipe *self, int level, int option,
  57. void *optval, size_t *optvallen);
  58. /******************************************************************************/
  59. /* Base class for all socket types. */
  60. /******************************************************************************/
  61. struct nn_sockbase;
  62. /* Any combination of these events can be returned from 'events' virtual
  63. function. */
  64. #define NN_SOCKBASE_EVENT_IN 1
  65. #define NN_SOCKBASE_EVENT_OUT 2
  66. /* To be implemented by individual socket types. */
  67. struct nn_sockbase_vfptr {
  68. /* Ask socket to stop. */
  69. void (*stop) (struct nn_sockbase *self);
  70. /* Deallocate the socket. */
  71. void (*destroy) (struct nn_sockbase *self);
  72. /* Management of pipes. 'add' registers a new pipe. The pipe cannot be used
  73. to send to or to be received from at the moment. 'rm' unregisters the
  74. pipe. The pipe should not be used after this call as it may already be
  75. deallocated. 'in' informs the socket that pipe is readable. 'out'
  76. informs it that it is writable. */
  77. int (*add) (struct nn_sockbase *self, struct nn_pipe *pipe);
  78. void (*rm) (struct nn_sockbase *self, struct nn_pipe *pipe);
  79. void (*in) (struct nn_sockbase *self, struct nn_pipe *pipe);
  80. void (*out) (struct nn_sockbase *self, struct nn_pipe *pipe);
  81. /* Return any combination of event flags defined above, thus specifying
  82. whether the socket should be readable, writable, both or none. */
  83. int (*events) (struct nn_sockbase *self);
  84. /* Send a message to the socket. Returns -EAGAIN if it cannot be done at
  85. the moment or zero in case of success. */
  86. int (*send) (struct nn_sockbase *self, struct nn_msg *msg);
  87. /* Receive a message from the socket. Returns -EAGAIN if it cannot be done
  88. at the moment or zero in case of success. */
  89. int (*recv) (struct nn_sockbase *self, struct nn_msg *msg);
  90. /* Set a protocol specific option. */
  91. int (*setopt) (struct nn_sockbase *self, int level, int option,
  92. const void *optval, size_t optvallen);
  93. /* Retrieve a protocol specific option. */
  94. int (*getopt) (struct nn_sockbase *self, int level, int option,
  95. void *optval, size_t *optvallen);
  96. };
  97. struct nn_sockbase {
  98. const struct nn_sockbase_vfptr *vfptr;
  99. struct nn_sock *sock;
  100. };
  101. /* Initialise the socket base class. 'hint' is the opaque value passed to the
  102. nn_transport's 'create' function. */
  103. void nn_sockbase_init (struct nn_sockbase *self,
  104. const struct nn_sockbase_vfptr *vfptr, void *hint);
  105. /* Terminate the socket base class. */
  106. void nn_sockbase_term (struct nn_sockbase *self);
  107. /* Call this function when stopping is done. */
  108. void nn_sockbase_stopped (struct nn_sockbase *self);
  109. /* Returns the AIO context associated with the socket. This function is
  110. useful when socket type implementation needs to create async objects,
  111. such as timers. */
  112. struct nn_ctx *nn_sockbase_getctx (struct nn_sockbase *self);
  113. /* Retrieve a NN_SOL_SOCKET-level option. */
  114. int nn_sockbase_getopt (struct nn_sockbase *self, int option,
  115. void *optval, size_t *optvallen);
  116. /* Add some statistics for socket */
  117. void nn_sockbase_stat_increment (struct nn_sockbase *self, int name,
  118. int increment);
  119. /******************************************************************************/
  120. /* The socktype class. */
  121. /******************************************************************************/
  122. /* This structure defines a class factory for individual socket types. */
  123. /* Specifies that the socket type can be never used to receive messages. */
  124. #define NN_SOCKTYPE_FLAG_NORECV 1
  125. /* Specifies that the socket type can be never used to send messages. */
  126. #define NN_SOCKTYPE_FLAG_NOSEND 2
  127. struct nn_socktype {
  128. /* Domain and protocol IDs as specified in nn_socket() function. */
  129. int domain;
  130. int protocol;
  131. /* Any combination of the flags defined above. */
  132. int flags;
  133. /* Function to create specific socket type. 'sockbase' is the output
  134. parameter to return reference to newly created socket. This function
  135. is called under global lock, so it is not possible that two sockets are
  136. being created in parallel. */
  137. int (*create) (void *hint, struct nn_sockbase **sockbase);
  138. /* Returns 1 if the supplied socket type is a valid peer for this socket,
  139. 0 otherwise. Note that the validation is done only within a single
  140. SP protocol. Peers speaking other SP protocols are discarded by the
  141. core and socket is not even asked to validate them. */
  142. int (*ispeer) (int socktype);
  143. /* This member is owned by the core. Never touch it directly from inside
  144. the protocol implementation. */
  145. struct nn_list_item item;
  146. };
  147. #endif