mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-25 18:37:19 +00:00
Merge pull request #1018 from munyirik/win32read
Adding WindowsBinding reading
This commit is contained in:
commit
b80025ea10
4 changed files with 185 additions and 29 deletions
|
|
@ -19,7 +19,33 @@ function WindowsBinding(opt) {
|
|||
this.fd = null;
|
||||
};
|
||||
|
||||
WindowsBinding.prototype.read = require('./read-unix');
|
||||
WindowsBinding.prototype.read = function(buffer, offset, length, cb) {
|
||||
if (!Buffer.isBuffer(buffer)) {
|
||||
throw new TypeError('"buffer" is not a Buffer');
|
||||
}
|
||||
|
||||
if (typeof offset !== 'number') {
|
||||
throw new TypeError('"offset" is not an integer');
|
||||
}
|
||||
|
||||
if (typeof length !== 'number') {
|
||||
throw new TypeError('"length" is not an integer');
|
||||
}
|
||||
|
||||
if (buffer.length < offset + length) {
|
||||
throw new TypeError('buffer is too small');
|
||||
}
|
||||
|
||||
if (typeof cb !== 'function') {
|
||||
throw new TypeError('"cb" is not a function');
|
||||
}
|
||||
|
||||
if (!this.isOpen) {
|
||||
return asyncError(cb, new Error('Port is not open'));
|
||||
}
|
||||
|
||||
binding.read(this.fd, buffer, offset, length, cb);
|
||||
};
|
||||
|
||||
WindowsBinding.prototype.open = function(path, options, cb) {
|
||||
if (!path) {
|
||||
|
|
|
|||
|
|
@ -336,6 +336,76 @@ void EIO_AfterWrite(uv_work_t* req) {
|
|||
delete queuedWrite;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
NAN_METHOD(Read) {
|
||||
// file descriptor
|
||||
if (!info[0]->IsInt32()) {
|
||||
Nan::ThrowTypeError("First argument must be an int");
|
||||
return;
|
||||
}
|
||||
int fd = Nan::To<v8::Int32>(info[0]).ToLocalChecked()->Value();
|
||||
|
||||
// buffer
|
||||
if (!info[1]->IsObject() || !node::Buffer::HasInstance(info[1])) {
|
||||
Nan::ThrowTypeError("Second argument must be a buffer");
|
||||
return;
|
||||
}
|
||||
|
||||
// offset
|
||||
if (!info[2]->IsInt32()) {
|
||||
Nan::ThrowTypeError("Third argument must be an int");
|
||||
return;
|
||||
}
|
||||
int offset = Nan::To<v8::Int32>(info[2]).ToLocalChecked()->Value();
|
||||
|
||||
// bytes to read
|
||||
if (!info[3]->IsInt32()) {
|
||||
Nan::ThrowTypeError("Fourth argument must be an int");
|
||||
return;
|
||||
}
|
||||
int bytesToRead = Nan::To<v8::Int32>(info[3]).ToLocalChecked()->Value();
|
||||
|
||||
// callback
|
||||
if (!info[4]->IsFunction()) {
|
||||
Nan::ThrowTypeError("Fifth argument must be a function");
|
||||
return;
|
||||
}
|
||||
|
||||
ReadBaton* data = new ReadBaton();
|
||||
memset(data, 0, sizeof(ReadBaton));
|
||||
data->fd = fd;
|
||||
data->offset = offset;
|
||||
data->bytesToRead = bytesToRead;
|
||||
v8::Local<v8::Object> buffer = info[1]->ToObject();
|
||||
data->bufferData = node::Buffer::Data(buffer);
|
||||
data->callback.Reset(info[4].As<v8::Function>());
|
||||
|
||||
uv_work_t* req = new uv_work_t();
|
||||
req->data = data;
|
||||
uv_queue_work(uv_default_loop(), req, EIO_Read, (uv_after_work_cb)EIO_AfterRead);
|
||||
}
|
||||
|
||||
void EIO_AfterRead(uv_work_t* req) {
|
||||
Nan::HandleScope scope;
|
||||
ReadBaton* data = static_cast<ReadBaton*>(req->data);
|
||||
|
||||
v8::Local<v8::Value> argv[2];
|
||||
|
||||
if (data->errorString[0]) {
|
||||
argv[0] = Nan::Error(data->errorString);
|
||||
argv[1] = Nan::Undefined();
|
||||
} else {
|
||||
argv[0] = Nan::Null();
|
||||
argv[1] = Nan::New<v8::Integer>((int)data->bytesRead);
|
||||
}
|
||||
|
||||
data->callback.Call(2, argv);
|
||||
|
||||
delete data;
|
||||
delete req;
|
||||
}
|
||||
#endif
|
||||
|
||||
NAN_METHOD(Close) {
|
||||
// file descriptor
|
||||
if (!info[0]->IsInt32()) {
|
||||
|
|
@ -681,6 +751,9 @@ extern "C" {
|
|||
Nan::SetMethod(target, "open", Open);
|
||||
Nan::SetMethod(target, "update", Update);
|
||||
Nan::SetMethod(target, "write", Write);
|
||||
#ifdef WIN32
|
||||
Nan::SetMethod(target, "read", Read);
|
||||
#endif
|
||||
Nan::SetMethod(target, "close", Close);
|
||||
Nan::SetMethod(target, "list", List);
|
||||
Nan::SetMethod(target, "flush", Flush);
|
||||
|
|
|
|||
|
|
@ -26,6 +26,12 @@ NAN_METHOD(Write);
|
|||
void EIO_Write(uv_work_t* req);
|
||||
void EIO_AfterWrite(uv_work_t* req);
|
||||
|
||||
#ifdef WIN32
|
||||
NAN_METHOD(Read);
|
||||
void EIO_Read(uv_work_t* req);
|
||||
void EIO_AfterRead(uv_work_t* req);
|
||||
#endif
|
||||
|
||||
NAN_METHOD(Close);
|
||||
void EIO_Close(uv_work_t* req);
|
||||
void EIO_AfterClose(uv_work_t* req);
|
||||
|
|
@ -141,6 +147,19 @@ struct QueuedWrite {
|
|||
}
|
||||
};
|
||||
|
||||
#ifdef WIN32
|
||||
struct ReadBaton {
|
||||
int fd;
|
||||
char* bufferData;
|
||||
size_t bufferLength;
|
||||
size_t bytesRead;
|
||||
size_t bytesToRead;
|
||||
size_t offset;
|
||||
char errorString[ERROR_STRING_SIZE];
|
||||
Nan::Callback callback;
|
||||
};
|
||||
#endif
|
||||
|
||||
struct ListResultItem {
|
||||
std::string comName;
|
||||
std::string manufacturer;
|
||||
|
|
|
|||
|
|
@ -157,18 +157,6 @@ void EIO_Open(uv_work_t* req) {
|
|||
data->result = (int)file;
|
||||
}
|
||||
|
||||
struct WatchPortBaton {
|
||||
HANDLE fd;
|
||||
DWORD bytesRead;
|
||||
char buffer[MAX_BUFFER_SIZE];
|
||||
char errorString[ERROR_STRING_SIZE];
|
||||
DWORD errorCode;
|
||||
bool disconnected;
|
||||
Nan::Callback* dataCallback;
|
||||
Nan::Callback* errorCallback;
|
||||
Nan::Callback* disconnectedCallback;
|
||||
};
|
||||
|
||||
void EIO_Update(uv_work_t* req) {
|
||||
ConnectionOptionsBaton* data = static_cast<ConnectionOptionsBaton*>(req->data);
|
||||
|
||||
|
|
@ -254,22 +242,6 @@ bool IsClosingHandle(int fd) {
|
|||
return false;
|
||||
}
|
||||
|
||||
void DisposeWatchPortCallbacks(WatchPortBaton* data) {
|
||||
delete data->dataCallback;
|
||||
delete data->errorCallback;
|
||||
delete data->disconnectedCallback;
|
||||
}
|
||||
|
||||
// FinalizerCallback will prevent WatchPortBaton::buffer from getting
|
||||
// collected by gc while finalizing v8::ArrayBuffer. The buffer will
|
||||
// get cleaned up through this callback.
|
||||
static void FinalizerCallback(char* data, void* hint) {
|
||||
uv_work_t* req = reinterpret_cast<uv_work_t*>(hint);
|
||||
WatchPortBaton* wpb = static_cast<WatchPortBaton*>(req->data);
|
||||
delete wpb;
|
||||
delete req;
|
||||
}
|
||||
|
||||
void EIO_Write(uv_work_t* req) {
|
||||
QueuedWrite* queuedWrite = static_cast<QueuedWrite*>(req->data);
|
||||
WriteBaton* data = static_cast<WriteBaton*>(queuedWrite->baton);
|
||||
|
|
@ -313,6 +285,72 @@ void EIO_Write(uv_work_t* req) {
|
|||
} while (data->bufferLength > data->offset);
|
||||
}
|
||||
|
||||
void EIO_Read(uv_work_t* req) {
|
||||
ReadBaton* data = static_cast<ReadBaton*>(req->data);
|
||||
data->bytesRead = 0;
|
||||
int errorCode = ERROR_SUCCESS;
|
||||
|
||||
char* offsetPtr = data->bufferData;
|
||||
offsetPtr += data->offset;
|
||||
|
||||
// Event used by GetOverlappedResult(..., TRUE) to wait for incoming data or timeout
|
||||
// Event MUST be used if program has several simultaneous asynchronous operations
|
||||
// on the same handle (i.e. ReadFile and WriteFile)
|
||||
HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||
|
||||
while (true) {
|
||||
OVERLAPPED ov = {0};
|
||||
ov.hEvent = hEvent;
|
||||
|
||||
// Start read operation - synchrounous or asynchronous
|
||||
DWORD bytesReadSync = 0;
|
||||
if (!ReadFile((HANDLE)data->fd, offsetPtr, data->bytesToRead, &bytesReadSync, &ov)) {
|
||||
errorCode = GetLastError();
|
||||
if (errorCode != ERROR_IO_PENDING) {
|
||||
// Read operation error
|
||||
if (errorCode == ERROR_OPERATION_ABORTED) {
|
||||
} else {
|
||||
ErrorCodeToString("Reading from COM port (ReadFile)", errorCode, data->errorString);
|
||||
CloseHandle(hEvent);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Read operation is asynchronous and is pending
|
||||
// We MUST wait for operation completion before deallocation of OVERLAPPED struct
|
||||
// or read data buffer
|
||||
|
||||
// Wait for async read operation completion or timeout
|
||||
DWORD bytesReadAsync = 0;
|
||||
if (!GetOverlappedResult((HANDLE)data->fd, &ov, &bytesReadAsync, TRUE)) {
|
||||
// Read operation error
|
||||
errorCode = GetLastError();
|
||||
if (errorCode == ERROR_OPERATION_ABORTED) {
|
||||
} else {
|
||||
ErrorCodeToString("Reading from COM port (GetOverlappedResult)", errorCode, data->errorString);
|
||||
CloseHandle(hEvent);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
// Read operation completed asynchronously
|
||||
data->bytesRead = bytesReadAsync;
|
||||
}
|
||||
} else {
|
||||
// Read operation completed synchronously
|
||||
data->bytesRead = bytesReadSync;
|
||||
}
|
||||
|
||||
// Return data received if any
|
||||
if (data->bytesRead > 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CloseHandle(hEvent);
|
||||
}
|
||||
|
||||
void EIO_Close(uv_work_t* req) {
|
||||
VoidBaton* data = static_cast<VoidBaton*>(req->data);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue