I am having some problems with my network IO code on OS5 of BlackBerry.
I keep getting sporadic hangs and eventually TCP timeout exceptions during my IO operations.
I am using the 5.0 networking APIs for establishing the connection which works flawlessly every time. The problem is when doing the actual IO. I have a background worker thread that services IO requests from a queue. There is only a single background thread so all requests are serialized onto this thread.
Completion notification is done through a delegate interface that is passed in when the request is queued. The completion delegate is called on the background worker thread but clients are free to repost this to the event thread via invokeLater to do UI updates etc.
Notes:
HttpRequest is my own class that holds data about the request.
MutableData is my own class that holds the data that is read.
BUFFER_SIZE = 2048
HttpConnection getConnectionForRequest(final HttpRequest inRequest) {
final String url = inRequest.getURL();
final int[] availableTransportTypes = TransportInfo.getAvailableTransportTypes();
final ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setPreferredTransportTypes(availableTransportTypes);
connectionFactory.setConnectionMode(ConnectionFactory.ACCESS_READ);
final ConnectionDescriptor connectionDescriptor = connectionFactory.getConnection(url);
HttpConnection connection = null;
if (connectionDescriptor != null) {
connection = (HttpConnection) connectionDescriptor.getConnection();
}
return connection;
}
public void run() {
while (isRunning()) {
final HttpRequest request = waitForRequest(); // This blocks waiting on a request to appear in the queue.
final HttpConnection connection = getConnectionForRequest(request);
final MutableData data = new MutableData();
final InputStream inputStream = connection.openInputStream();
final byte[] readBuffer = new byte[BUFFER_SIZE];
int chunkSize;
// *** The following read call sporadically hangs and eventually throws a TCP timeout exception.
while ((chunkSize = inputStream.read(readBuffer, 0, BUFFER_SIZE)) != -1) {
data.appendData(readBuffer, 0, chunkSize);
}
mDelegate.receivedDataForRequest(request, data);
}
}
When it hangs it always eventually throws a TCP timeout error after about 30 seconds or so. If this occured occasionaly I would just chalk it up to normal network congestion but it happens frequently enough to indicate a deeper problem.
Edit:
It happens on various simulators and the 2 physical devices I have. The simulators I have tried are... Storm 9550 Tour 9630 Bold 9000 Pearl 9100 Curve 8530
I have a Curve 8530 and Storm 9550 devices and it happens on both of those as well.
Any help would be appreciated.