diff --git a/test-rpc-server/package.json b/test-rpc-server/package.json deleted file mode 100644 index 1b62723..0000000 --- a/test-rpc-server/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "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 deleted file mode 100644 index 3dd4896..0000000 --- a/test-rpc-server/rpc-message.js +++ /dev/null @@ -1,77 +0,0 @@ -const path = require('path'); - -const VERSION = 1; - -const OPCODES = { - HANDSHAKE: 0, - FRAME: 1, - CLOSE: 2, - PING: 3, - PONG: 4, -}; - -let PipePath; -if (process.platform == 'win32') { - PipePath = '\\\\?\\pipe\\discord-ipc'; -} -else { - const temp = process.env.XDG_RUNTIME_DIR || process.env.TMPDIR || process.env.TMP || process.env.TEMP || '/tmp'; - PipePath = path.join(temp, 'discord-ipc'); -} - -class RpcMessage { - - static serialize(opcode, obj) { - const serializedJson = JSON.stringify(obj); - const msgLen = serializedJson.length; - let buff = Buffer.alloc(8 + msgLen); - buff.writeInt32LE(opcode, 0); - buff.writeInt32LE(msgLen, 4); - buff.write(serializedJson, 8, serializedJson.length, 'utf-8'); - return buff; - } - - static handshake(id) { - const opcode = OPCODES.HANDSHAKE; - return RpcMessage.serialize(opcode, { - client_id: id, - v: VERSION - }); - } - - static send(obj) { - const opcode = OPCODES.FRAME; - return RpcMessage.serialize(opcode, obj); - } - - static sendClose(code, message) { - const opcode = OPCODES.CLOSE; - 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; - } - const msg = buff.toString('utf-8', 8, msgLen + 8); - try { - return {opcode, data: JSON.parse(msg)}; - } catch(e) { - console.log(`failed to parse "${msg}"`); - console.error(e); - return {opcode: OPCODES.CLOSE, message: e.message}; - } - } -}; - -module.exports = {OPCODES, PipePath, RpcMessage}; diff --git a/test-rpc-server/rpc-server.js b/test-rpc-server/rpc-server.js deleted file mode 100644 index cb4df77..0000000 --- a/test-rpc-server/rpc-server.js +++ /dev/null @@ -1,75 +0,0 @@ -const net = require('net'); -const repl = require('repl'); -const {PipePath, RpcMessage} = require('./rpc-message'); - -let connectionNonce = 0; -global.connections = {}; - -const server = net.createServer(function(sock) { - connectionNonce += 1; - console.log('Server: on connection', connectionNonce); - let myConnection = connectionNonce; - let messages = 0; - - global.connections[myConnection] = sock; - - sock.on('data', function(data) { - messages++; - const msgObj = RpcMessage.deserialize(data); - if (msgObj != null) { - const {opcode, data} = msgObj; - console.log(`\nServer (${myConnection}): got opcode: ${opcode}, data: ${JSON.stringify(data)}`); - } - else { - console.log('\nServer: got some data', data.toString()); - } - }); - - sock.on('end', function() { - delete global.connections[myConnection]; - console.log('\nServer: on end', myConnection); - }); -}); - -server.on('close', function(){ - console.log('\nServer: on close'); -}); - -try { - server.listen(PipePath, function(){ - console.log('\nServer: on listening'); - }); -} catch(e) { - console.error('\nServer: could not start:', e); -} - -const replServer = repl.start({prompt: '> ', useGlobal: true, breakEvalOnSigint: true}); -replServer.defineCommand('kill', { - help: 'Kill a client', - action(who) { - this.bufferedCommand = ''; - who = parseInt(who, 10); - const sock = global.connections[who]; - if (sock) { - console.log('killing', who); - sock.end(RpcMessage.sendClose(123, 'killed')); - } - this.displayPrompt(); - } -}); - -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(); - } -}); - diff --git a/test-rpc-server/test-client.js b/test-rpc-server/test-client.js deleted file mode 100644 index 162a914..0000000 --- a/test-rpc-server/test-client.js +++ /dev/null @@ -1,63 +0,0 @@ -const net = require('net'); -const {OPCODES, PipePath, RpcMessage} = require('./rpc-message'); - -const APP_ID = '12345678910'; -global.isConnected = false; -global.timeoutId = null; - -function sendMesg(testUpdatesToSend, stream) { - const msgObj = { - state: (testUpdatesToSend % 2 == 0) ? 'In a match' : 'In Lobby', - details: 'Excited' - }; - console.log('Client: send update:', msgObj); - stream.write(RpcMessage.send(msgObj)); -} - -function sendMessageLoop(testUpdatesToSend, interval, stream) { - global.timeoutId = null; - if (!global.isConnected) { - return; - } - sendMesg(testUpdatesToSend, stream); - if (testUpdatesToSend > 1) { - global.timeoutId = setTimeout(() => {sendMessageLoop(testUpdatesToSend - 1, interval, stream)}, interval); - } else { - shutdown(); - } -} - -const client = net.connect(PipePath, function(stream) { - console.log('Client: on connection'); - global.isConnected = true; - client.write(RpcMessage.handshake(APP_ID)); - sendMessageLoop(10, 3000, client); -}); - -client.on('data', function(data) { - const msgObj = RpcMessage.deserialize(data); - if (msgObj != null) { - const {opcode, data} = msgObj; - console.log(`Client: got opcode: ${opcode}, data: ${JSON.stringify(data)}`); - - if (opcode == OPCODES.CLOSE) { - shutdown(); - } - - } else { - console.log('Client: got some data', data); - } -}); - -client.on('end', function() { - global.isConnected = false; - console.log('Client: on end'); -}); - -function shutdown() { - if (global.timeoutId !== null) { - clearTimeout(global.timeoutId); - global.timeoutId = null; - } - client.end(); -} \ No newline at end of file