rep.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "precompiled.hpp"
4 #include "rep.hpp"
5 #include "err.hpp"
6 #include "msg.hpp"
7 
8 zmq::rep_t::rep_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
9  router_t (parent_, tid_, sid_),
10  _sending_reply (false),
11  _request_begins (true)
12 {
13  options.type = ZMQ_REP;
14 }
15 
16 zmq::rep_t::~rep_t ()
17 {
18 }
19 
20 int zmq::rep_t::xsend (msg_t *msg_)
21 {
22  // If we are in the middle of receiving a request, we cannot send reply.
23  if (!_sending_reply) {
24  errno = EFSM;
25  return -1;
26  }
27 
28  const bool more = (msg_->flags () & msg_t::more) != 0;
29 
30  // Push message to the reply pipe.
31  const int rc = router_t::xsend (msg_);
32  if (rc != 0)
33  return rc;
34 
35  // If the reply is complete flip the FSM back to request receiving state.
36  if (!more)
37  _sending_reply = false;
38 
39  return 0;
40 }
41 
42 int zmq::rep_t::xrecv (msg_t *msg_)
43 {
44  // If we are in middle of sending a reply, we cannot receive next request.
45  if (_sending_reply) {
46  errno = EFSM;
47  return -1;
48  }
49 
50  // First thing to do when receiving a request is to copy all the labels
51  // to the reply pipe.
52  if (_request_begins) {
53  while (true) {
54  int rc = router_t::xrecv (msg_);
55  if (rc != 0)
56  return rc;
57 
58  if ((msg_->flags () & msg_t::more)) {
59  // Empty message part delimits the traceback stack.
60  const bool bottom = (msg_->size () == 0);
61 
62  // Push it to the reply pipe.
63  rc = router_t::xsend (msg_);
64  errno_assert (rc == 0);
65 
66  if (bottom)
67  break;
68  } else {
69  // If the traceback stack is malformed, discard anything
70  // already sent to pipe (we're at end of invalid message).
71  rc = router_t::rollback ();
72  errno_assert (rc == 0);
73  }
74  }
75  _request_begins = false;
76  }
77 
78  // Get next message part to return to the user.
79  const int rc = router_t::xrecv (msg_);
80  if (rc != 0)
81  return rc;
82 
83  // If whole request is read, flip the FSM to reply-sending state.
84  if (!(msg_->flags () & msg_t::more)) {
85  _sending_reply = true;
86  _request_begins = true;
87  }
88 
89  return 0;
90 }
91 
92 bool zmq::rep_t::xhas_in ()
93 {
94  if (_sending_reply)
95  return false;
96 
97  return router_t::xhas_in ();
98 }
99 
100 bool zmq::rep_t::xhas_out ()
101 {
102  if (!_sending_reply)
103  return false;
104 
105  return router_t::xhas_out ();
106 }
rep.hpp
EFSM
#define EFSM
Definition: zmq.h:159
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
precompiled.hpp
errno
int errno
errno_assert
#define errno_assert(x)
Definition: err.hpp:113
ZMQ_REP
#define ZMQ_REP
Definition: zmq.h:262
msg.hpp
err.hpp
true
#define true
Definition: cJSON.c:65
false
#define false
Definition: cJSON.c:70
bottom
GLint GLint bottom
Definition: glcorearb.h:4150


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58