Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
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
 
struct  Result
 

Public Member Functions

SC::Result start (AsyncEventLoop &eventLoop, const SocketDescriptor &descriptor, Span< char > data)
 Sets async request members and calls AsyncEventLoop::start.
 
Result start (AsyncEventLoop &loop)
 Shortcut for AsyncEventLoop::start.
 
- Public Member Functions inherited from SC::AsyncRequest
void setDebugName (const char *newDebugName)
 
AsyncEventLoopgetEventLoop () const
 Get the event loop associated with this AsyncRequest.
 
void cacheInternalEventLoop (AsyncEventLoop &loop)
 Caches the event loop associated with this AsyncRequest.
 
Result setThreadPoolAndTask (ThreadPool &pool, AsyncTask &task)
 Sets the thread pool and task to use for this request.
 
void resetThreadPoolAndTask ()
 Resets anything previously set with setThreadPoolAndTask.
 
 AsyncRequest (Type type)
 Constructs a free async request of given type.
 
Result stop (Function< void(AsyncResult &)> *afterStopped=nullptr)
 Ask to stop current async operation.
 
bool isFree () const
 Returns true if this request is free.
 
bool isCancelling () const
 Returns true if this request is being cancelled.
 
bool isActive () const
 Returns true if this request is active or being reactivated.
 
Type getType () const
 Returns request type.
 
Result start (AsyncEventLoop &loop)
 Shortcut for AsyncEventLoop::start.
 

Public Attributes

Function< void(Result &)> callback
 Called after data has been received.
 
Span< char > buffer
 The writeable span of memory where to data will be written.
 
SocketDescriptor::Handle handle = SocketDescriptor::Invalid
 
- Public Attributes inherited from SC::AsyncRequest
AsyncRequestnext = nullptr
 
AsyncRequestprev = nullptr
 

Friends

struct AsyncEventLoop
 The Socket Descriptor handle to read data from.
 

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 checkState ()
 
void queueSubmission (AsyncEventLoop &eventLoop)
 
- 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.

Additional notes:

  • SC::AsyncSocketReceive::CompletionData::disconnected will be set to true when client disconnects
// Assuming an already created (and running) AsyncEventLoop named `eventLoop`
// and a connected or accepted socket named `client`
// ...
char receivedData[100] = {0}; // A buffer to hold data read from the socket
AsyncSocketReceive receiveAsync;
receiveAsync.callback = [&](AsyncSocketReceive::Result& res)
{
Span<char> readData;
if(res.get(readData))
{
if(res.completionData.disconnected)
{
// Last callback invocation done when other side of the socket has disconnected.
// - completionData.disconnected is == true
// - readData.sizeInBytes() is == 0
console.print("Client disconnected");
}
else
{
// readData is a slice of receivedData with the received bytes
console.print("{} bytes have been read", readData.sizeInBytes());
// IMPORTANT: Reactivate the request to receive more data
res.reactivateRequest(true);
}
}
else
{
// Some error occurred, check res.returnCode
}
};
SC_TRY(receiveAsync.start(eventLoop, client, {receivedData, sizeof(receivedData)}));

Member Function Documentation

◆ start() [1/2]

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

Sets async request members and calls AsyncEventLoop::start.

◆ start() [2/2]

Result SC::AsyncRequest::start ( AsyncEventLoop & loop)

Shortcut for AsyncEventLoop::start.

Friends And Related Symbol Documentation

◆ AsyncEventLoop

friend struct AsyncEventLoop
friend

The Socket Descriptor handle to read data from.

Member Data Documentation

◆ buffer

Span<char> SC::AsyncSocketReceive::buffer

The writeable span of memory where to data will be written.

◆ callback

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

Called after data has been received.


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