MCPcopy Create free account
hub / github.com/Pagghiu/SaneCppLibraries / snippetForSocketReceive

Function snippetForSocketReceive

Tests/Libraries/Async/AsyncTest.cpp:513–553  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

511}
512
513SC::Result snippetForSocketReceive(AsyncEventLoop& eventLoop, Console& console)
514{
515SocketDescriptor client;
516//! [AsyncSocketReceiveSnippet]
517// Assuming an already created (and running) AsyncEventLoop named `eventLoop`
518// and an unconnected socket named `client`
519// ...
520char receivedData[100] = {0}; // A buffer to hold data read from the socket
521AsyncSocketReceive receiveAsync;
522receiveAsync.callback = [&](AsyncSocketReceive::Result& res)
523{
524 Span<char> readData;
525 if(res.get(readData))
526 {
527 if(res.completionData.disconnected)
528 {
529 // Last callback invocation is done when the other side of the socket
530 // has disconnected:
531 // - completionData.disconnected is == true
532 // - readData.sizeInBytes() is == 0
533 console.print("Client disconnected");
534 }
535 else
536 {
537 // readData is a slice of receivedData with the received bytes
538 console.print("{} bytes have been read", readData.sizeInBytes());
539
540 // IMPORTANT: Reactivate the request to receive more data
541 res.reactivateRequest(true);
542 }
543 }
544 else
545 {
546 // Some error occurred, check res.returnCode
547 }
548};
549SC_TRY(receiveAsync.start(eventLoop, client, {receivedData, sizeof(receivedData)}));
550//! [AsyncSocketReceiveSnippet]
551SC_TRY(eventLoop.run());
552return Result(true);
553}
554
555SC::Result snippetForSocketReceiveFrom(AsyncEventLoop& eventLoop, Console& console)
556{

Callers

nothing calls this directly

Calls 7

printMethod · 0.80
reactivateRequestMethod · 0.80
ResultClass · 0.50
getMethod · 0.45
sizeInBytesMethod · 0.45
startMethod · 0.45
runMethod · 0.45

Tested by

no test coverage detected