我的BlackBerry OS 5上的网络I/O代码有问题。网络I/O挂在BlackBerry OS 5上
在我的I/O操作期间,我不断收到零星的挂起和最终的TCP超时异常。
我使用5.0网络API来建立每次完美工作的连接。
问题在于执行实际的I/O时。我有一个后台工作线程来处理来自队列的I/O请求。只有一个后台线程,所以所有请求都被序列化到这个线程上。
完成通知是通过请求排队时传入的委托接口完成的。
完成委托调用后台工作线程,但客户可以自由通过invokeLater
重新发布这事件线程做UI更新等
注:
HttpRequest的是我自己的类拥有关于请求的数据。
MutableData是我自己的类,它保存读取的数据。
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()) {
// This blocks waiting on a request to appear in the queue.
final HttpRequest request = waitForRequest();
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);
}
}
当它挂起它约30秒左右之后总是最终抛出一个TCP超时错误。 如果偶尔发生这种情况,我只会将它记录为正常的网络拥塞,但它经常发生足以指示更深层的问题。
编辑:
它发生在各种模拟器和我有2个物理设备。 我曾尝试是模拟器...
- 风暴9550
- 粗体9000
- 明珠9100
- 曲线8530
我有一个曲线8530和Storm 9550设备,它也发生在这两者上。
任何帮助,将不胜感激。
这是在模拟器或真实设备? – 2010-07-08 19:56:56
添加设备和模拟器信息。 – 2010-07-08 21:01:54
我问的原因是因为我发现5.0模拟器在网络仿真方面相当麻烦 - 通常挂起连接没有明显的原因。但是,既然你也在真实设备上看到它,它肯定是不同的。 – 2010-07-08 21:31:13