COROIO: coroio/sockutils.hpp Source File
COROIO
 
Loading...
Searching...
No Matches
sockutils.hpp
1#pragma once
2
3#include <assert.h>
4#include <span>
5#include <algorithm>
6#include "corochain.hpp"
7
8#ifdef min
9#undef min
10#endif
11
12#ifdef max
13#undef max
14#endif
15
16namespace NNet {
17
18struct TLine {
19 std::string_view Part1;
20 std::string_view Part2;
21
22 size_t Size() const {
23 return Part1.size() + Part2.size();
24 }
25
26 operator bool() const {
27 return !Part1.empty();
28 }
29};
30
63template<typename TSocket>
70 : Socket(socket)
71 { }
89 TFuture<void> Read(void* data, size_t size) {
90 char* p = static_cast<char*>(data);
91
92 if (!Buffer.empty()) {
93 size_t toCopy = std::min(size, Buffer.size());
94 std::copy(Buffer.begin(), Buffer.begin() + toCopy, p);
95 p += toCopy;
96 size -= toCopy;
97 Buffer.erase(Buffer.begin(), Buffer.begin() + toCopy);
98 }
99
100 while (size != 0) {
101 auto readSize = co_await Socket.ReadSome(p, size);
102 if (readSize == 0) {
103 throw std::runtime_error("Connection closed");
104 }
105 if (readSize < 0) {
106 continue; // retry
107 }
108 p += readSize;
109 size -= readSize;
110 }
111 co_return;
112 }
113
114 TFuture<ssize_t> ReadSome(void* data, size_t size) {
115 char tempBuffer[1024];
116 // read by chunks, copy to user up-to size bytes
117
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);
122 co_return toCopy;
123 }
124
125 auto readSize = co_await Socket.ReadSome(tempBuffer, sizeof(tempBuffer));
126 if (readSize == 0) {
127 co_return 0;
128 }
129 if (readSize < 0) {
130 co_return -1; // retry
131 }
132
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);
137 }
138 co_return toCopy;
139 }
140
158 TFuture<std::string> ReadUntil(const std::string& delimiter)
159 {
160 std::string result;
161 char tempBuffer[1024];
162
163 while (true) {
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());
169 co_return result;
170 }
171
172 result.insert(result.end(), Buffer.begin(), Buffer.end());
173 Buffer.clear();
174
175 auto readSize = co_await Socket.ReadSome(tempBuffer, sizeof(tempBuffer));
176 if (readSize == 0) {
177 throw std::runtime_error("Connection closed");
178 }
179 if (readSize < 0) {
180 continue; // retry
181 }
182
183 Buffer.insert(Buffer.end(), tempBuffer, tempBuffer + readSize);
184 }
185
186 co_return result;
187 }
188
189private:
190 TSocket& Socket;
191 std::deque<char> Buffer;
192};
193
226template<typename TSocket>
233 : Socket(socket)
234 { }
250 TFuture<void> Write(const void* data, size_t size) {
251 const char* p = static_cast<const char*>(data);
252 while (size != 0) {
253 auto readSize = co_await Socket.WriteSome(p, size);
254 if (readSize == 0) {
255 throw std::runtime_error("Connection closed");
256 }
257 if (readSize < 0) {
258 continue; // retry
259 }
260 p += readSize;
261 size -= readSize;
262 }
263 co_return;
264 }
277 TFuture<void> Write(const TLine& line) {
278 co_await Write(line.Part1.data(), line.Part1.size());
279 co_await Write(line.Part2.data(), line.Part2.size());
280 co_return;
281 }
282
283private:
284 TSocket& Socket;
285};
286
324template<typename T, typename TSocket>
331 : Socket(socket)
332 { }
349 T res;
350 size_t size = sizeof(T);
351 char* p = reinterpret_cast<char*>(&res);
352 while (size != 0) {
353 auto readSize = co_await Socket.ReadSome(p, size);
354 if (readSize == 0) {
355 throw std::runtime_error("Connection closed");
356 }
357 if (readSize < 0) {
358 continue; // retry
359 }
360 p += readSize;
361 size -= readSize;
362 }
363 co_return res;
364 }
365
366private:
367 TSocket& Socket;
368};
369
415public:
420 TLineSplitter(int maxLen);
436 TLine Pop();
446 void Push(const char* buf, size_t size);
447
448private:
449 size_t WPos;
450 size_t RPos;
451 size_t Size;
452 size_t Cap;
453 std::string Data;
454 std::string_view View;
455};
456
507public:
512 TZeroCopyLineSplitter(int maxLen);
529 TLine Pop();
546 std::span<char> Acquire(size_t size);
555 void Commit(size_t size);
567 void Push(const char* p, size_t len);
568
569private:
570 size_t WPos;
571 size_t RPos;
572 size_t Size;
573 size_t Cap;
574 std::string Data;
575 std::string_view View;
576};
613template<typename TSocket>
624 TLineReader(TSocket& socket, int maxLineSize = 4096)
625 : Socket(socket)
626 , Splitter(maxLineSize)
627 , ChunkSize(maxLineSize / 2)
628 { }
641 auto line = Splitter.Pop();
642 while (!line) {
643 auto buf = Splitter.Acquire(ChunkSize);
644 auto size = co_await Socket.ReadSome(buf.data(), buf.size());
645 if (size < 0) {
646 continue;
647 }
648 if (size == 0) {
649 break;
650 }
651 Splitter.Commit(size);
652 line = Splitter.Pop();
653 }
654 co_return line;
655 }
656
657private:
658 TSocket& Socket;
659 TZeroCopyLineSplitter Splitter;
660 int ChunkSize;
661};
662
663} // namespace NNet {
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