MCPcopy Create free account
hub / github.com/andreasfertig/programming-with-cpp20 / main

Function main

02.18-coroutineParsingDataStream1/main.cpp:245–292  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

243}
244
245int main()
246{
247 std::vector<byte> fakeBytes1{0x70_B,
248 ESC,
249 SOF,
250 ESC,
251 'H'_B,
252 'e'_B,
253 'l'_B,
254 'l'_B,
255 'o'_B,
256 ESC,
257 SOF,
258 0x7_B,
259 ESC,
260 SOF};
261
262 std::vector<byte> fakeBytes2{
263 'W'_B, 'o'_B, 'r'_B, 'l'_B, 'd'_B, ESC, SOF, 0x99_B};
264
265 auto stream1 = sender(std::move(fakeBytes1));
266
267 DataStreamReader
268 dr{}; // #A Create a DataStreamReader Awaitable
269 auto p = Parse(dr); // #B Create the Parse coroutine and pass
270 // the DataStreamReader
271
272 for(const auto& b : stream1) {
273 dr.set(b); // #C Send the new byte to the waiting
274 // DataStreamReader
275
276 if(const auto& res = p(); res.length()) {
277 HandleFrame(res);
278 }
279 }
280
281 auto stream2 = sender(std::move(
282 fakeBytes2)); // #D Simulate a second network stream
283
284 for(const auto& b : stream2) {
285 dr.set(b); // #E We still use the former dr and p and feed
286 // it with new bytes
287
288 if(const auto& res = p(); res.length()) {
289 HandleFrame(res);
290 }
291 }
292}

Callers

nothing calls this directly

Calls 5

moveFunction · 0.85
senderFunction · 0.70
ParseFunction · 0.70
HandleFrameFunction · 0.70
setMethod · 0.45

Tested by

no test coverage detected