1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 07:24:58 +00:00
serenity/Userland/Services/RequestServer/Protocol.cpp
Andrew Kaster 4dd2ec68fc RequestServer: Transfer ownership of Protocols to all_protocols map
It's no change in application behavior to have these objects owned by
the function-scope static map in Protocol.cpp, while allowing us to
remove some ugly FIXMEs from time immemorial.
2024-03-06 08:15:03 +00:00

51 lines
1.2 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/HashMap.h>
#include <AK/NonnullOwnPtr.h>
#include <RequestServer/Protocol.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
namespace RequestServer {
static HashMap<ByteString, NonnullOwnPtr<Protocol>>& all_protocols()
{
static HashMap<ByteString, NonnullOwnPtr<Protocol>> map;
return map;
}
Protocol* Protocol::find_by_name(ByteString const& name)
{
return all_protocols().get(name).map([](auto& p) -> Protocol* { return p; }).value_or(nullptr);
}
Protocol::Protocol(ByteString const& name)
: m_name(name)
{
}
ErrorOr<Protocol::Pipe> 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 Error::from_errno(saved_errno);
}
fcntl(fd_pair[1], F_SETFL, fcntl(fd_pair[1], F_GETFL) | O_NONBLOCK);
return Pipe { fd_pair[0], fd_pair[1] };
}
void Protocol::install(NonnullOwnPtr<Protocol> protocol)
{
auto name = protocol->name();
all_protocols().set(move(name), move(protocol));
}
}