License:
BSD style: see license.txtAuthor:
Juan Jose ComellasSee Also:
ISelectorExamples:
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import tango.io.selector.model.ISelector; import tango.net.device.Socket; import tango.io.Stdout; AbstractSelector selector; SocketConduit conduit1; SocketConduit conduit2; MyClass object1; MyClass object2; uint eventCount; // Initialize the selector assuming that it will deal with 2 conduits and // will receive 2 events per invocation to the select() method. selector.open(2, 2); selector.register(conduit, Event.Read, object1); selector.register(conduit, Event.Write, object2); eventCount = selector.select(); if (eventCount > 0) { char[16] buffer; int count; foreach (SelectionKey key, selector.selectedSet()) { if (key.isReadable()) { count = (cast(SocketConduit) key.conduit).read(buffer); if (count != IConduit.Eof) { Stdout.format("Received '{0}' from peer\n", buffer[0..count]); selector.register(key.conduit, Event.Write, key.attachment); } else { selector.unregister(key.conduit); key.conduit.close(); } } if (key.isWritable()) { count = (cast(SocketConduit) key.conduit).write("MESSAGE"); if (count != IConduit.Eof) { Stdout("Sent 'MESSAGE' to peer\n"); selector.register(key.conduit, Event.Read, key.attachment); } else { selector.unregister(key.conduit); key.conduit.close(); } } if (key.isError() || key.isHangup() || key.isInvalidHandle()) { selector.unregister(key.conduit); key.conduit.close(); } } } selector.close(); |
Params:
size | value that provides a hint for the maximum amount of conduits that will be registered |
maxEvents | value that provides a hint for the maximum amount of conduit events that will be returned in the selection set per call to select. |
Remarks:
Not all of the selectors need to free resources other than allocated memory, but those that do will normally also add a call to close() in their destructors.Params:
conduit | conduit that will be associated to the selector |
events | bit mask of Event values that represent the events that will be tracked for the conduit. |
attachment | optional object with application-specific data that will be available when an event is triggered for the conduit |
Examples:
1 2 3 4 5 | AbstractSelector selector; SocketConduit conduit; MyClass object; selector.register(conduit, Event.Read | Event.Write, object); |
Params:
conduit | conduit 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.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.Remarks:
This method is the same as calling select(TimeSpan.max).Note:
This representation of timeout is not always accurate, so it is possible that the function will return with a timeout before the specified period. For more accuracy, use the TimeSpan version.Params:
timeout | the maximum amount of time in seconds 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.Params:
timeout | TimeSpan 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.Remarks:
If the call to select() was unsuccessful or it did not return any events, the returned value will be null.Remarks:
If the conduit is not registered to the selector the returned value will be null. No exception will be thrown by this method.Params:
file | name of the source file where the check is being made; you would normally use __FILE__ for this parameter. |
line | line number of the source file where this method was called; you would normally use __LINE__ for this parameter. |
Throws:
RegisteredConduitException when the conduit should not be registered but it is (EEXIST); UnregisteredConduitException when the conduit should be registered but it isn't (ENOENT); InterruptedSystemCallException when a system call has been interrupted (EINTR); OutOfMemoryException if a memory allocation fails (ENOMEM); SelectorException for any of the other cases in which errno is not 0.