@Override public void sendRequest(SelectionKey key, BufHandler output) { output.submit(key, false, ByteBuffer.wrap("HELO\n".getBytes())); }
private void doRead(final SelectionKey key) { SocketChannel ch = (SocketChannel) key.channel(); BufHandler bufHandler = atta(key).getInput(); try { buffer.clear(); // clear for read int read = ch.read(buffer); if (read == -1) { // remote entity shut the socket down cleanly. clientClose(key); } else if (read > 0) { buffer.flip(); // flip for read bufHandler.submit(key, false, buffer); } } catch (IOException e) { // the remote forcibly closed the connection clientClose(key); } }
@Override public boolean submit(SelectionKey key, boolean copyOnSchedule, ByteBuffer... buffers) { for (ByteBuffer buf : buffers) { if (buf == NetCtx.CLOSE) { numCloses++; } else { int pos = buf.position(); byte[] b = new byte[buf.remaining()]; buf.get(b); sb.append(new String(b)); if (output != null) { buf.position(pos); } } } return output == null || output.submit(key, copyOnSchedule, buffers); }
@Override public HttpEncoder finish(SelectionKey key) { finishHeaders(key); ByteBuffer bb = ByteBuffer.wrap(buf.get(), 0, buf.length()); BufHandler out = output != null ? output : NetCtx.fromKey(key).getOutput(); out.submit(key, true, bb, body, keepAlive() ? NetCtx.FLUSH : NetCtx.CLOSE); state |= FINISHED; return this; }
@Override public boolean submit(SelectionKey key, boolean copyOnSchedule, ByteBuffer... buffers) { // TODO make this thing fully streaming, do not use ByteArrayOutputStream, make it thread safe for (int i = 0; i < buffers.length; i++) { ByteBuffer buf = buffers[i]; while (buf.hasRemaining()) { byte b = buf.get(); os.write(b); if (b == '\n') { byte[] a = os.toByteArray(); os.reset(); ByteBuffer bb = ByteBuffer.wrap(a); BufHandler out = NetCtx.fromKey(key).getOutput(); //out.submit(key, true, bb, a[0] == 'q' ? NetAtta.CLOSE : NetAtta.NULL); if (isClose()) { out.submit(key, true, bb, NetCtx.CLOSE); } else { out.submit(key, true, bb); } return !(buf.hasRemaining() || i < buffers.length-1); } } } return true; }
private void forceClose(SelectionKey key, NetCtx atta, TlsContext tctx, SSLEngine engine) { try { if (!engine.isInboundDone()) { engine.closeInbound(); } if (!engine.isOutboundDone()) { engine.closeOutbound(); } atta.getInput().submit(key, false, NetCtx.CLOSE); } catch (Exception e) { log.debug("forcible close: " + e.getMessage()); } try { key.interestOps(0); key.channel().close(); } catch (Exception e) { log.debug("forcibly closing SSL connection: " + e.getMessage()); } tctx.setSslState(CLOSED); return; }
case OK: buffer.flip(); atta(key).getInput().submit(key, false, buffer); break; case BUFFER_OVERFLOW:
case OK: tctx.getSslNetBuf().flip(); if (!atta.getOutput().submit(key, true, tctx.getSslNetBuf())) { return;