YARP
Yet Another Robot Platform
DgramTwoWayStream.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_OS_IMPL_DGRAMTWOWAYSTREAM_H
8 #define YARP_OS_IMPL_DGRAMTWOWAYSTREAM_H
9 
10 #include <yarp/os/ManagedBytes.h>
11 #include <yarp/os/TwoWayStream.h>
12 
13 #include <cstdlib>
14 #include <mutex>
15 
16 #ifdef YARP_HAS_ACE
17 # include <ace/SOCK_Dgram.h>
18 # include <ace/SOCK_Dgram_Mcast.h>
19 // In one the ACE headers there is a definition of "main" for WIN32
20 # ifdef main
21 # undef main
22 # endif
23 #endif
24 
25 namespace yarp {
26 namespace os {
27 namespace impl {
28 
34  public TwoWayStream,
35  public InputStream,
36  public OutputStream
37 {
38 
39 public:
41  closed(false),
42  interrupting(false),
43  reader(false),
44 #ifndef YARP_HAS_ACE
45  dgram_sockfd(-1),
46 #endif
47  dgram(nullptr),
48  mgram(nullptr),
49  mutex(),
50  readAt(0),
51  readAvail(0),
52  writeAvail(0),
53  pct(0),
54  happy(true),
55  bufferAlertNeeded(false),
56  bufferAlerted(false),
57  multiMode(false),
58  errCount(0),
59  lastReportTime(0)
60  {
61  }
62 
63  virtual bool openMonitor(int readSize = 0, int writeSize = 0)
64  {
65  allocate(readSize, writeSize);
66  return true;
67  }
68 
69  virtual bool open(const Contact& remote);
70 
71  virtual bool open(const Contact& local, const Contact& remote);
72 
73  virtual bool openMcast(const Contact& group,
74  const Contact& ipLocal);
75 
76 #ifdef YARP_HAS_ACE
77  virtual int restrictMcast(ACE_SOCK_Dgram_Mcast* dmcast,
78  const Contact& group,
79  const Contact& ipLocal,
80  bool add);
81 #endif
82 
83  virtual bool join(const Contact& group, bool sender, const Contact& ipLocal);
84 
85  virtual bool join(const Contact& group, bool sender)
86  {
87  return join(group, sender, Contact());
88  }
89 
90  virtual ~DgramTwoWayStream();
91 
93  {
94  return *this;
95  }
96 
98  {
99  return *this;
100  }
101 
102  const Contact& getLocalAddress() const override
103  {
104  return localAddress;
105  }
106 
107  const Contact& getRemoteAddress() const override
108  {
109  return remoteAddress;
110  }
111 
112  void interrupt() override;
113 
114  void close() override
115  {
116  closeMain();
117  }
118 
119  virtual void closeMain();
120 
123 
125  void write(const yarp::os::Bytes& b) override;
126 
127  void flush() override;
128 
129  bool isOk() const override;
130 
131  void reset() override;
132 
133  void beginPacket() override;
134 
135  void endPacket() override;
136 
137  yarp::os::Bytes getMonitor();
138 
139  bool setTypeOfService(int tos) override;
140 
141  int getTypeOfService() override;
142 
143  void setMonitor(const yarp::os::Bytes& data)
144  {
145  monitor = yarp::os::ManagedBytes(data, false);
146  monitor.copy();
147  }
148 
149  void removeMonitor();
150 
151  virtual void onMonitorInput()
152  {
153  }
154 
155  virtual void onMonitorOutput()
156  {
157  }
158 
159 private:
160  yarp::os::ManagedBytes monitor;
161  bool closed, interrupting, reader;
162 #ifdef YARP_HAS_ACE
163  ACE_SOCK_Dgram* dgram;
164  ACE_SOCK_Dgram_Mcast* mgram;
165  ACE_INET_Addr localHandle, remoteHandle;
166 #else
167  int dgram_sockfd;
168  void* dgram;
169  void* mgram;
170  int localHandle, remoteHandle;
171 #endif
172  Contact localAddress, remoteAddress, restrictInterfaceIp;
173  yarp::os::ManagedBytes readBuffer, writeBuffer;
174  std::mutex mutex;
175  yarp::conf::ssize_t readAt, readAvail, writeAvail;
176  int pct;
177  bool happy;
178  bool bufferAlertNeeded;
179  bool bufferAlerted;
180  bool multiMode;
181  int errCount;
182  double lastReportTime;
183 
184  void allocate(int readSize = 0, int writeSize = 0);
185 
186  void configureSystemBuffers();
187 };
188 
189 } // namespace impl
190 } // namespace os
191 } // namespace yarp
192 
193 #endif // YARP_OS_IMPL_DGRAMTWOWAYSTREAM_H
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:26
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:20
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:22
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:22
virtual void write(char ch)
Write a single byte to the stream.
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:26
A stream abstraction for datagram communication.
void setMonitor(const yarp::os::Bytes &data)
InputStream & getInputStream() override
Get an InputStream to read from.
OutputStream & getOutputStream() override
Get an OutputStream to write to.
void close() override
Terminate the stream.
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
virtual bool openMonitor(int readSize=0, int writeSize=0)
virtual bool join(const Contact &group, bool sender)
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
ContainerT::value_type join(typename ContainerT::const_iterator begin, typename ContainerT::const_iterator end, const typename ContainerT::value_type &separator=", ")
Utility to join the elements in a container to a single string separated by a separator.
Definition: string.h:93
::ssize_t ssize_t
Definition: numeric.h:86
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:922
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1098
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_os_impl_API
Definition: api.h:46