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