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

Starts a socket send operation, sending bytes to a remote endpoint. More...

#include <Async.h>

Inheritance diagram for SC::AsyncSocketSend:
SC::AsyncRequest

Classes

struct  CompletionData
 Completion data for AsyncSocketSend. More...
 

Public Types

using Result = AsyncResultOf< AsyncSocketSend, CompletionData >
 Callback result for AsyncSocketSend. More...
 
- 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...
 

Public Member Functions

SC::Result start (AsyncEventLoop &eventLoop, const SocketDescriptor &socketDescriptor, Span< const char > data)
 Starts a socket send operation. More...
 
SC::Result start (AsyncEventLoop &eventLoop)
 Starts a socket send 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...
 
void cacheInternalEventLoop (AsyncEventLoop &loop)
 Caches the event loop associated with this AsyncRequest. More...
 
Result setThreadPoolAndTask (ThreadPool &pool, AsyncTask &task)
 Sets the thread pool and task to use for this request. More...
 
void resetThreadPoolAndTask ()
 Resets anything previously set with setThreadPoolAndTask. More...
 
 AsyncRequest (Type type)
 Constructs a free async request of given type. More...
 
Result stop ()
 Stops the async operation. More...
 
bool isFree () const
 

Public Attributes

Function< void(Result &)> callback
 Called when socket is ready to send more data. More...
 
Span< const char > buffer
 Span of bytes to send. More...
 
SocketDescriptor::Handle handle = SocketDescriptor::Invalid
 The socket to send data to. More...
 
- Public Attributes inherited from SC::AsyncRequest
AsyncRequestnext = nullptr
 
AsyncRequestprev = nullptr
 

Friends

struct AsyncEventLoop
 

Additional Inherited Members

- Protected Member Functions inherited from SC::AsyncRequest
Result validateAsync ()
 
void queueSubmission (AsyncEventLoop &eventLoop)
 
- Protected Attributes inherited from SC::AsyncRequest
AsyncEventLoopeventLoop = nullptr
 
AsyncTaskasyncTask = nullptr
 

Detailed Description

Starts a socket send operation, sending bytes to a remote endpoint.

Callback will be called when the given socket is ready to send more data.
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`
// ...
const char sendBuffer[] = {123, 111};
// The memory pointed by the span must be valid until callback is called
Span<const char> sendData = {sendBuffer, sizeof(sendBuffer)};
AsyncSocketSend sendAsync;
sendAsync.callback = [&](AsyncSocketSend::Result& res)
{
if(res.isValid())
{
// Now we could free the data pointed by span and queue new data
console.printLine("Ready to send more data");
}
};
SC_TRY(sendAsync.start(eventLoop, client, sendData));
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition: Result.h:48
AsyncResultOf< AsyncSocketSend, CompletionData > Result
Callback result for AsyncSocketSend.
Definition: Async.h:561

Member Typedef Documentation

◆ Result

Member Function Documentation

◆ start() [1/2]

SC::Result SC::AsyncSocketSend::start ( AsyncEventLoop eventLoop)

Starts a socket send operation.

Callback will be called when the given socket is ready to send more data.

Parameters
eventLoopThe event loop where queuing this async request
Returns
Valid Result if the request has been successfully queued
Note
Remember to fill AsyncSocketSend::buffer and AsyncSocketSend::handle before calling start

◆ start() [2/2]

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

Starts a socket send operation.

Callback will be called when the given socket is ready to send more data.

Parameters
eventLoopThe event loop where queuing this async request
socketDescriptorThe socket to send data to
dataThe data to be sent
Returns
Valid Result if the request has been successfully queued

Member Data Documentation

◆ buffer

Span<const char> SC::AsyncSocketSend::buffer

Span of bytes to send.

◆ callback

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

Called when socket is ready to send more data.

◆ handle

SocketDescriptor::Handle SC::AsyncSocketSend::handle = SocketDescriptor::Invalid

The socket to send data to.


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