Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::AsyncSocketSendTo Struct Reference

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

#include <Async.h>

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

Public Member Functions

SC::Result start (AsyncEventLoop &eventLoop, const SocketDescriptor &descriptor, SocketIPAddress ipAddress, Span< const char > data)
 
SC::Result start (AsyncEventLoop &eventLoop, const SocketDescriptor &descriptor, SocketIPAddress ipAddress, Span< Span< const char > > data)
 
- Public Member Functions inherited from SC::AsyncSocketSend
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 &eventLoop)
 Shortcut for AsyncEventLoop::start.
 
- Public Member Functions inherited from SC::AsyncRequest
void setDebugName (const char *newDebugName)
 
void executeOn (AsyncSequence &sequence)
 Adds the request to be executed on a specific AsyncSequence.
 
Result executeOn (AsyncTaskSequence &task, ThreadPool &pool)
 Adds the request to be executed on a specific AsyncTaskSequence.
 
void disableThreadPool ()
 Disables the thread-pool usage for this request.
 
 AsyncRequest (Type type)
 Constructs a free async request of given type.
 
Result stop (AsyncEventLoop &eventLoop, 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 &eventLoop)
 Shortcut for AsyncEventLoop::start.
 
void setUserFlags (uint16_t externalFlags)
 Sets user flags, holding some meaningful data for the caller.
 
uint16_t getUserFlags () const
 Gets user flags, holding some meaningful data for the caller.
 
Function< void(AsyncResult &)> * getCloseCallback ()
 Returns currently set close callback (if any) passed to AsyncRequest::stop.
 
const Function< void(AsyncResult &)> * getCloseCallback () const
 

Public Attributes

SocketIPAddress address
 
- Public Attributes inherited from SC::AsyncSocketSend
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

- Public Types inherited from SC::AsyncSocketSend
using Result = AsyncResultOf<AsyncSocketSend, CompletionData>
 
- Public Types inherited from SC::AsyncRequest
enum class  Type : uint8_t {
  LoopTimeout ,
  LoopWakeUp ,
  LoopWork ,
  ProcessExit ,
  SocketAccept ,
  SocketConnect ,
  SocketSend ,
  SocketSendTo ,
  SocketReceive ,
  SocketReceiveFrom ,
  FileRead ,
  FileWrite ,
  FilePoll ,
  FileSystemOperation
}
 Type of async request. More...
 
- Protected Member Functions inherited from SC::AsyncSocketSend
 AsyncSocketSend (Type type)
 
SC::Result validate (AsyncEventLoop &)
 
- Protected Member Functions inherited from SC::AsyncRequest
Result checkState ()
 
void queueSubmission (AsyncEventLoop &eventLoop)
 
AsyncTaskSequencegetTask ()
 
- Protected Attributes inherited from SC::AsyncSocketSend
size_t totalBytesWritten = 0
 
- Protected Attributes inherited from SC::AsyncRequest
AsyncSequencesequence = nullptr
 

Detailed Description

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

Callback will be called when the given socket is ready to send more data.
Typical use case is to send data to an unconnected UDP 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::associateExternallyCreatedSocket or though AsyncSocketAccept.
Alternatively SC::AsyncEventLoop::createAsyncUDPSocket 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`
// ...
SocketIPAddress destinationAddress;
SC_TRY(destinationAddress.fromAddressPort("127.0.0.1", 5051)); // Connect to localhost on port
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)};
AsyncSocketSendTo sendAsync;
sendAsync.callback = [&](AsyncSocketSendTo::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");
}
};
// Assuming client is an unconnected UDP Socket
SC_TRY(sendAsync.start(eventLoop, client, destinationAddress, sendData));
// Vectorized writes: use proper start overload or set
// AsyncSocketSend::buffers and AsyncSocketSend::singleBuffer = false

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