YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
ShiftStream.cpp
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
8
9#include <mutex>
10
16
18{
19public:
21 ~Private();
22 void close();
23
24 std::mutex streamMutex;
27};
28
30
35
37{
38 std::lock_guard<std::mutex> lg(streamMutex);
39 if (stream != nullptr) {
40 stream->close();
41 delete stream;
42 stream = nullptr;
43 }
44}
45
46
48 mPriv(new Private())
49{
50}
51
53{
54 delete mPriv;
55}
56
58{
59}
60
62{
63 check();
64 std::lock_guard<std::mutex> lg(mPriv->streamMutex);
65 if (mPriv->stream == nullptr) {
66 return mPriv->nullStream;
67 }
68 return mPriv->stream->getInputStream();
69}
70
72{
73 check();
74 std::lock_guard<std::mutex> lg(mPriv->streamMutex);
75 if (mPriv->stream == nullptr) {
76 return mPriv->nullStream;
77 }
78 return mPriv->stream->getOutputStream();
79}
80
82{
83 check();
84 std::lock_guard<std::mutex> lg(mPriv->streamMutex);
85 return (mPriv->stream == nullptr) ? mPriv->nullStream.getLocalAddress()
86 : (mPriv->stream->getLocalAddress());
87}
88
90{
91 check();
92 std::lock_guard<std::mutex> lg(mPriv->streamMutex);
93 return (mPriv->stream == nullptr) ? mPriv->nullStream.getRemoteAddress()
94 : (mPriv->stream->getRemoteAddress());
95}
96
98{
99 mPriv->close();
100}
101
103{
104 close();
105 std::lock_guard<std::mutex> lg(mPriv->streamMutex);
106 mPriv->stream = stream;
107}
108
110{
111 std::lock_guard<std::mutex> lg(mPriv->streamMutex);
112 TwoWayStream* result = mPriv->stream;
113 mPriv->stream = nullptr;
114 return result;
115}
116
118{
119 std::lock_guard<std::mutex> lg(mPriv->streamMutex);
120 return mPriv->stream;
121}
122
124{
125 std::lock_guard<std::mutex> lg(mPriv->streamMutex);
126 return mPriv->stream == nullptr;
127}
128
130{
131 std::lock_guard<std::mutex> lg(mPriv->streamMutex);
132 if (mPriv->stream != nullptr) {
133 return mPriv->stream->isOk();
134 }
135 return false;
136}
137
139{
140 std::lock_guard<std::mutex> lg(mPriv->streamMutex);
141 if (mPriv->stream != nullptr) {
142 mPriv->stream->reset();
143 }
144}
145
147{
148 if (mPriv->stream != nullptr) {
149 mPriv->stream->beginPacket();
150 }
151}
152
154{
155 if (mPriv->stream != nullptr) {
156 mPriv->stream->endPacket();
157 }
158}
159
161{
162 check();
163 std::lock_guard<std::mutex> lg(mPriv->streamMutex);
164 if (mPriv->stream != nullptr) {
165 mPriv->stream->getInputStream().interrupt();
166 }
167}
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
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
A "null" stream, always invalid.
Simple specification of the minimum functions needed from output streams.
A container for a stream, allowing the stream implementation to be replaced when needed.
Definition ShiftStream.h:22
void endPacket() override
Mark the end of a logical packet (see beginPacket).
virtual void takeStream(TwoWayStream *stream)
Wrap the supplied stream.
virtual ~ShiftStream()
Destructor.
bool isOk() const override
Check if the stream is ok or in an error state.
virtual TwoWayStream * giveStream()
Removes the wrapped stream and returns it.
InputStream & getInputStream() override
Get an InputStream to read from.
virtual void check() const
Perform maintenance actions, if needed.
virtual TwoWayStream * getStream() const
void reset() override
Reset the stream.
void beginPacket() override
Mark the beginning of a logical packet.
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
virtual bool isEmpty() const
ShiftStream()
Constructor.
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
void close() override
Terminate the stream.
OutputStream & getOutputStream() override
Get an OutputStream to write to.
A stream which can be asked to perform bidirectional communication.