diff --git a/serialport.js b/serialport.js index f7f287a..82f807b 100644 --- a/serialport.js +++ b/serialport.js @@ -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); diff --git a/src/serialport.cpp b/src/serialport.cpp index 0d9805f..fe3782f 100644 --- a/src/serialport.cpp +++ b/src/serialport.cpp @@ -109,6 +109,7 @@ v8::Handle Write(const v8::Arguments& args) { baton->buffer = buffer; baton->bufferData = bufferData; baton->bufferLength = bufferLength; + // baton->offset = 0; baton->callback = v8::Persistent::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); diff --git a/src/serialport.h b/src/serialport.h index 3e1dfe6..a3e375e 100644 --- a/src/serialport.h +++ b/src/serialport.h @@ -75,6 +75,7 @@ public: int fd; char* bufferData; size_t bufferLength; + size_t offset; v8::Persistent buffer; v8::Persistent callback; int result; diff --git a/src/serialport_unix.cpp b/src/serialport_unix.cpp index 7191dfb..c3cba1a 100644 --- a/src/serialport_unix.cpp +++ b/src/serialport_unix.cpp @@ -303,10 +303,33 @@ void EIO_Open(uv_work_t* req) { void EIO_Write(uv_work_t* req) { QueuedWrite* queuedWrite = static_cast(req->data); WriteBaton* data = static_cast(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) {