19 std::string_view Part1;
20 std::string_view Part2;
23 return Part1.size() + Part2.size();
26 operator bool()
const {
27 return !Part1.empty();
63template<
typename TSocket>
90 char* p =
static_cast<char*
>(data);
92 if (!Buffer.empty()) {
93 size_t toCopy = std::min(size, Buffer.size());
94 std::copy(Buffer.begin(), Buffer.begin() + toCopy, p);
97 Buffer.erase(Buffer.begin(), Buffer.begin() + toCopy);
101 auto readSize =
co_await Socket.
ReadSome(p, size);
103 throw std::runtime_error(
"Connection closed");
115 char tempBuffer[1024];
118 if (!Buffer.empty()) {
119 size_t toCopy = std::min(size, Buffer.size());
120 std::copy(Buffer.begin(), Buffer.begin() + toCopy,
static_cast<char*
>(data));
121 Buffer.erase(Buffer.begin(), Buffer.begin() + toCopy);
125 auto readSize =
co_await Socket.
ReadSome(tempBuffer,
sizeof(tempBuffer));
133 size_t toCopy = std::min(
static_cast<size_t>(readSize), size);
134 std::copy(tempBuffer, tempBuffer + toCopy,
static_cast<char*
>(data));
135 if (
static_cast<size_t>(readSize) > toCopy) {
136 Buffer.insert(Buffer.end(), tempBuffer + toCopy, tempBuffer + readSize);
161 char tempBuffer[1024];
164 auto pos = std::search(Buffer.begin(), Buffer.end(), delimiter.begin(), delimiter.end());
165 if (pos != Buffer.end()) {
166 size_t delimiterOffset = std::distance(Buffer.begin(), pos);
167 result.insert(result.end(), Buffer.begin(), Buffer.begin() + delimiterOffset + delimiter.size());
168 Buffer.erase(Buffer.begin(), Buffer.begin() + delimiterOffset + delimiter.size());
172 result.insert(result.end(), Buffer.begin(), Buffer.end());
175 auto readSize =
co_await Socket.
ReadSome(tempBuffer,
sizeof(tempBuffer));
177 throw std::runtime_error(
"Connection closed");
183 Buffer.insert(Buffer.end(), tempBuffer, tempBuffer + readSize);
191 std::deque<char> Buffer;
226template<
typename TSocket>
251 const char* p =
static_cast<const char*
>(data);
253 auto readSize =
co_await Socket.
WriteSome(p, size);
255 throw std::runtime_error(
"Connection closed");
278 co_await Write(line.Part1.data(), line.Part1.size());
279 co_await Write(line.Part2.data(), line.Part2.size());
324template<
typename T,
typename TSocket>
350 size_t size =
sizeof(T);
351 char* p =
reinterpret_cast<char*
>(&res);
353 auto readSize =
co_await Socket.
ReadSome(p, size);
355 throw std::runtime_error(
"Connection closed");
446 void Push(
const char* buf,
size_t size);
454 std::string_view View;
546 std::span<char>
Acquire(
size_t size);
567 void Push(
const char* p,
size_t len);
575 std::string_view View;
613template<
typename TSocket>
626 , Splitter(maxLineSize)
627 , ChunkSize(maxLineSize / 2)
641 auto line = Splitter.
Pop();
643 auto buf = Splitter.
Acquire(ChunkSize);
644 auto size =
co_await Socket.
ReadSome(buf.data(), buf.size());
652 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:64
TFuture< void > Read(void *data, size_t size)
Reads exactly size bytes and stores them into data.
Definition sockutils.hpp:89
TFuture< std::string > ReadUntil(const std::string &delimiter)
Reads data until the given delimiter is encountered.
Definition sockutils.hpp:158
TByteReader(TSocket &socket)
Constructs a reader for the given socket.
Definition sockutils.hpp:69
A utility for writing data to a socket-like object.
Definition sockutils.hpp:227
TFuture< void > Write(const TLine &line)
Writes a TLine object by sequentially writing its parts.
Definition sockutils.hpp:277
TByteWriter(TSocket &socket)
Constructs a writer for the given socket.
Definition sockutils.hpp:232
TFuture< void > Write(const void *data, size_t size)
Writes exactly size bytes from data to the socket.
Definition sockutils.hpp:250
Future type for coroutines returning a value of type T.
Definition corochain.hpp:182
Reads a complete line from a socket using a zero-copy line splitter.
Definition sockutils.hpp:614
TLineReader(TSocket &socket, int maxLineSize=4096)
Constructs a line reader with the given socket and maximum line size.
Definition sockutils.hpp:624
TFuture< TLine > Read()
Reads and returns the next complete line from the socket.
Definition sockutils.hpp:640
Splits incoming data into lines using a circular buffer of fixed capacity.
Definition sockutils.hpp:414
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
Definition sockutils.hpp:18
A utility for reading a fixed-size structure of type T from a socket-like object.
Definition sockutils.hpp:325
TStructReader(TSocket &socket)
Constructs a reader with a reference to the given socket.
Definition sockutils.hpp:330
TFuture< T > Read()
Reads a single instance of type T from the socket.
Definition sockutils.hpp:348
Splits incoming data into lines using a fixed-size circular buffer, enabling zero-copy writes via Acq...
Definition sockutils.hpp:506
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