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
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:28
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:23
InputStream()
Constructor.
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:25
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:29
::ssize_t ssize_t
Definition: numeric.h:60