Read until EAGAIN
To prepare for EPOLLET
This commit is contained in:
+1
-1
@@ -153,7 +153,7 @@ void Connection::send_response(ProtocolHandle handle,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Connection::readBytes(char *buf, size_t buffer_size) {
|
int Connection::read_bytes(char *buf, size_t buffer_size) {
|
||||||
int r;
|
int r;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
r = read(fd_, buf, buffer_size);
|
r = read(fd_, buf, buffer_size);
|
||||||
|
|||||||
+1
-1
@@ -279,7 +279,7 @@ private:
|
|||||||
friend Ref<T> make_ref(Args &&...args);
|
friend Ref<T> make_ref(Args &&...args);
|
||||||
|
|
||||||
// Networking interface - only accessible by Server
|
// Networking interface - only accessible by Server
|
||||||
int readBytes(char *buf, size_t buffer_size);
|
int read_bytes(char *buf, size_t buffer_size);
|
||||||
enum WriteBytesResult {
|
enum WriteBytesResult {
|
||||||
Error = 1 << 0,
|
Error = 1 << 0,
|
||||||
Progress = 1 << 1,
|
Progress = 1 << 1,
|
||||||
|
|||||||
+12
-3
@@ -409,7 +409,10 @@ void Server::process_connection_reads(Ref<Connection> &conn, int events) {
|
|||||||
auto buf_size = config_.server.read_buffer_size;
|
auto buf_size = config_.server.read_buffer_size;
|
||||||
g_read_buffer.resize(buf_size);
|
g_read_buffer.resize(buf_size);
|
||||||
char *buf = g_read_buffer.data();
|
char *buf = g_read_buffer.data();
|
||||||
int r = conn->readBytes(buf, buf_size);
|
|
||||||
|
// Once we do EPOLLET we must drain the socket until read returns EAGAIN.
|
||||||
|
for (;;) {
|
||||||
|
int r = conn->read_bytes(buf, buf_size);
|
||||||
|
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
// Error or EOF - connection should be closed
|
// Error or EOF - connection should be closed
|
||||||
@@ -418,12 +421,18 @@ void Server::process_connection_reads(Ref<Connection> &conn, int events) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (r == 0) {
|
if (r == 0) {
|
||||||
// No data available (EAGAIN) - skip read processing but continue
|
// No data available (EAGAIN) - read side drained
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call handler with connection reference - server retains ownership
|
// Call handler with connection reference - server retains ownership.
|
||||||
handler_.on_data_arrived(std::string_view{buf, size_t(r)}, *conn);
|
handler_.on_data_arrived(std::string_view{buf, size_t(r)}, *conn);
|
||||||
|
|
||||||
|
// The connection may have been closed by the handler; stop reading.
|
||||||
|
if (!conn) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user