Ejemplo n.º 1
1
  /**
   * Handle the socket as a SEND.
   *
   * @return false when socket is closed (or should be closed), true will cause the method to be
   *     called again.
   */
  protected boolean handleSend() {
    try {
      final byte[] data = new byte[blockSize];
      final int bytesRead = fileIn.read(data);
      readSize += bytesRead;

      if (bytesRead > 0) {
        for (DCCTransferHandler handler : handlers.get(DCCTransferHandler.class)) {
          handler.dataTransferred(this, bytesRead);
        }
        out.write(data, 0, bytesRead);
        out.flush();

        // Wait for acknowledgement packet.
        if (!turbo) {
          int bytesReceived;
          do {
            bytesReceived = in.readInt();
          } while (readSize - bytesReceived > 0);
        }

        if (readSize == size) {
          fileIn.close();

          // Process all the ack packets that may have been sent.
          // In true turbo dcc mode, none will have been sent and the socket
          // will just close, in fast-dcc mode all the acks will be here,
          // So keep reading acks until the socket closes (IOException) or we
          // have received all the acks.
          if (turbo) {
            int ack;
            do {
              try {
                ack = in.readInt();
              } catch (IOException e) {
                break;
              }
            } while (ack > 0 && readSize - ack > 0);
          }

          return false;
        }

        return true;
      } else if (bytesRead < 0) {
        fileIn.close();
        return true;
      }
    } catch (IOException e) {
      return false;
    }
    return false;
  }
Ejemplo n.º 2
0
  /**
   * Handle the socket as a RECEIVE.
   *
   * @return false when socket is closed (or should be closed), true will cause the method to be
   *     called again.
   */
  protected boolean handleReceive() {
    try {
      final byte[] data = new byte[blockSize];
      final int bytesRead = in.read(data);
      readSize += bytesRead;

      if (bytesRead > 0) {
        for (DCCTransferHandler handler : handlers.get(DCCTransferHandler.class)) {
          handler.dataTransferred(this, bytesRead);
        }
        fileOut.write(data, 0, bytesRead);

        if (!turbo) {
          // Send ack
          out.writeInt((int) readSize);
          out.flush();
        }

        if (readSize == size) {
          fileOut.close();

          if (turbo) {
            in.close();
          }

          return false;
        } else {
          return true;
        }
      } else if (bytesRead < 0) {
        fileOut.close();
        return false;
      }
    } catch (IOException e) {
      return false;
    }
    return false;
  }