Skip to content
Snippets Groups Projects
Commit c2c00a19 authored by deuce's avatar deuce
Browse files

Handle socket disconnection correctly.

A file is not successfully sent until we get an M_GOT from the remote.
That takes care of all the TODO items.
parent 773831cf
No related branches found
No related tags found
No related merge requests found
...@@ -109,8 +109,8 @@ BinkP.prototype.ack_file = function() ...@@ -109,8 +109,8 @@ BinkP.prototype.ack_file = function()
if (this.receiving.position >= this.receiving_len) { if (this.receiving.position >= this.receiving_len) {
this.receiving.date = this.receiving_date; this.receiving.date = this.receiving_date;
if (this.received_files.indexOf(this.receiving.name) == -1) { if (this.received_files.indexOf(this.receiving.name) == -1) {
this.sendCmd(this.command.M_GOT, file_getname(this.receiving.name)+' '+this.receiving.position+' '+this.receiving.date); if (this.sendCmd(this.command.M_GOT, file_getname(this.receiving.name)+' '+this.receiving.position+' '+this.receiving.date))
this.received_files.push(this.receiving.name); this.received_files.push(this.receiving.name);
} }
} }
else { else {
...@@ -278,6 +278,7 @@ BinkP.prototype.session = function(addr, password, port) ...@@ -278,6 +278,7 @@ BinkP.prototype.session = function(addr, password, port)
args = parse_args(this, pkt.data); args = parse_args(this, pkt.data);
for (i=0; i<this.pending_ack.length; i++) { for (i=0; i<this.pending_ack.length; i++) {
if (file_getname(this.pending_ack[i]) == args[0]) { if (file_getname(this.pending_ack[i]) == args[0]) {
this.sent_files.push(this.pending_ack[i]);
this.pending_ack.splice(i, 1); this.pending_ack.splice(i, 1);
i--; i--;
} }
...@@ -348,8 +349,8 @@ BinkP.prototype.session = function(addr, password, port) ...@@ -348,8 +349,8 @@ BinkP.prototype.session = function(addr, password, port)
if (this.receiving.position >= this.receiving_len) { if (this.receiving.position >= this.receiving_len) {
this.receiving.date = this.receiving_date; this.receiving.date = this.receiving_date;
if (this.received_files.indexOf(this.receiving.name) == -1) { if (this.received_files.indexOf(this.receiving.name) == -1) {
this.sendCmd(this.command.M_GOT, file_getname(this.receiving.name)+' '+this.receiving.position+' '+this.receiving.date); if (this.sendCmd(this.command.M_GOT, file_getname(this.receiving.name)+' '+this.receiving.position+' '+this.receiving.date))
this.received_files.push(this.receiving.name); this.received_files.push(this.receiving.name);
} }
} }
} }
...@@ -374,7 +375,6 @@ BinkP.prototype.session = function(addr, password, port) ...@@ -374,7 +375,6 @@ BinkP.prototype.session = function(addr, password, port)
this.sendData(this.sending.read(32767)); this.sendData(this.sending.read(32767));
if (this.eof || this.sending.position >= this.sending.length) { if (this.eof || this.sending.position >= this.sending.length) {
this.sending.close(); this.sending.close();
this.sent_files.push(this.sending.name);
this.sending = undefined; this.sending = undefined;
} }
} }
...@@ -425,10 +425,12 @@ BinkP.prototype.sendCmd = function(cmd, data) ...@@ -425,10 +425,12 @@ BinkP.prototype.sendCmd = function(cmd, data)
} }
var len = data.length+1; var len = data.length+1;
len |= 0x8000; len |= 0x8000;
// TODO: Check return values if (!this.sock.sendBin(len, 2))
this.sock.sendBin(len, 2); return false;
this.sock.sendBin(cmd, 1); if (!this.sock.sendBin(cmd, 1))
this.sock.send(data); return false;
if (!this.sock.send(data))
return false;
switch(cmd) { switch(cmd) {
case this.command.M_EOB: case this.command.M_EOB:
this.senteob=true; this.senteob=true;
...@@ -453,9 +455,11 @@ BinkP.prototype.sendData = function(data) ...@@ -453,9 +455,11 @@ BinkP.prototype.sendData = function(data)
var len = data.length; var len = data.length;
if (this.debug) if (this.debug)
log(LOG_DEBUG, "Sending "+data.length+" bytes of data"); log(LOG_DEBUG, "Sending "+data.length+" bytes of data");
// TODO: Check return values if (!this.sock.sendBin(len, 2))
this.sock.sendBin(len, 2); return false;
this.sock.send(data); if (!this.sock.send(data))
return false;
return true;
}; };
BinkP.prototype.recvFrame = function(timeout) BinkP.prototype.recvFrame = function(timeout)
{ {
...@@ -466,6 +470,7 @@ BinkP.prototype.recvFrame = function(timeout) ...@@ -466,6 +470,7 @@ BinkP.prototype.recvFrame = function(timeout)
var options; var options;
var tmp; var tmp;
var ver; var ver;
var avail;
if (timeout === undefined) if (timeout === undefined)
timeout = 0; timeout = 0;
...@@ -482,11 +487,19 @@ BinkP.prototype.recvFrame = function(timeout) ...@@ -482,11 +487,19 @@ BinkP.prototype.recvFrame = function(timeout)
else else
ret = this.partialFrame; ret = this.partialFrame;
// TODO: Do a timeout recv() using select() for older versions... if (this.sock.poll(timeout)) {
// Not sure how to tell if recv timeout is supported or avail = this.sock.nread;
// not though. if (avail == 0) {
// We also need to check the socket status. if (this.sock.is_connected)
ret.data += this.sock.recv(ret.length - ret.data.length, timeout); log(LOG_ERROR, "Poll returned true, but no data available on connected socket!");
this.sock.close();
this.sock = undefined;
return undefined;
}
if (avail > (ret.length - ret.data.length))
avail = ret.length - ret.data.length;
ret.data += this.sock.recv(avail);
}
if (ret.data.length < ret.length) { if (ret.data.length < ret.length) {
this.partialFrame = ret; this.partialFrame = ret;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment