dgram.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "precompiled.hpp"
4 #include "macros.hpp"
5 #include "dgram.hpp"
6 #include "pipe.hpp"
7 #include "wire.hpp"
8 #include "random.hpp"
9 #include "likely.hpp"
10 #include "err.hpp"
11 
12 zmq::dgram_t::dgram_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
13  socket_base_t (parent_, tid_, sid_), _pipe (NULL), _more_out (false)
14 {
15  options.type = ZMQ_DGRAM;
16  options.raw_socket = true;
17 }
18 
19 zmq::dgram_t::~dgram_t ()
20 {
21  zmq_assert (!_pipe);
22 }
23 
24 void zmq::dgram_t::xattach_pipe (pipe_t *pipe_,
25  bool subscribe_to_all_,
26  bool locally_initiated_)
27 {
28  LIBZMQ_UNUSED (subscribe_to_all_);
29  LIBZMQ_UNUSED (locally_initiated_);
30 
31  zmq_assert (pipe_);
32 
33  // ZMQ_DGRAM socket can only be connected to a single peer.
34  // The socket rejects any further connection requests.
35  if (_pipe == NULL)
36  _pipe = pipe_;
37  else
38  pipe_->terminate (false);
39 }
40 
41 void zmq::dgram_t::xpipe_terminated (pipe_t *pipe_)
42 {
43  if (pipe_ == _pipe) {
44  _pipe = NULL;
45  }
46 }
47 
48 void zmq::dgram_t::xread_activated (pipe_t *)
49 {
50  // There's just one pipe. No lists of active and inactive pipes.
51  // There's nothing to do here.
52 }
53 
54 void zmq::dgram_t::xwrite_activated (pipe_t *)
55 {
56  // There's just one pipe. No lists of active and inactive pipes.
57  // There's nothing to do here.
58 }
59 
60 int zmq::dgram_t::xsend (msg_t *msg_)
61 {
62  // If there's no out pipe, just drop it.
63  if (!_pipe) {
64  const int rc = msg_->close ();
65  errno_assert (rc == 0);
66  return -1;
67  }
68 
69  // If this is the first part of the message it's the ID of the
70  // peer to send the message to.
71  if (!_more_out) {
72  if (!(msg_->flags () & msg_t::more)) {
73  errno = EINVAL;
74  return -1;
75  }
76  } else {
77  // dgram messages are two part only, reject part if more is set
78  if (msg_->flags () & msg_t::more) {
79  errno = EINVAL;
80  return -1;
81  }
82  }
83 
84  // Push the message into the pipe.
85  if (!_pipe->write (msg_)) {
86  errno = EAGAIN;
87  return -1;
88  }
89 
90  if (!(msg_->flags () & msg_t::more))
91  _pipe->flush ();
92 
93  // flip the more flag
94  _more_out = !_more_out;
95 
96  // Detach the message from the data buffer.
97  const int rc = msg_->init ();
98  errno_assert (rc == 0);
99 
100  return 0;
101 }
102 
103 int zmq::dgram_t::xrecv (msg_t *msg_)
104 {
105  // Deallocate old content of the message.
106  int rc = msg_->close ();
107  errno_assert (rc == 0);
108 
109  if (!_pipe || !_pipe->read (msg_)) {
110  // Initialise the output parameter to be a 0-byte message.
111  rc = msg_->init ();
112  errno_assert (rc == 0);
113 
114  errno = EAGAIN;
115  return -1;
116  }
117 
118  return 0;
119 }
120 
121 bool zmq::dgram_t::xhas_in ()
122 {
123  if (!_pipe)
124  return false;
125 
126  return _pipe->check_read ();
127 }
128 
129 bool zmq::dgram_t::xhas_out ()
130 {
131  if (!_pipe)
132  return false;
133 
134  return _pipe->check_write ();
135 }
NULL
NULL
Definition: test_security_zap.cpp:405
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
EINVAL
#define EINVAL
Definition: errno.hpp:25
EAGAIN
#define EAGAIN
Definition: errno.hpp:14
precompiled.hpp
zmq_assert
#define zmq_assert(x)
Definition: err.hpp:102
random.hpp
errno
int errno
wire.hpp
errno_assert
#define errno_assert(x)
Definition: err.hpp:113
macros.hpp
LIBZMQ_UNUSED
#define LIBZMQ_UNUSED(object)
Definition: macros.hpp:6
pipe.hpp
dgram.hpp
ZMQ_DGRAM
#define ZMQ_DGRAM
Definition: zmq_draft.h:20
err.hpp
likely.hpp
false
#define false
Definition: cJSON.c:70


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