/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include namespace RequestServer { static HashMap>& all_protocols() { static HashMap> 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::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) { auto name = protocol->name(); all_protocols().set(move(name), move(protocol)); } }