YARP
Yet Another Robot Platform
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 
14 using namespace yarp::os;
15 using namespace yarp::os::impl;
16 
18 
20 {
21  return new UdpCarrier();
22 }
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:294
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:45
The basic state of a connection - route, streams in use, etc.
virtual TwoWayStream & getStreams()=0
Access the streams associated with the connection.
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition: Contact.cpp:239
std::string getHost() const
Get the host name associated with this Contact for socket communication.
Definition: Contact.cpp:228
virtual const Contact & getLocalAddress() const =0
Get the address of the local side of the stream.
virtual const Contact & getRemoteAddress() const =0
Get the address of the remote side of the stream.
A stream abstraction for datagram communication.
Communicating between two ports via UDP.
Definition: UdpCarrier.h:22
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
Definition: UdpCarrier.cpp:82
void setParameters(const Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition: UdpCarrier.cpp:44
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: UdpCarrier.cpp:49
Carrier * create() const override
Factory method.
Definition: UdpCarrier.cpp:19
virtual int getSpecifierCode() const
Definition: UdpCarrier.cpp:29
void getHeader(Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
Definition: UdpCarrier.cpp:39
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...
Definition: UdpCarrier.cpp:34
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
Definition: UdpCarrier.cpp:60
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: UdpCarrier.cpp:24
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: UdpCarrier.cpp:54
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