mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-26 02:47:17 +00:00
Fixing write to support non-blocking.
This commit is contained in:
parent
512bc80c2d
commit
dcadb0d842
4 changed files with 39 additions and 7 deletions
|
|
@ -148,8 +148,8 @@ function SerialPort (path, options, openImmediately) {
|
|||
// };
|
||||
|
||||
options.errorCallback = function (err) {
|
||||
// console.log("err:", JSON.stringify(err));
|
||||
self.emit('error', err);
|
||||
console.log("sp err:", JSON.stringify(err));
|
||||
self.emit('error', {spErr: err});
|
||||
};
|
||||
options.disconnectedCallback = function () {
|
||||
if (self.closing) {
|
||||
|
|
@ -190,7 +190,7 @@ SerialPort.prototype.open = function (callback) {
|
|||
SerialPortBinding.open(this.path, this.options, function (err, fd) {
|
||||
self.fd = fd;
|
||||
if (err) {
|
||||
self.emit('error', err);
|
||||
self.emit('error', {openErr: err});
|
||||
if (callback) { callback(err); }
|
||||
return;
|
||||
}
|
||||
|
|
@ -231,7 +231,7 @@ SerialPort.prototype.write = function (buffer, callback) {
|
|||
}
|
||||
SerialPortBinding.write(this.fd, buffer, function (err, results) {
|
||||
if (err) {
|
||||
self.emit('error', err);
|
||||
self.emit('error', {spWriteErr: err});
|
||||
}
|
||||
if (callback) {
|
||||
callback(err, results);
|
||||
|
|
|
|||
|
|
@ -109,6 +109,7 @@ v8::Handle<v8::Value> Write(const v8::Arguments& args) {
|
|||
baton->buffer = buffer;
|
||||
baton->bufferData = bufferData;
|
||||
baton->bufferLength = bufferLength;
|
||||
// baton->offset = 0;
|
||||
baton->callback = v8::Persistent<v8::Value>::New(callback);
|
||||
|
||||
QueuedWrite* queuedWrite = new QueuedWrite();
|
||||
|
|
@ -144,6 +145,13 @@ void EIO_AfterWrite(uv_work_t* req) {
|
|||
}
|
||||
v8::Function::Cast(*data->callback)->Call(v8::Context::GetCurrent()->Global(), 2, argv);
|
||||
|
||||
if (data->offset < data->bufferLength) {
|
||||
// We're not done with this baton, so throw it right back onto the queue.
|
||||
// TODO: Add a uv_poll here for unix...
|
||||
uv_queue_work(uv_default_loop(), req, EIO_Write, (uv_after_work_cb)EIO_AfterWrite);
|
||||
return;
|
||||
}
|
||||
|
||||
uv_mutex_lock(&write_queue_mutex);
|
||||
ngx_queue_remove(&queuedWrite->queue);
|
||||
|
||||
|
|
|
|||
|
|
@ -75,6 +75,7 @@ public:
|
|||
int fd;
|
||||
char* bufferData;
|
||||
size_t bufferLength;
|
||||
size_t offset;
|
||||
v8::Persistent<v8::Object> buffer;
|
||||
v8::Persistent<v8::Value> callback;
|
||||
int result;
|
||||
|
|
|
|||
|
|
@ -303,10 +303,33 @@ void EIO_Open(uv_work_t* req) {
|
|||
void EIO_Write(uv_work_t* req) {
|
||||
QueuedWrite* queuedWrite = static_cast<QueuedWrite*>(req->data);
|
||||
WriteBaton* data = static_cast<WriteBaton*>(queuedWrite->baton);
|
||||
|
||||
data->result = 0;
|
||||
errno = 0;
|
||||
|
||||
if ((data->result = write(data->fd, data->bufferData, data->bufferLength)) == -1) {
|
||||
snprintf(data->errorString, sizeof(data->errorString), "Error %s calling write(...)", strerror(errno) );
|
||||
}
|
||||
// We carefully *DON'T* break out of this loop.
|
||||
do {
|
||||
if ((data->result = write(data->fd, data->bufferData + data->offset, data->bufferLength - data->offset)) == -1) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
||||
return;
|
||||
|
||||
// The write call might be interrupted, if it is we just try again immediately.
|
||||
if (errno != EINTR) {
|
||||
snprintf(data->errorString, sizeof(data->errorString), "Error %s calling write(...)", strerror(errno) );
|
||||
return;
|
||||
}
|
||||
|
||||
// try again...
|
||||
continue;
|
||||
}
|
||||
// there wasn't an error, do the math on what we actually wrote...
|
||||
else {
|
||||
data->offset += data->result;
|
||||
}
|
||||
|
||||
// if we get there, we really don't want to loop
|
||||
// break;
|
||||
} while (data->bufferLength > data->offset);
|
||||
}
|
||||
|
||||
void EIO_Close(uv_work_t* req) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue