summaryrefslogtreecommitdiff
path: root/Userland/Services/RequestServer/Protocol.cpp
blob: 19f9fdb1f492d0c5c34a3b74e1b2c5cb521608ee (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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/HashMap.h>
#include <RequestServer/Protocol.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

namespace RequestServer {

static HashMap<String, Protocol*>& all_protocols()
{
    static HashMap<String, Protocol*> map;
    return map;
}

Protocol* Protocol::find_by_name(const String& name)
{
    return all_protocols().get(name).value_or(nullptr);
}

Protocol::Protocol(const String& name)
{
    all_protocols().set(name, this);
}

Protocol::~Protocol()
{
    VERIFY_NOT_REACHED();
}

Result<Protocol::Pipe, String> Protocol::get_pipe_for_request()
{
    int fd_pair[2] { 0 };
    if (pipe(fd_pair) != 0) {
        auto saved_errno = errno;
        dbgln("Protocol: pipe() failed: {}", strerror(saved_errno));
        return String { strerror(saved_errno) };
    }
    fcntl(fd_pair[1], F_SETFL, fcntl(fd_pair[1], F_GETFL) | O_NONBLOCK);
    return Pipe { fd_pair[0], fd_pair[1] };
}

}