| 511 | } |
| 512 | |
| 513 | SC::Result snippetForSocketReceive(AsyncEventLoop& eventLoop, Console& console) |
| 514 | { |
| 515 | SocketDescriptor client; |
| 516 | //! [AsyncSocketReceiveSnippet] |
| 517 | // Assuming an already created (and running) AsyncEventLoop named `eventLoop` |
| 518 | // and an unconnected socket named `client` |
| 519 | // ... |
| 520 | char receivedData[100] = {0}; // A buffer to hold data read from the socket |
| 521 | AsyncSocketReceive receiveAsync; |
| 522 | receiveAsync.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 | }; |
| 549 | SC_TRY(receiveAsync.start(eventLoop, client, {receivedData, sizeof(receivedData)})); |
| 550 | //! [AsyncSocketReceiveSnippet] |
| 551 | SC_TRY(eventLoop.run()); |
| 552 | return Result(true); |
| 553 | } |
| 554 | |
| 555 | SC::Result snippetForSocketReceiveFrom(AsyncEventLoop& eventLoop, Console& console) |
| 556 | { |
nothing calls this directly
no test coverage detected