Example #1
0
  private void readText(Connection c) throws IOException {
    sc = c.channel();
    buffer.clear();

    if (sc == null || c.isWriteDisabled() || !sc.isOpen()) {
      c.close(false);
    }

    try {
      synchronized (sc) {
        read = sc.read(buffer);
      }
    } catch (IOException e) {
      if (debugEnabled) log.debug("IOException reading from connection " + c, e);
      c.close(false);
    }

    if (read > 0) {
      buffer.flip();

      if (debugEnabled)
        log.debug("Connection " + c + " about to process data from buffer " + buffer);

      if (workersEnabled) {
        try {
          choose().add(c, buffer);
        } catch (RuntimeException e) {
          if (debugEnabled) log.debug("Cannot queue packet " + buffer + " for connection " + c, e);
          c.close(false);
        }
      } else {
        try {
          if (!c.processData(buffer)) {
            c.close(false);
            return;
          }
        } catch (Exception e) {
          log.error(
              e.getClass().getSimpleName()
                  + " while processing buffer "
                  + buffer
                  + " for connection "
                  + c,
              e);
        }
      }
    } else if (read < 0) {
      c.close(false);
    }
  }
Example #2
0
  private void readBinary(Connection c) throws IOException {
    sc = c.channel();
    buffer.clear();
    oldLimit = 0;
    bufferSize = 0;
    read = 0;
    totalRead = 0;

    if (sc == null || c.isWriteDisabled() || !sc.isOpen()) {
      c.close(false);
    }

    try {
      synchronized (sc) {
        totalRead = read = sc.read(buffer);
      }
    } catch (IOException e) {
      if (debugEnabled) log.debug("IOException reading from connection " + c, e);
      c.close(false);
    }

    if (read > 0) {
      bufferSize = buffer.getShort(0);
      tries = 0;

      if (debugEnabled)
        log.debug(
            "Connection " + c + " about to process " + buffer + ", packet size: " + bufferSize);

      while (buffer.position() < bufferSize && tries < maxTries) {
        tries++;

        before = System.currentTimeMillis();

        synchronized (sc) {
          totalRead += read = sc.read(buffer);
        }

        if (read == 0) {
          if (sc.keyFor(retrySelector) == null) {
            sc.register(retrySelector, SelectionKey.OP_READ);
          } else {
            sc.keyFor(retrySelector).interestOps(SelectionKey.OP_READ);
          }
          retrySelection = retrySelector.select();
          if (retrySelection > 0) {
            retrySelector.selectedKeys().clear();
          }
        }
      }

      if (sc.keyFor(retrySelector) != null) {
        sc.keyFor(retrySelector).interestOps(0);
      }

      if (tries == maxTries) {
        if (debugEnabled)
          log.error(
              "Too much read tries ("
                  + maxTries
                  + ") without any bytes read (read: "
                  + totalRead
                  + ", remaining: "
                  + buffer.remaining()
                  + ") for connection "
                  + c
                  + ", kicking client");
        c.close(false);
        return;
      } else if (tries > 0 && debugEnabled) {
        timeLostInRetries += System.currentTimeMillis() - before;
        log.debug(
            "Read successfully "
                + totalRead
                + " bytes after "
                + tries
                + " tries (total time lost: "
                + timeLostInRetries
                + " ms)");
      }

      buffer.flip();

      while (buffer.remaining() > 2 && buffer.remaining() >= buffer.getShort(buffer.position())) {
        try {
          bufferSize = buffer.getShort();

          if (bufferSize > 1) {
            bufferSize -= 2;
          }

          oldLimit = buffer.limit() - 2;
          buffer.compact();
          buffer.limit(bufferSize);
          buffer.position(0);
        } catch (IllegalArgumentException e) {
          if (debugEnabled)
            log.debug(
                "Illegal argument while parsing buffer "
                    + buffer
                    + " (read: "
                    + totalRead
                    + ", awaited: "
                    + bufferSize
                    + ") for connection "
                    + c,
                e);
          c.close(false);
          return;
        }

        if (debugEnabled)
          log.debug("Connection " + c + " about to process data from buffer " + buffer);

        if (workersEnabled) {
          try {
            choose().add(c, buffer);
          } catch (RuntimeException e) {
            if (debugEnabled)
              log.debug("Cannot queue packet " + buffer + " for connection " + c, e);
            c.close(false);
          }
        } else {
          try {
            if (c.processData(buffer)) {
              if (buffer.position() != bufferSize) {
                if (debugEnabled)
                  log.debug(
                      "After processing, buffer position is not as expected: expected "
                          + (bufferSize)
                          + ", buffer: "
                          + buffer
                          + ", fixing...");
                buffer.position(bufferSize);
              }
            } else {
              c.close(false);
              return;
            }
          } catch (Exception e) {
            if (debugEnabled)
              log.error(
                  e.getClass().getSimpleName()
                      + " while processing buffer "
                      + buffer
                      + " for connection "
                      + c,
                  e);
            c.close(false);
          }
        }

        if (oldLimit > buffer.position()) {
          buffer.limit(oldLimit);
          if (debugEnabled)
            log.debug(
                "Connection "
                    + c
                    + ": buffer "
                    + buffer
                    + " has more packets (old limit: "
                    + oldLimit
                    + ", last packet size: "
                    + bufferSize
                    + ", next packet size: "
                    + buffer.getShort(buffer.position())
                    + ") ...");
          buffer.compact();
          buffer.position(0);
          buffer.limit(oldLimit - bufferSize);
          if (debugEnabled)
            log.debug(
                "Connection "
                    + c
                    + " about to process next packet from buffer "
                    + buffer
                    + ", next packet size: "
                    + buffer.getShort(0));
        } else {
          if (debugEnabled)
            log.debug(
                "Connection "
                    + c
                    + " buffer "
                    + buffer
                    + " seems entirely read, old limit: "
                    + oldLimit
                    + ", read: "
                    + totalRead);
          break;
        }
      }

      if (buffer.hasRemaining()) {
        if (debugEnabled)
          log.error(
              "Buffer "
                  + buffer
                  + " still has data (awaited: "
                  + bufferSize
                  + ", read: "
                  + totalRead
                  + "), discarding and closing connection "
                  + c
                  + "...");
        c.close(false);
        return;
      }
    } else if (read < 0) {
      c.close(false);
    }
  }