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

Starts a socket accept operation, obtaining a new socket from a listening socket. More...

#include <Async.h>

Inheritance diagram for SC::AsyncSocketAccept:
SC::AsyncRequest

Classes

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

Public Member Functions

SC::Result start (AsyncEventLoop &eventLoop, const SocketDescriptor &socketDescriptor)
 Starts a socket accept operation, that returns a new socket connected to the given listening endpoint. 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...
 
 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 a new socket has been accepted. 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)
 
- Protected Attributes inherited from SC::AsyncRequest
AsyncEventLoopeventLoop = nullptr
 
AsyncTaskasyncTask = nullptr
 

Detailed Description

Starts a socket accept operation, obtaining a new socket from a listening socket.


The callback is called with a new socket connected to the given listening endpoint will be returned.
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.
Alternatively SC::AsyncEventLoop::createAsyncTCPSocket creates and associates the socket to the loop.

Note
To continue accepting new socket SC::AsyncResult::reactivateRequest must be called.
// Assuming an already created (and running) AsyncEventLoop named eventLoop
// ...
// Create a listening socket
constexpr uint32_t numWaitingConnections = 2;
SocketDescriptor serverSocket;
uint16_t tcpPort = 5050;
SocketIPAddress nativeAddress;
SC_TRY(nativeAddress.fromAddressPort("127.0.0.1", tcpPort));
SC_TRY(eventLoop.createAsyncTCPSocket(nativeAddress.getAddressFamily(), serverSocket));
SocketServer server(serverSocket);
SC_TRY(server.bind(nativeAddress));
SC_TRY(server.listen(numWaitingConnections));
// Accept connect for new clients
AsyncSocketAccept accept;
accept.callback = [&](AsyncSocketAccept::Result& res)
{
SocketDescriptor client;
if(res.moveTo(client))
{
// ...do something with new client
console.printLine("New client connected!");
res.reactivateRequest(true); // We want to receive more clients
}
};
SC_TRY(accept.start(eventLoop, serverSocket));
// ... at some later point
// Stop accepting new clients
SC_TRY(accept.stop());
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition: Result.h:47
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition: PrimitiveTypes.h:37
Result createAsyncTCPSocket(SocketFlags::AddressFamily family, SocketDescriptor &outDescriptor)
Helper to creates a TCP socket with AsyncRequest flags of the given family (IPV4 / IPV6).

Member Function Documentation

◆ start()

SC::Result SC::AsyncSocketAccept::start ( AsyncEventLoop eventLoop,
const SocketDescriptor socketDescriptor 
)

Starts a socket accept operation, that returns a new socket connected to the given listening endpoint.

Note
SocketDescriptor must be created with async flags and already bound and listening.
Parameters
eventLoopThe event loop where queuing this async request
socketDescriptorThe socket that will receive the accepted client.
Returns
Valid Result if the request has been successfully queued

Member Data Documentation

◆ callback

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

Called when a new socket has been accepted.


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