PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/socket.rs

https://github.com/decentfox/zmq.rs
Rust | 204 lines | 10 code | 6 blank | 188 comment | 0 complexity | 4ea704627caf7fbb19fa8a0334ef096d MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. use consts;
  2. use msg::Msg;
  3. use result::ZmqResult;
  4. /// Interface to ØMQ socket objects.
  5. ///
  6. /// # Key differences to conventional sockets
  7. ///
  8. /// Generally speaking, conventional sockets present a *synchronous* interface to either
  9. /// connection-oriented reliable byte streams (`SOCK_STREAM`), or connection-less unreliable
  10. /// datagrams (`SOCK_DGRAM`). In comparison, ØMQ sockets present an abstraction of an asynchronous
  11. /// *message queue*, with the exact queueing semantics depending on the socket type in use. Where
  12. /// conventional sockets transfer streams of bytes or discrete datagrams, ØMQ sockets transfer
  13. /// discrete *messages*.
  14. ///
  15. /// ØMQ sockets being *asynchronous* means that the timings of the physical connection setup and
  16. /// tear down, reconnect and effective delivery are transparent to the user and organized by ØMQ
  17. /// itself. Further, messages may be *queued* in the event that a peer is unavailable to receive
  18. /// them.
  19. ///
  20. /// Conventional sockets allow only strict one-to-one (two peers), many-to-one (many clients, one
  21. /// server), or in some cases one-to-many (multicast) relationships. With the exception of
  22. /// *`zmq::PAIR`*, ØMQ sockets may be connected **to multiple endpoints** using *`connect()`*, while
  23. /// simultaneously accepting incoming connections **from multiple endpoints** bound to the socket
  24. /// using *`bind()`*, thus allowing many-to-many relationships.
  25. pub trait ZmqSocket {
  26. /// This function shall retrieve the value for the option specified by the *`option`* argument
  27. /// for this ØMQ socket object and return it.
  28. fn getsockopt(&self, option: consts::SocketOption) -> int;
  29. /// This function binds the *socket* to a local endpoint and then accepts incoming connections
  30. /// on that endpoint.
  31. ///
  32. /// The *`endpoint`* is a string consisting of a *`transport://`* followed by an *`address`*.
  33. /// The *`transport`* specifies the underlying protocol to use. The *`address`* specifies the
  34. /// transport-specific address to bind to.
  35. ///
  36. /// ØMQ provides the the following transports:
  37. ///
  38. /// > ***`tcp`***
  39. /// >> unicast transport using TCP
  40. ///
  41. /// > ***`inproc`***
  42. /// >> local in-process (inter-task) communication transport
  43. ///
  44. /// Every ØMQ socket type except *`zmq::PAIR`* supports one-to-many and many-to-one semantics.
  45. ///
  46. /// # Errors
  47. ///
  48. /// > **`EINVAL`**
  49. /// >> The endpoint supplied is invalid.
  50. ///
  51. /// > **`EPROTONOSUPPORT`**
  52. /// >> The requested transport protocol is not supported.
  53. ///
  54. /// > **`ENOCOMPATPROTO`**
  55. /// >> The requested transport protocol is not compatible with the socket type.
  56. ///
  57. /// > **`EADDRINUSE`**
  58. /// >> The requested address is already in use.
  59. ///
  60. /// > **`EADDRNOTAVAIL`**
  61. /// >> The requested address was not local.
  62. ///
  63. /// > **`ENODEV`**
  64. /// >> The requested address specifies a nonexistent interface.
  65. ///
  66. /// > **`ETERM`**
  67. /// >> The ØMQ context associated with the specified socket was terminated.
  68. ///
  69. /// > **`ENOTSOCK`**
  70. /// >> The provided socket was invalid.
  71. ///
  72. /// # Example
  73. ///
  74. /// **Binding a publisher socket to an in-process and a tcp transport**
  75. ///
  76. /// ```rust
  77. /// // Create a zmq::PUB socket
  78. /// let socket = ctx.socket(zmq::PUB);
  79. /// // Bind it to a in-process transport with the address 'my_publisher'
  80. /// socket.bind("inproc://my_publisher");
  81. /// // Bind it to a TCP transport on port 5555 of the 'eth0' interface
  82. /// socket.bind("tcp://eth0:5555");
  83. /// ```
  84. fn bind(&self, endpoint: &str) -> ZmqResult<()>;
  85. /// This function connects the *socket* to an *endpoint* and then accepts incoming connections
  86. /// on that endpoint.
  87. ///
  88. /// The *endpoint* is a string consisting of a *`transport://`* followed by an *`address`*. The
  89. /// *`transport`* specifies the underlying protocol to use. The *`address`* specifies the
  90. /// transport-specific address to connect to.
  91. ///
  92. /// ØMQ provides the the following transports:
  93. ///
  94. /// > ***`tcp`***
  95. /// >> unicast transport using TCP
  96. ///
  97. /// > ***`inproc`***
  98. /// >> local in-process (inter-task) communication transport
  99. ///
  100. /// Every ØMQ socket type except *`zmq::PAIR`* supports one-to-many and many-to-one semantics.
  101. ///
  102. /// > for most transports and socket types the connection is not performed immediately but as
  103. /// > needed by ØMQ. Thus a successful call to *`connect()`* does not mean that the connection
  104. /// > was or could actually be established. Because of this, for most transports and socket
  105. /// > types the order in which a *server* socket is bound and a *client* socket is connected to
  106. /// > it does not matter. The first exception is when using the `inproc://` transport: you must
  107. /// > call *`bind()`* before calling *`connect()`*. The second exception are *`zmq::PAIR`*
  108. /// > sockets, which do not automatically reconnect to endpoints.
  109. ///
  110. /// # Errors
  111. ///
  112. /// > **`EINVAL`**
  113. /// >> The endpoint supplied is invalid.
  114. ///
  115. /// > **`EPROTONOSUPPORT`**
  116. /// >> The requested transport protocol is not supported.
  117. ///
  118. /// > **`ENOCOMPATPROTO`**
  119. /// >> The requested transport protocol is not compatible with the socket type.
  120. ///
  121. /// > **`ETERM`**
  122. /// >> The ØMQ context associated with the specified socket was terminated.
  123. ///
  124. /// > **`ENOTSOCK`**
  125. /// >> The provided socket was invalid.
  126. ///
  127. /// # Example
  128. ///
  129. /// **Connecting a subscriber socket to an in-process and a tcp transport**
  130. ///
  131. /// ```rust
  132. /// // Create a ZMQ_SUB socket
  133. /// let socket = ctx.socket(zmq::SUB);
  134. /// // Connect it to an in-process transport with the address 'my_publisher'
  135. /// socket.connect("inproc://my_publisher");
  136. /// // Connect it to the host server001, port 5555 using a TCP transport
  137. /// socket.connect("tcp://server001:5555");
  138. /// ```
  139. fn connect(&self, endpoint: &str) -> ZmqResult<()>;
  140. /// This function shall receive a message part from this socket and return it. If there are no
  141. /// message parts available on this socket the *`msg_recv()`* function shall block until the
  142. /// request can be satisfied.
  143. ///
  144. /// # Multi-part messages
  145. ///
  146. /// A ØMQ message is composed of 1 or more message parts. Each message part is an independent
  147. /// *`zmq::Msg`* in its own right. ØMQ ensures atomic delivery of messages: peers shall receive
  148. /// either all *message parts* of a message or none at all. The total number of message parts is
  149. /// unlimited except by available memory.
  150. ///
  151. /// # Errors
  152. ///
  153. /// > **`EAGAIN`**
  154. /// >> Non-blocking mode was requested and no messages are available at the moment.
  155. ///
  156. /// > **`ENOTSUP`**
  157. /// >> The *`msg_recv()`* operation is not supported by this socket type.
  158. ///
  159. /// > **`EFSM`**
  160. /// >> The *`msg_recv()`* operation cannot be performed on this socket at the moment due to the
  161. /// >> socket not being in the appropriate state. This error may occur with socket types that
  162. /// >> switch between several states, such as `zmq::REP`.
  163. ///
  164. /// > **`ETERM`**
  165. /// >> The ØMQ context associated with the specified socket was terminated.
  166. ///
  167. /// > **`ENOTSOCK`**
  168. /// >> The provided *socket* was invalid.
  169. ///
  170. /// > **`EINTR`**
  171. /// >> The operation was interrupted by delivery of a signal before a message was available.
  172. ///
  173. /// > **`EFAULT`**
  174. /// >> The message passed to the function was invalid.
  175. ///
  176. /// # Example
  177. ///
  178. /// **Receiving a message from a socket**
  179. ///
  180. /// ```rust
  181. /// // Block until a message is available to be received from socket
  182. /// let msg = socket.msg_recv();
  183. /// ```
  184. ///
  185. /// **Receiving a multi-part message**
  186. ///
  187. /// ```rust
  188. /// let mut more = true;
  189. /// while more {
  190. /// // Block until a message is available to be received from socket
  191. /// let msg = socket.msg_recv();
  192. /// // Determine if more message parts are to follow
  193. /// more = msg & 1 == 1;
  194. /// }
  195. /// ```
  196. fn msg_recv(&mut self) -> ZmqResult<Box<Msg>>;
  197. fn msg_send(&mut self, msg: Box<Msg>) -> ZmqResult<()>;
  198. }