Skip to content

Commit

Permalink
Client: fix and normalize receive operations
Browse files Browse the repository at this point in the history
There was a bug: in epoll net provider, in case of EAGAIN the
connection was marked as having data to decode, which is wrong.

Fix it by simple check that the connection has input data.

Implement this part int libev net provider similarly.

Part of #28
  • Loading branch information
alyapunov committed Aug 31, 2022
1 parent b6e49c8 commit 6581de7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/Client/EpollNetProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,17 @@ EpollNetProvider<BUFFER, NETWORK>::recv(Conn_t &conn)
size_t iov_cnt = 0;
/* Get IO vectors array pointing to the input buffer. */
struct iovec *iov = inBufferToIOV(conn, total, &iov_cnt);
int has_read_bytes = NETWORK::recvall(conn.getSocket(), iov, iov_cnt, true);
if (has_read_bytes < 0) {
ssize_t rcvd = NETWORK::recvall(conn.getSocket(), iov, iov_cnt, true);
hasNotRecvBytes(conn, total - (rcvd < 0 ? 0 : rcvd));
if (rcvd < 0) {
//Don't consider EWOULDBLOCK to be an error.
if (netWouldBlock(errno))
return 0;
conn.setError(std::string("Failed to receive response: ") +
strerror(errno), errno);
return -1;
}
hasNotRecvBytes(conn, total - has_read_bytes);
return total - has_read_bytes;
return 0;
}

template<class BUFFER, class NETWORK>
Expand Down Expand Up @@ -296,9 +296,9 @@ EpollNetProvider<BUFFER, NETWORK>::wait(int timeout)
* becomes ready to decode.
*/
int rc = recv(conn);
if (rc < 0)
if (rc != 0)
return -1;
if (rc == 0)
if (hasDataToDecode(conn))
m_Connector.readyToDecode(conn);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Client/LibevNetProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ connectionReceive(Connection<BUFFER, LibevNetProvider<BUFFER, NETWORK>> &conn)
size_t iov_cnt = 0;
struct iovec *iov =
inBufferToIOV(conn, total, &iov_cnt);
int read_bytes = NETWORK::recvall(conn.getSocket(), iov, iov_cnt, true);
hasNotRecvBytes(conn, total - read_bytes);
if (read_bytes < 0) {
ssize_t rcvd = NETWORK::recvall(conn.getSocket(), iov, iov_cnt, true);
hasNotRecvBytes(conn, total - (rcvd < 0 ? 0 : rcvd));
if (rcvd < 0) {
if (netWouldBlock(errno)) {
return 1;
return 0;
}
conn.setError(std::string("Failed to receive response: ") +
strerror(errno));
return -1;
}
return total - read_bytes;
return 0;
}

template<class BUFFER, class NETWORK>
Expand All @@ -158,9 +158,9 @@ recv_cb(struct ev_loop *loop, struct ev_io *watcher, int /* revents */)
timerDisable(loop, waitWatcher->timer);
int rc = connectionReceive(conn);
Connector_t *connector = waitWatcher->connector;
if (rc < 0)
if (rc != 0)
return;
if (rc == 0)
if (hasDataToDecode(conn))
connector->readyToDecode(conn);
}

Expand Down

0 comments on commit 6581de7

Please sign in to comment.