2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.nettyutil.handler.ssh.sshd1028;
10 import java.io.IOException;
11 import java.net.SocketAddress;
12 import java.nio.ByteBuffer;
13 import java.nio.channels.AsynchronousSocketChannel;
14 import org.opendaylight.netconf.shaded.sshd.common.FactoryManager;
15 import org.opendaylight.netconf.shaded.sshd.common.io.IoHandler;
16 import org.opendaylight.netconf.shaded.sshd.common.io.nio2.Nio2CompletionHandler;
17 import org.opendaylight.netconf.shaded.sshd.common.io.nio2.Nio2Service;
18 import org.opendaylight.netconf.shaded.sshd.common.io.nio2.Nio2Session;
19 import org.opendaylight.netconf.shaded.sshd.common.util.Readable;
22 * Custom Nio2Session which fixes the issue with connections not being properly closed.
23 * Should be removed when SSHD-1028 is fixed.
25 public class NetconfNio2Session extends Nio2Session {
27 public NetconfNio2Session(final Nio2Service service, final FactoryManager manager, final IoHandler handler,
28 final AsynchronousSocketChannel socket, final SocketAddress acceptanceAddress)
30 super(service, manager, handler, socket, acceptanceAddress);
34 * This method in sshd-osgi:2.5.0 and 2.5.1 contains a bug. The close(true) statement was removed. We can override
35 * it making a workaround for this issue - until SSHD-1028 is fixed.
38 @SuppressWarnings("IllegalCatch")
39 protected void handleReadCycleCompletion(final ByteBuffer buffer, final Readable bufReader,
40 final Nio2CompletionHandler<Integer, Object> completionHandler,
41 final Integer result, final Object attachment) {
43 boolean debugEnabled = log.isDebugEnabled();
46 log.debug("handleReadCycleCompletion({}) read {} bytes", this, result);
49 IoHandler handler = getIoHandler();
50 handler.messageReceived(this, bufReader);
51 if (!closeFuture.isClosed()) {
52 // re-use reference for next iteration since we finished processing it
54 doReadCycle(buffer, completionHandler);
57 log.debug("handleReadCycleCompletion({}) IoSession has been closed, stop reading", this);
62 log.debug("handleReadCycleCompletion({}) Socket has been disconnected (result={}), closing "
63 + "IoSession now", this, result);
67 } catch (Throwable exc) {
68 completionHandler.failed(exc, attachment);