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

Public Types

using Result = AsyncResultOf<AsyncSocketSend, CompletionData>
 
- 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 &descriptor, Span< const char > data)
 Sets async request members and calls AsyncEventLoop::start.
 
SC::Result start (AsyncEventLoop &eventLoop, const SocketDescriptor &descriptor, Span< Span< const 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 when socket is ready to send more data.
 
SocketDescriptor::Handle handle = SocketDescriptor::Invalid
 The socket to send data to.
 
Span< const char > buffer
 Span of bytes to send (singleBuffer == true)
 
Span< Span< const char > > buffers
 Spans of bytes to send (singleBuffer == false)
 
bool singleBuffer = true
 Controls if buffer or buffers will be used.
 
- Public Attributes inherited from SC::AsyncRequest
AsyncRequestnext = nullptr
 
AsyncRequestprev = nullptr
 

Friends

struct AsyncEventLoop
 

Additional Inherited Members

- 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 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));
// Vectorized writes: use proper start overload or set
// AsyncSocketSend::buffers and AsyncSocketSend::singleBuffer = false

Member Function Documentation

◆ start() [1/3]

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

Sets async request members and calls AsyncEventLoop::start.

◆ start() [2/3]

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

Sets async request members and calls AsyncEventLoop::start.

◆ start() [3/3]

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

Shortcut for AsyncEventLoop::start.

Member Data Documentation

◆ buffer

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

Span of bytes to send (singleBuffer == true)

◆ buffers

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

Spans of bytes to send (singleBuffer == false)

◆ 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.

◆ singleBuffer

bool SC::AsyncSocketSend::singleBuffer = true

Controls if buffer or buffers will be used.


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