From a5c50a4f262f0a880734623f79d4dc2f1aa8a0a2 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 13 Aug 2019 10:29:39 +0200 Subject: Pull in files from odr-mmbtools-common Replace ASIO by simpler implementation, meaning that the telnet RC now only supports a single connection. Move Log, RC to lib/ --- lib/Socket.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'lib/Socket.cpp') diff --git a/lib/Socket.cpp b/lib/Socket.cpp index c87606a..cd70a8e 100644 --- a/lib/Socket.cpp +++ b/lib/Socket.cpp @@ -381,7 +381,7 @@ bool TCPSocket::valid() const return m_sock != -1; } -void TCPSocket::connect(const std::string& hostname, int port, bool nonblock) +void TCPSocket::connect(const std::string& hostname, int port) { if (m_sock != INVALID_SOCKET) { throw std::logic_error("You may only connect an invalid TCPSocket"); @@ -415,16 +415,10 @@ void TCPSocket::connect(const std::string& hostname, int port, bool nonblock) if (sfd == -1) continue; - if (nonblock) { - int flags = fcntl(sfd, F_GETFL); - if (fcntl(sfd, F_SETFL, flags | O_NONBLOCK) == -1) { - std::string errstr(strerror(errno)); - throw std::runtime_error("TCP: Could not set O_NONBLOCK: " + errstr); - } - } - int ret = ::connect(sfd, rp->ai_addr, rp->ai_addrlen); if (ret != -1 or (ret == -1 and errno == EINPROGRESS)) { + // As the TCPClient could set the socket to nonblocking, we + // must handle EINPROGRESS here m_sock = sfd; break; } @@ -699,8 +693,13 @@ ssize_t TCPClient::recv(void *buffer, size_t length, int flags, int timeout_ms) void TCPClient::reconnect() { - const bool nonblock = true; - m_sock.connect(m_hostname, m_port, nonblock); + int flags = fcntl(m_sock.m_sock, F_GETFL); + if (fcntl(m_sock.m_sock, F_SETFL, flags | O_NONBLOCK) == -1) { + std::string errstr(strerror(errno)); + throw std::runtime_error("TCP: Could not set O_NONBLOCK: " + errstr); + } + + m_sock.connect(m_hostname, m_port); } TCPConnection::TCPConnection(TCPSocket&& sock) : -- cgit v1.2.3