EpollSelector

Selector that uses the Linux epoll* family of system calls.

This selector is the best option when dealing with large amounts of conduits under Linux. It will scale much better than any of the other options (PollSelector, SelectSelector). For small amounts of conduits (n < 20) the PollSelector will probably be more performant.

Destructor

~this
~this()

Destructor

Members

Aliases

select
alias select = AbstractSelector.select

Alias for the select() method as we're not reimplementing it in this class.

Classes

EpollSelectionSet
class EpollSelectionSet

Class used to hold the list of Conduits that have received events.

Functions

close
void close()

Close the selector, releasing the file descriptor that had been created in the previous call to open().

count
size_t count()

Return the number of keys resulting from the registration of a conduit to the selector.

key
SelectionKey key(ISelectable conduit)

Return the selection key resulting from the registration of a conduit to the selector.

opApply
int opApply(int delegate(ref SelectionKey) dg)

Iterate through the currently registered selection keys. Note that you should not erase or add any items from the selector while iterating, although you can register existing conduits again.

open
void open(uint size, uint maxEvents)

Open the epoll selector, makes a call to epoll_create()

register
void register(ISelectable conduit, Event events, Object attachment)

Associate a conduit to the selector and track specific I/O events. If a conduit is already associated, changes the events and attachment.

select
int select(TimeSpan timeout)

Wait for I/O events from the registered conduits for a specified amount of time.

selectedSet
ISelectionSet selectedSet()

Return the selection set resulting from the call to any of the select() methods.

unregister
void unregister(ISelectable conduit)

Remove a conduit from the selector.

Variables

DefaultMaxEvents
enum uint DefaultMaxEvents;

Default maximum number of events that will be received per invocation to select().

DefaultSize
enum uint DefaultSize;

Default number of SelectionKey's that will be handled by the EpollSelector.

Inherited Members

From AbstractSelector

_restartInterruptedSystemCall
bool _restartInterruptedSystemCall;

Restart interrupted system calls when blocking inside a call to select.

restartInterruptedSystemCall
bool restartInterruptedSystemCall()

Indicates whether interrupted system calls will be restarted when blocking inside a call to select.

restartInterruptedSystemCall
void restartInterruptedSystemCall(bool value)

Sets whether interrupted system calls will be restarted when blocking inside a call to select.

open
void open(uint size, uint maxEvents)

Initialize the selector.

close
void close()

Free any operating system resources that may have been allocated in the call to open().

register
void register(ISelectable conduit, Event events, Object attachment)

Associate a conduit to the selector and track specific I/O events.

reregister
deprecated void reregister(ISelectable conduit, Event events, Object attachment)

Deprecated, use register instead

unregister
void unregister(ISelectable conduit)

Remove a conduit from the selector.

select
int select()

Wait for I/O events from the registered conduits for a specified amount of time.

select
int select(double timeout)

Wait for I/O events from the registered conduits for a specified amount of time.

select
int select(TimeSpan timeout)

Wait for I/O events from the registered conduits for a specified amount of time.

selectedSet
ISelectionSet selectedSet()

Return the selection set resulting from the call to any of the select() methods.

key
SelectionKey key(ISelectable conduit)

Return the selection key resulting from the registration of a conduit to the selector.

count
size_t count()

Return the number of keys resulting from the registration of a conduit to the selector.

toTimeval
timeval* toTimeval(timeval* tv, TimeSpan interval)

* Cast the time duration to a C timeval struct.

checkErrno
void checkErrno(string file, size_t line)

Check the 'errno' global variable from the C standard library and throw an exception with the description of the error.

Examples

1 import tango.io.selector.EpollSelector;
2 import tango.net.device.Socket;
3 import tango.io.Stdout;
4 
5 SocketConduit conduit1;
6 SocketConduit conduit2;
7 EpollSelector selector = new EpollSelector();
8 MyClass object1 = new MyClass();
9 MyClass object2 = new MyClass();
10 uint eventCount;
11 
12 // Initialize the selector assuming that it will deal with 10 conduits and
13 // will receive 3 events per invocation to the select() method.
14 selector.open(10, 3);
15 
16 selector.register(conduit1, Event.Read, object1);
17 selector.register(conduit2, Event.Write, object2);
18 
19 eventCount = selector.select();
20 
21 if (eventCount > 0)
22 {
23     char[16] buffer;
24     int count;
25 
26     foreach (SelectionKey key; selector.selectedSet())
27     {
28         if (key.isReadable())
29         {
30             count = (cast(SocketConduit) key.conduit).read(buffer);
31             if (count != IConduit.Eof)
32             {
33                 Stdout.format("Received '{0}' from peer\n", buffer[0..count]);
34                 selector.register(key.conduit, Event.Write, key.attachment);
35             }
36             else
37             {
38                 selector.unregister(key.conduit);
39                 key.conduit.close();
40             }
41         }
42 
43         if (key.isWritable())
44         {
45             count = (cast(SocketConduit) key.conduit).write("MESSAGE");
46             if (count != IConduit.Eof)
47             {
48                 Stdout("Sent 'MESSAGE' to peer");
49                 selector.register(key.conduit, Event.Read, key.attachment);
50             }
51             else
52             {
53                 selector.unregister(key.conduit);
54                 key.conduit.close();
55             }
56         }
57 
58         if (key.isError() || key.isHangup() || key.isInvalidHandle())
59         {
60             selector.unregister(key.conduit);
61             key.conduit.close();
62         }
63     }
64 }
65 
66 selector.close();

See Also

ISelector, AbstractSelector

Meta