Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::AsyncSocketReceive Struct Reference

Starts a socket receive operation, receiving bytes from a remote endpoint. More...

#include <Async.h>

Inheritance diagram for SC::AsyncSocketReceive:
SC::AsyncRequest

Classes

struct  CompletionData
 Completion data for AsyncSocketReceive. More...
 
struct  Result
 Callback result for AsyncSocketReceive. More...
 

Public Member Functions

SC::Result start (AsyncEventLoop &eventLoop, const SocketDescriptor &socketDescriptor, Span< char > data)
 Starts a socket receive operation. More...
 
- Public Member Functions inherited from SC::AsyncRequest
void setDebugName (const char *newDebugName)
 
AsyncEventLoopgetEventLoop () const
 Get the event loop associated with this AsyncRequest. More...
 
 AsyncRequest (Type type)
 Constructs a free async request of given type. More...
 
Result stop ()
 Stops the async operation. More...
 

Public Attributes

Function< void(Result &)> callback
 Called after data has been received. More...
 
- Public Attributes inherited from SC::AsyncRequest
AsyncRequestnext = nullptr
 
AsyncRequestprev = nullptr
 

Friends

struct AsyncEventLoop
 

Additional Inherited Members

- Public Types inherited from SC::AsyncRequest
enum class  Type : uint8_t {
  LoopTimeout ,
  LoopWakeUp ,
  LoopWork ,
  ProcessExit ,
  SocketAccept ,
  SocketConnect ,
  SocketSend ,
  SocketReceive ,
  SocketClose ,
  FileRead ,
  FileWrite ,
  FileClose ,
  FilePoll
}
 Type of async request. More...
 
- Protected Member Functions inherited from SC::AsyncRequest
Result validateAsync ()
 
Result queueSubmission (AsyncEventLoop &eventLoop)
 
Result queueSubmission (AsyncEventLoop &eventLoop, ThreadPool &threadPool, AsyncTask &task)
 
- Static Protected Member Functions inherited from SC::AsyncRequest
static void updateTime (AsyncEventLoop &loop)
 
- Protected Attributes inherited from SC::AsyncRequest
AsyncEventLoopeventLoop = nullptr
 
AsyncTaskasyncTask = nullptr
 

Detailed Description

Starts a socket receive operation, receiving bytes from a remote endpoint.

Callback will be called when some data is read from socket.
Socket library can be used to create a Socket but the socket should be created with SC::SocketFlags::NonBlocking and associated to the event loop with SC::AsyncEventLoop::associateExternallyCreatedTCPSocket or though AsyncSocketAccept.
Alternatively SC::AsyncEventLoop::createAsyncTCPSocket creates and associates the socket to the loop.

// Assuming an already created (and running) AsyncEventLoop named `eventLoop`
// and a connected or accepted socket named `client`
// ...
AsyncSocketReceive receiveAsync;
char receivedData[100] = {0};
receiveAsync.callback = [&](AsyncSocketReceive::Result& res)
{
Span<char> readData;
if(res.get(readData))
{
// readData now contains a slice of receivedData with the received bytes
console.print("{} bytes have been read", readData.sizeInBytes());
}
// Ask to reactivate the request if we want to receive more data
res.reactivateRequest(true);
};
SC_TRY(receiveAsync.start(eventLoop, client, {receivedData, sizeof(receivedData)}));
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition: Result.h:47

Member Function Documentation

◆ start()

SC::Result SC::AsyncSocketReceive::start ( AsyncEventLoop eventLoop,
const SocketDescriptor socketDescriptor,
Span< char >  data 
)

Starts a socket receive operation.

Callback will be called when some data is read from socket.

Parameters
eventLoopThe event loop where queuing this async request
socketDescriptorThe socket from which to receive data
dataSpan of memory where to write received bytes
Returns
Valid Result if the request has been successfully queued

Member Data Documentation

◆ callback

Function<void(Result&)> SC::AsyncSocketReceive::callback

Called after data has been received.


The documentation for this struct was generated from the following file: