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
A stream abstraction for unix socket communication.
Communicating between two ports(IPC) via Unix Socket.
bool sendAck(yarp::os::ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
bool sendIndex(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
UnixSocketCarrier & operator=(UnixSocketCarrier &&)=delete
void setParameters(const yarp::os::Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
bool expectAck(yarp::os::ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
UnixSocketCarrier(const UnixSocketCarrier &)=delete
~UnixSocketCarrier() override=default
bool configure(yarp::os::ConnectionState &proto) override
Give carrier a shot at looking at how the connection is set up.
UnixSocketCarrier()=default
bool expectIndex(yarp::os::ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
UnixSocketCarrier & operator=(const UnixSocketCarrier &)=delete
bool respondToHeader(yarp::os::ConnectionState &proto) override
Respond to the header.
UnixSocketCarrier(UnixSocketCarrier &&)=delete
bool expectReplyToHeader(yarp::os::ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
bool configureFromProperty(yarp::os::Property &options) override
yarp::os::Carrier * create() const override
Factory method.
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...
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...
A starter class for implementing simple carriers.
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:48
The basic state of a connection - route, streams in use, etc.
A class for storing options and configuration information.
Definition: Property.h:37
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36