// 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; }
@Override public boolean send(ChannelIO cio) throws IOException { if (fileChannel == null) { throw new IllegalStateException(); } if (position < 0) { throw new IllegalStateException(); } if (position >= length) { return false; } position += cio.tranferTo(fileChannel, position, length - position); return (position < length); }
void runServer() throws Exception { for (; ; ) { SocketChannel sc = ssc.accept(); ChannelIO cio = (sslContext != null ? ChannelIOSecure.getInstance(sc, true /* blocking */, sslContext) : ChannelIO.getInstance(sc, true /* blocking */)); RequestServicer svc = new RequestServicer(cio); svc.run(); } }
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(); } } }
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); }
/* * 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(); }