|
49 | 49 | import org.apache.sshd.common.channel.throttle.ChannelStreamWriterResolverManager;
|
50 | 50 | import org.apache.sshd.common.digest.Digest;
|
51 | 51 | import org.apache.sshd.common.forward.Forwarder;
|
| 52 | +import org.apache.sshd.common.future.SshFutureListener; |
52 | 53 | import org.apache.sshd.common.io.IoSession;
|
53 | 54 | import org.apache.sshd.common.io.IoWriteFuture;
|
54 | 55 | import org.apache.sshd.common.kex.AbstractKexFactoryManager;
|
|
77 | 78 | /**
|
78 | 79 | * Contains split code in order to make {@link AbstractSession} class smaller
|
79 | 80 | */
|
| 81 | +@SuppressWarnings("checkstyle:MethodCount") // Number of methods exceeds max. allowed |
80 | 82 | public abstract class SessionHelper extends AbstractKexFactoryManager implements Session {
|
81 | 83 |
|
82 | 84 | // Session timeout measurements
|
@@ -628,6 +630,31 @@ protected void signalSendIdentification(SessionListener listener, String version
|
628 | 630 | listener.sessionPeerIdentificationSend(this, version, extraLines);
|
629 | 631 | }
|
630 | 632 |
|
| 633 | + protected void signalIdentificationSent(String version, List<String> extraLines, Throwable error) throws Exception { |
| 634 | + try { |
| 635 | + invokeSessionSignaller(l -> { |
| 636 | + signalIdentificationSent(l, version, extraLines, error); |
| 637 | + return null; |
| 638 | + }); |
| 639 | + } catch (Throwable err) { |
| 640 | + Throwable e = ExceptionUtils.peelException(err); |
| 641 | + if (e instanceof Exception) { |
| 642 | + throw (Exception) e; |
| 643 | + } else { |
| 644 | + throw new RuntimeSshException(e); |
| 645 | + } |
| 646 | + } |
| 647 | + } |
| 648 | + |
| 649 | + protected void signalIdentificationSent( |
| 650 | + SessionListener listener, String version, List<String> extraLines, Throwable err) throws Exception { |
| 651 | + if (listener == null) { |
| 652 | + return; |
| 653 | + } |
| 654 | + |
| 655 | + listener.sessionPeerIdentificationSent(this, version, extraLines, err); |
| 656 | + } |
| 657 | + |
631 | 658 | protected void signalReadPeerIdentificationLine(String line, List<String> extraLines) throws Exception {
|
632 | 659 | try {
|
633 | 660 | invokeSessionSignaller(l -> {
|
@@ -833,28 +860,45 @@ protected IoWriteFuture sendIdentification(String version, List<String> extraLin
|
833 | 860 | ReservedSessionMessagesHandler handler = getReservedSessionMessagesHandler();
|
834 | 861 | IoWriteFuture future = (handler == null) ? null : handler.sendIdentification(this, version, extraLines);
|
835 | 862 | boolean debugEnabled = log.isDebugEnabled();
|
836 |
| - if (future != null) { |
| 863 | + if (future == null) { |
| 864 | + String ident = version; |
| 865 | + if (GenericUtils.size(extraLines) > 0) { |
| 866 | + ident = GenericUtils.join(extraLines, "\r\n") + "\r\n" + version; |
| 867 | + } |
| 868 | + |
| 869 | + if (debugEnabled) { |
| 870 | + log.debug("sendIdentification({}): {}", |
| 871 | + this, ident.replace('\r', '|').replace('\n', '|')); |
| 872 | + } |
| 873 | + |
| 874 | + IoSession networkSession = getIoSession(); |
| 875 | + byte[] data = (ident + "\r\n").getBytes(StandardCharsets.UTF_8); |
| 876 | + future = networkSession.writeBuffer(new ByteArrayBuffer(data)); |
| 877 | + } else { |
837 | 878 | if (debugEnabled) {
|
838 | 879 | log.debug("sendIdentification({})[{}] sent {} lines via reserved handler",
|
839 | 880 | this, version, GenericUtils.size(extraLines));
|
840 | 881 | }
|
841 |
| - |
842 |
| - return future; |
843 | 882 | }
|
844 | 883 |
|
845 |
| - String ident = version; |
846 |
| - if (GenericUtils.size(extraLines) > 0) { |
847 |
| - ident = GenericUtils.join(extraLines, "\r\n") + "\r\n" + version; |
848 |
| - } |
| 884 | + future.addListener(new SshFutureListener<IoWriteFuture>() { |
| 885 | + @Override |
| 886 | + public void operationComplete(IoWriteFuture future) { |
| 887 | + try { |
| 888 | + signalIdentificationSent(version, extraLines, future.getException()); |
| 889 | + } catch (Throwable err) { |
| 890 | + Throwable e = ExceptionUtils.peelException(err); |
| 891 | + if (e instanceof RuntimeException) { |
| 892 | + throw (RuntimeException) e; |
| 893 | + } else { |
| 894 | + throw new RuntimeSshException(e); |
| 895 | + } |
849 | 896 |
|
850 |
| - if (debugEnabled) { |
851 |
| - log.debug("sendIdentification({}): {}", |
852 |
| - this, ident.replace('\r', '|').replace('\n', '|')); |
853 |
| - } |
| 897 | + } |
| 898 | + } |
| 899 | + }); |
854 | 900 |
|
855 |
| - IoSession networkSession = getIoSession(); |
856 |
| - byte[] data = (ident + "\r\n").getBytes(StandardCharsets.UTF_8); |
857 |
| - return networkSession.writeBuffer(new ByteArrayBuffer(data)); |
| 901 | + return future; |
858 | 902 | }
|
859 | 903 |
|
860 | 904 | /**
|
|
0 commit comments