Network I / O freezes on BlackBerry OS 5

I have some problems with my network I / O code on OS 5 BlackBerry.

I keep getting sporadic hangs and ultimately TCP timeout exceptions during I / O.

I use network API 5.0 to establish a connection that works flawlessly every time.

The problem is when you do the actual I / O. I have a worker workflow that serves queue I / O requests. There is only one background thread, so all requests are serialized into this thread.

Notification of completion is done through the delegate interface, which is sent when the request is sent.

The completion deligator is called in the background thread, but clients can re-send it to the event stream through invokeLaterto update the user interface, etc.

Notes:
HttpRequest is my own class that contains request data.
MutableData is my own class that contains 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()) {
        // 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);
    }
}

When it freezes, it always throws a TCP timeout error after about 30 seconds or so. If this happened occasionally, I would just prevent it from getting to the normal network congestion, but it happens often enough to indicate a deeper problem.

Edit:

This happens on different simulators and on two physical devices that I have. The simulators I tried ...

  • Storm 9550
  • Tour 9630
  • Bold 9000
  • Pearl 9100
  • 8530

Curve 8530 Storm 9550, .

.

+3
4

- - , . , , , reset . , , , , .

, , , , , , TCP, .

- , , , , . , .

, . , , , .

0

, Available(). , , , . - .

0

, ? , API , , , , , .

0

, read(byte[], int, int) BlackBerry, - 4.5 6.0.
InputStream, read(byte[], int, int) read(), , .

RIM read(byte[], int, int), :

read (b, off, len) InputStream read(). IOException, read (b, off, len). - read() IOException, , ; , , b, , , . .

, , . , , , . - available(), , . RIM available(), , read() , (). , . " " , .

, , :

public int read(byte[] bts, int st, int len) throws IOException {
    if(len == 0) {
        return 0;
    }
    int readByte = this.read();
    if(readByte == -1) {
        return 0;
    }
    bts[st] = (byte)readByte;
    return 1;
}
0
source

Source: https://habr.com/ru/post/1753778/


All Articles