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 // Now we could free the data pointed by span and queue new data
512 console.printLine("Ready to send more data");
513 }
514};
515// Assuming client is an unconnected UDP Socket
516SC_TRY(sendAsync.start(eventLoop, client, destinationAddress, sendData));
517// Vectorized writes: use proper start overload or set
518// AsyncSocketSend::buffers and AsyncSocketSend::singleBuffer = false
519
520//! [AsyncSocketSendToSnippet]
521SC_TRY(eventLoop.run());
522return Result(true);
523}
524
525SC::Result snippetForSocketReceive(AsyncEventLoop& eventLoop, Console& console)
526{
527SocketDescriptor client;
528//! [AsyncSocketReceiveSnippet]
529// Assuming an already created (and running) AsyncEventLoop named `eventLoop`
530// and an unconnected socket named `client`
531// ...
532char receivedData[100] = {0}; // A buffer to hold data read from the socket
533AsyncSocketReceive receiveAsync;
534receiveAsync.callback = [&](AsyncSocketReceive::Result& res)
535{
536 Span<char> readData;
537 if(res.get(readData))
538 {
539 if(res.completionData.disconnected)
540 {
541 // Last callback invocation is done when the other side of the socket
542 // has disconnected:
543 // - completionData.disconnected is == true
544 // - readData.sizeInBytes() is == 0
545 console.print("Client disconnected");
546 }
547 else
548 {
549 // readData is a slice of receivedData with the received bytes
550 console.print("{} bytes have been read", readData.sizeInBytes());
551
552 // IMPORTANT: Reactivate the request to receive more data
553 res.reactivateRequest(true);
554 }
555 }
556 else

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