@Override public NetCtx create(SocketChannel ch, BufHandler in, BufHandler out) { return new NetCtx(ch, in, out); } };
@Override public boolean submit(SelectionKey key, boolean copyOnSchedule, ByteBuffer... buffers) { return decoder.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; }
@BeforeClass public static void startServer() throws Exception { server = new NetServer("test-server", "127.0.0.1", 19001, new BufHandlerFactory() { @Override public BufHandler create(SocketChannel ch) { return new HttpProtocolHandler(new HttpConfig(), new SvcListener()); } }, null); server.start(); client = new NetClient("test-client", NetCtxFactory.DEFAULT, null); client.start(); }
protected void connect(SelectionKey key) { NetCtx atta = NetCtx.fromKey(key); NetRequest req = (NetRequest)atta.getInput(); try { boolean connected = atta.getChannel().finishConnect(); if (connected) { // TODO key.interestOps(OP_WRITE|OP_READ); // TODO if TLS enabled, begin handshake here ... req.sendRequest(key, this); } } catch (IOException e) { // TODO notify client that connection failed, or retry log.error("Cannot finish connect for " + req, e); } }
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); } }
@BeforeClass public static void startServer() throws Exception { server = new NetServer("test-server", "127.0.0.1", 19005, new BufHandlerFactory() { @Override public BufHandler create(SocketChannel ch) { return new EchoHandler(true); } }, null); server.start(); client = new NetClient("test-client", NetCtxFactory.DEFAULT, null); client.start(); }
private void saveNetBuf(TlsContext tctx) { int rem = tctx.getSslNetBuf().remaining(); byte[] b = new byte[rem]; tctx.getSslNetBuf().get(b); tctx.setSslReadChunk(b); if (rem == tctx.getSslNetBuf().capacity()) { // Did not consume anything; extend buffer by 1kB tctx.setSslNetBuf(ByteBuffer.allocate(tctx.getSslNetBuf().capacity() + 1024)); } }
protected void clientClose(final SelectionKey key) { log.traceNio(key,"clientClose()", "ENTER"); try { key.channel().close(); } catch (Exception ignore) { } NetCtx att = (NetCtx) key.attachment(); if (att != null) { synchronized (att) { if (att.getTlsContext() != null) att.getTlsContext().setSslState(CLOSING); } } }
@Override public boolean submit(NetRequest req) { req.sendRequest(this.clientKey(), this); return true; } }
@Override public RingHandler response(SelectionKey key, String httpVersion, int status, String statusMessage) { throw new NetException("Should not happen."); }
@AfterClass public static void stopServer() throws Exception { if (server != null) { server.stop(1000); server = null; } }
@Override public HttpMessageHandler error(SelectionKey key, int status, String message, Object data, Throwable e) { throw new ProtocolException(message + ": '" + data + "'", null); }
public void stop(int timeout) { try { serverChannel.close(); // stop accept any request } catch (IOException ignore) { } // wait all requests to finish, at most timeout milliseconds // TODO bufHandler.close(timeout); closeConnections(); }
public static <T> T coerceObj(Object v, Class<T> clazz, String msg) { if (v == null) { throw new ArgumentException(msg + ": expected " + clazz.getName() + ", got null"); } if (!clazz.isInstance(v)) { throw new ArgumentException(msg + ": expected " + clazz.getName() + ", got " + v.getClass().getName()); } return (T)v; }
public SSLContext getSslContext() { try { return sslContext != null ? sslContext : SSLContext.getDefault(); } catch (NoSuchAlgorithmException e) { throw new NetException("Error initializing SSL context", e); } }
@AfterClass public static void stopServer() throws Exception { if (server != null) { server.stop(1000); server = null; } }