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