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.
os_tcp_socket& tcp_socket()
-Used by the os_tcp_acceptor object to
establish a communication endpoint with the client application.void* run() -Called by
the Executor object to initiate service
handling for the client application.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.
template< class
ServiceHandler > void execute( ServiceHandler* handler ) -Calls
the void* run() method of the ServiceHandler
within the thread context specified by the Executor
concurrency strategy.The Creator , the third
template parameter, requires a class to have the following public interface.
template< class
ServiceHandler > -Templarizes the Creator
class so that the ServiceHandler object does
not have to inherit from a specific interface class.ServiceHandler* operator()()
const -Decouples the creation of the ServiceHandler
object to allow more flexibility.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.
Framework<ToolKit>
#include <ospace/framework/acceptor.h>
template< class ServiceHandler, class Executor, class Creator >
class os_tcp_acceptor : public os_tcp_connection_server
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
Throws: os_network_toolkit_error
ServiceHandler type objects.ServiceHandler type objects.ServiceHandler objects after
connection establishment.ServiceHandler objects after connection
establishment.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
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.