38 operator bool()
const {
39 return !
Part1.empty();
75template<
typename TSocket>
102 char* p =
static_cast<char*
>(data);
104 if (!Buffer.empty()) {
105 size_t toCopy = std::min(size, Buffer.size());
106 std::copy(Buffer.begin(), Buffer.begin() + toCopy, p);
109 Buffer.erase(Buffer.begin(), Buffer.begin() + toCopy);
113 auto readSize =
co_await Socket.
ReadSome(p, size);
115 throw std::runtime_error(
"Connection closed");
127 char tempBuffer[1024];
130 if (!Buffer.empty()) {
131 size_t toCopy = std::min(size, Buffer.size());
132 std::copy(Buffer.begin(), Buffer.begin() + toCopy,
static_cast<char*
>(data));
133 Buffer.erase(Buffer.begin(), Buffer.begin() + toCopy);
137 auto readSize =
co_await Socket.
ReadSome(tempBuffer,
sizeof(tempBuffer));
145 size_t toCopy = std::min(
static_cast<size_t>(readSize), size);
146 std::copy(tempBuffer, tempBuffer + toCopy,
static_cast<char*
>(data));
147 if (
static_cast<size_t>(readSize) > toCopy) {
148 Buffer.insert(Buffer.end(), tempBuffer + toCopy, tempBuffer + readSize);
173 char tempBuffer[1024];
176 auto pos = std::search(Buffer.begin(), Buffer.end(), delimiter.begin(), delimiter.end());
177 if (pos != Buffer.end()) {
178 size_t delimiterOffset = std::distance(Buffer.begin(), pos);
179 result.insert(result.end(), Buffer.begin(), Buffer.begin() + delimiterOffset + delimiter.size());
180 Buffer.erase(Buffer.begin(), Buffer.begin() + delimiterOffset + delimiter.size());
184 result.insert(result.end(), Buffer.begin(), Buffer.end());
187 auto readSize =
co_await Socket.
ReadSome(tempBuffer,
sizeof(tempBuffer));
189 throw std::runtime_error(
"Connection closed");
195 Buffer.insert(Buffer.end(), tempBuffer, tempBuffer + readSize);
203 std::deque<char> Buffer;
238template<
typename TSocket>
263 const char* p =
static_cast<const char*
>(data);
265 auto readSize =
co_await Socket.
WriteSome(p, size);
267 throw std::runtime_error(
"Connection closed");
336template<
typename T,
typename TSocket>
362 size_t size =
sizeof(T);
363 char* p =
reinterpret_cast<char*
>(&res);
365 auto readSize =
co_await Socket.
ReadSome(p, size);
367 throw std::runtime_error(
"Connection closed");
458 void Push(
const char* buf,
size_t size);
466 std::string_view View;
558 std::span<char>
Acquire(
size_t size);
579 void Push(
const char* p,
size_t len);
587 std::string_view View;
625template<
typename TSocket>
638 , Splitter(maxLineSize)
639 , ChunkSize(maxLineSize / 2)
653 auto line = Splitter.
Pop();
655 auto buf = Splitter.
Acquire(ChunkSize);
656 auto size =
co_await Socket.
ReadSome(buf.data(), buf.size());
664 line = Splitter.
Pop();
auto WriteSome(const void *buf, size_t size)
Asynchronously writes data from the provided buffer to the socket.
Definition socket.hpp:189
auto ReadSome(void *buf, size_t size)
Asynchronously reads data from the socket into the provided buffer.
Definition socket.hpp:139
High-level asynchronous socket for network communication.
Definition socket.hpp:367
Implementation of a promise/future system for coroutines.
A utility for reading data from a socket-like object, either a fixed number of bytes or until a speci...
Definition sockutils.hpp:76
TFuture< void > Read(void *data, size_t size)
Reads exactly size bytes and stores them into data.
Definition sockutils.hpp:101
TFuture< std::string > ReadUntil(const std::string &delimiter)
Reads data until the given delimiter is encountered.
Definition sockutils.hpp:170
TByteReader(TSocket &socket)
Constructs a reader for the given socket.
Definition sockutils.hpp:81
A utility for writing data to a socket-like object.
Definition sockutils.hpp:239
TFuture< void > Write(const TLine &line)
Writes a TLine object by sequentially writing its parts.
Definition sockutils.hpp:289
TByteWriter(TSocket &socket)
Constructs a writer for the given socket.
Definition sockutils.hpp:244
TFuture< void > Write(const void *data, size_t size)
Writes exactly size bytes from data to the socket.
Definition sockutils.hpp:262
Owned coroutine handle that carries a result of type T.
Definition corochain.hpp:185
Reads a complete line from a socket using a zero-copy line splitter.
Definition sockutils.hpp:626
TLineReader(TSocket &socket, int maxLineSize=4096)
Constructs a line reader with the given socket and maximum line size.
Definition sockutils.hpp:636
TFuture< TLine > Read()
Reads and returns the next complete line from the socket.
Definition sockutils.hpp:652
Splits incoming data into lines using a circular buffer of fixed capacity.
Definition sockutils.hpp:426
void Push(const char *buf, size_t size)
Appends new data to the circular buffer.
Definition sockutils.cpp:37
TLine Pop()
Retrieves and removes the next complete line from the buffer.
Definition sockutils.cpp:16
A line extracted from a circular read buffer, represented in up to two parts.
Definition sockutils.hpp:30
std::string_view Part2
Continuation segment if the line wraps; empty otherwise.
Definition sockutils.hpp:32
std::string_view Part1
First (or only) segment of the line.
Definition sockutils.hpp:31
A utility for reading a fixed-size structure of type T from a socket-like object.
Definition sockutils.hpp:337
TStructReader(TSocket &socket)
Constructs a reader with a reference to the given socket.
Definition sockutils.hpp:342
TFuture< T > Read()
Reads a single instance of type T from the socket.
Definition sockutils.hpp:360
Splits incoming data into lines using a fixed-size circular buffer, enabling zero-copy writes via Acq...
Definition sockutils.hpp:518
TLine Pop()
Extracts and removes the next complete line from the buffer, if available.
Definition sockutils.cpp:58
void Push(const char *p, size_t len)
(Optional) Copies data from an external buffer into the circular buffer.
Definition sockutils.cpp:97
void Commit(size_t size)
Finalizes the amount of data written into the span returned by Acquire().
Definition sockutils.cpp:92
std::span< char > Acquire(size_t size)
Reserves space in the circular buffer for writing data directly (e.g., from a socket read) without ex...
Definition sockutils.cpp:79