From eaa895120ae612ac2524926d821f20e832fd6b17 Mon Sep 17 00:00:00 2001 From: Robin Lambertz Date: Tue, 29 Jan 2013 00:44:00 +0000 Subject: [PATCH] Original skeleton changes. --- lib/client.js | 1 + lib/protocol.js | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/client.js b/lib/client.js index 3166a6c..1627d21 100644 --- a/lib/client.js +++ b/lib/client.js @@ -29,6 +29,7 @@ Client.prototype.setSocket = function(socket) { while (true) { parsed = parsePacket(incomingBuffer, self.isServer); if (! parsed) break; + if (typeof parsed.error !== "undefined") end(parsed.error); packet = parsed.results; incomingBuffer = incomingBuffer.slice(parsed.size); self.emit(packet.id, packet); diff --git a/lib/protocol.js b/lib/protocol.js index 6d3148c..3fab3a3 100644 --- a/lib/protocol.js +++ b/lib/protocol.js @@ -1163,7 +1163,9 @@ LongWriter.prototype.write = function(buffer, offset) { function get(packetId, toServer) { var packetInfo = packets[packetId]; - assert.ok(packetInfo, "unrecognized packet id: " + packetId); + if (!packetInfo) { + return null; + } return Array.isArray(packetInfo) ? packetInfo : toServer ? @@ -1175,6 +1177,7 @@ function createPacketBuffer(packetId, params, isServer) { var size = 1; var fields = [ new UByteWriter(packetId) ]; var packet = get(packetId, !isServer); + assert.notEqual(packet, null); packet.forEach(function(fieldInfo) { var value = params[fieldInfo.name]; var Writer = types[fieldInfo.type][1]; @@ -1198,14 +1201,28 @@ function parsePacket(buffer, isServer) { var size = 1; var results = { id: packetId }; var packetInfo = get(packetId, isServer); - assert.ok(packetInfo, "Unrecognized packetId: " + packetId); + if (packetInfo == null) { + return { + error: "Unrecognized packetId: " + packetId + } + } var i, fieldInfo, read, readResults; for (i = 0; i < packetInfo.length; ++i) { fieldInfo = packetInfo[i]; read = types[fieldInfo.type][0]; - assert.ok(read, "missing reader for data type: " + fieldInfo.type); + if (!read) { + return { + error: "missing reader for data type: " + fieldInfo.type; + } + } readResults = read(buffer, size); if (readResults) { + // if readResults.error is undef, error stays undef'd + if (typeof readResults.error !== "undefined") { + return { + error: readResults.error + } + } results[fieldInfo.name] = readResults.value; size += readResults.size; } else {