YARP
Yet Another Robot Platform
UnixSocketCarrier.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 
10 #ifndef YARP_UNIX_UNIXSOCKETCARRIER_H
11 #define YARP_UNIX_UNIXSOCKETCARRIER_H
12 
14 
15 #include "UnixSockTwoWayStream.h"
16 
17 // The compile pre-defines the "unix" macro, but we don't use it, and it
18 // conflicts with the generated yarp_plugin_unix.cpp file
19 #ifdef unix
20 # undef unix
21 #endif
22 
28 {
29 public:
30  UnixSocketCarrier() = default;
35 
36  ~UnixSocketCarrier() override = default;
37 
38  yarp::os::Carrier* create() const override;
39 
40  std::string getName() const override;
41 
42  bool requireAck() const override;
43  bool isConnectionless() const override;
44 
45  bool checkHeader(const yarp::os::Bytes& header) override;
46  void getHeader(yarp::os::Bytes& header) const override;
47 
48  bool respondToHeader(yarp::os::ConnectionState& proto) override;
49  bool expectReplyToHeader(yarp::os::ConnectionState& proto) override;
50 
51  bool expectIndex(yarp::os::ConnectionState& proto) override;
52  bool sendIndex(yarp::os::ConnectionState& proto, yarp::os::SizedWriter& writer) override;
53 
54  bool expectAck(yarp::os::ConnectionState& proto) override;
55  bool sendAck(yarp::os::ConnectionState& proto) override;
56 
57  // The initiator reads the carrier parameters from the connection string (i.e. unix_stream+ack)
58  bool configure(yarp::os::ConnectionState& proto) override;
59  bool configureFromProperty(yarp::os::Property& options) override;
60 
61  // The recipients must decipher the parameters from the first 8 bytes (i.e. UNIX_ACK)
62  void setParameters(const yarp::os::Bytes& header) override;
63 
64 private:
65  static constexpr const char* name = "unix_stream";
66  static constexpr int specifierCode = 11;
67  static constexpr const char* headerCode = "UNIX_STR";
68 
69  static constexpr const char* name_ack = "unix_stream_ack";
70  static constexpr int specifierCode_ack = 12;
71  static constexpr const char* headerCode_ack = "UNIX_ACK";
72 
73  static constexpr size_t headerSize = 8;
74 
75  static constexpr const char* ack_string = "ACK";
76  static constexpr size_t ack_string_size = 4;
77 
78  std::string socketPath;
79  bool requireAckFlag{false};
80  UnixSockTwoWayStream* stream{nullptr};
81 
82  bool becomeUnixSocket(yarp::os::ConnectionState& proto, bool sender = false);
83 };
84 
85 #endif // YARP_UNIX_UNIXSOCKETCARRIER_H
UnixSocketCarrier
Communicating between two ports(IPC) via Unix Socket.
Definition: UnixSocketCarrier.h:28
UnixSocketCarrier::getHeader
void getHeader(yarp::os::Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
Definition: UnixSocketCarrier.cpp:130
UnixSocketCarrier::expectAck
bool expectAck(yarp::os::ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
Definition: UnixSocketCarrier.cpp:162
UnixSocketCarrier::checkHeader
bool checkHeader(const yarp::os::Bytes &header) override
Given the first 8 bytes received on a connection, decide if this is the right carrier type to use for...
Definition: UnixSocketCarrier.cpp:107
UnixSocketCarrier::expectIndex
bool expectIndex(yarp::os::ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
Definition: UnixSocketCarrier.cpp:146
yarp::os::Carrier
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:48
UnixSocketCarrier::create
yarp::os::Carrier * create() const override
Factory method.
Definition: UnixSocketCarrier.cpp:87
UnixSockTwoWayStream.h
UnixSocketCarrier::configure
bool configure(yarp::os::ConnectionState &proto) override
Give carrier a shot at looking at how the connection is set up.
Definition: UnixSocketCarrier.cpp:240
UnixSocketCarrier::~UnixSocketCarrier
~UnixSocketCarrier() override=default
UnixSocketCarrier::expectReplyToHeader
bool expectReplyToHeader(yarp::os::ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
Definition: UnixSocketCarrier.cpp:190
AbstractCarrier.h
yarp::os::AbstractCarrier
A starter class for implementing simple carriers.
Definition: AbstractCarrier.h:25
UnixSocketCarrier::isConnectionless
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: UnixSocketCarrier.cpp:102
UnixSocketCarrier::operator=
UnixSocketCarrier & operator=(const UnixSocketCarrier &)=delete
UnixSocketCarrier::UnixSocketCarrier
UnixSocketCarrier(UnixSocketCarrier &&)=delete
UnixSocketCarrier::setParameters
void setParameters(const yarp::os::Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition: UnixSocketCarrier.cpp:256
UnixSocketCarrier::UnixSocketCarrier
UnixSocketCarrier(const UnixSocketCarrier &)=delete
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
yarp::os::ConnectionState
The basic state of a connection - route, streams in use, etc.
Definition: ConnectionState.h:31
UnixSocketCarrier::sendAck
bool sendAck(yarp::os::ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
Definition: UnixSocketCarrier.cpp:153
UnixSocketCarrier::UnixSocketCarrier
UnixSocketCarrier()=default
UnixSocketCarrier::operator=
UnixSocketCarrier & operator=(UnixSocketCarrier &&)=delete
UnixSocketCarrier::sendIndex
bool sendIndex(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
Definition: UnixSocketCarrier.cpp:138
UnixSockTwoWayStream
A stream abstraction for unix socket communication.
Definition: UnixSockTwoWayStream.h:25
UnixSocketCarrier::configureFromProperty
bool configureFromProperty(yarp::os::Property &options) override
Definition: UnixSocketCarrier.cpp:247
UnixSocketCarrier::respondToHeader
bool respondToHeader(yarp::os::ConnectionState &proto) override
Respond to the header.
Definition: UnixSocketCarrier.cpp:184
yarp::os::SizedWriter
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
UnixSocketCarrier::requireAck
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: UnixSocketCarrier.cpp:97
UnixSocketCarrier::getName
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: UnixSocketCarrier.cpp:92