2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-11-23 17:45:33 -03:00
|
|
|
#include <AK/HashMap.h>
|
2021-04-23 17:45:52 -03:00
|
|
|
#include <RequestServer/Protocol.h>
|
2021-03-12 13:29:37 -03:00
|
|
|
#include <errno.h>
|
2020-12-30 19:12:44 -03:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <string.h>
|
2021-03-12 13:29:37 -03:00
|
|
|
#include <unistd.h>
|
2019-11-23 17:45:33 -03:00
|
|
|
|
2021-04-23 17:45:52 -03:00
|
|
|
namespace RequestServer {
|
2020-05-17 11:33:09 -03:00
|
|
|
|
2019-11-23 17:45:33 -03:00
|
|
|
static HashMap<String, Protocol*>& all_protocols()
|
|
|
|
|
{
|
|
|
|
|
static HashMap<String, Protocol*> map;
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
Protocol* Protocol::find_by_name(String const& name)
|
2019-11-23 17:45:33 -03:00
|
|
|
{
|
|
|
|
|
return all_protocols().get(name).value_or(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
Protocol::Protocol(String const& name)
|
2019-11-23 17:45:33 -03:00
|
|
|
{
|
|
|
|
|
all_protocols().set(name, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Protocol::~Protocol()
|
|
|
|
|
{
|
2021-09-05 14:31:39 -03:00
|
|
|
// FIXME: Do proper de-registration.
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-11-23 17:45:33 -03:00
|
|
|
}
|
2020-05-17 11:33:09 -03:00
|
|
|
|
2021-11-07 07:39:36 -03:00
|
|
|
ErrorOr<Protocol::Pipe> Protocol::get_pipe_for_request()
|
2020-12-30 19:12:44 -03:00
|
|
|
{
|
|
|
|
|
int fd_pair[2] { 0 };
|
|
|
|
|
if (pipe(fd_pair) != 0) {
|
|
|
|
|
auto saved_errno = errno;
|
|
|
|
|
dbgln("Protocol: pipe() failed: {}", strerror(saved_errno));
|
2021-11-07 07:39:36 -03:00
|
|
|
return Error::from_errno(saved_errno);
|
2020-12-30 19:12:44 -03:00
|
|
|
}
|
|
|
|
|
fcntl(fd_pair[1], F_SETFL, fcntl(fd_pair[1], F_GETFL) | O_NONBLOCK);
|
|
|
|
|
return Pipe { fd_pair[0], fd_pair[1] };
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-17 11:33:09 -03:00
|
|
|
}
|