libzmq
master
ZeroMQ C++ Core Engine (LIBZMQ)
Main Page
Classes
Files
File List
File Members
rep.cpp
Go to the documentation of this file.
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
#include "
precompiled.hpp
"
31
#include "
rep.hpp
"
32
#include "
err.hpp
"
33
#include "
msg.hpp
"
34
35
zmq::rep_t::rep_t
(
class
ctx_t
*parent_, uint32_t tid_,
int
sid_) :
36
router_t
(parent_, tid_, sid_),
37
sending_reply (false),
38
request_begins (true)
39
{
40
options
.
type
=
ZMQ_REP
;
41
}
42
43
zmq::rep_t::~rep_t
()
44
{
45
}
46
47
int
zmq::rep_t::xsend
(
msg_t
*msg_)
48
{
49
// If we are in the middle of receiving a request, we cannot send reply.
50
if
(!
sending_reply
) {
51
errno =
EFSM
;
52
return
-1;
53
}
54
55
bool
more = msg_->
flags
() &
msg_t::more
?
true
:
false
;
56
57
// Push message to the reply pipe.
58
int
rc =
router_t::xsend
(msg_);
59
if
(rc != 0)
60
return
rc;
61
62
// If the reply is complete flip the FSM back to request receiving state.
63
if
(!more)
64
sending_reply
=
false
;
65
66
return
0;
67
}
68
69
int
zmq::rep_t::xrecv
(
msg_t
*msg_)
70
{
71
// If we are in middle of sending a reply, we cannot receive next request.
72
if
(
sending_reply
) {
73
errno =
EFSM
;
74
return
-1;
75
}
76
77
// First thing to do when receiving a request is to copy all the labels
78
// to the reply pipe.
79
if
(
request_begins
) {
80
while
(
true
) {
81
int
rc =
router_t::xrecv
(msg_);
82
if
(rc != 0)
83
return
rc;
84
85
if
((msg_->
flags
() &
msg_t::more
)) {
86
// Empty message part delimits the traceback stack.
87
bool
bottom = (msg_->
size
() == 0);
88
89
// Push it to the reply pipe.
90
rc =
router_t::xsend
(msg_);
91
errno_assert
(rc == 0);
92
93
if
(bottom)
94
break
;
95
}
96
else
{
97
// If the traceback stack is malformed, discard anything
98
// already sent to pipe (we're at end of invalid message).
99
rc =
router_t::rollback
();
100
errno_assert
(rc == 0);
101
}
102
}
103
request_begins
=
false
;
104
}
105
106
// Get next message part to return to the user.
107
int
rc =
router_t::xrecv
(msg_);
108
if
(rc != 0)
109
return
rc;
110
111
// If whole request is read, flip the FSM to reply-sending state.
112
if
(!(msg_->
flags
() &
msg_t::more
)) {
113
sending_reply
=
true
;
114
request_begins
=
true
;
115
}
116
117
return
0;
118
}
119
120
bool
zmq::rep_t::xhas_in
()
121
{
122
if
(
sending_reply
)
123
return
false
;
124
125
return
router_t::xhas_in
();
126
}
127
128
bool
zmq::rep_t::xhas_out
()
129
{
130
if
(!
sending_reply
)
131
return
false
;
132
133
return
router_t::xhas_out
();
134
}
EFSM
#define EFSM
Definition:
zmq.h:167
zmq::rep_t::rep_t
rep_t(zmq::ctx_t *parent_, uint32_t tid_, int sid)
Definition:
rep.cpp:35
zmq::ctx_t
Definition:
ctx.hpp:68
zmq::msg_t
Definition:
msg.hpp:55
zmq::router_t::xhas_in
bool xhas_in()
Definition:
router.cpp:371
ZMQ_REP
#define ZMQ_REP
Definition:
zmq.h:250
rep.hpp
zmq::rep_t::~rep_t
~rep_t()
Definition:
rep.cpp:43
zmq::options_t::type
int type
Definition:
options.hpp:97
zmq::router_t::xsend
int xsend(zmq::msg_t *msg_)
Definition:
router.cpp:193
zmq::router_t::xhas_out
bool xhas_out()
Definition:
router.cpp:412
zmq::msg_t::size
unsigned char size
Definition:
msg.hpp:188
zmq::rep_t::sending_reply
bool sending_reply
Definition:
rep.hpp:60
zmq::router_t::xrecv
int xrecv(zmq::msg_t *msg_)
Definition:
router.cpp:288
zmq::router_t::rollback
int rollback()
Definition:
router.cpp:361
precompiled.hpp
zmq::msg_t::more
Definition:
msg.hpp:78
zmq::router_t
Definition:
router.hpp:49
msg.hpp
errno_assert
#define errno_assert(x)
Definition:
err.hpp:129
zmq::rep_t::xsend
int xsend(zmq::msg_t *msg_)
Definition:
rep.cpp:47
err.hpp
zmq::rep_t::xhas_in
bool xhas_in()
Definition:
rep.cpp:120
zmq::own_t::options
options_t options
Definition:
own.hpp:109
zmq::rep_t::xrecv
int xrecv(zmq::msg_t *msg_)
Definition:
rep.cpp:69
zmq::rep_t::request_begins
bool request_begins
Definition:
rep.hpp:64
zmq::msg_t::flags
unsigned char flags
Definition:
msg.hpp:181
zmq::rep_t::xhas_out
bool xhas_out()
Definition:
rep.cpp:128
zeromq
libzmq
src
rep.cpp
Generated by
1.8.11