Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3865 for trunk/example

Show
Ignore:
Timestamp:
08/08/08 17:50:28 (4 months ago)
Author:
schveiguy
Message:

Fixed selector example to work with new TimeSpan? and Selector code, fixed a previously unseen bug.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/example/networking/selector.d

    r3856 r3865  
    113113    try 
    114114    { 
    115         TimeSpan            timeout         = TimeSpan.seconds(1); 
     115        TimeSpan            timeout         = TimeSpan.fromSeconds(1); 
    116116        InternetAddress     addr            = new InternetAddress(SERVER_ADDR, SERVER_PORT); 
    117117        ServerSocket        serverSocket    = new ServerSocket(addr, 5); 
     
    139139            if (eventCount > 0) 
    140140            { 
     141                ISelectable[] removeThese; 
    141142                foreach (SelectionKey selectionKey; selector.selectedSet()) 
    142143                { 
     
    178179                                    log.trace(sprint("[{0}] Received {1} from client ({2} bytes)", 
    179180                                                     i, buffer[0..count], count)); 
    180                                 selector.reregister(selectionKey.conduit, Event.Write); 
     181                                selector.register(selectionKey.conduit, Event.Write); 
    181182                                receiveCount++; 
    182183                            } 
     
    186187                                    log.trace(sprint("[{0}] Handle {1} was closed; removing it from Selector", 
    187188                                                     i, cast(int) selectionKey.conduit.fileHandle())); 
    188                                 selector.unregister(selectionKey.conduit); 
    189                                 (cast(SocketConduit) selectionKey.conduit).close(); 
     189                                // note, we cannot unregister because we are 
     190                                // in the middle of a foreach loop.  Delay 
     191                                // unregistering and closing until after the 
     192                                // loop is done. 
     193                                //selector.unregister(selectionKey.conduit); 
     194                                //(cast(SocketConduit) selectionKey.conduit).close(); 
     195                                removeThese ~= selectionKey.conduit; 
    190196                                failedReceiveCount++; 
    191197                                continue; 
     
    205211                                log.trace(sprint("[{0}] Sent PONG to client ({1} bytes)", i, count)); 
    206212 
    207                             selector.reregister(selectionKey.conduit, Event.Read); 
     213                            selector.register(selectionKey.conduit, Event.Read); 
    208214                            sendCount++; 
    209215                        } 
     
    213219                                log.trace(sprint("[{0}] Handle {1} was closed; removing it from Selector", 
    214220                                                 i, selectionKey.conduit.fileHandle())); 
    215                             selector.unregister(selectionKey.conduit); 
    216                             (cast(SocketConduit) selectionKey.conduit).close(); 
     221                            // note, see comment above 
     222                            //selector.unregister(selectionKey.conduit); 
     223                            //(cast(SocketConduit) selectionKey.conduit).close(); 
     224                            removeThese ~= selectionKey.conduit; 
    217225                            failedSendCount++; 
    218226                            continue; 
     
    246254                                             i, cast(int) selectionKey.conduit.fileHandle())); 
    247255                        } 
    248                         selector.unregister(selectionKey.conduit); 
    249                         (cast(Conduit) selectionKey.conduit).close(); 
     256                        // note, see comment above 
     257                        //selector.unregister(selectionKey.conduit); 
     258                        //(cast(Conduit) selectionKey.conduit).close(); 
     259                        removeThese ~= selectionKey.conduit; 
    250260 
    251261                        if (selectionKey.conduit !is serverSocket) 
     
    258268                        } 
    259269                    } 
     270                } 
     271                foreach(c; removeThese) 
     272                { 
     273                    selector.unregister(c); 
     274                    (cast(Conduit) c).close(); 
    260275                } 
    261276            }