send a ping to test

This commit is contained in:
Chris Marsh 2017-07-18 14:49:44 -07:00
parent 29641da939
commit 0d6282fe33
3 changed files with 29 additions and 3 deletions

View file

@ -89,12 +89,12 @@ bool RpcConnection::Read(rapidjson::Document& message)
return false; return false;
} }
readFrame.message[readFrame.length] = 0; readFrame.message[readFrame.length] = 0;
message.ParseInsitu(readFrame.message);
} }
switch (readFrame.opcode) { switch (readFrame.opcode) {
case Opcode::Close: case Opcode::Close:
{ {
message.ParseInsitu(readFrame.message);
lastErrorCode = message["code"].GetInt(); lastErrorCode = message["code"].GetInt();
const auto& m = message["message"]; const auto& m = message["message"];
StringCopy(lastErrorMessage, m.GetString(), sizeof(lastErrorMessage)); StringCopy(lastErrorMessage, m.GetString(), sizeof(lastErrorMessage));
@ -102,11 +102,12 @@ bool RpcConnection::Read(rapidjson::Document& message)
return false; return false;
} }
case Opcode::Frame: case Opcode::Frame:
message.ParseInsitu(readFrame.message);
return true; return true;
case Opcode::Ping: case Opcode::Ping:
{ {
MessageFrameHeader frame{ Opcode::Pong, 0 }; readFrame.opcode = Opcode::Pong;
if (!connection->Write(&frame, sizeof(MessageFrameHeader))) { if (!connection->Write(&readFrame, sizeof(MessageFrameHeader) + readFrame.length)) {
Close(); Close();
} }
break; break;

View file

@ -6,6 +6,8 @@ const OPCODES = {
HANDSHAKE: 0, HANDSHAKE: 0,
FRAME: 1, FRAME: 1,
CLOSE: 2, CLOSE: 2,
PING: 3,
PONG: 4,
}; };
let PipePath; let PipePath;
@ -47,9 +49,17 @@ class RpcMessage {
return RpcMessage.serialize(opcode, {code, message}); return RpcMessage.serialize(opcode, {code, message});
} }
static sendPing(message) {
const opcode = OPCODES.PING;
return RpcMessage.serialize(opcode, {message});
}
static deserialize(buff) { static deserialize(buff) {
const opcode = buff.readInt32LE(0); const opcode = buff.readInt32LE(0);
const msgLen = buff.readInt32LE(4); const msgLen = buff.readInt32LE(4);
if (msgLen == 0) {
return {opcode, data: ''};
}
if (buff.length < (msgLen + 8)) { if (buff.length < (msgLen + 8)) {
return null; return null;
} }

View file

@ -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();
}
});