Changeset 1250


Ignore:
Timestamp:
Oct 22, 2010, 8:23:21 AM (13 years ago)
Author:
Nicklas Nordborg
Message:

Added NetworkReader.disconnect() to make sure that the port used for incoming connections is closed when stopping the client.

Fixes a race condition that caused startup to hang if the new thread in NetworkLoop.connect() reaches the selector.select() line in the listen() method before the selector has been registered with the server channel by the calling code in NetworkReader.connect(): serverCh.register(selector, SelectionKey.OP_ACCEPT);).

Location:
extensions/net.sf.basedb.torrent/trunk/src/external/hpbtc/hpbtc/protocol/network
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.torrent/trunk/src/external/hpbtc/hpbtc/protocol/network/NetworkLoop.java

    r1248 r1250  
    3535    public int connect() throws IOException {
    3636        selector = register.openSelector(stype);
     37        registerSelectors(selector);
    3738        running = true;
    3839        new Thread(new Runnable() {
     
    5556    }
    5657
     58    protected void registerSelectors(Selector selector)
     59      throws IOException
     60    {}
     61   
    5762    private void listen() throws IOException, NoSuchAlgorithmException {
    5863        while (running) {
  • extensions/net.sf.basedb.torrent/trunk/src/external/hpbtc/hpbtc/protocol/network/NetworkReader.java

    r1248 r1250  
    1616import java.nio.channels.SelectableChannel;
    1717import java.nio.channels.SelectionKey;
     18import java.nio.channels.Selector;
    1819import java.nio.channels.ServerSocketChannel;
    1920import java.nio.channels.SocketChannel;
     
    4041        serverCh.socket().bind(new InetSocketAddress(
    4142                InetAddress.getLocalHost(), port));
     43        serverCh.configureBlocking(false);
    4244        super.connect();
    43         serverCh.configureBlocking(false);
     45    }
     46
     47    @Override
     48    protected void registerSelectors(Selector selector)
     49      throws IOException
     50    {
     51      super.registerSelectors(selector);
    4452        serverCh.register(selector, SelectionKey.OP_ACCEPT);
    4553    }
    46 
     54   
    4755    @Override
    4856    public int connect() throws IOException {
     
    5058        ServerSocket s = serverCh.socket();
    5159        s.bind(null);
     60        serverCh.configureBlocking(false);
    5261        super.connect();
    53         serverCh.configureBlocking(false);
    54         serverCh.register(selector, SelectionKey.OP_ACCEPT);
    5562        return s.getLocalPort();
    5663    }
     
    102109
    103110    @Override
     111    public void disconnect() {
     112      try
     113      {
     114        serverCh.socket().close();
     115      }
     116      catch (IOException ex)
     117      {
     118        logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
     119      }
     120        super.disconnect();
     121    }
     122   
     123    @Override
    104124    protected void disconnect(final SelectionKey key) throws IOException {
    105125        final Peer peer = (Peer) key.attachment();
Note: See TracChangeset for help on using the changeset viewer.