Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow initialization to fail if the program gets stuck in the postServiceCheck while-loop #22

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/addon.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "pcsclite.h"
#include "cardreader.h"

void init_all(v8::Handle<v8::Object> target) {
void init_all(v8::Local<v8::Object> target) {
PCSCLite::init(target);
CardReader::init(target);
}
Expand Down
40 changes: 26 additions & 14 deletions src/cardreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace node;

Nan::Persistent<Function> CardReader::constructor;

void CardReader::init(Handle<Object> target) {
void CardReader::init(Local<Object> target) {

// Prepare constructor template
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
Expand Down Expand Up @@ -58,8 +58,10 @@ void CardReader::init(Handle<Object> target) {
Nan::SetPrototypeTemplate(tpl, "SCARD_UNPOWER_CARD", Nan::New(SCARD_UNPOWER_CARD));
Nan::SetPrototypeTemplate(tpl, "SCARD_EJECT_CARD", Nan::New(SCARD_EJECT_CARD));

constructor.Reset(tpl->GetFunction());
target->Set(Nan::New("CardReader").ToLocalChecked(), tpl->GetFunction());
Local <Context> context = Nan::GetCurrentContext();

constructor.Reset(tpl->GetFunction(context).ToLocalChecked());
target->Set(Nan::New("CardReader").ToLocalChecked(), tpl->GetFunction(context).ToLocalChecked());
}

CardReader::CardReader(const std::string &reader_name): m_card_context(0),
Expand Down Expand Up @@ -88,10 +90,11 @@ NAN_METHOD(CardReader::New) {

Nan::HandleScope scope;

v8::String::Utf8Value reader_name(info[0]->ToString());
Nan::Utf8String reader_name(Nan::To<String>(info[0]).ToLocalChecked());

CardReader* obj = new CardReader(*reader_name);
obj->Wrap(info.Holder());
obj->handle()->Set(Nan::New(name_symbol), info[0]->ToString());
obj->handle()->Set(Nan::New(name_symbol), Nan::To<String>(info[0]).ToLocalChecked());
obj->handle()->Set(Nan::New(connected_symbol), Nan::False());

info.GetReturnValue().Set(info.Holder());
Expand Down Expand Up @@ -133,9 +136,11 @@ NAN_METHOD(CardReader::Connect) {
return Nan::ThrowError("Third argument must be a callback function");
}

Local<Context> context = Nan::GetCurrentContext();

ConnectInput* ci = new ConnectInput();
ci->share_mode = info[0]->Uint32Value();
ci->pref_protocol = info[1]->Uint32Value();
ci->share_mode = info[0]->Uint32Value(context).FromJust();
ci->pref_protocol = info[1]->Uint32Value(context).FromJust();
Local<Function> cb = Local<Function>::Cast(info[2]);

// This creates our work request, including the libuv struct.
Expand Down Expand Up @@ -169,7 +174,9 @@ NAN_METHOD(CardReader::Disconnect) {
return Nan::ThrowError("Second argument must be a callback function");
}

DWORD disposition = info[0]->Uint32Value();
Local<Context> context = Nan::GetCurrentContext();

DWORD disposition = info[0]->Uint32Value(context).FromJust();
Local<Function> cb = Local<Function>::Cast(info[1]);

// This creates our work request, including the libuv struct.
Expand Down Expand Up @@ -215,9 +222,12 @@ NAN_METHOD(CardReader::Transmit) {
return Nan::ThrowError("Fourth argument must be a callback function");
}

Local<Object> buffer_data = info[0]->ToObject();
uint32_t out_len = info[1]->Uint32Value();
uint32_t protocol = info[2]->Uint32Value();
Local<Context> context = Nan::GetCurrentContext();

Local<Object> buffer_data = Nan::To<Object>(info[0]).ToLocalChecked();
uint32_t out_len = info[1]->Uint32Value(context).FromJust();
uint32_t protocol = info[2]->Uint32Value(context).FromJust();

Local<Function> cb = Local<Function>::Cast(info[3]);

// This creates our work request, including the libuv struct.
Expand Down Expand Up @@ -270,9 +280,11 @@ NAN_METHOD(CardReader::Control) {
return Nan::ThrowError("Fourth argument must be a callback function");
}

Local<Object> in_buf = info[0]->ToObject();
DWORD control_code = info[1]->Uint32Value();
Local<Object> out_buf = info[2]->ToObject();
Local<Context> context = Nan::GetCurrentContext();

Local<Object> in_buf = Nan::To<Object>(info[0]).ToLocalChecked();
DWORD control_code = info[1]->Uint32Value(context).FromJust();
Local<Object> out_buf = Nan::To<Object>(info[2]).ToLocalChecked();
Local<Function> cb = Local<Function>::Cast(info[3]);

// This creates our work request, including the libuv struct.
Expand Down
4 changes: 2 additions & 2 deletions src/cardreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class CardReader: public Nan::ObjectWrap {

public:

static void init(v8::Handle<v8::Object> target);
static void init(v8::Local<v8::Object> target);

const SCARDHANDLE& GetHandler() const { return m_card_handle; };

Expand Down Expand Up @@ -120,7 +120,7 @@ class CardReader: public Nan::ObjectWrap {
static void AfterTransmit(uv_work_t* req, int status);
static void AfterControl(uv_work_t* req, int status);

static v8::Handle<v8::Value> CreateBufferInstance(char* data, unsigned long size);
static v8::Local<v8::Value> CreateBufferInstance(char* data, unsigned long size);

private:

Expand Down
16 changes: 12 additions & 4 deletions src/pcsclite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ using namespace node;

Nan::Persistent<Function> PCSCLite::constructor;

void PCSCLite::init(Handle<Object> target) {
void PCSCLite::init(Local<Object> target) {

Local<Context> context = Nan::GetCurrentContext();

// Prepare constructor template
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
Expand All @@ -16,8 +18,9 @@ void PCSCLite::init(Handle<Object> target) {
Nan::SetPrototypeTemplate(tpl, "start", Nan::New<FunctionTemplate>(Start));
Nan::SetPrototypeTemplate(tpl, "close", Nan::New<FunctionTemplate>(Close));

constructor.Reset(tpl->GetFunction());
target->Set(Nan::New("PCSCLite").ToLocalChecked(), tpl->GetFunction());

constructor.Reset(tpl->GetFunction(context).ToLocalChecked());
target->Set(Nan::New("PCSCLite").ToLocalChecked(), tpl->GetFunction(context).ToLocalChecked());
}

PCSCLite::PCSCLite(): m_card_context(0),
Expand Down Expand Up @@ -64,12 +67,17 @@ PCSCLite::PCSCLite(): m_card_context(0),

postServiceCheck:
LONG result;
int numRetries = 0;
do {
result = SCardEstablishContext(SCARD_SCOPE_SYSTEM,
NULL,
NULL,
&m_card_context);
} while(result == SCARD_E_NO_SERVICE || result == SCARD_E_SERVICE_STOPPED);
if (result != SCARD_S_SUCCESS) {
numRetries++;
Sleep(750);
}
} while((result == SCARD_E_NO_SERVICE || result == SCARD_E_SERVICE_STOPPED) && numRetries < 3);
if (result != SCARD_S_SUCCESS) {
Nan::ThrowError(error_msg("SCardEstablishContext", result).c_str());
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/pcsclite.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PCSCLite: public Nan::ObjectWrap {

public:

static void init(v8::Handle<v8::Object> target);
static void init(v8::Local<v8::Object> target);

private:

Expand Down