17 # include <ace/INET_Addr.h>
18 # include <ace/os_include/netinet/os_tcp.h>
24 # include <netinet/tcp.h>
34 if (address.getPort() == -1) {
37 std::string host = address.getHost();
38 yarp::os::impl::TcpConnector connector;
40 if (address.getHost() ==
"localhost") {
44 ACE_INET_Addr addr(address.getPort(), host.c_str());
47 if (address.hasTimeout()) {
48 openTimeout.set(address.getTimeout());
49 timeout = &openTimeout;
51 int result = connector.connect(stream, addr, timeout, ACE_Addr::sap_any, 1);
55 if (address.hasTimeout()) {
58 timeout.tv_sec =
static_cast<int>(address.getTimeout());
59 timeout.tv_usec = (address.getTimeout() - timeout.tv_sec) * 1000000;
60 result = connector.connect(stream, address, &timeout);
62 result = connector.connect(stream, address,
nullptr);
71 "TCP connection to tcp:/%s failed to open",
72 address.toURI(
false).c_str());
80 int result = acceptor.accept(stream);
89 void SocketTwoWayStream::updateAddresses()
92 stream.set_option(IPPROTO_TCP, TCP_NODELAY, &one,
sizeof(
int));
96 stream.get_local_addr(local);
97 stream.get_remote_addr(remote);
99 char localHostAddress[256];
100 char remoteHostAddress[256];
101 local.get_host_addr(localHostAddress, 256);
102 remote.get_host_addr(remoteHostAddress, 256);
103 localAddress =
Contact(localHostAddress, local.get_port_number());
104 remoteAddress =
Contact(remoteHostAddress, remote.get_port_number());
106 struct sockaddr local;
107 struct sockaddr remote;
108 memset(&local, 0,
sizeof(local));
109 memset(&remote, 0,
sizeof(remote));
110 stream.get_local_addr(local);
111 stream.get_remote_addr(remote);
112 if (local.sa_family == AF_INET || local.sa_family == AF_INET6) {
113 char* localHostAddress =
new char[local.sa_family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN];
114 char* remoteHostAddress =
new char[remote.sa_family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN];
115 const char*
ret =
nullptr;
116 ret = inet_ntop(local.sa_family,
117 (local.sa_family == AF_INET ?
reinterpret_cast<void*
>(&
reinterpret_cast<struct sockaddr_in*
>(&local)->sin_addr) :
reinterpret_cast<void*
>(&
reinterpret_cast<struct sockaddr_in6*
>(&local)->sin6_addr)),
119 (local.sa_family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN));
121 localAddress =
Contact(localHostAddress, ntohs(
reinterpret_cast<struct sockaddr_in*
>(&local)->sin_port));
125 ret = inet_ntop(remote.sa_family,
126 (remote.sa_family == AF_INET ?
reinterpret_cast<void*
>(&
reinterpret_cast<struct sockaddr_in*
>(&remote)->sin_addr) :
reinterpret_cast<void*
>(&
reinterpret_cast<struct sockaddr_in6*
>(&remote)->sin6_addr)),
128 (remote.sa_family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN));
130 remoteAddress =
Contact(remoteHostAddress, ntohs(
reinterpret_cast<struct sockaddr_in*
>(&remote)->sin_port));
134 delete[] localHostAddress;
135 delete[] remoteHostAddress;
148 return (stream.set_option(IPPROTO_IP, IP_TOS, &tos,
static_cast<int>(
sizeof(tos))) == 0);
155 stream.get_option(IPPROTO_IP, IP_TOS, &tos, &optlen);
const yarp::os::LogComponent & SOCKETTWOWAYSTREAM()
static std::string getHostName(bool prefer_loopback=false, const std::string &seed="")
A stream abstraction for socket communication.
int getTypeOfService() override
int open(const Contact &address)
bool setTypeOfService(int tos) override
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
struct timeval YARP_timeval
An interface to the operating system, including Port based communication.