tcp_acceptor


The os_tcp_acceptor template class extends the os_tcp_connection_server class. The os_tcp_connection_server class allows accepting incoming connections on a particular address. The os_tcp_acceptor extends it by providing functionality for creating and executing service handlers to serve the accepted connections. The type of the service handler, its creation, and its execution strategies can be specified as template instantiation parameters to the os_tcp_acceptor class.

The ServiceHandler , the first template parameter, requires a class to have following public interface.

Alternately, the ServiceHandler object could inherit from os_handler_stream class for the os_tcp_socket member data and os_tcp_socket& tcp_socket() member function, thereby not requiring to implement that functionality directly in its own class. The ServiceHandler object should delete itself in void* run() after it is done serving the client.

The Executor , the second template parameter, can be passed one of three concurrency strategy implementations provided (os_same_thread_executor , os_new_thread_executor , and os_thread_pool_executor ) or a custom implementation with the following public interface.

The Creator , the third template parameter, requires a class to have the following public interface.

Connection requests are handled in the handle_accept() method. A ServiceHandler object is created using the Creator function object. The created ServiceHandler object should contain an os_tcp_socket object member which is used to establish the connection with the client socket. A ServiceHandler object could inherit from os_handler_stream class for the os_tcp_socket object or alternatively implement the functionality of os_handler_stream class directly in its own class without inheriting from it. After the connection is established the ServiceHandler object is passed to the Executor object for execution. The Executor object calls the void* run() method of the ServiceHandler to initiate service handling.

The application can wait for the connection requests using the handle_accept() method or through the os_dispatcher instance. A single thread application could use os_dispatcher class and a multithread application could use a separate thread that repeated calls handle_accept() to accept connections and initiate service handlers.

The os_tcp_acceptor , os_tcp_connector , and os_dispatcher classes can be collectively used to synchronously or asynchronously establish connections and perform service processing at the two connection endpoints.

This class is implemented based on the Acceptor-Connector pattern described in the paper - "Acceptor-Connector - An Object Creational Pattern for connecting and Initializing Communication Services" by Douglas C. Schmidt published in the book "Pattern Languages of Program Design 3", Addison-Wesley 1997.

Use of member template feature gives the flexibility of not having the various classes to inherit from a specific interface class. Therefore, the compiler must be able to support member templates and OS_ENABLE_MEMBER_TEMPLATE_FEATURES must be defined to use this class.

Library

Framework<ToolKit>

Declaration

#include <ospace/framework/acceptor.h>

template< class ServiceHandler, class Executor, class Creator >
class os_tcp_acceptor : public os_tcp_connection_server

Interface

Constructor
os_tcp_acceptor( const os_socket_address& address , Executor* executor , os_dispatcher* dispatcher , Creator creator , bool lock_accept_call )
Constructs an acceptor to bind to address . The executor concurrency strategy is used to execute ServiceHandler objects that are created using the creator (default os_creator ) function object to service established connections. If dispatcher (default 0 ) is not null then the acceptor is registered with the dispatcher to monitor connection requests on the connection server descriptor. The application should not call handle_accept() or run() method to accept connections since accepting connections will be handled by the the dispatcher 's event handling mechanism. In a multithreaded application, if more than one thread is running the same acceptor object then lock_accept_call (default false ) must be set to true since some systems do not allow accept() system call to be called from multiple threads on the same descriptor. If there is a system call failure, then an exception is thrown.

Throws: os_network_toolkit_error

Constructor
os_tcp_acceptor( os_sock_t descriptor , Executor* executor , os_dispatcher* dispatcher , Creator creator , bool lock_accept_call )
Same as the above constructor except that the socket descriptor descriptor is used to accept incoming connection requests. If there is a system call failure, then an exception is thrown.

Throws: os_network_toolkit_error

Destructor
~os_tcp_acceptor()
Unregisters the acceptor from the dispatcher if present.
Constructor
os_user( const os_user& user )
Constructs a user that is a copy of user .
creator
const Creator& creator() const
Returns a const reference to the creator object used to create ServiceHandler type objects.
creator
Creator& creator()
Returns a reference to the creator object used to create ServiceHandler type objects.
executor
const Executor* executor() const
Returns a const pointer to the executor object used to execute ServiceHandler objects after connection establishment.
executor
Executor* executor()
Returns a pointer to the executor object used to execute ServiceHandler objects after connection establishment.
handle_accept
void handle_accept()
Blocks until there is a pending connection request. A new ServiceHandler object is created using creator() and is connected to the client socket. After the connection is established, the ServiceHandler object is executed using the executor() to service the connected client. If there is a system call failure, then an exception is thrown.

Throws: os_network_toolkit_error

run
void* run()
Runs handle_accept() repeatedly. This function does not return. This method is useful to create an acceptor in a separate thread and monitor connection requests.

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.