From 493ca64411a25f53a34a91cb5c7da5c27a41ca38 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Sun, 5 Oct 2014 18:25:31 +0400 Subject: [PATCH] Extend remote debugger communication protocol for supporting messages larger than 65536 characters (limitation of web sockets). --- .../chromerdp/ChromeRDPDebuggerEndpoint.java | 20 ++++++++++++--- teavm-chrome-rdp/src/main/js/chrome/main.js | 25 ++++++++++++++++--- .../build.properties | 3 ++- .../teavm-eclipse-core-plugin/logback.xml | 25 +++++++++++++++++++ 4 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 teavm-eclipse/teavm-eclipse-core-plugin/logback.xml diff --git a/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPDebuggerEndpoint.java b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPDebuggerEndpoint.java index e57263a1a..d7712ebd4 100644 --- a/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPDebuggerEndpoint.java +++ b/teavm-chrome-rdp/src/main/java/org/teavm/chromerdp/ChromeRDPDebuggerEndpoint.java @@ -27,9 +27,11 @@ import javax.websocket.server.ServerEndpoint; */ @ServerEndpoint("/") public class ChromeRDPDebuggerEndpoint implements ChromeRDPExchange { + public static final int MAX_MESSAGE_SIZE = 65534; private Session session; private ChromeRDPExchangeConsumer debugger; private List listeners = new ArrayList<>(); + private StringBuilder messageBuffer = new StringBuilder(); @OnOpen public void open(Session session) { @@ -61,14 +63,26 @@ public class ChromeRDPDebuggerEndpoint implements ChromeRDPExchange { @OnMessage public void receive(String message) throws IOException { - for (ChromeRDPExchangeListener listener : listeners) { - listener.received(message); + char ctl = message.charAt(0); + messageBuffer.append(message.substring(1)); + if (ctl == '.') { + message = messageBuffer.toString(); + for (ChromeRDPExchangeListener listener : listeners) { + listener.received(message); + } + messageBuffer = new StringBuilder(); } } @Override public void send(String message) { - session.getAsyncRemote().sendText(message); + int index = 0; + while (message.length() - index > MAX_MESSAGE_SIZE) { + int next = index + MAX_MESSAGE_SIZE; + session.getAsyncRemote().sendText("," + message.substring(index, next)); + index = next; + } + session.getAsyncRemote().sendText("." + message.substring(index)); } @Override diff --git a/teavm-chrome-rdp/src/main/js/chrome/main.js b/teavm-chrome-rdp/src/main/js/chrome/main.js index d03c61e78..2f8500960 100644 --- a/teavm-chrome-rdp/src/main/js/chrome/main.js +++ b/teavm-chrome-rdp/src/main/js/chrome/main.js @@ -9,8 +9,10 @@ function DebuggerAgent(tab) { this.tab = null; this.debuggee = { tabId : tab.id }; this.attachedToDebugger = false; + this.messageBuffer = ""; debuggerAgentMap[tab.id] = this; } +DebuggerAgent.MAX_MESSAGE_SIZE = 65534; DebuggerAgent.prototype.attach = function() { chrome.debugger.attach(this.debuggee, "1.0", (function(callback) { this.attachedToDebugger = true; @@ -20,7 +22,13 @@ DebuggerAgent.prototype.attach = function() { DebuggerAgent.prototype.connectToServer = function() { this.connection = new WebSocket("ws://localhost:2357/"); this.connection.onmessage = function(event) { - this.receiveMessage(JSON.parse(event.data)); + var str = event.data; + var ctl = str.substring(0, 1); + this.messageBuffer += str.substring(1); + if (ctl == '.') { + this.receiveMessage(JSON.parse(this.messageBuffer)); + this.messageBuffer = ""; + } }.bind(this); this.connection.onclose = function(event) { if (this.connection != null) { @@ -30,7 +38,7 @@ DebuggerAgent.prototype.connectToServer = function() { }.bind(this); this.connection.onopen = function() { for (var i = 0; i < this.pendingMessages.length; ++i) { - this.connection.send(JSON.stringify(this.pendingMessages[i])); + this.sendMessage(this.pendingMessages[i]); } this.pendingMessages = null; }.bind(this); @@ -40,10 +48,19 @@ DebuggerAgent.prototype.receiveMessage = function(message) { if (message.id) { var responseToServer = { id : message.id, result : response, error : response ? undefined : chrome.runtime.lastError }; - this.connection.send(JSON.stringify(responseToServer)); + this.sendMessage(responseToServer); } }.bind(this)); }; +DebuggerAgent.prototype.sendMessage = function(message) { + var str = JSON.stringify(message); + while (str.length > DebuggerAgent.MAX_MESSAGE_SIZE) { + var part = "," + str.substring(0, DebuggerAgent.MAX_MESSAGE_SIZE); + this.connection.send(part); + str = str.substring(DebuggerAgent.MAX_MESSAGE_SIZE); + } + this.connection.send("." + str); +} DebuggerAgent.prototype.disconnect = function() { if (this.connection) { var conn = this.connection; @@ -69,7 +86,7 @@ chrome.debugger.onEvent.addListener(function(source, method, params) { if (agent.pendingMessages) { agent.pendingMessages.push(message); } else if (agent.connection) { - agent.connection.send(JSON.stringify(message)); + agent.sendMessage(message); } }); chrome.debugger.onDetach.addListener(function(source) { diff --git a/teavm-eclipse/teavm-eclipse-core-plugin/build.properties b/teavm-eclipse/teavm-eclipse-core-plugin/build.properties index 2d584ec29..e41e7c47c 100644 --- a/teavm-eclipse/teavm-eclipse-core-plugin/build.properties +++ b/teavm-eclipse/teavm-eclipse-core-plugin/build.properties @@ -43,5 +43,6 @@ bin.includes = META-INF/,\ lib/websocket-client-9.2.1.v20140609.jar,\ lib/websocket-common-9.2.1.v20140609.jar,\ lib/websocket-server-9.2.1.v20140609.jar,\ - lib/websocket-servlet-9.2.1.v20140609.jar, + lib/websocket-servlet-9.2.1.v20140609.jar,\ + logback.xml jars.compile.order = . diff --git a/teavm-eclipse/teavm-eclipse-core-plugin/logback.xml b/teavm-eclipse/teavm-eclipse-core-plugin/logback.xml new file mode 100644 index 000000000..b86b410ea --- /dev/null +++ b/teavm-eclipse/teavm-eclipse-core-plugin/logback.xml @@ -0,0 +1,25 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + \ No newline at end of file