Search code examples
androidnetwork-programmingnio

NIO selector.select() not working properly on long poll HTTP since Android 2.3?


I use comet style communication with my android app. It worked all fine until I used Gingerbread (Emulator, CyanogenMod 7). The problem I have is that

Selector selector = Selector.open();
channel.configureBlocking(false);
channel.connect(socketAddress);
channel.socket().setKeepAlive(true);
channel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ);

if(selector.select() > 0){
 //DO STUFF
}

the

selector.select()>0

returned true if I sent an event from the server but now it just ignores it. Exactly the same code works in Android 1.6 - 2.2

I think this is a bug... someone can confirm the same issue or provide a workaround?


Solution

  • i found the solution, but not why it worked before.

    channel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ);
    

    does not work anymore.

    I had to register another selector only dealing with OP_READ and now it works.

    channel.register(readSelector, SelectionKey.OP_READ);
    channel.register(connectSelector, SelectionKey.OP_CONNECT);
    
    if(connectSelector.select() > 0){
       //Do connect stuff
    } else if(readSelector.select() > 0){
       //Do read stuff
    }
    

    I hope this saves some headaches.

    EDIT: http://code.google.com/p/android/issues/detail?id=15055

    I posted it as a issue on the android issuetracker and it is under investigation