Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #pragma once
16 : :
17 : : #include <hpactor/net/acceptor.hpp>
18 : : #include <hpactor/net/connection_pool.hpp>
19 : : #include <hpactor/net/event_loop.hpp>
20 : : #include <hpactor/net/registrar.hpp>
21 : : #include <hpactor/net/tls_connection.hpp>
22 : : #include <hpactor/net/tls_context.hpp>
23 : : #include <hpactor/net/transport.hpp>
24 : : #include <hpactor/net/wireframe_connection.hpp>
25 : :
26 : : #include <unordered_map>
27 : :
28 : : namespace hpactor {
29 : :
30 : : namespace net {
31 : :
32 : : // -----------------------------------------------------------------------------
33 : : // TcpTransport - TCP implementation of Transport with TLS and pooling
34 : : // -----------------------------------------------------------------------------
35 : : class TcpTransport : public Transport {
36 : : public:
37 : : TcpTransport(EndPoint endpoint, const TlsConfig& tls_config,
38 : : const PoolConfig& pool_config, NodeRegistry* registry = nullptr);
39 : : ~TcpTransport() override;
40 : :
41 : : // Transport interface
42 : : ConnectionPtr connect(EndPoint remote_endpoint, const std::string& host,
43 : : uint16_t port) override;
44 : :
45 : : ConnectionPtr connect(EndPoint remote_endpoint) override;
46 : :
47 : : // Connect via UNIX domain socket
48 : : // Returns ConnectionPtr on success, nullptr on failure
49 : : ConnectionPtr
50 : : connect_unix_domain(EndPoint remote_endpoint, const std::string& socket_path);
51 : :
52 : : void listen(uint16_t port) override;
53 : : void stop_listening() override;
54 : :
55 : : bool try_send(const ActorAddress& target, const StreamBuffer& encoded) override;
56 : :
57 : : bool is_connected(EndPoint remote_endpoint) const override;
58 : 0 : EndPoint endpoint() const override {
59 : 0 : return endpoint_;
60 : : }
61 : :
62 : : void close_connection(EndPoint remote_endpoint) override;
63 : :
64 : : // Set RPC response handler - propagates to all connection pools
65 : : void set_rpc_handler(rpc_response_handler handler) override;
66 : :
67 : : // Set actor message handler - propagates to all connection pools
68 : 0 : void set_actor_message_handler(std::function<void(const net::WireFrame&)> h) {
69 : 0 : actor_msg_handler_ = std::move(h);
70 : 0 : }
71 : :
72 : : private:
73 : : void handle_accept(int client_fd, EndPoint remote_endpoint);
74 : :
75 : : // Get or create a connection pool for a remote node
76 : : std::shared_ptr<ConnectionPool> get_or_create_pool(EndPoint remote_endpoint);
77 : :
78 : : void register_connection(ConnectionPtr conn, int fd);
79 : : void unregister_connection(int fd);
80 : :
81 : : // Wait for non-blocking connect via EventLoop write_handler, then
82 : : // complete post-connect setup (SO_ERROR check, read registration,
83 : : // TLS handshake start). Returns true on success.
84 : : bool complete_connect(int fd, bool use_tls);
85 : :
86 : : // Derive UDS socket path from node identifier string
87 : : // /tmp/hpactor/<sanitized_node_id>.sock
88 : : std::string derive_uds_path(const std::string& node_id) const;
89 : :
90 : : EndPoint endpoint_;
91 : : EventLoop loop_;
92 : : TcpAcceptor acceptor_;
93 : : TlsContext tls_context_;
94 : : PoolConfig pool_config_;
95 : : NodeRegistry* registry_ = nullptr; // Optional registry for node lookup
96 : : HostResolver host_resolver_;
97 : : std::unordered_map<EndPoint, std::shared_ptr<ConnectionPool>> pools_;
98 : : std::function<void(const RpcResponseFrame&)> rpc_handler_;
99 : : std::function<void(const net::WireFrame&)> actor_msg_handler_;
100 : :
101 : : // Map of fd -> Connection for completion routing
102 : : std::unordered_map<int, ConnectionPtr> connections_;
103 : :
104 : : // Completion callback for async send routing
105 : : std::function<void(OpCompletion)> completion_callback_;
106 : : };
107 : :
108 : : } // namespace net
109 : : } // namespace hpactor
|