YARP
Yet Another Robot Platform
UnixSockTwoWayStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_UNIX_UNIXSOCKTWOWAYSTREAM_H
10 #define YARP_UNIX_UNIXSOCKTWOWAYSTREAM_H
11 
12 #include <yarp/os/ManagedBytes.h>
13 #include <yarp/os/Semaphore.h>
14 #include <yarp/os/TwoWayStream.h>
15 
16 #include <mutex>
17 
23  public yarp::os::InputStream,
25 {
26 
27 public:
28  UnixSockTwoWayStream(const std::string& _socketPath = "");
29 
30  ~UnixSockTwoWayStream() override;
31 
32  InputStream& getInputStream() override;
33 
34  OutputStream& getOutputStream() override;
35 
36  const yarp::os::Contact& getLocalAddress() const override;
37 
38  const yarp::os::Contact& getRemoteAddress() const override;
39 
40  void interrupt() override;
41 
42  void close() override;
43 
46 
48  void write(const yarp::os::Bytes& b) override;
49 
50  bool isOk() const override;
51 
52  void reset() override;
53 
54  void beginPacket() override;
55  void endPacket() override;
56 
57  bool open(bool sender = false);
58  void setLocalAddress(yarp::os::Contact& _localAddress);
59  void setRemoteAddress(yarp::os::Contact& _remoteAddress);
60 
61 private:
62  bool closed{false};
63  bool openedAsReader{false};
64  yarp::os::Contact localAddress;
65  yarp::os::Contact remoteAddress;
66  bool happy{true};
67 
68  std::string socketPath;
69  int reader_fd{-1};
70  int sender_fd{-1};
71 
72  static constexpr size_t maxAttempts = 5;
73  static constexpr double delayBetweenAttempts = 0.1;
74 };
75 
76 #endif // YARP_UNIX_UNIXSOCKTWOWAYSTREAM_H
yarp::os::TwoWayStream
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:29
UnixSockTwoWayStream::~UnixSockTwoWayStream
~UnixSockTwoWayStream() override
Definition: UnixSockTwoWayStream.cpp:93
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::InputStream::InputStream
InputStream()
Constructor.
UnixSockTwoWayStream::setLocalAddress
void setLocalAddress(yarp::os::Contact &_localAddress)
Definition: UnixSockTwoWayStream.cpp:118
TwoWayStream.h
UnixSockTwoWayStream::write
void write(const yarp::os::Bytes &b) override
Write a block of bytes to the stream.
Definition: UnixSockTwoWayStream.cpp:173
UnixSockTwoWayStream::setRemoteAddress
void setRemoteAddress(yarp::os::Contact &_remoteAddress)
Definition: UnixSockTwoWayStream.cpp:123
ManagedBytes.h
yarp::conf::ssize_t
::ssize_t ssize_t
Definition: numeric.h:60
UnixSockTwoWayStream::UnixSockTwoWayStream
UnixSockTwoWayStream(const std::string &_socketPath="")
Definition: UnixSockTwoWayStream.cpp:27
Semaphore.h
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
UnixSockTwoWayStream::open
bool open(bool sender=false)
Definition: UnixSockTwoWayStream.cpp:32
UnixSockTwoWayStream::beginPacket
void beginPacket() override
Mark the beginning of a logical packet.
Definition: UnixSockTwoWayStream.cpp:198
UnixSockTwoWayStream::getOutputStream
OutputStream & getOutputStream() override
Get an OutputStream to write to.
Definition: UnixSockTwoWayStream.cpp:103
UnixSockTwoWayStream::reset
void reset() override
Reset the stream.
Definition: UnixSockTwoWayStream.cpp:194
UnixSockTwoWayStream::getLocalAddress
const yarp::os::Contact & getLocalAddress() const override
Get the address of the local side of the stream.
Definition: UnixSockTwoWayStream.cpp:108
UnixSockTwoWayStream::interrupt
void interrupt() override
Interrupt the stream.
Definition: UnixSockTwoWayStream.cpp:128
UnixSockTwoWayStream::endPacket
void endPacket() override
Mark the end of a logical packet (see beginPacket).
Definition: UnixSockTwoWayStream.cpp:202
yarp::os::InputStream::read
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:23
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
UnixSockTwoWayStream::getRemoteAddress
const yarp::os::Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
Definition: UnixSockTwoWayStream.cpp:113
UnixSockTwoWayStream
A stream abstraction for unix socket communication.
Definition: UnixSockTwoWayStream.h:25
UnixSockTwoWayStream::isOk
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: UnixSockTwoWayStream.cpp:189
UnixSockTwoWayStream::getInputStream
InputStream & getInputStream() override
Get an InputStream to read from.
Definition: UnixSockTwoWayStream.cpp:98
yarp::os::InputStream
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
UnixSockTwoWayStream::close
void close() override
Terminate the stream.
Definition: UnixSockTwoWayStream.cpp:134