7#include <PhosphorChannel/phosphorchannel_export.h>
70 std::uint32_t
id()
const
74 const std::string&
codec()
const
101 std::size_t m_window = 0;
102 std::size_t m_credit = 0;
103 std::deque<std::string> m_queue;
104 std::size_t m_sent = 0;
105 bool m_granted =
false;
106 bool m_closing =
false;
107 bool m_closeSent =
false;
108 bool m_released =
false;
109 bool m_blocked =
false;
121 std::uint32_t
id()
const
145 std::deque<std::string> m_frames;
146 std::string m_partial;
147 std::size_t m_peerCredit = 0;
148 std::size_t m_unreturned = 0;
149 std::size_t m_creditToSend = 0;
150 bool m_inFrame =
false;
151 bool m_closed =
false;
152 bool m_released =
false;
172 std::size_t
window = std::size_t(4) << 20;
276 return m_error.empty() && !m_ended;
288 enum class Farewell {
292 enum class EventKind {
303 bool canWrite()
const
305 return m_error.empty() && !m_outputEnded;
307 void fail(std::string reason, Farewell farewell = Farewell::Send);
308 void refusePeer(std::string reason);
312 bool handlePacket(std::uint8_t type, std::uint8_t flags, std::uint32_t
id, std::string_view payload);
313 bool handleHello(std::uint8_t type, std::string_view payload);
314 std::string helloMissing()
const;
316 void queueControl(std::uint8_t type, std::uint32_t
id, std::string_view payload);
318 void returnCredit(StreamReader& reader, std::size_t bytes);
319 void release(StreamReader& reader);
321 long writeOut(
const char* bytes, std::size_t size);
326 int m_readFlags = -1;
327 int m_writeFlags = -1;
328 bool m_writeIsSocket =
false;
329 std::unique_ptr<char[]> m_readBuffer;
331 std::size_t m_inHead = 0;
333 std::size_t m_outHead = 0;
334 std::string m_control;
335 std::set<std::uint32_t> m_creditDue;
336 std::set<std::uint32_t> m_releaseDue;
337 std::set<std::uint32_t> m_sending;
338 std::map<std::uint32_t, std::unique_ptr<StreamWriter>> m_writers;
339 std::map<std::uint32_t, std::unique_ptr<StreamReader>> m_readers;
340 std::deque<Event> m_events;
341 std::shared_ptr<bool> m_alive;
342 std::uint32_t m_nextTurn = 0;
343 std::size_t m_openReaders = 0;
344 std::size_t m_openWriters = 0;
345 std::size_t m_peerMaxChannels = 0;
346 std::uint32_t m_peerMaxChannelId = 0;
347 bool m_ready =
false;
348 bool m_helloScheduled =
false;
349 bool m_helloWritten =
false;
350 bool m_ended =
false;
351 bool m_outputEnded =
false;
352 bool m_peerBroke =
false;
std::uint32_t id() const
Definition Stream.h:121
StreamReader & operator=(const StreamReader &)=delete
const std::string & codec() const
Definition Stream.h:125
StreamReader(const StreamReader &)=delete
ReadResult read(std::string &frame)
The next complete frame, without waiting.
friend class Stream
Definition Stream.h:139
std::uint32_t id() const
Definition Stream.h:70
WriteResult write(std::string_view frame, int timeoutMs)
Waits up to timeoutMs (-1: no limit) for the window, and runs the stream meanwhile,...
StreamWriter(const StreamWriter &)=delete
const std::string & codec() const
Definition Stream.h:74
std::size_t maxFrame() const
The largest frame: the window less Stream::FrameCost.
StreamWriter & operator=(const StreamWriter &)=delete
friend class Stream
Definition Stream.h:95
WriteResult tryWrite(std::string_view frame)
Queues one frame without waiting.
void close()
No frame follows. The queued ones still go out.
bool flush(int timeoutMs)
Writes until nothing waits, up to timeoutMs, and reads meanwhile.
StreamWriter * openWriter(std::uint32_t id, std::string codec, std::string *error)
Opens channel id for writing.
friend class StreamWriter
Definition Stream.h:285
int readFd() const
Definition Stream.h:232
Stream & operator=(const Stream &)=delete
bool pump(int timeoutMs)
One round of poll on readFd(), and on writeFd() while wantsWrite(), up to timeoutMs.
friend class StreamReader
Definition Stream.h:286
static constexpr std::size_t MinWindow
The smallest window.
Definition Stream.h:162
bool readable()
Reads what the fd has, up to a budget, and handles it.
bool open() const
The stream works: no failure and no end of its input.
Definition Stream.h:274
StreamReader * reader(std::uint32_t id)
The channel id that the other end opened, or nullptr.
int writeFd() const
-1 once the stream gave back an output fd separate from its input: after shutdown() got past its flus...
Definition Stream.h:244
bool shutdown(int timeoutMs)
Ends the stream cleanly: flushes, ends this end's output, and reads until the other end ends too,...
std::function< void(StreamReader &)> onReadable
A frame arrived on a reader, its writer closed it, or the stream ended or failed: read it until read(...
Definition Stream.h:228
static constexpr std::size_t FrameCost
What a frame costs against the window on top of its size, so that a window also bounds the number of ...
Definition Stream.h:160
bool ready() const
The hello of the other end arrived, with this end's version.
Definition Stream.h:268
std::function< void(StreamWriter &)> onWritable
A writer that returned WouldBlock may write again.
Definition Stream.h:230
Stream(int readFd, int writeFd, Options options)
Takes readFd and writeFd (the same fd for a socket), makes them non-blocking, and queues the hello.
Stream(int fd)
Definition Stream.h:192
bool writable()
Writes what waits. False when the stream failed.
const std::string & error() const
Why the stream failed. Empty while it works, and after a clean end.
Definition Stream.h:279
std::function< void(StreamReader &)> onReader
The other end opened a channel.
Definition Stream.h:219
Stream(const Stream &)=delete
WriteResult
Definition Channel.h:43
ReadResult
Definition Channel.h:56
std::uint32_t maxChannelId
The ids of the channels that the other end opens are below this.
Definition Stream.h:182
std::size_t maxChannels
The most channels that the other end may have open at once.
Definition Stream.h:177
std::size_t fragment
The largest fragment that this end sends: 1 KiB to 1 MiB.
Definition Stream.h:174
std::size_t window
The window, in bytes, that this end grants each channel that it reads.
Definition Stream.h:172