tango.io.selector.SelectSelector

License:

BSD style: see license.txt

Author:

Juan Jose Comellas
class SelectSelector : AbstractSelector [public] #
Selector that uses the select() system call to receive I/O events for the registered conduits. To use this class you would normally do something like this:

Examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import tango.io.selector.SelectSelector;

Socket socket;
ISelector selector = new SelectSelector();

selector.open(100, 10);

// Register to read from socket
selector.register(socket, Event.Read);

int eventCount = selector.select(0.1); // 0.1 seconds
if (eventCount > 0)
{
    // We can now read from the socket
    socket.read();
}
else if (eventCount == 0)
{
    // Timeout
}
else if (eventCount == -1)
{
    // Another thread called the wakeup() method.
}
else
{
    // Error: should never happen.
}

selector.close();
alias AbstractSelector.select select #
Alias for the select() method as we're not reimplementing it in this class.
uint DefaultSize [public, const] #
Default number of SelectionKey's that will be handled by the SelectSelector.
void open(uint size = DefaultSize, uint maxEvents = DefaultSize) [public] #
Open the select()-based selector.

Params:

sizemaximum amount of conduits that will be registered; it will grow dynamically if needed.
maxEventsmaximum amount of conduit events that will be returned in the selection set per call to select(); this value is currently not used by this selector.
void close() [public] #
Close the selector.

Remarks:

It can be called multiple times without harmful side-effects.
void register(ISelectable conduit, Event events, Object attachment = null) [public] #
Associate a conduit to the selector and track specific I/O events. If a conduit is already associated with the selector, the events and attachment are upated.

Params:

conduitconduit that will be associated to the selector; must be a valid conduit (i.e. not null and open).
eventsbit mask of Event values that represent the events that will be tracked for the conduit.
attachmentoptional object with application-specific data that will be available when an event is triggered for the conduit

Throws:

RegisteredConduitException if the conduit had already been registered to the selector.

Examples:

1
selector.register(conduit, Event.Read | Event.Write, object);
void unregister(ISelectable conduit) [public] #
Remove a conduit from the selector.

Params:

conduitconduit that had been previously associated to the selector; it can be null.

Remarks:

Unregistering a null conduit is allowed and no exception is thrown if this happens.

Throws:

UnregisteredConduitException if the conduit had not been previously registered to the selector.
int select(TimeSpan timeout) [public] #
Wait for I/O events from the registered conduits for a specified amount of time.

Params:

timeoutTimeSpan with the maximum amount of time that the selector will wait for events from the conduits; the amount of time is relative to the current system time (i.e. just the number of milliseconds that the selector has to wait for the events).

Returns:

The amount of conduits that have received events; 0 if no conduits have received events within the specified timeout; and -1 if the wakeup() method has been called from another thread.

Throws:

InterruptedSystemCallException if the underlying system call was interrupted by a signal and the 'restartInterruptedSystemCall' property was set to false; SelectorException if there were no resources available to wait for events from the conduits.
ISelectionSet selectedSet() [public] #
Return the selection set resulting from the call to any of the select() methods.

Remarks:

If the call to select() was unsuccessful or it did not return any events, the returned value will be null.
SelectionKey key(ISelectable conduit) [public] #
Return the selection key resulting from the registration of a conduit to the selector.

Remarks:

If the conduit is not registered to the selector the returned value will be null. No exception will be thrown by this method.
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.
class SelectSelectionSet : ISelectionSet [private] #
SelectionSet for the select()-based Selector.
struct HandleSet [private] #
Helper class used by the select()-based Selector to store handles. On Windows the handles are kept in an array of uints and the first element of the array stores the array "length" (i.e. number of handles in the array). Everything is stored so that the native select() API can use the HandleSet without additional conversions by just casting it to a fd_set*.
uint DefaultSize [const] #
Default number of handles that will be held in the HandleSet.
void setup(uint size = DefaultSize) #
Constructor. Sets the initial number of handles that will be held in the HandleSet.
bool initialized() #
return true if this handle set has been initialized.
uint length() #
Return the number of handles present in the HandleSet.
void set(ISelectable.Handle handle) #
Add the handle to the set.
void clear(ISelectable.Handle handle) #
Remove the handle from the set.
HandleSet copy(HandleSet handleSet) #
Copy the contents of the HandleSet into this instance.
bool isSet(ISelectable.Handle handle) [public] #
Check whether the handle has been set.
fd_set* opCast() [public] #
Cast the current object to a pointer to an fd_set, to be used with the select() system call.