SelectListener

SelectListener class template

The additional T constructor argument parameters must appear after those for the mandatory IConnectionHandler constructor.

Constructors

this
this(sockaddr* address, ISocket socket, Args args, int backlog)

Constructor

Members

Functions

connectionLog
void connectionLog()

Writes connection information to log file.

connection_limit
size_t connection_limit(size_t limit)

Sets the limit of the number of connections. 0 disables the limitation.

connection_limit
size_t connection_limit()

(Overriding wrapper to fix method matching.)

getConnectionHandler
IConnectionHandler getConnectionHandler()

Obtains a connection handler instance from the pool.

minimize
size_t minimize(uint n)

Minimizes the connection pool to n connections by deleting idle connection objects. If more than n connections are currently busy, all idle connections are deleted.

poolInfo
ISelectListenerPoolInfo poolInfo()
shutdown
void shutdown()

Closes all connections and terminates the listener.

Inherited Members

From ISelectListener

events
Event events()

Implements ISelectClient abstract method.

fileHandle
Handle fileHandle()

Implements ISelectClient abstract method.

handle
bool handle(Event event)

I/O event handler

terminate
bool terminate()

Closes the server socket and sets this instance to terminated mode.

poolInfo
IPoolInfo poolInfo()
connection_limit
size_t connection_limit(size_t limit)

Sets the limit of the number of connections. 0 disables the limitation.

connection_limit
size_t connection_limit()
shutdown
void shutdown()

Closes all connections and terminates the listener.

getConnectionHandler
IConnectionHandler getConnectionHandler()

Obtains a connection handler instance from the pool.

Parameters

T

connection handler class

Args

additional constructor arguments for T

TODO: try using the non-auto ctor pool, for template simplicity!

Examples

1 // A shared resource, owned by the server. A reference is passed to each
2 // connection handler.
3 class SomeSharedResource
4 {
5 }
6 
7 // The connection handler class. The server owns a select listener instance.
8 // The select listener owns a pool of connection handlers. When a new
9 // connection is accepted by the select listener, a connection handler is
10 // taken from the pool (or allocated, if there are no free items in the
11 // pool) and set to handle the incoming connection.
12 // (Connection handler classes must not be nested, in order for the pool to
13 // be able to automatically allocate instances. In this case, it must be
14 // declared static, as the class definition is "nested" inside a unittest.)
15 static class MyConnectionHandler : IConnectionHandler
16 {
17     import ocean.net.server.connection.IConnectionHandlerInfo;
18     import ocean.sys.socket.AddressIPSocket;
19 
20     /// Reference to the global shared resource (passed to the ctor).
21     private SomeSharedResource shared_resource;
22 
23     /// Flag set when an IO error occurs (see error()).
24     private bool error_occurred;
25 
26     /***********************************************************************
27 
28         Constructs a connection handler.
29 
30         Params:
31             finalize_dg = delegate required by the super class.
32                 Automatically set by SelectListenerPool to be
33                 SelectListener.returnToPool. Called by the super class when
34                 finalize() is called
35             shared_resource = reference to a global shared resource
36 
37     ***********************************************************************/
38 
39     public this ( scope FinalizeDg finalize_dg, SomeSharedResource shared_resource )
40     {
41         this.shared_resource = shared_resource;
42 
43         super(new AddressIPSocket!(), finalize_dg, &this.error);
44     }
45 
46     /// The actual logic for handling a connection.
47     override public void handleConnection ( )
48     {
49         // Do something in here.
50 
51         // When you're finished, call finalize(), which recycles this
52         // connection into the pool owned by the select listener.
53         this.finalize();
54     }
55 
56     /// Tells the super class whether an I/O error occurred.
57     override protected bool io_error ( )
58     {
59         return this.error_occurred;
60     }
61 
62     /// Called by epoll when an I/O error occurs.
63     private void error ( Exception exception, Event event,
64         IConnectionHandlerInfo info )
65     {
66         this.error_occurred = true;
67     }
68 
69     /// Unregisters the socket from the epoll before closing it. Called by
70     /// finalize.
71     override protected void unregisterSocket ()
72     {
73     }
74 }
75 
76 // Top-level server class which owns the select listener and all global
77 // resources.
78 class MyServer
79 {
80     import ocean.sys.socket.AddressIPSocket;
81     import ocean.sys.socket.InetAddress;
82     import ocean.io.select.EpollSelectDispatcher;
83 
84     /// Select listener alias.
85     private alias SelectListener!(MyConnectionHandler, SomeSharedResource)
86         MySelectListener;
87 
88     /// Select listener instance.
89     private MySelectListener listener;
90 
91     /// Epoll instance.
92     private EpollSelectDispatcher epoll;
93 
94     /// Constructs a server.
95     public this ( )
96     {
97         InetAddress!(false) addr;
98         auto socket = new AddressIPSocket!();
99         auto shared_resource = new SomeSharedResource;
100 
101         this.listener = new MySelectListener(addr("127.0.0.1", 2009),
102             socket, shared_resource);
103 
104         this.epoll = new EpollSelectDispatcher;
105     }
106 
107     /// Starts the server.
108     public void start ( )
109     {
110         // The listener is an ISelectClient, so must be registered with
111         // epoll in order to do anything.
112         this.epoll.register(this.listener);
113 
114         // The event loop must also be running.
115         this.epoll.eventLoop();
116     }
117 
118     /// Stops the server.
119     public void stop ( )
120     {
121         this.listener.shutdown();
122     }
123 }

Meta