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::memcpy(p, Buffer.data(), toCopy);
97 Buffer.erase(0, toCopy);
101 auto readSize =
co_await Socket.
ReadSome(p, size);
103 throw std::runtime_error(
"Connection closed");
133 char tempBuffer[1024];
136 auto pos = std::search(Buffer.begin(), Buffer.end(), delimiter.begin(), delimiter.end());
137 if (pos != Buffer.end()) {
138 size_t delimiterOffset = std::distance(Buffer.begin(), pos);
139 result.append(Buffer.substr(0, delimiterOffset + delimiter.size()));
140 Buffer.erase(0, delimiterOffset + delimiter.size());
144 result.append(Buffer);
147 auto readSize =
co_await Socket.
ReadSome(tempBuffer,
sizeof(tempBuffer));
149 throw std::runtime_error(
"Connection closed");
155 Buffer.append(tempBuffer, readSize);
198template<
typename TSocket>
223 const char* p =
static_cast<const char*
>(data);
225 auto readSize =
co_await Socket.
WriteSome(p, size);
227 throw std::runtime_error(
"Connection closed");
250 co_await Write(line.Part1.data(), line.Part1.size());
251 co_await Write(line.Part2.data(), line.Part2.size());
296template<
typename T,
typename TSocket>
322 size_t size =
sizeof(T);
323 char* p =
reinterpret_cast<char*
>(&res);
325 auto readSize =
co_await Socket.
ReadSome(p, size);
327 throw std::runtime_error(
"Connection closed");
418 void Push(
const char* buf,
size_t size);
426 std::string_view View;
518 std::span<char>
Acquire(
size_t size);
539 void Push(
const char* p,
size_t len);
547 std::string_view View;
585template<
typename TSocket>
598 , Splitter(maxLineSize)
599 , ChunkSize(maxLineSize / 2)
613 auto line = Splitter.
Pop();
615 auto buf = Splitter.
Acquire(ChunkSize);
616 auto size =
co_await Socket.
ReadSome(buf.data(), buf.size());
624 line = Splitter.
Pop();
auto WriteSome(const void *buf, size_t size)
Asynchronously writes data from the provided buffer to the socket.
Definition socket.hpp:186
auto ReadSome(void *buf, size_t size)
Asynchronously reads data from the socket into the provided buffer.
Definition socket.hpp:138
High-level asynchronous socket for network communication.
Definition socket.hpp:364
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:130
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:199
TFuture< void > Write(const TLine &line)
Writes a TLine object by sequentially writing its parts.
Definition sockutils.hpp:249
TByteWriter(TSocket &socket)
Constructs a writer for the given socket.
Definition sockutils.hpp:204
TFuture< void > Write(const void *data, size_t size)
Writes exactly size bytes from data to the socket.
Definition sockutils.hpp:222
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:586
TLineReader(TSocket &socket, int maxLineSize=4096)
Constructs a line reader with the given socket and maximum line size.
Definition sockutils.hpp:596
TFuture< TLine > Read()
Reads and returns the next complete line from the socket.
Definition sockutils.hpp:612
Splits incoming data into lines using a circular buffer of fixed capacity.
Definition sockutils.hpp:386
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:297
TStructReader(TSocket &socket)
Constructs a reader with a reference to the given socket.
Definition sockutils.hpp:302
TFuture< T > Read()
Reads a single instance of type T from the socket.
Definition sockutils.hpp:320
Splits incoming data into lines using a fixed-size circular buffer, enabling zero-copy writes via Acq...
Definition sockutils.hpp:478
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