YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
UdpCarrier.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
8
10#include <yarp/os/Log.h>
11
12#include <string>
13
14using namespace yarp::os;
15using namespace yarp::os::impl;
16
18
23
25{
26 return "udp";
27}
28
30{
31 return 0;
32}
33
35{
36 return getSpecifier(header) % 16 == getSpecifierCode();
37}
38
40{
41 createStandardHeader(getSpecifierCode(), header);
42}
43
45{
46 YARP_UNUSED(header);
47}
48
50{
51 return false;
52}
53
55{
56 return true;
57}
58
59
61{
62 // I am the receiver
63
64 // issue: need a fresh port number...
65 auto* stream = new DgramTwoWayStream();
66 yAssert(stream != nullptr);
67
68 Contact remote = proto.getStreams().getRemoteAddress();
69 bool ok = stream->open(remote);
70 if (!ok) {
71 delete stream;
72 return false;
73 }
74
75 int myPort = stream->getLocalAddress().getPort();
76 writeYarpInt(myPort, proto);
77 proto.takeStreams(stream);
78
79 return true;
80}
81
83{
84 // I am the sender
85 int myPort = proto.getStreams().getLocalAddress().getPort();
86 std::string myName = proto.getStreams().getLocalAddress().getHost();
87 std::string altName = proto.getStreams().getRemoteAddress().getHost();
88
89 int altPort = readYarpInt(proto);
90
91 if (altPort == -1) {
92 return false;
93 }
94
95 auto* stream = new DgramTwoWayStream();
96 yAssert(stream != nullptr);
97
98 proto.takeStreams(nullptr); // free up port from tcp
99 bool ok = stream->open(Contact(myName, myPort), Contact(altName, altPort));
100 if (!ok) {
101 delete stream;
102 return false;
103 }
104 proto.takeStreams(stream);
105 return true;
106}
#define yAssert(x)
Definition Log.h:388
A mini-server for performing network communication in the background.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
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
A stream abstraction for datagram communication.
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
void setParameters(const Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Carrier * create() const override
Factory method.
virtual int getSpecifierCode() const
void getHeader(Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
bool checkHeader(const Bytes &header) override
Given the first 8 bytes received on a connection, decide if this is the right carrier type to use for...
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
#define YARP_UNUSED(var)
Definition api.h:162