Skip to content

Commit

Permalink
Merging fix for gh-369
Browse files Browse the repository at this point in the history
Debug logging

CLOEXEC and SYNC flags

Stop polling before closing port.

Better error message for polling error, propergate errors to disconnected callback

trying to fix travis build for node 11
  • Loading branch information
Jonas authored and sweetpi committed Oct 5, 2014
1 parent 81be32c commit 2cab68f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
38 changes: 21 additions & 17 deletions serialport.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ function SerialPortFactory() {
options.parser(self, data);
};

options.disconnectedCallback = options.disconnectedCallback || function () {
options.disconnectedCallback = options.disconnectedCallback || function (err) {
if (self.closing) {
return;
}
var err = new Error("Disconnected");
if(!err) {
err = new Error("Disconnected");
}
self.emit("disconnect",err);
};

Expand Down Expand Up @@ -200,7 +202,7 @@ function SerialPortFactory() {
if(!err) {
self._read();
} else {
self.disconnected();
self.disconnected(err);
}
});
self.serialPoller.start();
Expand Down Expand Up @@ -274,10 +276,9 @@ function SerialPortFactory() {
self.serialPoller.start();
}
} else if (err.code && (err.code === "EBADF" || err.code === 'ENXIO' || (err.errno===-1 || err.code === 'UNKNOWN'))) { // handle edge case were mac/unix doesn't clearly know the error.
self.disconnected();
self.disconnected(err);
} else {
self.fd = null;
// console.log("afterRead");
self.emit('error', err);
self.readable = false;
}
Expand Down Expand Up @@ -351,15 +352,15 @@ function SerialPortFactory() {
} // if !'win32'


SerialPort.prototype.disconnected = function (callback) {
SerialPort.prototype.disconnected = function (err) {
var self = this;
var fd = self.fd;

// send notification of disconnect
if (self.options.disconnectedCallback) {
self.options.disconnectedCallback();
self.options.disconnectedCallback(err);
} else {
self.emit("disconnect");
self.emit("disconnect", err);
}
self.paused = true;
self.closing = true;
Expand All @@ -371,7 +372,11 @@ function SerialPortFactory() {

try {
factory.SerialPortBinding.close(fd, function (err) {
console.log('Disconnect completed' + (err ? ' with error' : ''), err);
if(err) {
console.log('Disconnect completed with error:' + err);
} else {
console.log('Disconnect completed');
}
});
} catch (e) {
console.log('Disconnect failed with exception', e);
Expand All @@ -386,9 +391,6 @@ function SerialPortFactory() {
self.serialPoller.close();
}

if (callback) {
callback();
}
};


Expand All @@ -412,6 +414,13 @@ function SerialPortFactory() {
}

self.closing = true;

// Stop polling before closing the port.
if (process.platform !== 'win32') {
self.readable = false;
self.serialPoller.close();
}

try {
factory.SerialPortBinding.close(fd, function (err) {

Expand All @@ -430,11 +439,6 @@ function SerialPortFactory() {
self.closing = false;
self.fd = 0;

if (process.platform !== 'win32') {
self.readable = false;
self.serialPoller.close();
}

if (callback) {
callback();
}
Expand Down
1 change: 1 addition & 0 deletions src/serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ void EIO_AfterWrite(uv_work_t* req) {
// We're not done with this baton, so throw it right back onto the queue.
// Don't re-push the write in the event loop if there was an error; because same error could occur again!
// TODO: Add a uv_poll here for unix...
fprintf(stderr, "Write again...\n");
uv_queue_work(uv_default_loop(), req, EIO_Write, (uv_after_work_cb)EIO_AfterWrite);
return;
}
Expand Down
17 changes: 13 additions & 4 deletions src/serialport_poller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ SerialportPoller::~SerialportPoller() {
};

void _serialportReadable(uv_poll_t *req, int status, int events) {
SerialportPoller* obj = (SerialportPoller*) req->data;
SerialportPoller* sp = (SerialportPoller*) req->data;
// We can stop polling until we have read all of the data...
obj->_stop();
obj->callCallback(status);
sp->_stop();
sp->callCallback(status);
}

void SerialportPoller::callCallback(int status) {
Expand All @@ -29,7 +29,16 @@ void SerialportPoller::callCallback(int status) {

v8::Handle<v8::Value> argv[1];
if(status != 0) {
argv[0] = v8::Exception::Error(NanNew<v8::String>("polling error"));
// error handling changed in libuv, see:
// https://github.com/joyent/libuv/commit/3ee4d3f183331
#ifdef UV_ERRNO_H_
const char* err_string = uv_strerror(status);
#else
uv_err_t errno = uv_last_error(uv_default_loop());
const char* err_string = uv_strerror(errno);
#endif
snprintf(this->errorString, sizeof(this->errorString), "Error %s on polling", err_string);
argv[0] = v8::Exception::Error(NanNew<v8::String>(this->errorString));
} else {
argv[0] = NanUndefined();
}
Expand Down
2 changes: 2 additions & 0 deletions src/serialport_poller.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define SERIALPORT_POLLER_H

#include <nan.h>
#include "serialport.h"

class SerialportPoller : public node::ObjectWrap {
public:
Expand All @@ -26,6 +27,7 @@ class SerialportPoller : public node::ObjectWrap {

uv_poll_t poll_handle_;
int fd_;
char errorString[ERROR_STRING_SIZE];

NanCallback* callback_;
};
Expand Down
2 changes: 1 addition & 1 deletion src/serialport_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void EIO_Open(uv_work_t* req) {
}


int flags = (O_RDWR | O_NOCTTY | O_NONBLOCK);
int flags = (O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC | O_SYNC);
int fd = open(data->path, flags);

if (fd == -1) {
Expand Down

0 comments on commit 2cab68f

Please sign in to comment.