diff --git a/reflector/TCSocket.cpp b/reflector/TCSocket.cpp index 3b80521..a950df3 100644 --- a/reflector/TCSocket.cpp +++ b/reflector/TCSocket.cpp @@ -149,47 +149,49 @@ bool CTCSocket::receive(int fd, STCPacket &packet) return false; } -// returns true on error +// returns false if there is data to return bool CTCServer::Receive(char module, STCPacket &packet, int ms) { + bool rv = false; const auto pos = m_Modules.find(module); if (pos == std::string::npos) { std::cerr << "Can't receive on unconfigured module '" << module << "'" << std::endl; - return true; + return rv; } auto pfds = &m_Pfd[pos]; if (pfds->fd < 0) { std::cerr << "Can't receive on module '" << module << "' because it's closed" << std::endl; - return true; + return rv; } - auto rv = poll(pfds, 1, ms); - if (rv < 0) + auto n = poll(pfds, 1, ms); + if (n < 0) { perror("Recieve poll"); Close(pfds->fd); - return true; + return rv; } - if (0 == rv) - return false; // timeout + if (0 == n) + return rv; // timeout if (pfds->revents & POLLIN) { - return receive(pfds->fd, packet); + rv = ! receive(pfds->fd, packet); } + // I think it's possible that we read the data, but the socket had an error after the read... + // So we'll check... if (pfds->revents & POLLERR || pfds->revents & POLLHUP) { std::cerr << ((pfds->revents & POLLERR) ? "POLLERR" : "POLLHUP") << " received on module " << module << "', closing socket" << std::endl; Close(pfds->fd); - return true; } - return false; + return rv; } bool CTCServer::Open(const std::string &address, const std::string &modules, uint16_t port) @@ -404,6 +406,7 @@ bool CTCClient::ReConnect() return rv; } +// returns true if there's an error, but there still make be something in the returned queue bool CTCClient::Receive(std::queue> &queue, int ms) { for (auto &pfd : m_Pfd) diff --git a/reflector/TCSocket.h b/reflector/TCSocket.h index fb66ae0..3a612d4 100644 --- a/reflector/TCSocket.h +++ b/reflector/TCSocket.h @@ -39,7 +39,7 @@ public: void Close(int fd); // close a specific file descriptor bool receive(int fd, STCPacket &packet); - // All bool functions return true if there was an error + // All bool functions, except Server Receive, return true if there was an error bool Send(const STCPacket *packet); int GetFD(char module) const; // can return -1! @@ -56,6 +56,7 @@ public: CTCServer() : CTCSocket() {} ~CTCServer() {} bool Open(const std::string &address, const std::string &modules, uint16_t port); + // Returns true if there is data bool Receive(char module, STCPacket &packet, int ms); bool Accept();