YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
TcpAcceptor.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2010 Anne van Rossum <anne@almende.com>
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <yarp/conf/system.h>
8#ifndef YARP_HAS_ACE
9
10// General files
11
14
15#include <cerrno>
16
17using namespace yarp::os::impl;
18using namespace yarp::os;
19
20#define BACKLOG 1
21
22namespace {
23YARP_OS_LOG_COMPONENT(TCPACCEPTOR_POSIX, "yarp.os.impl.TcpAcceptor.posix")
24}
25
26/* **************************************************************************************
27 * Implementation of TcpAcceptor
28 * **************************************************************************************/
29
30int TcpAcceptor::open(const Contact& address)
31{
32 yCDebug(TCPACCEPTOR_POSIX, "TCP/IP start in server mode");
33 set_handle(socket(AF_INET, SOCK_STREAM, 0));
34 if (get_handle() < 0) {
35 yCError(TCPACCEPTOR_POSIX, "At TcpAcceptor::open there was an error: %d, %s", errno, strerror(errno));
36 return -1;
37 }
38
39 int yes=1;
40 if (setsockopt(get_handle(), SOL_SOCKET, SO_REUSEADDR, &yes,
41 sizeof(int)) == -1) {
42 yCError(TCPACCEPTOR_POSIX, "At TcpAcceptor::open there was an error: %d, %s", errno, strerror(errno));
43 return -1;
44 }
45
46 return shared_open(address);
47}
48
52int TcpAcceptor::shared_open(const Contact& address)
53{
54 struct sockaddr_in servAddr;
55 servAddr.sin_addr.s_addr = INADDR_ANY;
56 servAddr.sin_family = AF_INET;
57 servAddr.sin_port = htons((address.getPort()>0)?address.getPort():0);
58 inet_pton(AF_INET, address.getHost().c_str(), &servAddr.sin_addr);
59 memset(servAddr.sin_zero, '\0', sizeof servAddr.sin_zero);
60
61 if (bind(get_handle(), (struct sockaddr *)&servAddr, sizeof (struct sockaddr)) < 0) {
62 yCError(TCPACCEPTOR_POSIX, "At bind(sockfd) there was an error: %d, %s", errno, strerror(errno));
63 return -1;
64 }
65
66 if (listen(get_handle(), BACKLOG) < 0) {
67 yCError(TCPACCEPTOR_POSIX, "At listen(sockfd) there was an error: %d, %s", errno, strerror(errno));
68 return -1;
69 }
70
71 struct sockaddr_in sin;
72 socklen_t addrlen = sizeof(sin);
73 if (getsockname(get_handle(), (struct sockaddr *)&sin, &addrlen) == 0 &&
74 sin.sin_family == AF_INET &&
75 addrlen == sizeof(sin)) {
76 port_number = static_cast<int>(ntohs(sin.sin_port));
77 } else {
78 yCError(TCPACCEPTOR_POSIX, "At getsockname(address) there was an error: %d, %s", errno, strerror(errno));
79 return -1;
80 }
81
82 return 1;
83}
84
85
89int TcpAcceptor::accept(TcpStream &new_stream)
90{
91 sockaddr* addr = nullptr;
92 int len = 0;
93 int* len_ptr = &len;
94
95 new_stream.set_handle(::accept(get_handle(), reinterpret_cast<struct sockaddr*>(&addr), reinterpret_cast<socklen_t*>(len_ptr)));
96 if (new_stream.get_handle() < 0) {
97 yCError(TCPACCEPTOR_POSIX, "At accept(sockfd) there was an error: %d, %s", errno, strerror(errno));
98 return -1;
99 }
100
101 return 1;
102}
103
104int TcpAcceptor::close()
105{
106 int result = 0;
107
108 if (get_handle() != -1) {
109 result = ::close(get_handle());
110 set_handle (-1);
111 }
112 return result;
113}
114
115#endif
#define BACKLOG
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
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
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.