YARP
Yet Another Robot Platform
TcpStream.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2010 Anne van Rossum <anne@almende.com>
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 
10 #include <yarp/conf/system.h>
11 #ifndef YARP_HAS_ACE
12 
13 
14 // General files
15 #include <sys/socket.h>
16 #include <cstdio>
17 
18 #include <yarp/os/impl/TcpStream.h>
20 
21 using namespace yarp::os::impl;
22 
23 namespace {
24 YARP_OS_LOG_COMPONENT(TCPSTREAM_POSIX, "yarp.os.impl.TcpStream.posix")
25 }
26 
27 /* **************************************************************************************
28  * Implementation of TcpStream
29  * **************************************************************************************/
30 
31 TcpStream::TcpStream() = default;
32 
33 TcpStream::~TcpStream() = default;
34 
35 int TcpStream::open()
36 {
37  set_handle(socket(AF_INET, SOCK_STREAM, 0));
38  if (get_handle() == -1) {
39  yCError(TCPSTREAM_POSIX, "At TcpStream::open there was an error: %d, %s", errno, strerror(errno));
40  return -1;
41  }
42  return 0;
43 }
44 
45 int TcpStream::get_local_addr(sockaddr & sa)
46 {
47  int len = sizeof(sa);
48  if (::getsockname(get_handle(), &sa, reinterpret_cast<socklen_t*>(&len)) == -1) {
49  return -1;
50  }
51  return 0;
52 }
53 
54 int TcpStream::get_remote_addr (sockaddr & sa)
55 {
56  int len = sizeof(sa);
57  if (::getpeername(get_handle(), &sa, reinterpret_cast<socklen_t*>(&len)) == -1) {
58  return -1;
59  }
60  return 0;
61 }
62 
63 
64 #endif
LogComponent.h
TcpStream.h
system.h
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
YARP_OS_LOG_COMPONENT
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:37
yarp::os::impl
The components from which ports and connections are built.