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

Public Member Functions

SC::Result start (AsyncEventLoop &eventLoop, const SocketDescriptor &socketDescriptor)
 Sets async request members and calls AsyncEventLoop::start.
 

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::associateExternallyCreatedSocket.
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(eventLoop));

Member Function Documentation

◆ start()

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

Sets async request members and calls AsyncEventLoop::start.


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