YARP
Yet Another Robot Platform
UnixSockTwoWayStream.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_UNIX_UNIXSOCKTWOWAYSTREAM_H
7 #define YARP_UNIX_UNIXSOCKTWOWAYSTREAM_H
8 
9 #include <yarp/os/ManagedBytes.h>
10 #include <yarp/os/Semaphore.h>
11 #include <yarp/os/TwoWayStream.h>
12 
13 #include <mutex>
14 
20  public yarp::os::InputStream,
22 {
23 
24 public:
25  UnixSockTwoWayStream(const std::string& _socketPath = "");
26 
27  ~UnixSockTwoWayStream() override;
28 
29  InputStream& getInputStream() override;
30 
31  OutputStream& getOutputStream() override;
32 
33  const yarp::os::Contact& getLocalAddress() const override;
34 
35  const yarp::os::Contact& getRemoteAddress() const override;
36 
37  void interrupt() override;
38 
39  void close() override;
40 
43 
45  void write(const yarp::os::Bytes& b) override;
46 
47  bool isOk() const override;
48 
49  void reset() override;
50 
51  void beginPacket() override;
52  void endPacket() override;
53 
54  bool open(bool sender = false);
55  void setLocalAddress(yarp::os::Contact& _localAddress);
56  void setRemoteAddress(yarp::os::Contact& _remoteAddress);
57 
58 private:
59  bool closed{false};
60  bool openedAsReader{false};
61  yarp::os::Contact localAddress;
62  yarp::os::Contact remoteAddress;
63  bool happy{true};
64 
65  std::string socketPath;
66  int reader_fd{-1};
67  int sender_fd{-1};
68 
69  static constexpr size_t maxAttempts = 5;
70  static constexpr double delayBetweenAttempts = 0.1;
71 };
72 
73 #endif // YARP_UNIX_UNIXSOCKTWOWAYSTREAM_H
A stream abstraction for unix socket communication.
bool isOk() const override
Check if the stream is ok or in an error state.
OutputStream & getOutputStream() override
Get an OutputStream to write to.
InputStream & getInputStream() override
Get an InputStream to read from.
void beginPacket() override
Mark the beginning of a logical packet.
void interrupt() override
Interrupt the stream.
bool open(bool sender=false)
const yarp::os::Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
UnixSockTwoWayStream(const std::string &_socketPath="")
void reset() override
Reset the stream.
void close() override
Terminate the stream.
void setLocalAddress(yarp::os::Contact &_localAddress)
void endPacket() override
Mark the end of a logical packet (see beginPacket).
void write(const yarp::os::Bytes &b) override
Write a block of bytes to the stream.
void setRemoteAddress(yarp::os::Contact &_remoteAddress)
const yarp::os::Contact & getLocalAddress() const override
Get the address of the local side of the stream.
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
InputStream()
Constructor.
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
::ssize_t ssize_t
Definition: numeric.h:86