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
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
25namespace yarp::os::impl {
26
32 public TwoWayStream,
33 public InputStream,
34 public OutputStream
35{
36
37public:
39 closed(false),
40 interrupting(false),
41 reader(false),
42#ifndef YARP_HAS_ACE
43 dgram_sockfd(-1),
44#endif
45 dgram(nullptr),
46 mgram(nullptr),
47 mutex(),
48 readAt(0),
49 readAvail(0),
50 writeAvail(0),
51 pct(0),
52 happy(true),
53 bufferAlertNeeded(false),
54 bufferAlerted(false),
55 multiMode(false),
56 errCount(0),
57 lastReportTime(0)
58 {
59 }
60
61 virtual bool openMonitor(int readSize = 0, int writeSize = 0)
62 {
63 allocate(readSize, writeSize);
64 return true;
65 }
66
67 virtual bool open(const Contact& remote);
68
69 virtual bool open(const Contact& local, const Contact& remote);
70
71 virtual bool openMcast(const Contact& group,
72 const Contact& ipLocal);
73
74#ifdef YARP_HAS_ACE
75 virtual int restrictMcast(ACE_SOCK_Dgram_Mcast* dmcast,
76 const Contact& group,
77 const Contact& ipLocal,
78 bool add);
79#endif
80
81 virtual bool join(const Contact& group, bool sender, const Contact& ipLocal);
82
83 virtual bool join(const Contact& group, bool sender)
84 {
85 return join(group, sender, Contact());
86 }
87
88 virtual ~DgramTwoWayStream();
89
91 {
92 return *this;
93 }
94
96 {
97 return *this;
98 }
99
100 const Contact& getLocalAddress() const override
101 {
102 return localAddress;
103 }
104
105 const Contact& getRemoteAddress() const override
106 {
107 return remoteAddress;
108 }
109
110 void interrupt() override;
111
112 void close() override
113 {
114 closeMain();
115 }
116
117 virtual void closeMain();
118
121
123 void write(const yarp::os::Bytes& b) override;
124
125 void flush() override;
126
127 bool isOk() const override;
128
129 void reset() override;
130
131 void beginPacket() override;
132
133 void endPacket() override;
134
135 yarp::os::Bytes getMonitor();
136
137 bool setTypeOfService(int tos) override;
138
139 int getTypeOfService() override;
140
141 void setMonitor(const yarp::os::Bytes& data)
142 {
143 monitor = yarp::os::ManagedBytes(data, false);
144 monitor.copy();
145 }
146
147 void removeMonitor();
148
149 virtual void onMonitorInput()
150 {
151 }
152
153 virtual void onMonitorOutput()
154 {
155 }
156
157private:
159 bool closed, interrupting, reader;
160#ifdef YARP_HAS_ACE
161 ACE_SOCK_Dgram* dgram;
162 ACE_SOCK_Dgram_Mcast* mgram;
163 ACE_INET_Addr localHandle, remoteHandle;
164#else
165 int dgram_sockfd;
166 void* dgram;
167 void* mgram;
168 int localHandle, remoteHandle;
169#endif
170 Contact localAddress, remoteAddress, restrictInterfaceIp;
171 yarp::os::ManagedBytes readBuffer, writeBuffer;
172 std::mutex mutex;
173 yarp::conf::ssize_t readAt, readAvail, writeAvail;
174 int pct;
175 bool happy;
176 bool bufferAlertNeeded;
177 bool bufferAlerted;
178 bool multiMode;
179 int errCount;
180 double lastReportTime;
181
182 void allocate(int readSize = 0, int writeSize = 0);
183
184 void configureSystemBuffers();
185};
186
187} // namespace yarp::os::impl
188
189#endif // YARP_OS_IMPL_DGRAMTWOWAYSTREAM_H
A simple abstraction for a block of bytes.
Definition: Bytes.h:24
Represents how to reach a part of a YARP network.
Definition: Contact.h:33
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:25
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:21
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:21
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:25
A stream abstraction for datagram communication.
void setMonitor(const yarp::os::Bytes &data)
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
void close() override
Terminate the stream.
InputStream & getInputStream() override
Get an InputStream to read from.
virtual bool openMonitor(int readSize=0, int writeSize=0)
virtual bool join(const Contact &group, bool sender)
OutputStream & getOutputStream() override
Get an OutputStream to write to.
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:92
::ssize_t ssize_t
Definition: numeric.h:86
The components from which ports and connections are built.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:915
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1091
#define YARP_os_impl_API
Definition: api.h:46