Пример #1
0
  // Returns true when request is complete
  // May expand rbb if more room required
  //
  private boolean receive(SelectionKey sk) throws IOException {
    ByteBuffer tmp = null;

    if (requestReceived) {
      return true;
    }

    if (!cio.doHandshake(sk)) {
      return false;
    }

    if ((cio.read() < 0) || Request.isComplete(cio.getReadBuf())) {
      rbb = cio.getReadBuf();
      return (requestReceived = true);
    }
    return false;
  }
Пример #2
0
  public void handle(SelectionKey sk) throws IOException {
    try {

      if (request == null) {
        if (!receive(sk)) return;
        rbb.flip();
        if (parse()) build();
        try {
          reply.prepare();
        } catch (IOException x) {
          reply.release();
          reply = new Reply(Reply.Code.NOT_FOUND, new StringContent(x));
          reply.prepare();
        }
        if (send()) {
          // More bytes remain to be written
          sk.interestOps(SelectionKey.OP_WRITE);
        } else {
          // Reply completely written; we're done
          if (cio.shutdown()) {
            cio.close();
            reply.release();
          }
        }
      } else {
        if (!send()) { // Should be rp.send()
          if (cio.shutdown()) {
            cio.close();
            reply.release();
          }
        }
      }
    } catch (IOException x) {
      String m = x.getMessage();
      if (!m.equals("Broken pipe") && !m.equals("Connection reset by peer")) {
        System.err.println("RequestHandler: " + x.toString());
      }

      try {
        /*
         * We had a failure here, so we'll try to be nice
         * before closing down and send off a close_notify,
         * but if we can't get the message off with one try,
         * we'll just shutdown.
         */
        cio.shutdown();
      } catch (IOException e) {
        // ignore
      }

      cio.close();
      if (reply != null) {
        reply.release();
      }
    }
  }
Пример #3
0
  public boolean send(ChannelIO cio) throws IOException {
    if (fc == null) throw new IllegalStateException();
    if (position < 0) // NB only
    throw new IllegalStateException();

    /*
     * Short-circuit if we're already done.
     */
    if (position >= length) {
      return false;
    }

    position += cio.transferTo(fc, position, length - position);
    return (position < length);
  }
Пример #4
0
 /*
  * Writes bb to the SocketChannel.
  * <P>
  * Returns true when the ByteBuffer has no remaining data.
  */
 private boolean tryFlush(ByteBuffer bb) throws IOException {
   super.write(bb);
   return !bb.hasRemaining();
 }
Пример #5
0
  static ChannelIO getInstance(SocketChannel sc, boolean blocking) throws IOException {
    ChannelIO cio = new ChannelIO(sc, blocking);
    cio.requestBB = ByteBuffer.allocate(requestBBSize);

    return cio;
  }