From 0d6282fe330455c7974a3740c33d24b089e41b5a Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Tue, 18 Jul 2017 14:49:44 -0700 Subject: [PATCH] send a ping to test --- src/rpc_connection.cpp | 7 ++++--- test-rpc-server/rpc-message.js | 10 ++++++++++ test-rpc-server/rpc-server.js | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/rpc_connection.cpp b/src/rpc_connection.cpp index 6942003..ec75aff 100644 --- a/src/rpc_connection.cpp +++ b/src/rpc_connection.cpp @@ -89,12 +89,12 @@ bool RpcConnection::Read(rapidjson::Document& message) return false; } readFrame.message[readFrame.length] = 0; - message.ParseInsitu(readFrame.message); } switch (readFrame.opcode) { case Opcode::Close: { + message.ParseInsitu(readFrame.message); lastErrorCode = message["code"].GetInt(); const auto& m = message["message"]; StringCopy(lastErrorMessage, m.GetString(), sizeof(lastErrorMessage)); @@ -102,11 +102,12 @@ bool RpcConnection::Read(rapidjson::Document& message) return false; } case Opcode::Frame: + message.ParseInsitu(readFrame.message); return true; case Opcode::Ping: { - MessageFrameHeader frame{ Opcode::Pong, 0 }; - if (!connection->Write(&frame, sizeof(MessageFrameHeader))) { + readFrame.opcode = Opcode::Pong; + if (!connection->Write(&readFrame, sizeof(MessageFrameHeader) + readFrame.length)) { Close(); } break; diff --git a/test-rpc-server/rpc-message.js b/test-rpc-server/rpc-message.js index 40ad3ee..3dd4896 100644 --- a/test-rpc-server/rpc-message.js +++ b/test-rpc-server/rpc-message.js @@ -6,6 +6,8 @@ const OPCODES = { HANDSHAKE: 0, FRAME: 1, CLOSE: 2, + PING: 3, + PONG: 4, }; let PipePath; @@ -47,9 +49,17 @@ class RpcMessage { return RpcMessage.serialize(opcode, {code, message}); } + static sendPing(message) { + const opcode = OPCODES.PING; + return RpcMessage.serialize(opcode, {message}); + } + static deserialize(buff) { const opcode = buff.readInt32LE(0); const msgLen = buff.readInt32LE(4); + if (msgLen == 0) { + return {opcode, data: ''}; + } if (buff.length < (msgLen + 8)) { return null; } diff --git a/test-rpc-server/rpc-server.js b/test-rpc-server/rpc-server.js index be3d51f..cb4df77 100644 --- a/test-rpc-server/rpc-server.js +++ b/test-rpc-server/rpc-server.js @@ -58,3 +58,18 @@ replServer.defineCommand('kill', { } }); +replServer.defineCommand('ping', { + help: 'Ping all clients', + action() { + this.bufferedCommand = ''; + Object.keys(global.connections).forEach((who) => { + const sock = global.connections[who]; + if (sock) { + console.log('pinging', who); + sock.write(RpcMessage.sendPing('hello')); + } + }) + this.displayPrompt(); + } +}); +