@Override protected OpenFuture doInit(Buffer buffer) { OpenFuture f = new DefaultOpenFuture(this); try { out = new ChannelOutputStream( this, getRemoteWindow(), log, SshConstants.SSH_MSG_CHANNEL_DATA, true); authSocket = PropertyResolverUtils.getString(this, SshAgent.SSH_AUTHSOCKET_ENV_NAME); pool = Pool.create(AprLibrary.getInstance().getRootPool()); handle = Local.create(authSocket, pool); int result = Local.connect(handle, 0); if (result != Status.APR_SUCCESS) { throwException(result); } ExecutorService service = getExecutorService(); forwardService = (service == null) ? ThreadUtils.newSingleThreadExecutor("ChannelAgentForwarding[" + authSocket + "]") : service; shutdownForwarder = service != forwardService || isShutdownOnExit(); final int copyBufSize = PropertyResolverUtils.getIntProperty( this, FORWARDER_BUFFER_SIZE, DEFAULT_FORWARDER_BUF_SIZE); ValidateUtils.checkTrue( copyBufSize >= MIN_FORWARDER_BUF_SIZE, "Copy buf size below min.: %d", copyBufSize); ValidateUtils.checkTrue( copyBufSize <= MAX_FORWARDER_BUF_SIZE, "Copy buf size above max.: %d", copyBufSize); forwarder = forwardService.submit( () -> { try { byte[] buf = new byte[copyBufSize]; while (true) { int len = Socket.recv(handle, buf, 0, buf.length); if (len > 0) { out.write(buf, 0, len); out.flush(); } } } catch (IOException e) { close(true); } }); signalChannelOpenSuccess(); f.setOpened(); } catch (Throwable t) { Throwable e = GenericUtils.peelException(t); signalChannelOpenFailure(e); f.setException(e); } return f; }
public void run() { try { byte[] buf = new byte[1024]; while (true) { int result = Socket.recv(socket, buf, 0, buf.length); if (result == Status.APR_EOF) { break; } else if (result < Status.APR_SUCCESS) { throwException(result); } messageReceived(new Buffer(buf, 0, result)); } } catch (Exception e) { e.printStackTrace(); } finally { Socket.close(socket); } }