2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-07-17 10:20:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-09-12 11:44:00 +02:00
|
|
|
#include <LibIPC/Connection.h>
|
2019-07-17 10:20:07 +02:00
|
|
|
|
2020-02-05 19:57:18 +01:00
|
|
|
namespace IPC {
|
|
|
|
|
|
2019-12-02 09:58:25 +01:00
|
|
|
template<typename T, class... Args>
|
|
|
|
|
NonnullRefPtr<T> new_client_connection(Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
return T::construct(forward<Args>(args)...) /* arghs */;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 11:44:00 +02:00
|
|
|
template<typename ClientEndpoint, typename ServerEndpoint>
|
2021-05-03 13:33:59 +02:00
|
|
|
class ClientConnection : public Connection<ServerEndpoint, ClientEndpoint>
|
|
|
|
|
, public ServerEndpoint::Stub
|
|
|
|
|
, public ClientEndpoint::template Proxy<ServerEndpoint> {
|
2019-12-02 09:58:25 +01:00
|
|
|
public:
|
2021-05-03 08:46:40 +02:00
|
|
|
using ServerStub = typename ServerEndpoint::Stub;
|
2021-06-20 21:10:54 +02:00
|
|
|
using IPCProxy = typename ClientEndpoint::template Proxy<ServerEndpoint>;
|
2021-05-03 08:46:40 +02:00
|
|
|
|
|
|
|
|
ClientConnection(ServerStub& stub, NonnullRefPtr<Core::LocalSocket> socket, int client_id)
|
|
|
|
|
: IPC::Connection<ServerEndpoint, ClientEndpoint>(stub, move(socket))
|
2021-05-03 13:33:59 +02:00
|
|
|
, ClientEndpoint::template Proxy<ServerEndpoint>(*this, {})
|
2019-12-02 09:58:25 +01:00
|
|
|
, m_client_id(client_id)
|
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(this->socket().is_connected());
|
2021-10-23 22:59:04 +02:00
|
|
|
this->socket().on_ready_to_read = [this] { this->drain_messages_from_peer(); };
|
2019-12-02 09:58:25 +01:00
|
|
|
}
|
2019-08-03 19:41:02 +02:00
|
|
|
|
2020-02-05 19:57:18 +01:00
|
|
|
virtual ~ClientConnection() override
|
2019-12-02 09:58:25 +01:00
|
|
|
{
|
|
|
|
|
}
|
2019-08-03 19:41:02 +02:00
|
|
|
|
2019-12-02 09:58:25 +01:00
|
|
|
void did_misbehave()
|
|
|
|
|
{
|
2021-01-31 09:24:46 +01:00
|
|
|
dbgln("{} (id={}) misbehaved, disconnecting.", *this, m_client_id);
|
2020-09-12 11:44:00 +02:00
|
|
|
this->shutdown();
|
2019-12-02 09:58:25 +01:00
|
|
|
}
|
2019-08-03 19:41:02 +02:00
|
|
|
|
2019-12-02 15:55:14 +01:00
|
|
|
void did_misbehave(const char* message)
|
|
|
|
|
{
|
2021-01-31 09:24:46 +01:00
|
|
|
dbgln("{} (id={}) misbehaved ({}), disconnecting.", *this, m_client_id, message);
|
2020-09-12 11:44:00 +02:00
|
|
|
this->shutdown();
|
2019-12-02 09:58:25 +01:00
|
|
|
}
|
2019-08-03 19:41:02 +02:00
|
|
|
|
2021-11-06 10:43:09 +01:00
|
|
|
void shutdown_with_error(Error const& error)
|
|
|
|
|
{
|
|
|
|
|
dbgln("{} (id={}) had error ({}), disconnecting.", *this, m_client_id, error);
|
|
|
|
|
this->shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-02 09:58:25 +01:00
|
|
|
int client_id() const { return m_client_id; }
|
2020-06-21 21:20:36 +02:00
|
|
|
|
2019-12-02 09:58:25 +01:00
|
|
|
virtual void die() = 0;
|
2019-08-03 19:41:02 +02:00
|
|
|
|
2019-12-02 09:58:25 +01:00
|
|
|
private:
|
|
|
|
|
int m_client_id { -1 };
|
|
|
|
|
};
|
2020-02-05 19:57:18 +01:00
|
|
|
|
|
|
|
|
}
|
2021-01-11 21:13:30 +01:00
|
|
|
|
|
|
|
|
template<typename ClientEndpoint, typename ServerEndpoint>
|
|
|
|
|
struct AK::Formatter<IPC::ClientConnection<ClientEndpoint, ServerEndpoint>> : Formatter<Core::Object> {
|
|
|
|
|
};
|