YARP
Yet Another Robot Platform
UdpCarrier.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
11 
13 #include <yarp/os/Log.h>
14 
15 #include <string>
16 
17 using namespace yarp::os;
18 using namespace yarp::os::impl;
19 
21 
23 {
24  return new UdpCarrier();
25 }
26 
28 {
29  return "udp";
30 }
31 
33 {
34  return 0;
35 }
36 
38 {
39  return getSpecifier(header) % 16 == getSpecifierCode();
40 }
41 
43 {
44  createStandardHeader(getSpecifierCode(), header);
45 }
46 
48 {
49  YARP_UNUSED(header);
50 }
51 
53 {
54  return false;
55 }
56 
58 {
59  return true;
60 }
61 
62 
64 {
65  // I am the receiver
66 
67  // issue: need a fresh port number...
68  auto* stream = new DgramTwoWayStream();
69  yAssert(stream != nullptr);
70 
71  Contact remote = proto.getStreams().getRemoteAddress();
72  bool ok = stream->open(remote);
73  if (!ok) {
74  delete stream;
75  return false;
76  }
77 
78  int myPort = stream->getLocalAddress().getPort();
79  writeYarpInt(myPort, proto);
80  proto.takeStreams(stream);
81 
82  return true;
83 }
84 
86 {
87  // I am the sender
88  int myPort = proto.getStreams().getLocalAddress().getPort();
89  std::string myName = proto.getStreams().getLocalAddress().getHost();
90  std::string altName = proto.getStreams().getRemoteAddress().getHost();
91 
92  int altPort = readYarpInt(proto);
93 
94  if (altPort == -1) {
95  return false;
96  }
97 
98  auto* stream = new DgramTwoWayStream();
99  yAssert(stream != nullptr);
100 
101  proto.takeStreams(nullptr); // free up port from tcp
102  bool ok = stream->open(Contact(myName, myPort), Contact(altName, altPort));
103  if (!ok) {
104  delete stream;
105  return false;
106  }
107  proto.takeStreams(stream);
108  return true;
109 }
yarp::os::TwoWayStream::getLocalAddress
virtual const Contact & getLocalAddress() const =0
Get the address of the local side of the stream.
yarp::os::impl::UdpCarrier::expectReplyToHeader
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
Definition: UdpCarrier.cpp:85
yarp::os::Carrier
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:48
yarp::os::impl::UdpCarrier::UdpCarrier
UdpCarrier()
yarp::os::impl::UdpCarrier::getHeader
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:42
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
yarp::os::impl::UdpCarrier::checkHeader
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:37
yarp::os::impl::UdpCarrier::getName
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: UdpCarrier.cpp:27
yarp::os::impl::UdpCarrier::requireAck
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: UdpCarrier.cpp:52
Log.h
yarp::os::ConnectionState::takeStreams
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
ConnectionState.h
yarp::os::Contact::getPort
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition: Contact.cpp:242
yarp::os::TwoWayStream::getRemoteAddress
virtual const Contact & getRemoteAddress() const =0
Get the address of the remote side of the stream.
yarp::os::impl::DgramTwoWayStream
A stream abstraction for datagram communication.
Definition: DgramTwoWayStream.h:40
yarp::os::impl::UdpCarrier
Communicating between two ports via UDP.
Definition: UdpCarrier.h:25
yarp::os::ConnectionState::getStreams
virtual TwoWayStream & getStreams()=0
Access the streams associated with the connection.
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
yarp::os::impl::UdpCarrier::setParameters
void setParameters(const Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition: UdpCarrier.cpp:47
yarp::os::impl::UdpCarrier::create
Carrier * create() const override
Factory method.
Definition: UdpCarrier.cpp:22
yarp::os::impl::UdpCarrier::respondToHeader
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
Definition: UdpCarrier.cpp:63
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::os::Contact::getHost
std::string getHost() const
Get the host name associated with this Contact for socket communication.
Definition: Contact.cpp:231
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
UdpCarrier.h
yAssert
#define yAssert(x)
Definition: Log.h:297
yarp::os::impl
The components from which ports and connections are built.
yarp::os::impl::UdpCarrier::getSpecifierCode
virtual int getSpecifierCode() const
Definition: UdpCarrier.cpp:32
yarp::os::impl::UdpCarrier::isConnectionless
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: UdpCarrier.cpp:57