@Override public void close() throws IOException { channel.close(); } }
@Override public <A> void read(final ByteBuffer[] dsts, final int offset, final int length, final long timeout, final TimeUnit unit, final A attach, final CompletionHandler<Long, ? super A> handler) { channel.read(dsts, offset, length, timeout, unit, attach, handler); }
@Override void write(ByteBuffer data, final org.glassfish.jersey.jdk.connector.internal.CompletionHandler<ByteBuffer> completionHandler) { socketChannel.isOpen(); socketChannel.write(data, data, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(Integer result, ByteBuffer buffer) { if (buffer.hasRemaining()) { write(buffer, completionHandler); return; } completionHandler.completed(buffer); } @Override public void failed(Throwable exc, ByteBuffer buffer) { completionHandler.failed(exc); } }); }
@Override void close() { if (socketChannel == null || !socketChannel.isOpen()) { return; } try { socketChannel.close(); } catch (IOException e) { LOGGER.log(Level.INFO, LocalizationMessages.TRANSPORT_CONNECTION_NOT_CLOSED(), e); } synchronized (TransportFilter.class) { openedConnections.decrementAndGet(); if (openedConnections.get() == 0 && channelGroup != null) { scheduleClose(); } } }
//Create an Asynchronous channel. No connection has actually been established yet AsynchronousSocketChannel asynchronousSocketChannel = AsynchronousSocketChannel.open(); /**Connect to an actual server on the given port and address. The operation returns a type of Future, the basis of the all asynchronous operations in java. In this case, a Void is returned because nothing is returned after a successful socket connection */ Void connect = asynchronousSocketChannel.connect(new InetSocketAddress("127.0.0.1", 5000)).get(); //Allocate data structures to use to communicate over the wire ByteBuffer helloBuffer = ByteBuffer.wrap("Hello !".getBytes()); //Send the message Future<Integer> successfullyWritten= asynchronousSocketChannel.write(helloBuffer); //Do some stuff here. The point here is that asynchronousSocketChannel.write() //returns almost immediately, not waiting to actually finish writing //the hello to the channel before returning control to the currently executing thread doSomethingElse(); //now you can come back and check if it was all written (or not) System.out.println("Bytes written "+successfullyWritten.get());
SSLEngineResult r = sslEngine.wrap(src, socketWriteBuffer); written += r.bytesConsumed(); Status s = r.getStatus(); throw new IllegalStateException(sm.getString( "asyncChannelWrapperSecure.statusWrap")); if (r.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { Runnable runnable = sslEngine.getDelegatedTask(); while (runnable != null) { runnable.run(); runnable = sslEngine.getDelegatedTask(); while (toWrite > 0) { Future<Integer> f = socketChannel.write(socketWriteBuffer); Integer socketWrite = f.get(); toWrite -= socketWrite.intValue(); future.complete(Long.valueOf(written)); } else { future.fail(new IllegalStateException(sm.getString( "asyncChannelWrapperSecure.wrongStateWrite")));
idle = AsynchronousSocketChannel.open(group); Future<Void> fut = idle.connect(new InetSocketAddress("localhost",50000)); fut.get();
@Override public void run() { try { sslEngine.beginHandshake(); handshakeStatus = sslEngine.getHandshakeStatus(); resultStatus = Status.OK; socketWriteBuffer.clear(); SSLEngineResult r = sslEngine.wrap(DUMMY, socketWriteBuffer); checkResult(r, true); socketWriteBuffer.flip(); Future<Integer> fWrite = socketChannel.write(socketWriteBuffer); fWrite.get(); break; resultStatus == Status.BUFFER_UNDERFLOW) { Future<Integer> fRead = socketChannel.read(socketReadBuffer); fRead.get(); sm.getString("asyncChannelWrapperSecure.notHandshaking"));
@Override public void connect(InetSocketAddress remoteAddress) throws IOException { String remoteHostname = remoteAddress.getHostString(); try { Future<Void> connectFuture = socketChannel.connect(remoteAddress); connectFuture.get(DEFAULT_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS); connected.set(true); } catch (ExecutionException | TimeoutException e) { throw TransportException.Wrapper.wrap(e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw TransportException.Wrapper.wrap(e); } packetReader.start(remoteHostname, this.soTimeout); }
@Override public NioChannel acceptChannel(AsynchronousServerSocketChannel listener) throws IOException { try { AsynchronousSocketChannel asyncChannel = listener.accept().get(); InetSocketAddress addr = (InetSocketAddress) asyncChannel.getRemoteAddress(); SSLEngine engine = sslContext.createSSLEngine(addr.getHostString(), addr.getPort()); NioChannel channel = new SecureNioChannel(asyncChannel, engine); return channel; } catch (Exception e) { throw new IOException(MESSAGES.sslHandshakeError(), e); } }
if (sslEngine.isOutboundDone()) { return; sslEngine.closeOutbound(); : this.netInBuffer.compact(); while (!sslEngine.isOutboundDone()) { SSLEngineResult res = sslEngine.wrap(this.netInBuffer, this.netOutBuffer); switch (res.getStatus()) { case OK: if (this.channel.write(this.netOutBuffer).get() < 0) { break;
if (sslEngine.isInboundDone()) { return -1; int bytesRead = c.read(netRecvBuffer).get(); log(format("BYTESREAD = %s ", bytesRead)); SSLEngineResult r = sslEngine.unwrap(netRecvBuffer, appRecvBuffer); netRecvBuffer.compact(); log(format("read status %s", r.getStatus())); log(format("appRecvBuffer %s ", appRecvBuffer)); switch(r.getStatus()) { case BUFFER_OVERFLOW: // appRecvBuffer do not have enough space throw new BufferOverflowException(); return 0; case CLOSED: c.shutdownInput(); break; case OK: if (sslEngine.isInboundDone()) { return -1;
public void close() throws InterruptedException, ExecutionException, IOException { try { netSendBuffer.flip(); c.write(netSendBuffer).get(); netSendBuffer.compact(); if (!sslEngine.isOutboundDone()) { sslEngine.closeOutbound(); handshake(); } if (!sslEngine.isInboundDone()) { sslEngine.closeInbound(); handshake(); } } finally { c.close(); } }
SSLEngineResult r = sslEngine.wrap(appSendBuffer, netSendBuffer); int posAfterWrap = appSendBuffer.position(); appSendBuffer.compact(); switch(r.getStatus()) { case BUFFER_OVERFLOW: // netSendBuffer do not have enough space throw new BufferOverflowException(); int bytesWritten = c.write(netSendBuffer).get(); netSendBuffer.compact();
void ready() { try { channel = AsynchronousSocketChannel.open(); channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); Future<Void> future = channel.connect(remote.getAddress()); if (future.get() != null) { log.log(Level.WARNING, "Can't connect to {0}", remote); } else { log.log(Level.INFO, "Connected to server {0}", remote); } } catch (Exception e) { e.printStackTrace(); } }
public int readBytes(ByteBuffer dst, long timeout, TimeUnit unit) throws Exception { // The handshake is completed checkHandshake(); if (this.netInBuffer.position() == 0) { this.reset(this.netInBuffer); int x = this.channel.read(this.netInBuffer).get(timeout, unit); if (x < 0) { throw new ClosedChannelException(); } } // Unwrap the data read, and return the number of unwrapped bytes return this.unwrap(this.netInBuffer, dst); }