1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:38:10 +00:00

LibCore+Userland+Tests: Convert Stream APIs to construct on heap

As per previous discussion, it was decided that the Stream classes
should be constructed on the heap.

While I don't personally agree with this change, it does have the
benefit of avoiding Function object reconstructions due to the lambda
passed to Notifier pointing to a stale object reference. This also has
the benefit of not having to "box" objects for virtual usage, as the
objects come pre-boxed.

However, it means that we now hit the heap everytime we construct a
TCPSocket for instance, which might not be desirable.
This commit is contained in:
sin-ack 2021-12-29 22:31:45 +00:00 committed by Ali Mohammad Pur
parent eb389db92c
commit dbd25916a3
14 changed files with 163 additions and 162 deletions

View file

@ -28,7 +28,7 @@
namespace WebServer {
Client::Client(Core::Stream::BufferedTCPSocket socket, Core::Object* parent)
Client::Client(NonnullOwnPtr<Core::Stream::BufferedTCPSocket> socket, Core::Object* parent)
: Core::Object(parent)
, m_socket(move(socket))
{
@ -36,16 +36,16 @@ Client::Client(Core::Stream::BufferedTCPSocket socket, Core::Object* parent)
void Client::die()
{
m_socket.close();
m_socket->close();
deferred_invoke([this] { remove_from_parent(); });
}
void Client::start()
{
m_socket.on_ready_to_read = [this] {
m_socket->on_ready_to_read = [this] {
StringBuilder builder;
auto maybe_buffer = ByteBuffer::create_uninitialized(m_socket.buffer_size());
auto maybe_buffer = ByteBuffer::create_uninitialized(m_socket->buffer_size());
if (!maybe_buffer.has_value()) {
warnln("Could not create buffer for client (possibly out of memory)");
die();
@ -54,7 +54,7 @@ void Client::start()
auto buffer = maybe_buffer.release_value();
for (;;) {
auto maybe_can_read = m_socket.can_read_without_blocking();
auto maybe_can_read = m_socket->can_read_without_blocking();
if (maybe_can_read.is_error()) {
warnln("Failed to get the blocking status for the socket: {}", maybe_can_read.error());
die();
@ -64,14 +64,14 @@ void Client::start()
if (!maybe_can_read.value())
break;
auto maybe_nread = m_socket.read_until_any_of(buffer, Array { "\r"sv, "\n"sv, "\r\n"sv });
auto maybe_nread = m_socket->read_until_any_of(buffer, Array { "\r"sv, "\n"sv, "\r\n"sv });
if (maybe_nread.is_error()) {
warnln("Failed to read a line from the request: {}", maybe_nread.error());
die();
return;
}
if (m_socket.is_eof()) {
if (m_socket->is_eof()) {
die();
break;
}
@ -182,7 +182,7 @@ ErrorOr<void> Client::send_response(InputStream& response, HTTP::HttpRequest con
builder.append("\r\n");
auto builder_contents = builder.to_byte_buffer();
TRY(m_socket.write(builder_contents));
TRY(m_socket->write(builder_contents));
log_response(200, request);
char buffer[PAGE_SIZE];
@ -193,7 +193,7 @@ ErrorOr<void> Client::send_response(InputStream& response, HTTP::HttpRequest con
ReadonlyBytes write_buffer { buffer, size };
while (!write_buffer.is_empty()) {
auto nwritten = TRY(m_socket.write(write_buffer));
auto nwritten = TRY(m_socket->write(write_buffer));
if (nwritten == 0) {
dbgln("EEEEEE got 0 bytes written!");
@ -216,7 +216,7 @@ ErrorOr<void> Client::send_redirect(StringView redirect_path, HTTP::HttpRequest
builder.append("\r\n");
auto builder_contents = builder.to_byte_buffer();
TRY(m_socket.write(builder_contents));
TRY(m_socket->write(builder_contents));
log_response(301, request);
return {};
@ -341,7 +341,7 @@ ErrorOr<void> Client::send_error_response(unsigned code, HTTP::HttpRequest const
builder.append("</h1></body></html>");
auto builder_contents = builder.to_byte_buffer();
TRY(m_socket.write(builder_contents));
TRY(m_socket->write(builder_contents));
log_response(code, request);
return {};