10 std::string_view Part1;
11 std::string_view Part2;
14 return Part1.size() + Part2.size();
17 operator bool()
const {
18 return !Part1.empty();
54template<
typename TSocket>
81 char* p =
static_cast<char*
>(data);
83 if (!Buffer.empty()) {
84 size_t toCopy = std::min(size, Buffer.size());
85 std::memcpy(p, Buffer.data(), toCopy);
88 Buffer.erase(0, toCopy);
92 auto readSize =
co_await Socket.ReadSome(p, size);
94 throw std::runtime_error(
"Connection closed");
124 char tempBuffer[1024];
127 auto pos = std::search(Buffer.begin(), Buffer.end(), delimiter.begin(), delimiter.end());
128 if (pos != Buffer.end()) {
129 size_t delimiterOffset = std::distance(Buffer.begin(), pos);
130 result.append(Buffer.substr(0, delimiterOffset + delimiter.size()));
131 Buffer.erase(0, delimiterOffset + delimiter.size());
135 result.append(Buffer);
138 auto readSize =
co_await Socket.ReadSome(tempBuffer,
sizeof(tempBuffer));
140 throw std::runtime_error(
"Connection closed");
146 Buffer.append(tempBuffer, readSize);
189template<
typename TSocket>
214 const char* p =
static_cast<const char*
>(data);
216 auto readSize =
co_await Socket.WriteSome(p, size);
218 throw std::runtime_error(
"Connection closed");
241 co_await Write(line.Part1.data(), line.Part1.size());
242 co_await Write(line.Part2.data(), line.Part2.size());
287template<
typename T,
typename TSocket>
313 size_t size =
sizeof(T);
314 char* p =
reinterpret_cast<char*
>(&res);
316 auto readSize =
co_await Socket.ReadSome(p, size);
318 throw std::runtime_error(
"Connection closed");
409 void Push(
const char* buf,
size_t size);
417 std::string_view View;
530 void Push(
const char* p,
size_t len);
538 std::string_view View;
576template<
typename TSocket>
589 , Splitter(maxLineSize)
590 , ChunkSize(maxLineSize / 2)
604 auto line = Splitter.Pop();
606 auto buf = Splitter.Acquire(ChunkSize);
607 auto size =
co_await Socket.ReadSome(buf.data(), buf.size());
614 Splitter.Commit(size);
615 line = Splitter.Pop();
High-level asynchronous socket for network communication.
Definition socket.hpp:364
Implementation of a promise/future system for coroutines.
TFuture< void > Read(void *data, size_t size)
Reads exactly size bytes and stores them into data.
Definition sockutils.hpp:80
TFuture< std::string > ReadUntil(const std::string &delimiter)
Reads data until the given delimiter is encountered.
Definition sockutils.hpp:121
TByteReader(TSocket &socket)
Constructs a reader for the given socket.
Definition sockutils.hpp:60
TFuture< void > Write(const TLine &line)
Writes a TLine object by sequentially writing its parts.
Definition sockutils.hpp:240
TByteWriter(TSocket &socket)
Constructs a writer for the given socket.
Definition sockutils.hpp:195
TFuture< void > Write(const void *data, size_t size)
Writes exactly size bytes from data to the socket.
Definition sockutils.hpp:213
Future type for coroutines returning a value of type T.
Definition corochain.hpp:177
Definition sockutils.hpp:9
TLineReader(TSocket &socket, int maxLineSize=4096)
Constructs a line reader with the given socket and maximum line size.
Definition sockutils.hpp:587
TFuture< TLine > Read()
Reads and returns the next complete line from the socket.
Definition sockutils.hpp:603
void Push(const char *buf, size_t size)
Appends new data to the circular buffer.
TLineSplitter(int maxLen)
Constructs a line splitter with a fixed ring buffer capacity.
TLine Pop()
Retrieves and removes the next complete line from the buffer.
TStructReader(TSocket &socket)
Constructs a reader with a reference to the given socket.
Definition sockutils.hpp:293
TFuture< T > Read()
Reads a single instance of type T from the socket.
Definition sockutils.hpp:311
Splits incoming data into lines using a fixed-size circular buffer, enabling zero-copy writes via Acq...
Definition sockutils.hpp:469
TLine Pop()
Extracts and removes the next complete line from the buffer, if available.
void Push(const char *p, size_t len)
(Optional) Copies data from an external buffer into the circular buffer.
void Commit(size_t size)
Finalizes the amount of data written into the span returned by Acquire().
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...
TZeroCopyLineSplitter(int maxLen)
Constructs a zero-copy line splitter with a fixed ring buffer capacity.