Files
ladybird/Userland/rpcdump.cpp
Andreas Kling f89944e804 Inspector+LibCore+rpcdump: Rework the RPC stuff to be request/response
RPC clients now send JSON-encoded requests to the RPC server.
The connection also stays alive instead of disconnecting automatically
after the initial CObject graph dump.

JSON payloads are preceded by a single host-order encoded 32-bit int
containing the length of the payload.

So far, we have three RPC commands:

    - Identify
    - GetAllObjects
    - Disconnect

We'll be adding more of these as we go along. :^)
2019-09-11 21:19:23 +02:00

56 lines
1.2 KiB
C++

#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <LibCore/CEventLoop.h>
#include <LibCore/CLocalSocket.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
if (argc != 2) {
printf("usage: %s <pid>\n", argv[0]);
return 0;
}
CEventLoop loop;
int pid = atoi(argv[1]);
CLocalSocket socket;
socket.on_connected = [&] {
dbg() << "Connected to PID " << pid;
JsonObject request;
request.set("type", "GetAllObjects");
auto serialized = request.to_string();
i32 length = serialized.length();
socket.write((const u8*)&length, sizeof(length));
socket.write(serialized);
};
socket.on_ready_to_read = [&] {
if (socket.eof()) {
dbg() << "Disconnected from PID " << pid;
loop.quit(0);
return;
}
auto data = socket.read_all();
for (int i = 0; i < data.size(); ++i)
putchar(data[i]);
printf("\n");
loop.quit(0);
};
auto success = socket.connect(CSocketAddress::local(String::format("/tmp/rpc.%d", pid)));
if (!success) {
fprintf(stderr, "Couldn't connect to PID %d\n", pid);
return 1;
}
return loop.exec();
}