summaryrefslogtreecommitdiff
path: root/Userland/Services/InspectorServer/InspectableProcess.cpp
blob: 9e2c98f5f18d97e7d1f45cf078a93f05f3615b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "InspectableProcess.h"
#include <AK/JsonObject.h>

namespace InspectorServer {

HashMap<pid_t, NonnullOwnPtr<InspectableProcess>> g_processes;

InspectableProcess* InspectableProcess::from_pid(pid_t pid)
{
    return g_processes.get(pid).value_or(nullptr);
}

InspectableProcess::InspectableProcess(pid_t pid, NonnullRefPtr<Core::LocalSocket> socket)
    : m_pid(pid)
    , m_socket(move(socket))
{
    m_socket->set_blocking(true);
    m_socket->on_ready_to_read = [this] {
        [[maybe_unused]] auto buffer = m_socket->read(1);
        if (m_socket->eof()) {
            g_processes.remove(m_pid);
            return;
        }
    };
}

InspectableProcess::~InspectableProcess()
{
}

String InspectableProcess::wait_for_response()
{
    if (m_socket->eof()) {
        dbgln("InspectableProcess disconnected: PID {}", m_pid);
        m_socket->close();
        return {};
    }

    u32 length {};
    auto nread = m_socket->read((u8*)&length, sizeof(length));
    if (nread != sizeof(length)) {
        dbgln("InspectableProcess got malformed data: 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;
        if (auto result = data.try_append(packet.data(), packet.size()); result.is_error()) {
            dbgln("Failed to append {} bytes to data buffer: {}", packet.size(), result.error());
            break;
        }
        remaining_bytes -= packet.size();
    }

    VERIFY(data.size() == length);
    dbgln("Got data size {} and read that many bytes", length);

    return String::copy(data);
}

void InspectableProcess::send_request(JsonObject const& request)
{
    auto serialized = request.to_string();
    u32 length = serialized.length();
    m_socket->write((u8 const*)&length, sizeof(length));
    m_socket->write(serialized);
}

}