2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2022-03-03 18:37:49 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-07-17 08:20:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-09-12 09:44:00 +00:00
|
|
|
#include <LibIPC/Connection.h>
|
2022-03-03 18:37:49 +00:00
|
|
|
#include <LibIPC/Stub.h>
|
2019-07-17 08:20:07 +00:00
|
|
|
|
2020-02-05 18:57:18 +00:00
|
|
|
namespace IPC {
|
|
|
|
|
2019-12-02 08:58:25 +00:00
|
|
|
template<typename T, class... Args>
|
|
|
|
NonnullRefPtr<T> new_client_connection(Args&&... args)
|
|
|
|
{
|
|
|
|
return T::construct(forward<Args>(args)...) /* arghs */;
|
|
|
|
}
|
|
|
|
|
2020-09-12 09:44:00 +00:00
|
|
|
template<typename ClientEndpoint, typename ServerEndpoint>
|
2022-02-25 10:18:30 +00:00
|
|
|
class ConnectionFromClient : public Connection<ServerEndpoint, ClientEndpoint>
|
2021-05-03 11:33:59 +00:00
|
|
|
, public ServerEndpoint::Stub
|
|
|
|
, public ClientEndpoint::template Proxy<ServerEndpoint> {
|
2019-12-02 08:58:25 +00:00
|
|
|
public:
|
2021-05-03 06:46:40 +00:00
|
|
|
using ServerStub = typename ServerEndpoint::Stub;
|
2021-06-20 19:10:54 +00:00
|
|
|
using IPCProxy = typename ClientEndpoint::template Proxy<ServerEndpoint>;
|
2021-05-03 06:46:40 +00:00
|
|
|
|
2024-10-22 21:47:33 +00:00
|
|
|
ConnectionFromClient(ServerStub& stub, Transport transport, int client_id)
|
|
|
|
: IPC::Connection<ServerEndpoint, ClientEndpoint>(stub, move(transport))
|
2021-05-03 11:33:59 +00:00
|
|
|
, ClientEndpoint::template Proxy<ServerEndpoint>(*this, {})
|
2019-12-02 08:58:25 +00:00
|
|
|
, m_client_id(client_id)
|
|
|
|
{
|
2024-10-22 21:47:33 +00:00
|
|
|
this->transport().set_up_read_hook([this] {
|
|
|
|
NonnullRefPtr protect = *this;
|
2021-11-07 10:59:43 +00:00
|
|
|
// FIXME: Do something about errors.
|
|
|
|
(void)this->drain_messages_from_peer();
|
2024-10-22 21:47:33 +00:00
|
|
|
});
|
2019-12-02 08:58:25 +00:00
|
|
|
}
|
2019-08-03 17:41:02 +00:00
|
|
|
|
2022-03-03 18:37:49 +00:00
|
|
|
virtual ~ConnectionFromClient() override = default;
|
2019-08-03 17:41:02 +00:00
|
|
|
|
2019-12-02 08:58:25 +00:00
|
|
|
void did_misbehave()
|
|
|
|
{
|
2021-01-31 08:24:46 +00:00
|
|
|
dbgln("{} (id={}) misbehaved, disconnecting.", *this, m_client_id);
|
2020-09-12 09:44:00 +00:00
|
|
|
this->shutdown();
|
2019-12-02 08:58:25 +00:00
|
|
|
}
|
2019-08-03 17:41:02 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
void did_misbehave(char const* message)
|
2019-12-02 14:55:14 +00:00
|
|
|
{
|
2021-01-31 08:24:46 +00:00
|
|
|
dbgln("{} (id={}) misbehaved ({}), disconnecting.", *this, m_client_id, message);
|
2020-09-12 09:44:00 +00:00
|
|
|
this->shutdown();
|
2019-12-02 08:58:25 +00:00
|
|
|
}
|
2019-08-03 17:41:02 +00:00
|
|
|
|
2022-06-10 15:21:00 +00:00
|
|
|
virtual void shutdown_with_error(Error const& error) override
|
2021-11-06 09:43:09 +00:00
|
|
|
{
|
2022-06-10 15:21:00 +00:00
|
|
|
dbgln("{} (id={}) had an error ({}), disconnecting.", *this, m_client_id, error);
|
2021-11-06 09:43:09 +00:00
|
|
|
this->shutdown();
|
|
|
|
}
|
|
|
|
|
2019-12-02 08:58:25 +00:00
|
|
|
int client_id() const { return m_client_id; }
|
2020-06-21 19:20:36 +00:00
|
|
|
|
2021-11-29 16:03:08 +00:00
|
|
|
virtual void die() override = 0;
|
2019-08-03 17:41:02 +00:00
|
|
|
|
2019-12-02 08:58:25 +00:00
|
|
|
private:
|
|
|
|
int m_client_id { -1 };
|
|
|
|
};
|
2020-02-05 18:57:18 +00:00
|
|
|
|
|
|
|
}
|
2021-01-11 20:13:30 +00:00
|
|
|
|
|
|
|
template<typename ClientEndpoint, typename ServerEndpoint>
|
2023-08-06 16:09:39 +00:00
|
|
|
struct AK::Formatter<IPC::ConnectionFromClient<ClientEndpoint, ServerEndpoint>> : Formatter<Core::EventReceiver> {
|
2021-01-11 20:13:30 +00:00
|
|
|
};
|