From 6271f29996cf3a1862bcde0fe8bd1eb52b2c0574 Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Tue, 27 Jun 2017 12:23:09 -0700 Subject: [PATCH] adding a js test server --- test-rpc-server/package.json | 9 +++++++ test-rpc-server/rpc-message.js | 24 +++++++++++++++++ test-rpc-server/rpc-server.js | 45 +++++++++++++++++++++++++++++++ test-rpc-server/test-client.js | 48 ++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 test-rpc-server/package.json create mode 100644 test-rpc-server/rpc-message.js create mode 100644 test-rpc-server/rpc-server.js create mode 100644 test-rpc-server/test-client.js diff --git a/test-rpc-server/package.json b/test-rpc-server/package.json new file mode 100644 index 0000000..1b62723 --- /dev/null +++ b/test-rpc-server/package.json @@ -0,0 +1,9 @@ +{ + "name": "test-rpc-server", + "version": "0.0.0", + "private": true, + "scripts": { + "server": "node rpc-server.js", + "client": "node test-client.js" + } +} \ No newline at end of file diff --git a/test-rpc-server/rpc-message.js b/test-rpc-server/rpc-message.js new file mode 100644 index 0000000..0508b83 --- /dev/null +++ b/test-rpc-server/rpc-message.js @@ -0,0 +1,24 @@ + +module.exports = class RpcMessage { + static serialize(obj) { + const serializedJson = JSON.stringify(obj); + const msgLen = 4 + serializedJson.length; + let buff = Buffer.alloc(msgLen); + buff.writeInt32LE(msgLen, 0); + buff.write(serializedJson, 4, serializedJson.length, 'utf-8'); + return buff; + } + + static deserialize(buff) { + const msgLen = buff.readInt32LE(0); + if (buff.length < msgLen) { + return null; + } + const msg = buff.toString('utf-8', 4, msgLen); + try { + return JSON.parse(msg); + } catch(e) { + return null; + } + } +}; diff --git a/test-rpc-server/rpc-server.js b/test-rpc-server/rpc-server.js new file mode 100644 index 0000000..67933e7 --- /dev/null +++ b/test-rpc-server/rpc-server.js @@ -0,0 +1,45 @@ +const net = require('net'); +const RpcMessage = require('./rpc-message'); + +console.log('Start up'); + +let PipePrefix; +if (process.platform == 'win32') { + PipePrefix = "\\\\.\\pipe\\"; +} +else { + PipePrefix = "/tmp"; +} + +const PipePath = PipePrefix + "DiscordRpcServer"; + +var server = net.createServer(function(stream) { + console.log('Server: on connection') + + stream.on('data', function(data) { + const msgObj = RpcMessage.deserialize(data); + if (msgObj != null) { + console.log('Server: on data:', msgObj); + } + else { + console.log('Server: got some data'); + } + }); + + stream.on('end', function() { + console.log('Server: on end') + server.close(); + }); +}); + +server.on('close', function(){ + console.log('Server: on close'); +}) + +try { + server.listen(PipePath, function(){ + console.log('Server: on listening'); + }); +} catch(e) { + console.error('could not start server:', e); +} \ No newline at end of file diff --git a/test-rpc-server/test-client.js b/test-rpc-server/test-client.js new file mode 100644 index 0000000..44f00bc --- /dev/null +++ b/test-rpc-server/test-client.js @@ -0,0 +1,48 @@ +const net = require('net'); +const RpcMessage = require('./rpc-message'); + +var PIPE_NAME = "DiscordRpcServer"; +var PIPE_PATH = "\\\\.\\pipe\\" + PIPE_NAME; + +const msg = new RpcMessage(); + +function sendMesg(testUpdatesToSend, stream) { + const msgObj = { + name: 'My Awesome Game', + state: (testUpdatesToSend % 2 == 0) ? 'In a match' : 'In Lobby' + }; + console.log('Client: send update:', msgObj); + stream.write(RpcMessage.serialize(msgObj)); +} + +function sendMessageLoop(testUpdatesToSend, interval, stream) { + sendMesg(testUpdatesToSend, stream); + if (testUpdatesToSend > 1) { + setTimeout(() => {sendMessageLoop(testUpdatesToSend - 1, interval, stream)}, interval); + } else { + shutdown(); + } +} + +const client = net.connect(PIPE_PATH, function(stream) { + console.log('Client: on connection'); + + sendMessageLoop(5, 3000, client); +}); + +client.on('data', function(data) { + const msgObj = RpcMessage.deserialize(data); + if (msgObj != null) { + console.log('Client: got data:', msgObj); + } else { + console.log('Client: got some data'); + } +}); + +client.on('end', function() { + console.log('Client: on end'); +}); + +function shutdown() { + client.end(); +} \ No newline at end of file