From a097624a2a76f7503562752d85855e8dffd12ff2 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 16 May 2016 23:35:07 +0200 Subject: [PATCH] [DEV] try poll for receiving data --- enet/Tcp.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++--- enet/Tcp.h | 7 +++++- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/enet/Tcp.cpp b/enet/Tcp.cpp index 5490dd8..dca65e0 100644 --- a/enet/Tcp.cpp +++ b/enet/Tcp.cpp @@ -170,6 +170,13 @@ bool enet::Tcp::link() { ENET_DEBUG("Connection done"); } } + #if 1 + //Initialize the pollfd structure + memset(m_fds, 0 , sizeof(m_fds)); + //Set up the initial listening socket + m_fds[0].fd = m_socketIdClient; + m_fds[0].events = POLLIN | POLLERR; + #endif ENET_INFO("End configuring Socket ..."); return true; } @@ -192,12 +199,14 @@ bool enet::Tcp::unlink() { int32_t enet::Tcp::read(void* _data, int32_t _maxLen) { + ENET_ERROR("read [START]"); if (m_status != status::link) { ENET_ERROR("Can not read on unlink connection"); return -1; } - #if 1 - int32_t size = ::read(m_socketIdClient, _data, _maxLen); + int32_t size = -1; + #if 0 + size = ::read(m_socketIdClient, _data, _maxLen); if ( size != 0 && errno == 2) { // simply the socket en empty @@ -206,8 +215,51 @@ int32_t enet::Tcp::read(void* _data, int32_t _maxLen) { m_status = status::error; return -1; } - return size; #else + #ifndef SDFGSDFGSDFGSDFGSDFGSDFG + int nfds = 1; + // Initialize the timeout to 3 minutes. If no activity after 3 minutes this program will end. timeout value is based on milliseconds. + int timeout = (3 * 60 * 1000); + // Call poll() and wait 3 minutes for it to complete. + ENET_INFO("Waiting on poll()..."); + int rc = poll(m_fds, nfds, timeout); + // Check to see if the poll call failed. + if (rc < 0) { + ENET_ERROR(" poll() failed"); + return-1; + } + // Check to see if the 3 minute time out expired. + if (rc == 0) { + ENET_ERROR(" poll() timed out. End program.\n"); + return -1; + } + bool closeConn = false; + // Receive all incoming data on this socket before we loop back and call poll again. + // Receive data on this connection until the recv fails with EWOULDBLOCK. + // If any other failure occurs, we will close the connection. + rc = recv(m_fds[0].fd, _data, _maxLen, 0); + if (rc < 0) { + if (errno != EWOULDBLOCK) { + ENET_ERROR(" recv() failed"); + closeConn = true; + } + } + // Check to see if the connection has been closed by the client + if (rc == 0) { + ENET_ERROR(" Connection closed"); + closeConn = true; + } + if (closeConn == false) { + // Data was received + size = rc; + ENET_INFO(" " << size << " bytes received"); + } else { + // If the close_conn flag was turned on, we need to clean up this active connection. + // This clean up process includes removing the descriptor. + ENET_ERROR(" Set status at remote close ..."); + m_status = status::linkRemoteClose; + } + #else //Initialize the pollfd structure memset(m_fds, 0 , sizeof(m_fds)); //Set up the initial listening socket @@ -328,7 +380,10 @@ int32_t enet::Tcp::read(void* _data, int32_t _maxLen) { close(m_fds[iii].fd); } } + #endif #endif + ENET_ERROR("read [STOP]"); + return size; } diff --git a/enet/Tcp.h b/enet/Tcp.h index 6815018..a531e9b 100644 --- a/enet/Tcp.h +++ b/enet/Tcp.h @@ -5,12 +5,16 @@ */ #pragma once +#include + namespace enet { class Tcp { private: int32_t m_socketId; //!< socket linux interface generic int32_t m_socketIdClient; - struct pollfd m_fds[1]; + #if 1 + struct pollfd m_fds[1]; + #endif public: Tcp(); virtual ~Tcp(); @@ -71,6 +75,7 @@ namespace enet { enum class status { unlink, link, + linkRemoteClose, error }; private: