TC server Receive retval bug

pull/3/head
Tom Early 2 years ago
parent a46e900121
commit f0486ec1f9

@ -149,47 +149,49 @@ bool CTCSocket::receive(int fd, STCPacket &packet)
return false; return false;
} }
// returns true on error // returns false if there is data to return
bool CTCServer::Receive(char module, STCPacket &packet, int ms) bool CTCServer::Receive(char module, STCPacket &packet, int ms)
{ {
bool rv = false;
const auto pos = m_Modules.find(module); const auto pos = m_Modules.find(module);
if (pos == std::string::npos) if (pos == std::string::npos)
{ {
std::cerr << "Can't receive on unconfigured module '" << module << "'" << std::endl; std::cerr << "Can't receive on unconfigured module '" << module << "'" << std::endl;
return true; return rv;
} }
auto pfds = &m_Pfd[pos]; auto pfds = &m_Pfd[pos];
if (pfds->fd < 0) if (pfds->fd < 0)
{ {
std::cerr << "Can't receive on module '" << module << "' because it's closed" << std::endl; std::cerr << "Can't receive on module '" << module << "' because it's closed" << std::endl;
return true; return rv;
} }
auto rv = poll(pfds, 1, ms); auto n = poll(pfds, 1, ms);
if (rv < 0) if (n < 0)
{ {
perror("Recieve poll"); perror("Recieve poll");
Close(pfds->fd); Close(pfds->fd);
return true; return rv;
} }
if (0 == rv) if (0 == n)
return false; // timeout return rv; // timeout
if (pfds->revents & POLLIN) 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) if (pfds->revents & POLLERR || pfds->revents & POLLHUP)
{ {
std::cerr << ((pfds->revents & POLLERR) ? "POLLERR" : "POLLHUP") << " received on module " << module << "', closing socket" << std::endl; std::cerr << ((pfds->revents & POLLERR) ? "POLLERR" : "POLLHUP") << " received on module " << module << "', closing socket" << std::endl;
Close(pfds->fd); Close(pfds->fd);
return true;
} }
return false; return rv;
} }
bool CTCServer::Open(const std::string &address, const std::string &modules, uint16_t port) bool CTCServer::Open(const std::string &address, const std::string &modules, uint16_t port)
@ -404,6 +406,7 @@ bool CTCClient::ReConnect()
return rv; return rv;
} }
// returns true if there's an error, but there still make be something in the returned queue
bool CTCClient::Receive(std::queue<std::unique_ptr<STCPacket>> &queue, int ms) bool CTCClient::Receive(std::queue<std::unique_ptr<STCPacket>> &queue, int ms)
{ {
for (auto &pfd : m_Pfd) for (auto &pfd : m_Pfd)

@ -39,7 +39,7 @@ public:
void Close(int fd); // close a specific file descriptor void Close(int fd); // close a specific file descriptor
bool receive(int fd, STCPacket &packet); 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); bool Send(const STCPacket *packet);
int GetFD(char module) const; // can return -1! int GetFD(char module) const; // can return -1!
@ -56,6 +56,7 @@ public:
CTCServer() : CTCSocket() {} CTCServer() : CTCSocket() {}
~CTCServer() {} ~CTCServer() {}
bool Open(const std::string &address, const std::string &modules, uint16_t port); 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 Receive(char module, STCPacket &packet, int ms);
bool Accept(); bool Accept();

Loading…
Cancel
Save

Powered by TurnKey Linux.