remove write callback

knowing when the packet has been sent is not an useful information : the fact that it's sent doesn't mean it is received and what we usually want is that the packets are in the proper order no when they are sent
This commit is contained in:
Romain Beaumont 2015-05-23 01:10:58 +02:00
parent f82f037f29
commit 3c61f24991
3 changed files with 19 additions and 38 deletions

View File

@ -168,11 +168,7 @@ Client.prototype.setCompressionThreshold = function(threshold) {
} }
} }
function noop(err) { Client.prototype.write = function(packetId, params) {
if(err) throw err;
}
Client.prototype.write = function(packetId, params, cb = noop) {
if(Array.isArray(packetId)) { if(Array.isArray(packetId)) {
if(packetId[0] !== this.state) if(packetId[0] !== this.state)
return false; return false;
@ -183,7 +179,7 @@ Client.prototype.write = function(packetId, params, cb = noop) {
var packetName = protocol.packetNames[this.state][this.isServer ? "toClient" : "toServer"][packetId]; var packetName = protocol.packetNames[this.state][this.isServer ? "toClient" : "toServer"][packetId];
debug("writing packetId " + this.state + "." + packetName + " (0x" + packetId.toString(16) + ")"); debug("writing packetId " + this.state + "." + packetName + " (0x" + packetId.toString(16) + ")");
debug(params); debug(params);
this.serializer.write({ packetId, params }, cb); this.serializer.write({ packetId, params });
}; };
Client.prototype.writeRaw = function(buffer) { Client.prototype.writeRaw = function(buffer) {

View File

@ -125,11 +125,8 @@ function createServer(options) {
} }
client.once([states.STATUS, 0x01], function(packet) { client.once([states.STATUS, 0x01], function(packet) {
client.write(0x01, {time: packet.time},function(err){ client.write(0x01, {time: packet.time});
if(err) client.end();
throw err;
client.end();
});
}); });
client.write(0x00, {response: JSON.stringify(response)}); client.write(0x00, {response: JSON.stringify(response)});
} }
@ -215,22 +212,19 @@ function createServer(options) {
} }
//client.write('compress', { threshold: 256 }); // Default threshold is 256 //client.write('compress', { threshold: 256 }); // Default threshold is 256
//client.compressionThreshold = 256; //client.compressionThreshold = 256;
client.write(0x02, {uuid: client.uuid, username: client.username},function(err){ client.write(0x02, {uuid: client.uuid, username: client.username});
if(err) client.state = states.PLAY;
throw err; loggedIn = true;
client.state = states.PLAY; startKeepAlive();
loggedIn = true;
startKeepAlive();
clearTimeout(loginKickTimer); clearTimeout(loginKickTimer);
loginKickTimer = null; loginKickTimer = null;
server.playerCount += 1; server.playerCount += 1;
client.once('end', function() { client.once('end', function() {
server.playerCount -= 1; server.playerCount -= 1;
});
server.emit('login', client);
}); });
server.emit('login', client);
} }
}); });
server.listen(port, host); server.listen(port, host);
@ -301,13 +295,10 @@ function createClient(options) {
serverHost: host, serverHost: host,
serverPort: port, serverPort: port,
nextState: 2 nextState: 2
},function(err){ });
if(err) client.state = states.LOGIN;
throw err; client.write(0x00, {
client.state = states.LOGIN; username: client.username
client.write(0x00, {
username: client.username
});
}); });
} }
@ -366,9 +357,6 @@ function createClient(options) {
client.write(0x01, { client.write(0x01, {
sharedSecret: encryptedSharedSecretBuffer, sharedSecret: encryptedSharedSecretBuffer,
verifyToken: encryptedVerifyTokenBuffer, verifyToken: encryptedVerifyTokenBuffer,
},function(err){
if(err)
throw err;
}); });
client.setEncryption(sharedSecret); client.setEncryption(sharedSecret);
} }

View File

@ -37,11 +37,8 @@ function ping(options, cb) {
serverHost: host, serverHost: host,
serverPort: port, serverPort: port,
nextState: 1 nextState: 1
},function(err){
if(err)
throw err;
client.state = states.STATUS;
}); });
client.state = states.STATUS;
}); });
client.connect(port, host); client.connect(port, host);