mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:57:44 +00:00
LibCore+Inspector: Reverse the direction of Inspector connections
Core::EventLoop now makes an outbound connection to InspectorServer instead of listening for incoming connections on a /tmp/rpc/PID socket. This has many benefits, for example: - We no longer keep an open listening socket in most applications - We stop leaking socket files in /tmp/rpc - We can tighten the pledges in many programs (patch coming)
This commit is contained in:
parent
3d3a5b431f
commit
dc25a4e249
6 changed files with 77 additions and 121 deletions
|
@ -8,11 +8,37 @@
|
|||
#include "RemoteObject.h"
|
||||
#include "RemoteObjectGraphModel.h"
|
||||
#include "RemoteObjectPropertyModel.h"
|
||||
#include <InspectorServer/InspectorClientEndpoint.h>
|
||||
#include <InspectorServer/InspectorServerEndpoint.h>
|
||||
#include <LibIPC/ServerConnection.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace Inspector {
|
||||
|
||||
class InspectorServerClient final
|
||||
: public IPC::ServerConnection<InspectorClientEndpoint, InspectorServerEndpoint>
|
||||
, public InspectorClientEndpoint {
|
||||
C_OBJECT(InspectorServerClient);
|
||||
|
||||
public:
|
||||
virtual void handshake() override
|
||||
{
|
||||
greet();
|
||||
}
|
||||
|
||||
virtual ~InspectorServerClient() override { }
|
||||
|
||||
private:
|
||||
InspectorServerClient()
|
||||
: IPC::ServerConnection<InspectorClientEndpoint, InspectorServerEndpoint>(*this, "/tmp/portal/inspector")
|
||||
{
|
||||
handshake();
|
||||
}
|
||||
|
||||
virtual void dummy() override { }
|
||||
};
|
||||
|
||||
RemoteProcess* s_the;
|
||||
|
||||
RemoteProcess& RemoteProcess::the()
|
||||
|
@ -23,10 +49,13 @@ RemoteProcess& RemoteProcess::the()
|
|||
RemoteProcess::RemoteProcess(pid_t pid)
|
||||
: m_pid(pid)
|
||||
, m_object_graph_model(RemoteObjectGraphModel::create(*this))
|
||||
, m_socket(Core::LocalSocket::construct())
|
||||
{
|
||||
s_the = this;
|
||||
m_socket->set_blocking(true);
|
||||
m_client = InspectorServerClient::construct();
|
||||
}
|
||||
|
||||
RemoteProcess::~RemoteProcess()
|
||||
{
|
||||
}
|
||||
|
||||
void RemoteProcess::handle_identify_response(const JsonObject& response)
|
||||
|
@ -79,106 +108,28 @@ void RemoteProcess::handle_get_all_objects_response(const JsonObject& response)
|
|||
on_update();
|
||||
}
|
||||
|
||||
void RemoteProcess::send_request(const JsonObject& request)
|
||||
{
|
||||
auto serialized = request.to_string();
|
||||
i32 length = serialized.length();
|
||||
m_socket->write((const u8*)&length, sizeof(length));
|
||||
m_socket->write(serialized);
|
||||
}
|
||||
|
||||
void RemoteProcess::set_inspected_object(FlatPtr address)
|
||||
{
|
||||
JsonObject request;
|
||||
request.set("type", "SetInspectedObject");
|
||||
request.set("address", address);
|
||||
send_request(request);
|
||||
m_client->async_set_inspected_object(m_pid, address);
|
||||
}
|
||||
|
||||
void RemoteProcess::set_property(FlatPtr object, const StringView& name, const JsonValue& value)
|
||||
{
|
||||
JsonObject request;
|
||||
request.set("type", "SetProperty");
|
||||
request.set("address", object);
|
||||
request.set("name", JsonValue(name));
|
||||
request.set("value", value);
|
||||
send_request(request);
|
||||
m_client->async_set_object_property(m_pid, object, name, value.to_string());
|
||||
}
|
||||
|
||||
void RemoteProcess::update()
|
||||
{
|
||||
m_socket->on_connected = [this] {
|
||||
dbgln("Connected to PID {}", m_pid);
|
||||
{
|
||||
auto raw_json = m_client->identify(m_pid);
|
||||
auto json = JsonValue::from_string(raw_json);
|
||||
handle_identify_response(json.value().as_object());
|
||||
}
|
||||
|
||||
{
|
||||
JsonObject request;
|
||||
request.set("type", "Identify");
|
||||
send_request(request);
|
||||
}
|
||||
|
||||
{
|
||||
JsonObject request;
|
||||
request.set("type", "GetAllObjects");
|
||||
send_request(request);
|
||||
}
|
||||
};
|
||||
|
||||
m_socket->on_ready_to_read = [this] {
|
||||
if (m_socket->eof()) {
|
||||
dbgln("Disconnected from PID {}", m_pid);
|
||||
m_socket->close();
|
||||
return;
|
||||
}
|
||||
|
||||
u32 length;
|
||||
int nread = m_socket->read((u8*)&length, sizeof(length));
|
||||
if (nread != sizeof(length)) {
|
||||
dbgln("Disconnected from PID {}", m_pid);
|
||||
m_socket->close();
|
||||
return;
|
||||
}
|
||||
|
||||
ByteBuffer data;
|
||||
size_t remaining_bytes = length;
|
||||
|
||||
while (remaining_bytes) {
|
||||
auto packet = m_socket->read(remaining_bytes);
|
||||
if (packet.size() == 0)
|
||||
break;
|
||||
data.append(packet.data(), packet.size());
|
||||
remaining_bytes -= packet.size();
|
||||
}
|
||||
|
||||
VERIFY(data.size() == length);
|
||||
dbgln("Got data size {} and read that many bytes", length);
|
||||
|
||||
auto json_value = JsonValue::from_string(data);
|
||||
VERIFY(json_value.has_value());
|
||||
VERIFY(json_value.value().is_object());
|
||||
|
||||
dbgln("Got JSON response {}", json_value.value());
|
||||
|
||||
auto& response = json_value.value().as_object();
|
||||
|
||||
auto response_type = response.get("type").as_string_or({});
|
||||
if (response_type.is_null())
|
||||
return;
|
||||
|
||||
if (response_type == "GetAllObjects") {
|
||||
handle_get_all_objects_response(response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (response_type == "Identify") {
|
||||
handle_identify_response(response);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
auto success = m_socket->connect(Core::SocketAddress::local(String::formatted("/tmp/rpc/{}", m_pid)));
|
||||
if (!success) {
|
||||
warnln("Couldn't connect to PID {}", m_pid);
|
||||
exit(1);
|
||||
{
|
||||
auto raw_json = m_client->get_all_objects(m_pid);
|
||||
auto json = JsonValue::from_string(raw_json);
|
||||
handle_get_all_objects_response(json.value().as_object());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue