|
| | TThreadedNode (TPoller &poller, TMessagesFactory &factory, TResolver &resolver, const std::function< TSocket(const TAddress &)> &socketFactory, const THostPort &hostPort) |
| | Constructs the node and its I/O coroutine; does not connect.
|
| | ~TThreadedNode () |
|
TLock & | ProducerLock () |
| | The lock of the producer side. Hold it across Offset, Acquire and Commit of one frame.
|
| size_t | Segments () const |
| void | Send (TEnvelope &&envelope) override |
| | Serializes envelope and appends it to the outbound buffer.
|
| std::span< char > | Acquire (size_t size) override |
| void | Commit (size_t size) override |
| | Marks size previously acquired bytes as ready to send.
|
| uint64_t | Offset () const override |
| void | StartConnect () override |
| | Initiates an async TCP connection if not already connected or connecting.
|
| void | Drain () override |
| | Calls StartConnect() then asynchronously writes all buffered bytes to the socket.
|
| THostPort | GetHostPort () const override |
| | Returns the remote host:port this node connects to.
|
template<typename TPoller, typename TLock = TNullLock>
class NNet::NActors::TThreadedNode< TPoller, TLock >
INode that writes to its socket on another thread.
The node has two sides:
- Producer side — the thread of the actor system. Send, Acquire, Commit and Offset put frames into a segment in memory, exactly as TNode puts them into its buffer. Drain hands the segment to the I/O side and takes an empty one.
- I/O side — a coroutine on poller, which belongs to another thread (see TLoopThread). It connects, writes the segments to the socket in the order of Drain calls and returns them for reuse. At most MaxSpareSegments written segments are kept, each with the memory of its buffer; the rest are deleted.
The byte stream is the one TNode writes: reconnects with a 1 s back-off, a failed write drops the segment being written, and a reconnected stream starts with a filler frame once a frame with PayloadAlignment has been written (see WriteFiller). Unlike TNode, committed bytes are not sent until the next Drain.
Send defers the serialization of a Near object of a type registered with TMessagesFactory::RegisterPayloadWriter, which states what such a type must allow. The producer side takes the size of its payload and frames it, so the frame keeps its place among the frames written around it and the stream stays that of TNode. The I/O side writes the payload into the frame with SerializeTo before the segment goes to the socket, and destroys the object there; it does not read the factory. All other payloads are serialized in Send, as by TNode. A segment grows without zero-filling (TOutputBuffer): the producer writes the headers, the padding and the other payloads, the I/O side the deferred ones, and every byte of a frame is written before it is sent.
TLock guards the producer side. With the default TNullLock it has one producer thread. With a real lock (std::mutex) any thread may produce: Send, StartConnect and Drain take the lock themselves, and a caller of Offset, Acquire and Commit holds ProducerLock() across the three calls that write one frame.
poller, factory, resolver,
...
io.Stop();
TThreadedNode(TPoller &poller, TMessagesFactory &factory, TResolver &resolver, const std::function< TSocket(const TAddress &)> &socketFactory, const THostPort &hostPort)
Constructs the node and its I/O coroutine; does not connect.
Definition threaded_node.hpp:101
A class representing an IPv4 or IPv6 address (with port).
Definition address.hpp:38
int Domain() const
Gets the domain (address family) of the stored address.
Definition address.cpp:45
A host:port pair that resolves to a TAddress.
Definition resolver.hpp:261
A thread that runs its own event loop.
Definition loop_thread.hpp:61
void Join()
Waits for the loop thread to exit. Owner thread only; does nothing if it is not running.
Definition loop_thread.hpp:131
TPoller & Poller()
The poller of the loop. See the class description for which thread may use it.
Definition loop_thread.hpp:86
void Start(TFailureHook onFailure={})
Starts the loop thread. Call once, from the owner thread.
Definition loop_thread.hpp:99
NNet::TSocket TSocket
Alias for the socket type.
Definition poll.hpp:42
Async DNS resolver over UDP.
Definition resolver.hpp:115
- Template Parameters
-
| TPoller | Poller type of the I/O thread; determines the socket type. |
| TLock | BasicLockable guarding the producer side. |
- Note
- Construct the node before the loop of poller starts and destroy it after that loop has stopped: both touch the poller. resolver and the sockets of socketFactory must belong to the same poller.
-
Not available on Windows: it needs TNotifier.