ladybird/Libraries/LibDevTools/Connection.h
Sam Atkins cb88229c3f LibDevTools: Handle fragmented protocol packets
Firefox can split a DevTools protocol packet across multiple TCP
reads. The previous reader only checked that some data was available,
then tried to synchronously read the whole length-prefixed packet from
the readiness callback. Once the socket was nonblocking this could make
startup flaky when Firefox opened the inspector.

Buffer incoming bytes instead, and only dispatch messages once a full
length-prefixed JSON payload has arrived. Add a protocol test that sends
a request in two fragments through the real DevTools server.
2026-06-18 22:53:30 +02:00

45 lines
1 KiB
C++

/*
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/Error.h>
#include <AK/Function.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/RefCounted.h>
#include <AK/Weakable.h>
#include <LibCore/Socket.h>
#include <LibDevTools/Forward.h>
namespace DevTools {
class DEVTOOLS_API Connection
: public RefCounted<Connection>
, public Weakable<Connection> {
public:
static NonnullRefPtr<Connection> create(NonnullOwnPtr<Core::BufferedTCPSocket>);
~Connection();
Function<void()> on_connection_closed;
Function<void(JsonObject)> on_message_received;
void send_message(JsonValue const&);
private:
explicit Connection(NonnullOwnPtr<Core::BufferedTCPSocket>);
ErrorOr<void> on_ready_to_read();
ErrorOr<void> read_available_data();
ErrorOr<Optional<JsonValue>> read_message();
NonnullOwnPtr<Core::BufferedTCPSocket> m_socket;
ByteBuffer m_incoming_buffer;
};
}