| 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) != 0; |
| 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 | } else { |
| 96 | // If the traceback stack is malformed, discard anything |
| 97 | // already sent to pipe (we're at end of invalid message). |
| 98 | rc = router_t::rollback (); |
| 99 | errno_assert (rc == 0); |
| 100 | } |
| 101 | } |
| 102 | _request_begins = false; |
| 103 | } |
| 104 | |
| 105 | // Get next message part to return to the user. |
| 106 | int rc = router_t::xrecv (msg_); |
| 107 | if (rc != 0) |
| 108 | return rc; |
| 109 | |
| 110 | // If whole request is read, flip the FSM to reply-sending state. |
| 111 | if (!(msg_->flags () & msg_t::more)) { |
| 112 | _sending_reply = true; |
| 113 | _request_begins = true; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | bool zmq::rep_t::xhas_in () |
| 120 | { |
| 121 | if (_sending_reply) |
| 122 | return false; |
| 123 | |
| 124 | return router_t::xhas_in (); |
| 125 | } |
| 126 | |
| 127 | bool zmq::rep_t::xhas_out () |
| 128 | { |
| 129 | if (!_sending_reply) |
| 130 | return false; |
| 131 | |
| 132 | return router_t::xhas_out (); |
| 133 | } |
| 134 | |