mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-25 18:37:19 +00:00
Remove the last bit of references to self (#765)
- Sometimes we didn’t need it, sometimes a bind was cleaner
This commit is contained in:
parent
555136f36b
commit
edfa5e72bf
1 changed files with 100 additions and 117 deletions
|
|
@ -203,30 +203,29 @@ SerialPort.prototype.open = function (callback) {
|
|||
this.reading = false;
|
||||
this.opening = true;
|
||||
|
||||
var self = this;
|
||||
SerialPortBinding.open(this.path, this.options, function (err, fd) {
|
||||
if (err) {
|
||||
debug('SerialPortBinding.open had an error', err);
|
||||
return self._error(err, callback);
|
||||
return this._error(err, callback);
|
||||
}
|
||||
self.fd = fd;
|
||||
self.paused = false;
|
||||
self.opening = false;
|
||||
this.fd = fd;
|
||||
this.paused = false;
|
||||
this.opening = false;
|
||||
|
||||
if (process.platform !== 'win32') {
|
||||
self.serialPoller = new SerialPortBinding.SerialportPoller(self.fd, function (err) {
|
||||
this.serialPoller = new SerialPortBinding.SerialportPoller(this.fd, function (err) {
|
||||
if (!err) {
|
||||
self._read();
|
||||
this._read();
|
||||
} else {
|
||||
self.disconnected(err);
|
||||
this.disconnected(err);
|
||||
}
|
||||
});
|
||||
self.serialPoller.start();
|
||||
}.bind(this));
|
||||
this.serialPoller.start();
|
||||
}
|
||||
|
||||
self.emit('open');
|
||||
this.emit('open');
|
||||
if (callback) { callback() }
|
||||
});
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
// underlying code is written to update all options, but for now
|
||||
|
|
@ -265,91 +264,87 @@ SerialPort.prototype.write = function (buffer, callback) {
|
|||
buffer = new Buffer(buffer);
|
||||
}
|
||||
debug('write data: ' + JSON.stringify(buffer));
|
||||
|
||||
var self = this;
|
||||
SerialPortBinding.write(this.fd, buffer, function (err, results) {
|
||||
if (callback) {
|
||||
callback(err, results);
|
||||
} else {
|
||||
if (err) {
|
||||
self.emit('error', err);
|
||||
}
|
||||
SerialPortBinding.write(this.fd, buffer, function (err, result) {
|
||||
if (err) {
|
||||
debug('SerialPortBinding.write had an error', err);
|
||||
return this._error(err, callback);
|
||||
}
|
||||
});
|
||||
if (callback) { callback(null, result) }
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
if (process.platform !== 'win32') {
|
||||
SerialPort.prototype._read = function () {
|
||||
var self = this;
|
||||
if (!self.readable || self.paused || self.reading || this.closing) {
|
||||
if (!this.readable || this.paused || this.reading || this.closing) {
|
||||
return;
|
||||
}
|
||||
|
||||
self.reading = true;
|
||||
this.reading = true;
|
||||
|
||||
if (!self.pool || self.pool.length - self.pool.used < kMinPoolSpace) {
|
||||
if (!this.pool || this.pool.length - this.pool.used < kMinPoolSpace) {
|
||||
// discard the old pool. Can't add to the free list because
|
||||
// users might have references to slices on it.
|
||||
self.pool = new Buffer(kPoolSize);
|
||||
self.pool.used = 0;
|
||||
this.pool = new Buffer(kPoolSize);
|
||||
this.pool.used = 0;
|
||||
}
|
||||
|
||||
// Grab another reference to the pool in the case that while we're in the
|
||||
// thread pool another read() finishes up the pool, and allocates a new
|
||||
// one.
|
||||
var toRead = Math.min(self.pool.length - self.pool.used, ~~self.bufferSize);
|
||||
var start = self.pool.used;
|
||||
var toRead = Math.min(this.pool.length - this.pool.used, ~~this.bufferSize);
|
||||
var start = this.pool.used;
|
||||
|
||||
function afterRead(err, bytesRead, readPool, bytesRequested) {
|
||||
self.reading = false;
|
||||
var _afterRead = function(err, bytesRead, readPool, bytesRequested) {
|
||||
this.reading = false;
|
||||
if (err) {
|
||||
if (err.code && err.code === 'EAGAIN') {
|
||||
if (!self.closing && self.isOpen()) {
|
||||
self.serialPoller.start();
|
||||
if (this.isOpen()) {
|
||||
this.serialPoller.start();
|
||||
}
|
||||
// handle edge case were mac/unix doesn't clearly know the error.
|
||||
} else if (err.code && (err.code === 'EBADF' || err.code === 'ENXIO' || (err.errno === -1 || err.code === 'UNKNOWN'))) {
|
||||
self.disconnected(err);
|
||||
this.disconnected(err);
|
||||
} else {
|
||||
self.fd = null;
|
||||
self.readable = false;
|
||||
self.emit('error', err);
|
||||
this.fd = null;
|
||||
this.readable = false;
|
||||
this.emit('error', err);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Since we will often not read the number of bytes requested,
|
||||
// let's mark the ones we didn't need as available again.
|
||||
this.pool.used -= bytesRequested - bytesRead;
|
||||
|
||||
if (bytesRead === 0) {
|
||||
if (this.isOpen()) {
|
||||
this.serialPoller.start();
|
||||
}
|
||||
} else {
|
||||
// Since we will often not read the number of bytes requested,
|
||||
// let's mark the ones we didn't need as available again.
|
||||
self.pool.used -= bytesRequested - bytesRead;
|
||||
var b = this.pool.slice(start, start + bytesRead);
|
||||
|
||||
if (bytesRead === 0) {
|
||||
if (self.isOpen()) {
|
||||
self.serialPoller.start();
|
||||
}
|
||||
} else {
|
||||
var b = self.pool.slice(start, start + bytesRead);
|
||||
|
||||
// do not emit events if the stream is paused
|
||||
if (self.paused) {
|
||||
self.buffer = Buffer.concat([self.buffer, b]);
|
||||
return;
|
||||
}
|
||||
self._emitData(b);
|
||||
|
||||
// do not emit events anymore after we declared the stream unreadable
|
||||
if (!self.readable) {
|
||||
return;
|
||||
}
|
||||
self._read();
|
||||
// do not emit events if the stream is paused
|
||||
if (this.paused) {
|
||||
this.buffer = Buffer.concat([this.buffer, b]);
|
||||
return;
|
||||
}
|
||||
this._emitData(b);
|
||||
|
||||
// do not emit events anymore after we declared the stream unreadable
|
||||
if (!this.readable) {
|
||||
return;
|
||||
}
|
||||
this._read();
|
||||
}
|
||||
}
|
||||
}.bind(this);
|
||||
|
||||
fs.read(self.fd, self.pool, self.pool.used, toRead, null, function (err, bytesRead) {
|
||||
var readPool = self.pool;
|
||||
fs.read(this.fd, this.pool, this.pool.used, toRead, null, function (err, bytesRead) {
|
||||
var readPool = this.pool;
|
||||
var bytesRequested = toRead;
|
||||
afterRead(err, bytesRead, readPool, bytesRequested);
|
||||
});
|
||||
_afterRead(err, bytesRead, readPool, bytesRequested);
|
||||
}.bind(this));
|
||||
|
||||
self.pool.used += toRead;
|
||||
this.pool.used += toRead;
|
||||
};
|
||||
|
||||
SerialPort.prototype._emitData = function (data) {
|
||||
|
|
@ -357,18 +352,16 @@ if (process.platform !== 'win32') {
|
|||
};
|
||||
|
||||
SerialPort.prototype.pause = function () {
|
||||
var self = this;
|
||||
self.paused = true;
|
||||
this.paused = true;
|
||||
};
|
||||
|
||||
SerialPort.prototype.resume = function () {
|
||||
var self = this;
|
||||
self.paused = false;
|
||||
this.paused = false;
|
||||
|
||||
if (self.buffer) {
|
||||
var buffer = self.buffer;
|
||||
self.buffer = null;
|
||||
self._emitData(buffer);
|
||||
if (this.buffer) {
|
||||
var buffer = this.buffer;
|
||||
this.buffer = null;
|
||||
this._emitData(buffer);
|
||||
}
|
||||
|
||||
// No longer open?
|
||||
|
|
@ -376,28 +369,24 @@ if (process.platform !== 'win32') {
|
|||
return;
|
||||
}
|
||||
|
||||
self._read();
|
||||
this._read();
|
||||
};
|
||||
} // if !'win32'
|
||||
|
||||
SerialPort.prototype.disconnected = function (err) {
|
||||
var self = this;
|
||||
var fd = self.fd;
|
||||
|
||||
// send notification of disconnect
|
||||
if (self.options.disconnectedCallback) {
|
||||
self.options.disconnectedCallback(err);
|
||||
if (this.options.disconnectedCallback) {
|
||||
this.options.disconnectedCallback(err);
|
||||
} else {
|
||||
self.emit('disconnect', err);
|
||||
this.emit('disconnect', err);
|
||||
}
|
||||
self.paused = true;
|
||||
self.closing = true;
|
||||
this.paused = true;
|
||||
this.closing = true;
|
||||
|
||||
self.emit('close');
|
||||
this.emit('close');
|
||||
|
||||
// clean up all other items
|
||||
fd = self.fd;
|
||||
|
||||
var fd = this.fd;
|
||||
try {
|
||||
SerialPortBinding.close(fd, function (err) {
|
||||
if (err) {
|
||||
|
|
@ -410,20 +399,19 @@ SerialPort.prototype.disconnected = function (err) {
|
|||
debug('Disconnect completed with an exception: ' + JSON.stringify(e));
|
||||
}
|
||||
|
||||
self.closing = false;
|
||||
self.fd = null;
|
||||
this.closing = false;
|
||||
this.fd = null;
|
||||
|
||||
if (process.platform !== 'win32') {
|
||||
self.readable = false;
|
||||
self.serialPoller.close();
|
||||
this.readable = false;
|
||||
this.serialPoller.close();
|
||||
}
|
||||
};
|
||||
|
||||
SerialPort.prototype.close = function (callback) {
|
||||
var self = this;
|
||||
var fd = self.fd;
|
||||
var fd = this.fd;
|
||||
|
||||
if (self.closing) {
|
||||
if (this.closing) {
|
||||
debug('close attempted, but port is already closing');
|
||||
return this._error(new Error('Port is not open'), callback);
|
||||
}
|
||||
|
|
@ -433,26 +421,25 @@ SerialPort.prototype.close = function (callback) {
|
|||
return this._error(new Error('Port is not open'), callback);
|
||||
}
|
||||
|
||||
self.closing = true;
|
||||
this.closing = true;
|
||||
|
||||
// Stop polling before closing the port.
|
||||
if (process.platform !== 'win32') {
|
||||
self.readable = false;
|
||||
self.serialPoller.close();
|
||||
this.readable = false;
|
||||
this.serialPoller.close();
|
||||
}
|
||||
|
||||
try {
|
||||
SerialPortBinding.close(fd, function (err) {
|
||||
self.closing = false;
|
||||
this.closing = false;
|
||||
if (err) {
|
||||
debug('SerialPortBinding.close had an error', err);
|
||||
return self._error(err, callback);
|
||||
return this._error(err, callback);
|
||||
}
|
||||
|
||||
self.fd = null;
|
||||
self.emit('close');
|
||||
this.fd = null;
|
||||
this.emit('close');
|
||||
if (callback) { callback() }
|
||||
});
|
||||
}.bind(this));
|
||||
} catch (err) {
|
||||
this.closing = false;
|
||||
debug('SerialPortBinding.close had an throwing error', err);
|
||||
|
|
@ -461,21 +448,18 @@ SerialPort.prototype.close = function (callback) {
|
|||
};
|
||||
|
||||
SerialPort.prototype.flush = function (callback) {
|
||||
var self = this;
|
||||
var fd = self.fd;
|
||||
|
||||
if (!this.isOpen()) {
|
||||
debug('flush attempted, but port is not open');
|
||||
return this._error(new Error('Port is not open'), callback);
|
||||
}
|
||||
|
||||
SerialPortBinding.flush(fd, function (err, result) {
|
||||
if (callback) {
|
||||
callback(err, result);
|
||||
} else if (err) {
|
||||
self.emit('error', err);
|
||||
SerialPortBinding.flush(this.fd, function (err, result) {
|
||||
if (err) {
|
||||
debug('SerialPortBinding.flush had an error', err);
|
||||
return this._error(err, callback);
|
||||
}
|
||||
});
|
||||
if (callback) { callback(null, result) }
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
SerialPort.prototype.set = function (options, callback) {
|
||||
|
|
@ -510,7 +494,6 @@ SerialPort.prototype.set = function (options, callback) {
|
|||
};
|
||||
|
||||
SerialPort.prototype.drain = function (callback) {
|
||||
var self = this;
|
||||
var fd = this.fd;
|
||||
|
||||
if (!this.isOpen()) {
|
||||
|
|
@ -519,12 +502,12 @@ SerialPort.prototype.drain = function (callback) {
|
|||
}
|
||||
|
||||
SerialPortBinding.drain(fd, function (err, result) {
|
||||
if (callback) {
|
||||
callback(err, result);
|
||||
} else if (err) {
|
||||
self.emit('error', err);
|
||||
if (err) {
|
||||
debug('SerialPortBinding.drain had an error', err);
|
||||
return this._error(err, callback);
|
||||
}
|
||||
});
|
||||
if (callback) { callback(null, result) }
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
module.exports = factory;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue