YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
ShmemCarrier.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include "ShmemCarrier.h"
8#include "ShmemHybridStream.h"
9#include "ShmemLogComponent.h"
10
12#include <yarp/os/Log.h>
13
14#include <string>
15#include <cstdlib>
16
17
19
21
23{
24 return new ShmemCarrier();
25}
26
27std::string ShmemCarrier::getName() const
28{
29 return "shmem";
30}
31
33{
34 // specifier codes are a very old yarp feature,
35 // not necessary any more really, should be replaced.
36 return 14;
37}
38
40{
41 return true;
42}
43
45{
46 return false;
47}
48
50{
51 return getSpecifier(header) % 16 == getSpecifierCode();
52}
53
58
60{
61 YARP_UNUSED(header);
62}
63
64bool ShmemCarrier::becomeShmemVersionHybridStream(yarp::os::ConnectionState& proto, bool sender)
65{
66 auto* stream = new ShmemHybridStream();
67 yCAssert(SHMEMCARRIER, stream != nullptr);
69
70 bool ok = true;
71
72 if (!sender) {
73 ACE_INET_Addr anywhere((u_short)0, (ACE_UINT32)INADDR_ANY);
74 base = yarp::os::Contact(anywhere.get_host_addr(), anywhere.get_port_number());
75 bool ok = stream->open(base, sender) == 0;
76 if (ok) {
77 int myPort = stream->getLocalAddress().getPort();
78 writeYarpInt(myPort, proto);
79 stream->accept();
80 proto.takeStreams(nullptr);
81 proto.takeStreams(stream);
82 }
83 } else {
84 int altPort = readYarpInt(proto);
85 std::string myName = proto.getStreams().getLocalAddress().getHost();
86 proto.takeStreams(nullptr);
87 base = yarp::os::Contact(myName, altPort);
88 ok = stream->open(base, sender) == 0;
89 if (ok) {
90 proto.takeStreams(stream);
91 }
92 }
93
94 if (!ok) {
95 delete stream;
96 stream = nullptr;
97 return false;
98 }
99
100 return true;
101}
102
103bool ShmemCarrier::becomeShmem(yarp::os::ConnectionState& proto, bool sender)
104{
105 return becomeShmemVersionHybridStream(proto, sender);
106}
107
109{
110 // i am the receiver
111 return becomeShmem(proto, false);
112}
113
115{
116 // i am the sender
117 return becomeShmem(proto, true);
118}
const yarp::os::LogComponent & SHMEMCARRIER()
bool respondToHeader(yarp::os::ConnectionState &proto) override
Respond to the header.
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 expectReplyToHeader(yarp::os::ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
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...
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
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...
Carrier * create() const override
Factory method.
virtual ~ShmemCarrier()
void setParameters(const yarp::os::Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
virtual int getSpecifierCode() const
A stream abstraction for shared memory communication.
int getSpecifier(const Bytes &b) const
void writeYarpInt(int n, ConnectionState &proto)
Write n as an 8 bytes yarp number.
void createStandardHeader(int specifier, yarp::os::Bytes &header) const
int readYarpInt(ConnectionState &proto)
Read 8 bytes and interpret them as a YARP number.
A simple abstraction for a block of bytes.
Definition Bytes.h:24
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition Carrier.h:44
The basic state of a connection - route, streams in use, etc.
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
virtual TwoWayStream & getStreams()=0
Access the streams associated with the connection.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
#define yCAssert(component, x)
#define YARP_UNUSED(var)
Definition api.h:162