Skip to content

Commit 1e2c922

Browse files
committed
Client: fix and normalize receive operations
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
1 parent 807def0 commit 1e2c922

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/Client/EpollNetProvider.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,17 @@ EpollNetProvider<BUFFER, NETWORK>::recv(Conn_t &conn)
222222
size_t iov_cnt = 0;
223223
/* Get IO vectors array pointing to the input buffer. */
224224
struct iovec *iov = inBufferToIOV(conn, total, &iov_cnt);
225-
int has_read_bytes = NETWORK::recvall(conn.getSocket(), iov, iov_cnt, true);
226-
if (has_read_bytes < 0) {
225+
ssize_t rcvd = NETWORK::recvall(conn.getSocket(), iov, iov_cnt, true);
226+
hasNotRecvBytes(conn, total - (rcvd < 0 ? 0 : rcvd));
227+
if (rcvd < 0) {
227228
//Don't consider EWOULDBLOCK to be an error.
228229
if (netWouldBlock(errno))
229230
return 0;
230231
conn.setError(std::string("Failed to receive response: ") +
231232
strerror(errno), errno);
232233
return -1;
233234
}
234-
hasNotRecvBytes(conn, total - has_read_bytes);
235-
return total - has_read_bytes;
235+
return 0;
236236
}
237237

238238
template<class BUFFER, class NETWORK>
@@ -296,9 +296,9 @@ EpollNetProvider<BUFFER, NETWORK>::wait(int timeout)
296296
* becomes ready to decode.
297297
*/
298298
int rc = recv(conn);
299-
if (rc < 0)
299+
if (rc != 0)
300300
return -1;
301-
if (rc == 0)
301+
if (hasDataToDecode(conn))
302302
m_Connector.readyToDecode(conn);
303303
}
304304

src/Client/LibevNetProvider.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,17 @@ connectionReceive(Connection<BUFFER, LibevNetProvider<BUFFER, NETWORK>> &conn)
129129
size_t iov_cnt = 0;
130130
struct iovec *iov =
131131
inBufferToIOV(conn, total, &iov_cnt);
132-
int read_bytes = NETWORK::recvall(conn.getSocket(), iov, iov_cnt, true);
133-
hasNotRecvBytes(conn, total - read_bytes);
134-
if (read_bytes < 0) {
132+
ssize_t rcvd = NETWORK::recvall(conn.getSocket(), iov, iov_cnt, true);
133+
hasNotRecvBytes(conn, total - (rcvd < 0 ? 0 : rcvd));
134+
if (rcvd < 0) {
135135
if (netWouldBlock(errno)) {
136-
return 1;
136+
return 0;
137137
}
138138
conn.setError(std::string("Failed to receive response: ") +
139139
strerror(errno));
140140
return -1;
141141
}
142-
return total - read_bytes;
142+
return 0;
143143
}
144144

145145
template<class BUFFER, class NETWORK>
@@ -158,9 +158,9 @@ recv_cb(struct ev_loop *loop, struct ev_io *watcher, int /* revents */)
158158
timerDisable(loop, waitWatcher->timer);
159159
int rc = connectionReceive(conn);
160160
Connector_t *connector = waitWatcher->connector;
161-
if (rc < 0)
161+
if (rc != 0)
162162
return;
163-
if (rc == 0)
163+
if (hasDataToDecode(conn))
164164
connector->readyToDecode(conn);
165165
}
166166

0 commit comments

Comments
 (0)