| 
									
										
										
										
											2020-09-12 11:44:00 +02: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-09-12 11:44:00 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							|  |  |  | #include <AK/NonnullOwnPtrVector.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-23 21:48:42 +02:00
										 |  |  | #include <AK/Result.h>
 | 
					
						
							|  |  |  | #include <AK/Try.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  | #include <LibCore/Event.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-12 13:03:39 +02:00
										 |  |  | #include <LibCore/EventLoop.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  | #include <LibCore/LocalSocket.h>
 | 
					
						
							|  |  |  | #include <LibCore/Notifier.h>
 | 
					
						
							|  |  |  | #include <LibCore/Timer.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-23 21:48:42 +02:00
										 |  |  | #include <LibIPC/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  | #include <LibIPC/Message.h>
 | 
					
						
							| 
									
										
										
										
											2021-04-26 19:09:41 +02:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-21 21:55:00 +03:00
										 |  |  | #include <stdint.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <sys/socket.h>
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace IPC { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-23 21:48:42 +02:00
										 |  |  | class ConnectionBase : public Core::Object { | 
					
						
							|  |  |  |     C_OBJECT_ABSTRACT(ConnectionBase); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-10-23 21:48:42 +02:00
										 |  |  |     virtual ~ConnectionBase() override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool is_open() const { return m_socket->is_open(); } | 
					
						
							|  |  |  |     void post_message(Message const&); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void shutdown(); | 
					
						
							|  |  |  |     virtual void die() { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2021-10-23 22:59:04 +02:00
										 |  |  |     explicit ConnectionBase(IPC::Stub&, NonnullRefPtr<Core::LocalSocket>, u32 local_endpoint_magic); | 
					
						
							| 
									
										
										
										
											2021-10-23 21:48:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Core::LocalSocket& socket() { return *m_socket; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void may_have_become_unresponsive() { } | 
					
						
							|  |  |  |     virtual void did_become_responsive() { } | 
					
						
							| 
									
										
										
										
											2021-10-23 22:16:53 +02:00
										 |  |  |     virtual void try_parse_messages(Vector<u8> const& bytes, size_t& index) = 0; | 
					
						
							| 
									
										
										
										
											2021-10-23 21:48:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-23 22:59:04 +02:00
										 |  |  |     OwnPtr<IPC::Message> wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id); | 
					
						
							| 
									
										
										
										
											2021-10-23 21:48:42 +02:00
										 |  |  |     void wait_for_socket_to_become_readable(); | 
					
						
							|  |  |  |     Result<Vector<u8>, bool> read_as_much_as_possible_from_socket_without_blocking(); | 
					
						
							| 
									
										
										
										
											2021-10-23 22:59:04 +02:00
										 |  |  |     bool drain_messages_from_peer(); | 
					
						
							| 
									
										
										
										
											2021-10-23 22:16:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-23 21:48:42 +02:00
										 |  |  |     void post_message(MessageBuffer); | 
					
						
							| 
									
										
										
										
											2021-10-23 22:59:04 +02:00
										 |  |  |     void handle_messages(); | 
					
						
							| 
									
										
										
										
											2021-10-23 21:48:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     IPC::Stub& m_local_stub; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NonnullRefPtr<Core::LocalSocket> m_socket; | 
					
						
							|  |  |  |     RefPtr<Core::Timer> m_responsiveness_timer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     RefPtr<Core::Notifier> m_notifier; | 
					
						
							|  |  |  |     NonnullOwnPtrVector<Message> m_unprocessed_messages; | 
					
						
							|  |  |  |     ByteBuffer m_unprocessed_bytes; | 
					
						
							| 
									
										
										
										
											2021-10-23 22:59:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     u32 m_local_endpoint_magic { 0 }; | 
					
						
							| 
									
										
										
										
											2021-10-23 21:48:42 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-05-03 08:46:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-23 21:48:42 +02:00
										 |  |  | template<typename LocalEndpoint, typename PeerEndpoint> | 
					
						
							|  |  |  | class Connection : public ConnectionBase { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     Connection(IPC::Stub& local_stub, NonnullRefPtr<Core::LocalSocket> socket) | 
					
						
							| 
									
										
										
										
											2021-10-23 22:59:04 +02:00
										 |  |  |         : ConnectionBase(local_stub, move(socket), LocalEndpoint::static_magic()) | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-09-12 13:03:39 +02:00
										 |  |  |         m_notifier->on_ready_to_read = [this] { | 
					
						
							| 
									
										
										
										
											2021-02-20 11:07:29 +01:00
										 |  |  |             NonnullRefPtr protect = *this; | 
					
						
							| 
									
										
										
										
											2021-10-23 22:59:04 +02:00
										 |  |  |             drain_messages_from_peer(); | 
					
						
							|  |  |  |             handle_messages(); | 
					
						
							| 
									
										
										
										
											2020-09-12 13:03:39 +02:00
										 |  |  |         }; | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename MessageType> | 
					
						
							|  |  |  |     OwnPtr<MessageType> wait_for_specific_message() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return wait_for_specific_endpoint_message<MessageType, LocalEndpoint>(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename RequestType, typename... Args> | 
					
						
							| 
									
										
										
										
											2021-04-20 10:53:31 +03:00
										 |  |  |     NonnullOwnPtr<typename RequestType::ResponseType> send_sync(Args&&... args) | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         post_message(RequestType(forward<Args>(args)...)); | 
					
						
							|  |  |  |         auto response = wait_for_specific_endpoint_message<typename RequestType::ResponseType, PeerEndpoint>(); | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(response); | 
					
						
							| 
									
										
										
										
											2021-04-20 10:53:31 +03:00
										 |  |  |         return response.release_nonnull(); | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-20 11:07:29 +01:00
										 |  |  |     template<typename RequestType, typename... Args> | 
					
						
							|  |  |  |     OwnPtr<typename RequestType::ResponseType> send_sync_but_allow_failure(Args&&... args) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         post_message(RequestType(forward<Args>(args)...)); | 
					
						
							|  |  |  |         return wait_for_specific_endpoint_message<typename RequestType::ResponseType, PeerEndpoint>(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  | protected: | 
					
						
							|  |  |  |     template<typename MessageType, typename Endpoint> | 
					
						
							|  |  |  |     OwnPtr<MessageType> wait_for_specific_endpoint_message() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-10-23 22:59:04 +02:00
										 |  |  |         if (auto message = wait_for_specific_endpoint_message_impl(Endpoint::static_magic(), MessageType::static_message_id())) | 
					
						
							| 
									
										
										
										
											2021-10-23 22:21:47 +02:00
										 |  |  |             return message.template release_nonnull<MessageType>(); | 
					
						
							| 
									
										
										
										
											2021-01-10 16:29:28 -07:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-23 22:16:53 +02:00
										 |  |  |     virtual void try_parse_messages(Vector<u8> const& bytes, size_t& index) override | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-05-24 09:04:22 +02:00
										 |  |  |         u32 message_size = 0; | 
					
						
							| 
									
										
										
										
											2020-11-21 21:55:00 +03:00
										 |  |  |         for (; index + sizeof(message_size) < bytes.size(); index += message_size) { | 
					
						
							| 
									
										
										
										
											2021-05-24 09:04:22 +02:00
										 |  |  |             memcpy(&message_size, bytes.data() + index, sizeof(message_size)); | 
					
						
							| 
									
										
										
										
											2020-11-21 21:55:00 +03:00
										 |  |  |             if (message_size == 0 || bytes.size() - index - sizeof(uint32_t) < message_size) | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             index += sizeof(message_size); | 
					
						
							| 
									
										
										
										
											2021-08-11 11:31:58 +02:00
										 |  |  |             auto remaining_bytes = ReadonlyBytes { bytes.data() + index, message_size }; | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  |             if (auto message = LocalEndpoint::decode_message(remaining_bytes, m_socket->fd())) { | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  |                 m_unprocessed_messages.append(message.release_nonnull()); | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  |             } else if (auto message = PeerEndpoint::decode_message(remaining_bytes, m_socket->fd())) { | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  |                 m_unprocessed_messages.append(message.release_nonnull()); | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2020-11-21 21:55:00 +03:00
										 |  |  |                 dbgln("Failed to parse a message"); | 
					
						
							| 
									
										
										
										
											2020-10-25 11:37:42 +01:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-11-21 21:55:00 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-01-11 21:13:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<typename LocalEndpoint, typename PeerEndpoint> | 
					
						
							|  |  |  | struct AK::Formatter<IPC::Connection<LocalEndpoint, PeerEndpoint>> : Formatter<Core::Object> { | 
					
						
							|  |  |  | }; |