34 while(waitpid(-1,
nullptr, WNOHANG) > 0) {}
42 int TcpAcceptor::open(
const Contact& address)
44 yCDebug(TCPACCEPTOR_POSIX,
"TCP/IP start in server mode");
45 set_handle(socket(AF_INET, SOCK_STREAM, 0));
46 if (get_handle() < 0) {
47 yCError(TCPACCEPTOR_POSIX,
"At TcpAcceptor::open there was an error: %d, %s", errno, strerror(errno));
52 if (setsockopt(get_handle(), SOL_SOCKET, SO_REUSEADDR, &yes,
54 yCError(TCPACCEPTOR_POSIX,
"At TcpAcceptor::open there was an error: %d, %s", errno, strerror(errno));
58 return shared_open(address);
64 int TcpAcceptor::shared_open(
const Contact& address)
66 struct sockaddr_in servAddr;
67 servAddr.sin_addr.s_addr = INADDR_ANY;
68 servAddr.sin_family = AF_INET;
69 servAddr.sin_port = htons((address.
getPort()>0)?address.
getPort():0);
70 inet_pton(AF_INET, address.
getHost().c_str(), &servAddr.sin_addr);
71 memset(servAddr.sin_zero,
'\0',
sizeof servAddr.sin_zero);
73 if (bind(get_handle(), (
struct sockaddr *)&servAddr,
sizeof (
struct sockaddr)) < 0) {
74 yCError(TCPACCEPTOR_POSIX,
"At bind(sockfd) there was an error: %d, %s", errno, strerror(errno));
78 if (listen(get_handle(),
BACKLOG) < 0) {
79 yCError(TCPACCEPTOR_POSIX,
"At listen(sockfd) there was an error: %d, %s", errno, strerror(errno));
85 sigemptyset(&sa.sa_mask);
86 sa.sa_flags = SA_RESTART;
87 if (sigaction(SIGCHLD, &sa,
nullptr) < 0) {
88 yCError(TCPACCEPTOR_POSIX,
"At sigaction(address) there was an error: %d, %s", errno, strerror(errno));
92 struct sockaddr_in sin;
93 socklen_t addrlen =
sizeof(sin);
94 if (getsockname(get_handle(), (
struct sockaddr *)&sin, &addrlen) == 0 &&
95 sin.sin_family == AF_INET &&
96 addrlen ==
sizeof(sin)) {
97 port_number =
static_cast<int>(ntohs(sin.sin_port));
99 yCError(TCPACCEPTOR_POSIX,
"At getsockname(address) there was an error: %d, %s", errno, strerror(errno));
112 sockaddr* addr =
nullptr;
116 new_stream.
set_handle(::accept(get_handle(),
reinterpret_cast<struct sockaddr*
>(&addr),
reinterpret_cast<socklen_t*
>(len_ptr)));
118 yCError(TCPACCEPTOR_POSIX,
"At accept(sockfd) there was an error: %d, %s", errno, strerror(errno));
125 int TcpAcceptor::close()
129 if (get_handle() != -1) {
130 result = ::close(get_handle());