n3utrino
n3utrino

Reputation: 2381

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?

Upvotes: 0

Views: 805

Answers (2)

n3utrino
n3utrino

Reputation: 2381

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

Upvotes: 1

Mlove
Mlove

Reputation: 203

I have run into a similar issue. When I try to send something the selector does not get out of the blocking mode. So I can only recieve the network packets and not send any. I will give this a shot and let you know. I am running android 2.3.3 on a nexus one.

Upvotes: 0

Related Questions