1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:07:36 +00:00

LibCore+Userland: Convert TCPServer to use the Serenity Stream API

This is intended as a real-usecase test of the Serenity Stream API, and
seemed like a good candidate due to its low amount of users.
This commit is contained in:
sin-ack 2021-09-12 11:55:40 +00:00 committed by Ali Mohammad Pur
parent 2341b0159a
commit dfdb52efa7
11 changed files with 263 additions and 124 deletions

View file

@ -5,34 +5,52 @@
*/
#include "Client.h"
#include "LibCore/EventLoop.h"
Client::Client(int id, RefPtr<Core::TCPSocket> socket)
Client::Client(int id, Core::Stream::TCPSocket socket)
: m_id(id)
, m_socket(move(socket))
{
m_socket->on_ready_to_read = [this] { drain_socket(); };
m_socket.on_ready_to_read = [this] {
if (m_socket.is_eof())
return;
auto result = drain_socket();
if (result.is_error()) {
dbgln("Failed while trying to drain the socket: {}", result.error());
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
}
};
}
void Client::drain_socket()
ErrorOr<void> Client::drain_socket()
{
NonnullRefPtr<Client> protect(*this);
while (m_socket->can_read()) {
auto buf = m_socket->read(1024);
dbgln("Read {} bytes.", buf.size());
auto maybe_buffer = ByteBuffer::create_uninitialized(1024);
if (!maybe_buffer.has_value())
return ENOMEM;
auto buffer = maybe_buffer.release_value();
if (m_socket->eof()) {
quit();
while (TRY(m_socket.can_read_without_blocking())) {
auto nread = TRY(m_socket.read(buffer));
dbgln("Read {} bytes.", nread);
if (m_socket.is_eof()) {
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
break;
}
m_socket->write(buf);
TRY(m_socket.write({ buffer.data(), nread }));
}
return {};
}
void Client::quit()
{
m_socket->close();
m_socket.close();
if (on_exit)
on_exit();
}