nn_recv.adoc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. nn_recv(3)
  2. ==========
  3. NAME
  4. ----
  5. nn_recv - receive a message
  6. SYNOPSIS
  7. --------
  8. *#include <nanomsg/nn.h>*
  9. *int nn_recv (int 's', void '*buf', size_t 'len', int 'flags');*
  10. DESCRIPTION
  11. -----------
  12. Receive a message from the socket 's' and store it in the buffer referenced by
  13. the 'buf' argument. Any bytes exceeding the length specified by the 'len'
  14. argument will be truncated.
  15. Alternatively, _nanomsg_ can allocate the buffer for you. To do so,
  16. let the 'buf' parameter be a pointer to a void* variable (pointer to pointer)
  17. to the receive buffer and set the 'len' parameter to _NN_MSG_. If the call is
  18. successful the user is responsible for deallocating the message using
  19. the <<nn_freemsg#,nn_freemsg(3)>> function.
  20. The 'flags' argument is a combination of the flags defined below:
  21. *NN_DONTWAIT*::
  22. Specifies that the operation should be performed in non-blocking mode. If the
  23. message cannot be received straight away, the function will fail with 'errno'
  24. set to EAGAIN.
  25. RETURN VALUE
  26. ------------
  27. If the function succeeds number of bytes in the message is returned. Otherwise,
  28. -1 is returned and 'errno' is set to to one of the values defined
  29. below.
  30. ERRORS
  31. ------
  32. *EBADF*::
  33. The provided socket is invalid.
  34. *ENOTSUP*::
  35. The operation is not supported by this socket type.
  36. *EFSM*::
  37. The operation cannot be performed on this socket at the moment because socket is
  38. not in the appropriate state. This error may occur with socket types that
  39. switch between several states.
  40. *EAGAIN*::
  41. Non-blocking mode was requested and there's no message to receive at the moment.
  42. *EINTR*::
  43. The operation was interrupted by delivery of a signal before the message was
  44. received.
  45. *ETIMEDOUT*::
  46. Individual socket types may define their own specific timeouts. If such timeout
  47. is hit this error will be returned.
  48. *ETERM*::
  49. The library is terminating.
  50. EXAMPLES
  51. --------
  52. Receiving a message into a buffer allocated by the user::
  53. This example code will retrieve a message of either 100 bytes or less. If a
  54. larger message was sent it will be truncated to 100 bytes.
  55. ----
  56. char buf [100];
  57. nbytes = nn_recv (s, buf, sizeof (buf), 0);
  58. ----
  59. Receiving a message into a buffer allocated by _nanomsg_::
  60. The following will get a message from the pipe with a buffer allocated by
  61. the system. It is large enough to accommodate the entire message. This is a good
  62. way to get the entire message without truncating if the size of the message is
  63. unknown. It is the user's responsibility to call <<nn_freemsg#,nn_freemsg(3)>> after
  64. processing the message.
  65. ----
  66. void *buf = NULL;
  67. nbytes = nn_recv (s, &buf, NN_MSG, 0);
  68. if (nbytes < 0) {
  69. /* handle error */
  70. ...
  71. }
  72. else {
  73. /* process message */
  74. ...
  75. nn_freemsg (buf);
  76. }
  77. ----
  78. Note that this can be more efficient than manually allocating a buffer since
  79. it is a zero-copy operation.
  80. SEE ALSO
  81. --------
  82. <<nn_send#,nn_send(3)>>
  83. <<nn_recvmsg#,nn_recvmsg(3)>>
  84. <<nn_socket#,nn_socket(3)>>
  85. <<nanomsg#,nanomsg(7)>>
  86. AUTHORS
  87. -------
  88. link:mailto:sustrik@250bpm.com[Martin Sustrik]