mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-26 02:47:17 +00:00
Adding WindowsBinding reading
This commit is contained in:
parent
908b75356e
commit
975b270ba3
4 changed files with 199 additions and 21 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,56 @@ void EIO_AfterWrite(uv_work_t* req) {
|
|||
delete queuedWrite;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
v8::Local<v8::Object> buffer = info[1]->ToObject();
|
||||
char* bufferData = node::Buffer::Data(buffer);
|
||||
size_t bufferLength = node::Buffer::Length(buffer);
|
||||
|
||||
// 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;
|
||||
data->bufferLength = bufferLength;
|
||||
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);
|
||||
}
|
||||
|
||||
NAN_METHOD(Close) {
|
||||
// file descriptor
|
||||
if (!info[0]->IsInt32()) {
|
||||
|
|
@ -681,6 +731,7 @@ extern "C" {
|
|||
Nan::SetMethod(target, "open", Open);
|
||||
Nan::SetMethod(target, "update", Update);
|
||||
Nan::SetMethod(target, "write", Write);
|
||||
Nan::SetMethod(target, "read", Read);
|
||||
Nan::SetMethod(target, "close", Close);
|
||||
Nan::SetMethod(target, "list", List);
|
||||
Nan::SetMethod(target, "flush", Flush);
|
||||
|
|
|
|||
|
|
@ -26,6 +26,10 @@ NAN_METHOD(Write);
|
|||
void EIO_Write(uv_work_t* req);
|
||||
void EIO_AfterWrite(uv_work_t* req);
|
||||
|
||||
NAN_METHOD(Read);
|
||||
void EIO_Read(uv_work_t* req);
|
||||
void EIO_AfterRead(uv_work_t* req);
|
||||
|
||||
NAN_METHOD(Close);
|
||||
void EIO_Close(uv_work_t* req);
|
||||
void EIO_AfterClose(uv_work_t* req);
|
||||
|
|
@ -141,6 +145,17 @@ struct QueuedWrite {
|
|||
}
|
||||
};
|
||||
|
||||
struct ReadBaton {
|
||||
int fd;
|
||||
char* bufferData;
|
||||
size_t bufferLength;
|
||||
DWORD bytesRead;
|
||||
DWORD bytesToRead;
|
||||
int offset;
|
||||
char errorString[ERROR_STRING_SIZE];
|
||||
Nan::Callback callback;
|
||||
};
|
||||
|
||||
struct ListResultItem {
|
||||
std::string comName;
|
||||
std::string manufacturer;
|
||||
|
|
|
|||
|
|
@ -155,18 +155,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);
|
||||
|
||||
|
|
@ -252,19 +240,16 @@ 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;
|
||||
ReadBaton* rb = static_cast<ReadBaton*>(req->data);
|
||||
if (rb->bufferData) {
|
||||
delete rb->bufferData;
|
||||
}
|
||||
delete rb;
|
||||
delete req;
|
||||
}
|
||||
|
||||
|
|
@ -311,6 +296,107 @@ 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;
|
||||
if (data->bufferData == NULL) {
|
||||
data->bufferData = new char[data->bufferLength];
|
||||
memset(data->bufferData, 0, data->bufferLength);
|
||||
}
|
||||
|
||||
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_AfterRead(uv_work_t* req) {
|
||||
Nan::HandleScope scope;
|
||||
bool skipCleanup = false;
|
||||
ReadBaton* data = static_cast<ReadBaton*>(req->data);
|
||||
|
||||
v8::Local<v8::Value> argv[3];
|
||||
|
||||
if (data->errorString[0]) {
|
||||
argv[0] = Nan::Error(data->errorString);
|
||||
argv[1] = argv[2] = Nan::Undefined();
|
||||
Sleep(100); // prevent the errors from occurring too fast
|
||||
} else {
|
||||
argv[0] = Nan::Null();
|
||||
argv[1] = Nan::New<v8::Integer>((int)data->bytesRead);
|
||||
argv[2] = Nan::NewBuffer(data->bufferData, data->bufferLength, FinalizerCallback, req).ToLocalChecked();
|
||||
skipCleanup = true;
|
||||
}
|
||||
|
||||
data->callback.Call(3, argv);
|
||||
|
||||
cleanup:
|
||||
if (!skipCleanup) {
|
||||
if (data->bufferData) {
|
||||
delete data->bufferData;
|
||||
}
|
||||
delete data;
|
||||
delete req;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void EIO_Close(uv_work_t* req) {
|
||||
VoidBaton* data = static_cast<VoidBaton*>(req->data);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue