COROIO: coroio/actors/node.hpp Source File
COROIO
Loading...
Searching...
No Matches
node.hpp
1#pragma once
2
3#include "actor.hpp"
4#include "envelope_reader.hpp"
5#include "messages_factory.hpp"
6
7#include <coroio/address.hpp>
8#include <coroio/dns/resolver.hpp>
9
10#include <span>
11
12namespace NNet {
13namespace NActors {
14
23public:
24 virtual ~IOutputStream() = default;
27 virtual std::span<char> Acquire(size_t size) = 0;
29 virtual void Commit(size_t size) = 0;
30};
31
39class INode : public IOutputStream {
40public:
41 virtual ~INode() = default;
43 virtual void Send(TEnvelope&& envelope) = 0;
45 virtual void StartConnect() = 0;
47 virtual void Drain() = 0;
49 virtual THostPort GetHostPort() const = 0;
50};
51
61template<typename TPoller>
62class TNode final : public INode {
63public:
64 using TSocket = typename TPoller::TSocket;
76 TNode(TPoller& poller, TMessagesFactory& factory, TResolver& resolver, const std::function<TSocket(const TAddress&)>& socketFactory, const THostPort& hostPort)
77 : Poller(poller)
78 , Factory(factory)
79 , Resolver(resolver)
80 , SocketFactory(socketFactory)
81 , HostPort(hostPort)
82 { }
83
84 // TODO: remove me, envelope needed only for local actors
85 void Send(TEnvelope&& envelope) override {
86 auto blob = std::move(envelope.Blob);
87 // TODO: a blob already in Far form (a received non-POD message Forward-ed to another
88 // node) is serialized again as if it held a T object; pass such blobs through as bytes.
89 if (blob.Size > 0) {
90 blob = Factory.SerializeFar(envelope.MessageId, std::move(blob));
91 }
92 THeader data{
93 .Sender = envelope.Sender,
94 .Recipient = envelope.Recipient,
95 .MessageId = envelope.MessageId,
96 .Size = blob.Size
97 };
98 auto buf = Acquire(sizeof(data) + blob.Size);
99 std::memcpy(buf.data(), &data, sizeof(data));
100 if (blob.Size > 0) {
101 std::memcpy(buf.data() + sizeof(data), blob.Data.get(), blob.Size);
102 }
103 Commit(sizeof(data) + blob.Size);
104 }
105
106 std::span<char> Acquire(size_t size) override {
107 if (CommittedBytes + size > OutputBuffer.size()) {
108 OutputBuffer.resize(CommittedBytes + size);
109 }
110 return std::span<char>(OutputBuffer.data() + CommittedBytes, size);
111 }
112
113 void Commit(size_t size) override {
114 CommittedBytes += size;
115 }
116
117 void StartConnect() override {
118 if (!Connected) {
119 Connect();
120 return;
121 }
122 }
123
124 void Drain() override {
125 StartConnect();
126 if (Connected && (!Drainer.raw() || Drainer.done())) {
127 Drainer = DoDrain();
128 }
129 }
130
131 THostPort GetHostPort() const override {
132 return HostPort;
133 }
134
135private:
136 TFuture<void> DoDrain() {
137 try {
138 while (CommittedBytes > 0) {
139 SendBuffer.swap(OutputBuffer);
140 auto readSize = CommittedBytes;
141 CommittedBytes = 0;
142 co_await TByteWriter(Socket).Write(SendBuffer.data(), readSize);
143 }
144 co_return;
145 } catch (const std::exception& ex) {
146 std::cerr << "Error during draining: " << ex.what() << "\n";
147 Connect();
148 co_return;
149 }
150 }
151
152 void Connect() {
153 if (Connector.raw() && !Connector.done()) {
154 return;
155 }
156
157 Connector = DoConnect();
158 }
159
160 TFuture<void> DoConnect() {
161 Connected = false;
162 std::cout << "Connecting to " << HostPort.ToString() << "\n";
163 while (!Connected) {
164 try {
165 //auto deadline = NNet::TClock::now() + std::chrono::milliseconds(5000);
166 TAddress addr = co_await HostPort.Resolve(Resolver);
167 Socket = SocketFactory(addr);
168 // Small messages (acks) must not wait for Nagle and delayed ACK.
169 Socket.SetNoDelay();
170 co_await Socket.Connect(addr /*, deadline*/);
171 Connected = true;
172 std::cout << "Connected to " << HostPort.ToString() << "\n";
173 } catch (const std::exception& ex) {
174 std::cerr << "Error connecting to " << HostPort.ToString() << ": " << ex.what() << "\n";
175 Connected = false;
176 }
177 if (!Connected) {
178 co_await Poller.Sleep(std::chrono::milliseconds(1000));
179 }
180 }
181 co_return;
182 }
183
184 bool Connected = false;
185 TFuture<void> Drainer;
186 TFuture<void> Connector;
187 TSocket Socket;
188
189 TPoller& Poller;
190 TMessagesFactory& Factory;
191 TResolver& Resolver;
192 std::function<TSocket(TAddress&)> SocketFactory;
193 THostPort HostPort;
194 std::vector<char> OutputBuffer;
195 size_t CommittedBytes = 0;
196 std::vector<char> SendBuffer;
197};
198
199} // namespace NActors
200} // namespace NNet
Actor system implementation with message passing and behavior support.
Interface for a remote node connection in the actor transport layer.
Definition node.hpp:39
virtual void Drain()=0
Calls StartConnect() then asynchronously writes all buffered bytes to the socket.
virtual void Send(TEnvelope &&envelope)=0
Serializes envelope and appends it to the outbound buffer.
virtual THostPort GetHostPort() const =0
Returns the remote host:port this node connects to.
virtual void StartConnect()=0
Initiates an async TCP connection if not already connected or connecting.
Abstract write-buffer interface used by the actor transport layer.
Definition node.hpp:22
virtual std::span< char > Acquire(size_t size)=0
virtual void Commit(size_t size)=0
Marks size previously acquired bytes as ready to send.
Message envelope containing routing and payload information.
Definition actor.hpp:122
Definition messages_factory.hpp:10
THostPort GetHostPort() const override
Returns the remote host:port this node connects to.
Definition node.hpp:131
void StartConnect() override
Initiates an async TCP connection if not already connected or connecting.
Definition node.hpp:117
void Send(TEnvelope &&envelope) override
Serializes envelope and appends it to the outbound buffer.
Definition node.hpp:85
std::span< char > Acquire(size_t size) override
Definition node.hpp:106
void Commit(size_t size) override
Marks size previously acquired bytes as ready to send.
Definition node.hpp:113
TNode(TPoller &poller, TMessagesFactory &factory, TResolver &resolver, const std::function< TSocket(const TAddress &)> &socketFactory, const THostPort &hostPort)
Constructs a TNode but does not connect immediately.
Definition node.hpp:76
void Drain() override
Calls StartConnect() then asynchronously writes all buffered bytes to the socket.
Definition node.hpp:124
A class representing an IPv4 or IPv6 address (with port).
Definition address.hpp:38
A host:port pair that resolves to a TAddress.
Definition resolver.hpp:261
Async DNS resolver over UDP.
Definition resolver.hpp:115
Wire header prepended to every remote actor message.
Definition actorid.hpp:100
A utility for writing data to a socket-like object.
Definition sockutils.hpp:245
TFuture< void > Write(const void *data, size_t size)
Writes exactly size bytes from data to the socket.
Definition sockutils.hpp:268
Owned coroutine handle that carries a result of type T.
Definition corochain.hpp:185