mirror of
https://github.com/unmojang/node-minecraft-protocol.git
synced 2025-09-30 06:30:39 -04:00
Original skeleton changes.
This commit is contained in:
parent
3402260b7b
commit
eaa895120a
@ -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);
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user