From 6923be5ee172485e8634f7a470ebb942f0077fe4 Mon Sep 17 00:00:00 2001 From: RetGal Date: Tue, 5 Mar 2019 22:05:19 +0100 Subject: [PATCH] Fixes issue #13 --- .../network/NetworkAssistantEngine.java | 48 ++++++++----------- .../network/NetworkAssistedEngine.java | 25 ++-------- .../dayon/common/network/NetworkEngine.java | 19 +++++++- 3 files changed, 43 insertions(+), 49 deletions(-) diff --git a/src/main/java/mpo/dayon/assistant/network/NetworkAssistantEngine.java b/src/main/java/mpo/dayon/assistant/network/NetworkAssistantEngine.java index ab932d5c..9872c4dd 100644 --- a/src/main/java/mpo/dayon/assistant/network/NetworkAssistantEngine.java +++ b/src/main/java/mpo/dayon/assistant/network/NetworkAssistantEngine.java @@ -200,21 +200,7 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException, throw new IOException("Unexpected message [HELLO]!"); } - if (https != null) { - https.cancel(); - https = null; - } - - final NetworkHelloMessage hello = NetworkHelloMessage.unmarshall(in); - fireOnByteReceived(1 + hello.getWireSize()); // +1 : magic number (byte) - - final Version version = Version.get(); - final boolean isProd = isProd(version, hello.getMajor(), hello.getMinor()); - - if (isProd && (version.getMajor() != hello.getMajor() || version.getMinor() != hello.getMinor())) { - throw new IOException("Version Error!"); - } - + introduce(in); introduced = true; fireOnConnected(connection); break; @@ -226,7 +212,6 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException, final NetworkCaptureMessage capture = NetworkCaptureMessage.unmarshall(in); fireOnByteReceived(1 + capture.getWireSize()); // +1 : magic number (byte) - captureMessageHandler.handleCapture(capture); break; @@ -237,7 +222,6 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException, final NetworkMouseLocationMessage mouse = NetworkMouseLocationMessage.unmarshall(in); fireOnByteReceived(1 + mouse.getWireSize()); // +1 : magic number (byte) - mouseMessageHandler.handleLocation(mouse); break; @@ -248,8 +232,8 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException, final NetworkClipboardTextMessage clipboardTextMessage = NetworkClipboardTextMessage.unmarshall(in); fireOnByteReceived(1 + clipboardTextMessage.getWireSize()); // +1 : magic number (byte) - fireOnClipboardReceived(); setClipboardContents(clipboardTextMessage.getText(), clipboardOwner); + fireOnClipboardReceived(); break; case CLIPBOARD_FILES: @@ -259,18 +243,9 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException, final NetworkClipboardFilesMessage clipboardFiles = NetworkClipboardFilesMessage.unmarshall(in, filesHelper); fireOnByteReceived(1 + clipboardFiles.getWireSize()); // +1 : magic number (byte) - filesHelper.setTotalFileBytesLeft(clipboardFiles.getWireSize() - 1L); - + filesHelper = handleNetworkClipboardFilesHelper(filesHelper, clipboardFiles, clipboardOwner); if (filesHelper.isIdle()) { fireOnClipboardReceived(); - filesHelper = new NetworkClipboardFilesHelper(); - setClipboardContents(clipboardFiles.getFiles(), clipboardOwner); - } else { - filesHelper.setFiles(clipboardFiles.getFiles()); - filesHelper.setFileNames(clipboardFiles.getFileNames()); - filesHelper.setFileSizes(clipboardFiles.getFileSizes()); - filesHelper.setPosition(clipboardFiles.getPosition()); - filesHelper.setFileBytesLeft(clipboardFiles.getRemainingFileSize()); } break; @@ -297,6 +272,23 @@ private void receivingLoop() throws KeyStoreException, NoSuchAlgorithmException, fireOnReady(); } + private void introduce(ObjectInputStream in) throws IOException { + if (https != null) { + https.cancel(); + https = null; + } + + final NetworkHelloMessage hello = NetworkHelloMessage.unmarshall(in); + fireOnByteReceived(1 + hello.getWireSize()); // +1 : magic number (byte) + + final Version version = Version.get(); + final boolean isProd = isProd(version, hello.getMajor(), hello.getMinor()); + + if (isProd && (version.getMajor() != hello.getMajor() || version.getMinor() != hello.getMinor())) { + throw new IOException("Version Error!"); + } + } + private void closeConnection(ObjectInputStream in, ObjectOutputStream out) { if (sender != null) { sender.cancel(); diff --git a/src/main/java/mpo/dayon/assisted/network/NetworkAssistedEngine.java b/src/main/java/mpo/dayon/assisted/network/NetworkAssistedEngine.java index 9ea04001..b00a92bb 100644 --- a/src/main/java/mpo/dayon/assisted/network/NetworkAssistedEngine.java +++ b/src/main/java/mpo/dayon/assisted/network/NetworkAssistedEngine.java @@ -15,7 +15,6 @@ import mpo.dayon.common.network.message.*; import mpo.dayon.common.security.CustomTrustManager; import mpo.dayon.common.squeeze.CompressionMethod; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.net.ssl.*; @@ -149,13 +148,16 @@ private void receivingLoop() throws IOException { case CLIPBOARD_TEXT: final NetworkClipboardTextMessage clipboardTextMessage = NetworkClipboardTextMessage.unmarshall(in); - sender.ping(); setClipboardContents(clipboardTextMessage.getText(), clipboardOwner); + sender.ping(); break; case CLIPBOARD_FILES: final NetworkClipboardFilesMessage clipboardFiles = NetworkClipboardFilesMessage.unmarshall(in, filesHelper); - filesHelper = handleNetworkClipboardFilesHelper(filesHelper, clipboardFiles); + filesHelper = handleNetworkClipboardFilesHelper(filesHelper, clipboardFiles, clipboardOwner); + if (filesHelper.isIdle()) { + sender.ping(); + } break; case PING: @@ -167,23 +169,6 @@ private void receivingLoop() throws IOException { } } - @NotNull - private NetworkClipboardFilesHelper handleNetworkClipboardFilesHelper(NetworkClipboardFilesHelper filesHelper, NetworkClipboardFilesMessage clipboardFiles) { - filesHelper.setTotalFileBytesLeft(clipboardFiles.getWireSize() - 1L); - if (filesHelper.isIdle()) { - filesHelper = new NetworkClipboardFilesHelper(); - sender.ping(); - setClipboardContents(clipboardFiles.getFiles(), clipboardOwner); - } else { - filesHelper.setFiles(clipboardFiles.getFiles()); - filesHelper.setFileNames(clipboardFiles.getFileNames()); - filesHelper.setFileSizes(clipboardFiles.getFileSizes()); - filesHelper.setPosition(clipboardFiles.getPosition()); - filesHelper.setFileBytesLeft(clipboardFiles.getRemainingFileSize()); - } - return filesHelper; - } - /** * The first message being sent to the assistant (e.g., version * identification). diff --git a/src/main/java/mpo/dayon/common/network/NetworkEngine.java b/src/main/java/mpo/dayon/common/network/NetworkEngine.java index 1e224f6a..c6023bd4 100644 --- a/src/main/java/mpo/dayon/common/network/NetworkEngine.java +++ b/src/main/java/mpo/dayon/common/network/NetworkEngine.java @@ -1,6 +1,8 @@ package mpo.dayon.common.network; import mpo.dayon.common.log.Log; +import mpo.dayon.common.network.message.NetworkClipboardFilesHelper; +import mpo.dayon.common.network.message.NetworkClipboardFilesMessage; import mpo.dayon.common.utils.TransferableFiles; import java.awt.*; @@ -25,10 +27,25 @@ protected void setClipboardContents(String string, ClipboardOwner clipboardOwner Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, clipboardOwner); } - protected void setClipboardContents(List files, ClipboardOwner clipboardOwner) { + private void setClipboardContents(List files, ClipboardOwner clipboardOwner) { Log.debug("setClipboardContents " + files.toString()); TransferableFiles transferableFiles = new TransferableFiles(files); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferableFiles, clipboardOwner); } + protected NetworkClipboardFilesHelper handleNetworkClipboardFilesHelper(NetworkClipboardFilesHelper filesHelper, NetworkClipboardFilesMessage clipboardFiles, ClipboardOwner clipboardOwner) { + filesHelper.setTotalFileBytesLeft(clipboardFiles.getWireSize() - 1L); + if (filesHelper.isIdle()) { + filesHelper = new NetworkClipboardFilesHelper(); + setClipboardContents(clipboardFiles.getFiles(), clipboardOwner); + } else { + filesHelper.setFiles(clipboardFiles.getFiles()); + filesHelper.setFileNames(clipboardFiles.getFileNames()); + filesHelper.setFileSizes(clipboardFiles.getFileSizes()); + filesHelper.setPosition(clipboardFiles.getPosition()); + filesHelper.setFileBytesLeft(clipboardFiles.getRemainingFileSize()); + } + return filesHelper; + } + }