req.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_REQ_HPP_INCLUDED__
  25. #define __ZMQ_REQ_HPP_INCLUDED__
  26. #include "dealer.hpp"
  27. #include "stdint.hpp"
  28. namespace zmq
  29. {
  30. class ctx_t;
  31. class msg_t;
  32. class io_thread_t;
  33. class socket_base_t;
  34. class req_t ZMQ_FINAL : public dealer_t
  35. {
  36. public:
  37. req_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_);
  38. ~req_t ();
  39. // Overrides of functions from socket_base_t.
  40. int xsend (zmq::msg_t *msg_);
  41. int xrecv (zmq::msg_t *msg_);
  42. bool xhas_in ();
  43. bool xhas_out ();
  44. int xsetsockopt (int option_, const void *optval_, size_t optvallen_);
  45. void xpipe_terminated (zmq::pipe_t *pipe_);
  46. protected:
  47. // Receive only from the pipe the request was sent to, discarding
  48. // frames from other pipes.
  49. int recv_reply_pipe (zmq::msg_t *msg_);
  50. private:
  51. // If true, request was already sent and reply wasn't received yet or
  52. // was received partially.
  53. bool _receiving_reply;
  54. // If true, we are starting to send/recv a message. The first part
  55. // of the message must be empty message part (backtrace stack bottom).
  56. bool _message_begins;
  57. // The pipe the request was sent to and where the reply is expected.
  58. zmq::pipe_t *_reply_pipe;
  59. // Whether request id frames shall be sent and expected.
  60. bool _request_id_frames_enabled;
  61. // The current request id. It is incremented every time before a new
  62. // request is sent.
  63. uint32_t _request_id;
  64. // If false, send() will reset its internal state and terminate the
  65. // reply_pipe's connection instead of failing if a previous request is
  66. // still pending.
  67. bool _strict;
  68. ZMQ_NON_COPYABLE_NOR_MOVABLE (req_t)
  69. };
  70. class req_session_t ZMQ_FINAL : public session_base_t
  71. {
  72. public:
  73. req_session_t (zmq::io_thread_t *io_thread_,
  74. bool connect_,
  75. zmq::socket_base_t *socket_,
  76. const options_t &options_,
  77. address_t *addr_);
  78. ~req_session_t ();
  79. // Overrides of the functions from session_base_t.
  80. int push_msg (msg_t *msg_);
  81. void reset ();
  82. private:
  83. enum
  84. {
  85. bottom,
  86. request_id,
  87. body
  88. } _state;
  89. ZMQ_NON_COPYABLE_NOR_MOVABLE (req_session_t)
  90. };
  91. }
  92. #endif