private final void remainingRequest() { immediateRequestsSent = 0; passiveRequestsSent = 0; for (OnDemandNode onDemandNode = (OnDemandNode) sentRequests.getBack(); onDemandNode != null; onDemandNode = (OnDemandNode) sentRequests.getPrevious()) { if (onDemandNode.immediate) { immediateRequestsSent++; } else { passiveRequestsSent++; } } while (immediateRequestsSent < 10) { OnDemandNode onDemandNode = (OnDemandNode) toRequest.popTail(); if (onDemandNode == null) { break; } if (filePriorities[onDemandNode.type][onDemandNode.id] != 0) { extrasLoaded++; } filePriorities[onDemandNode.type][onDemandNode.id] = (byte) 0; sentRequests.insertBack(onDemandNode); immediateRequestsSent++; sendRequest(onDemandNode); expectData = true; } }
private final void handleResp() { try { int available = inputStream.available(); if ((toRead == 0) && (available >= 6)) { expectData = true; for (int i = 0; i < 6; i += inputStream.read(inputBuffer, i, 6 - i)) {; } int type = inputBuffer[0] & 0xff; int id = ((inputBuffer[1] & 0xff) << 8) + (inputBuffer[2] & 0xff); int size = ((inputBuffer[3] & 0xff) << 8) + (inputBuffer[4] & 0xff); int part = inputBuffer[5] & 0xff; onDemandNode = null; for (OnDemandNode ondemandnode = (OnDemandNode) sentRequests.getBack(); ondemandnode != null; ondemandnode = (OnDemandNode) sentRequests.getPrevious()) { if ((ondemandnode.type == type) && (ondemandnode.id == id)) { onDemandNode = ondemandnode; } if (onDemandNode != null) { ondemandnode.cyclesSinceSend = 0; } } if (onDemandNode != null) { idleCycles = 0; if (size == 0) { SignLink.reportError("Rej: " + type + "," + id); onDemandNode.buffer = null; if (onDemandNode.immediate) { synchronized (completed) { completed.insertBack(onDemandNode); } } else { onDemandNode.remove(); } onDemandNode = null; } else { if ((onDemandNode.buffer == null) && (part == 0)) { onDemandNode.buffer = new byte[size]; } if ((onDemandNode.buffer == null) && (part != 0)) { throw new IOException("missing start of file"); } } } offset = part * 500; toRead = 500; if (toRead > (size - (part * 500))) { toRead = size - (part * 500); } } if ((toRead <= 0) || (available < toRead)) { return; } expectData = true; byte[] buffer = inputBuffer; int bufferOffset = 0; if (onDemandNode != null) { buffer = onDemandNode.buffer; bufferOffset = offset; } for (int i = 0; i < toRead; i += inputStream.read(buffer, i + bufferOffset, toRead - i)) {; } if (((toRead + offset) >= buffer.length) && (onDemandNode != null)) { if (client.stores[0] != null) { client.stores[onDemandNode.type + 1].put(buffer.length, buffer, onDemandNode.id); } if (!onDemandNode.immediate && (onDemandNode.type == 3)) { onDemandNode.immediate = true; onDemandNode.type = 93; } if (onDemandNode.immediate) { synchronized (completed) { completed.insertBack(onDemandNode); } } else { onDemandNode.remove(); } } toRead = 0; } catch (IOException ioexception) { try { socket.close(); } catch (Exception exception) { /* empty */ } socket = null; inputStream = null; outputStream = null; toRead = 0; } }
@Override public final void run() { try { while (running) { cycle++; int toWait = 20; if ((highestPriority == 0) && (client.stores[0] != null)) { toWait = 50; } Thread.sleep(toWait); expectData = true; for (int i = 0; i < 100; i++) { if (!expectData) { break; } expectData = false; localComplete(); remainingRequest(); if ((immediateRequestsSent == 0) && (i >= 5)) { break; } passivesRequest(); if (inputStream != null) { handleResp(); } } boolean idle = false; for (OnDemandNode onDemandNode = (OnDemandNode) sentRequests.getBack(); onDemandNode != null; onDemandNode = (OnDemandNode) sentRequests.getPrevious()) { if (onDemandNode.immediate) { idle = true; onDemandNode.cyclesSinceSend++; if (onDemandNode.cyclesSinceSend > 50) { onDemandNode.cyclesSinceSend = 0; sendRequest(onDemandNode); } } } if (!idle) { for (OnDemandNode onDemandNode = (OnDemandNode) sentRequests.getBack(); onDemandNode != null; onDemandNode = (OnDemandNode) sentRequests.getPrevious()) { idle = true; onDemandNode.cyclesSinceSend++; if (onDemandNode.cyclesSinceSend > 50) { onDemandNode.cyclesSinceSend = 0; sendRequest(onDemandNode); } } } if (idle) { idleCycles++; if (idleCycles > 750) { socket.close(); socket = null; inputStream = null; outputStream = null; toRead = 0; } } else { idleCycles = 0; message = ""; } if (client.loggedIn && (socket != null) && (outputStream != null) && ((highestPriority > 0) || (client.stores[0] == null))) { sinceKeepAlive++; if (sinceKeepAlive > 500) { sinceKeepAlive = 0; inputBuffer[0] = (byte) 0; inputBuffer[1] = (byte) 0; inputBuffer[2] = (byte) 0; inputBuffer[3] = (byte) 10; try { outputStream.write(inputBuffer, 0, 4); } catch (IOException ioexception) { idleCycles = 5000; } } } } } catch (Exception exception) { SignLink.reportError("od_ex " + exception); } }