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/adt/stream_buffer.hpp>
18 : : #include <hpactor/ref/actor_address.hpp>
19 : : #include <hpactor/rpc/rpc_types.hpp>
20 : : #include <hpactor/types/types.hpp>
21 : :
22 : : #include <functional>
23 : : #include <memory>
24 : :
25 : : namespace hpactor {
26 : :
27 : : namespace net {
28 : :
29 : : // -----------------------------------------------------------------------------
30 : : // TransportError - error codes for network operations
31 : : // -----------------------------------------------------------------------------
32 : : enum class TransportError {
33 : : Success = 0,
34 : : ConnectionFailed = 1,
35 : : Timeout = 2,
36 : : SerializationFailed = 3,
37 : : BufferOverflow = 4,
38 : : NotConnected = 5,
39 : : };
40 : :
41 : : // -----------------------------------------------------------------------------
42 : : // ConnectionState - state of a network connection
43 : : // -----------------------------------------------------------------------------
44 : : enum class ConnectionState {
45 : : Disconnected = 0,
46 : : Connecting = 1,
47 : : Handshake = 2,
48 : : Connected = 3,
49 : : Error = 4,
50 : : };
51 : :
52 : : // Forward declarations
53 : : class Connection;
54 : : using ConnectionPtr = std::shared_ptr<Connection>;
55 : : class EventLoop;
56 : :
57 : : // -----------------------------------------------------------------------------
58 : : // Connection callback types
59 : : // -----------------------------------------------------------------------------
60 : : // Callback for when connection becomes ready
61 : : using connection_ready_handler = std::function<void(ConnectionPtr)>;
62 : : // Callback for incoming frames — receives a complete WireFrame envelope
63 : : // (magic + length header + protobuf payload) as an owning StreamBuffer.
64 : : using frame_handler = std::function<void(StreamBuffer)>;
65 : : // Callback for connection errors
66 : : using connection_error_handler = std::function<void(ConnectionPtr, const error&)>;
67 : :
68 : : // -----------------------------------------------------------------------------
69 : : // Connection - represents a connection to a remote node
70 : : // -----------------------------------------------------------------------------
71 : : // Owns the socket fd, local/remote endpoints, and event loop reference.
72 : : // Derived classes implement protocol-specific read/framing via handle_read().
73 : : // -----------------------------------------------------------------------------
74 : : class Connection : public std::enable_shared_from_this<Connection> {
75 : : public:
76 : : Connection(int fd, EndPoint local_endpoint, EndPoint remote_endpoint,
77 : : EventLoop* loop);
78 : : virtual ~Connection();
79 : :
80 : 3 : int fd() const {
81 : 3 : return fd_;
82 : : }
83 : : EndPoint local_endpoint() const {
84 : : return local_endpoint_;
85 : : }
86 : : EndPoint remote_endpoint() const {
87 : : return remote_endpoint_;
88 : : }
89 : 0 : EventLoop* event_loop() const {
90 : 0 : return loop_;
91 : : }
92 : 18 : ConnectionState state() const {
93 : 18 : return state_;
94 : : }
95 : :
96 : : // Send data on this connection
97 : : virtual void send(const StreamBuffer& data) = 0;
98 : :
99 : : // Close this connection
100 : : virtual void close() = 0;
101 : :
102 : : // Protocol-specific read/framing — called when fd is readable
103 : : virtual void handle_read() = 0;
104 : :
105 : : // Handle send completion (called by EventLoop on async_send completion)
106 : : virtual void handle_send_completion(int result);
107 : :
108 : : // Transition to a new state
109 : : void set_state(ConnectionState new_state);
110 : :
111 : : protected:
112 : : int fd_ = -1;
113 : : EndPoint local_endpoint_;
114 : : EndPoint remote_endpoint_;
115 : : EventLoop* loop_ = nullptr;
116 : : ConnectionState state_ = ConnectionState::Disconnected;
117 : : };
118 : :
119 : : // Connection pointer type
120 : : using ConnectionPtr = std::shared_ptr<Connection>;
121 : :
122 : : // -----------------------------------------------------------------------------
123 : : // Transport - abstraction for network communication
124 : : // -----------------------------------------------------------------------------
125 : : // The Transport interface provides connection management and message sending
126 : : // for distributed actor communication. Each Transport is associated with
127 : : // a specific node and handles all outgoing connections to remote nodes.
128 : : // -----------------------------------------------------------------------------
129 : : class Transport {
130 : : public:
131 : 19 : virtual ~Transport() = default;
132 : :
133 : : // Connect to a remote node using explicit host/port (blocking)
134 : : // Returns a Connection pointer on success, nullptr on failure
135 : : virtual ConnectionPtr
136 : : connect(EndPoint remote_endpoint, const std::string& host, uint16_t port) = 0;
137 : :
138 : : // Connect to a remote node using registry lookup (DNS resolution if needed)
139 : : // Returns ConnectionPtr on success, nullptr on failure
140 : : virtual ConnectionPtr connect(EndPoint remote_endpoint) = 0;
141 : :
142 : : // Start listening for incoming connections (non-blocking)
143 : : virtual void listen(uint16_t port) = 0;
144 : :
145 : : // Stop listening
146 : : virtual void stop_listening() = 0;
147 : :
148 : : // Try to send a message to a remote actor.
149 : : // Returns true if the message was accepted by the transport layer
150 : : // (either sent immediately or queued for later delivery).
151 : : // Returns false if the transport cannot accept the message
152 : : // (no connection, queue full, shutting down).
153 : : virtual bool
154 : : try_send(const ActorAddress& target, const StreamBuffer& encoded) = 0;
155 : :
156 : : // Send a message to a remote actor (fire-and-forget).
157 : : // Default implementation calls try_send() and discards the result.
158 : : // The encoded parameter contains the serialized message.
159 : 14 : virtual void send(const ActorAddress& target, const StreamBuffer& encoded) {
160 : 14 : (void)try_send(target, encoded);
161 : 14 : }
162 : :
163 : : // Check if connected to a specific node
164 : : virtual bool is_connected(EndPoint remote_endpoint) const = 0;
165 : :
166 : : // Get this transport's node ID
167 : : virtual EndPoint endpoint() const = 0;
168 : :
169 : : // Close connection to a specific node
170 : : virtual void close_connection(EndPoint remote_endpoint) = 0;
171 : :
172 : : // Set RPC response handler - called when RPC response frames are received
173 : : using rpc_response_handler = std::function<void(const RpcResponseFrame&)>;
174 : : virtual void set_rpc_handler(rpc_response_handler handler) = 0;
175 : : };
176 : :
177 : : } // namespace net
178 : : } // namespace hpactor
|