Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
Should fix missing incoming messages (#531)
Browse files Browse the repository at this point in the history
* Should fix missing incoming messages

* Final fix
  • Loading branch information
Ron Korving committed May 22, 2016
1 parent 7722986 commit 597cb72
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
63 changes: 45 additions & 18 deletions binding.cc
Expand Up @@ -103,7 +103,10 @@ namespace zmq {
public:
static NAN_MODULE_INIT(Initialize);
virtual ~Socket();
void NotifyReadReady();
void NotifySendReady();
void CallbackIfReady();

#if ZMQ_CAN_MONITOR
void MonitorEvent(uint16_t event_id, int32_t event_value, char *endpoint);
void MonitorError(const char *error_msg);
Expand Down Expand Up @@ -181,7 +184,9 @@ namespace zmq {
static void UV_PollCallback(uv_poll_t* handle, int status, int events);
};

Nan::Persistent<String> callback_symbol;
Nan::Persistent<String> send_callback_symbol;
Nan::Persistent<String> read_callback_symbol;

#if ZMQ_CAN_MONITOR
Nan::Persistent<String> monitor_symbol;
Nan::Persistent<String> monitor_error;
Expand Down Expand Up @@ -339,7 +344,8 @@ namespace zmq {

Nan::Set(target, Nan::New("SocketBinding").ToLocalChecked(), Nan::GetFunction(t).ToLocalChecked());

callback_symbol.Reset(Nan::New("onReady").ToLocalChecked());
read_callback_symbol.Reset(Nan::New("onReadReady").ToLocalChecked());
send_callback_symbol.Reset(Nan::New("onSendReady").ToLocalChecked());
}

Socket::~Socket() {
Expand Down Expand Up @@ -386,22 +392,32 @@ namespace zmq {
return item.revents & item.events;
}

void
Socket::NotifyReadReady() {
Nan::HandleScope scope;
Local<Value> callback_v = Nan::Get(this->handle(), Nan::New(read_callback_symbol)).ToLocalChecked();

Nan::MakeCallback(this->handle(), callback_v.As<Function>(), 0, NULL);
}

void
Socket::NotifySendReady() {
Nan::HandleScope scope;
Local<Value> callback_v = Nan::Get(this->handle(), Nan::New(send_callback_symbol)).ToLocalChecked();

Nan::MakeCallback(this->handle(), callback_v.As<Function>(), 0, NULL);
}

void
Socket::CallbackIfReady() {
short events = PollForEvents();
if (events != 0) {
Nan::HandleScope scope;

Local<Value> callback_v = Nan::Get(this->handle(), Nan::New(callback_symbol)).ToLocalChecked();
if (!callback_v->IsFunction()) {
return;
}

Local<Value> argv[2];
argv[0] = Nan::New<Boolean>((events & ZMQ_POLLIN) != 0);
argv[1] = Nan::New<Boolean>((events & ZMQ_POLLOUT) != 0);
if ((events & ZMQ_POLLIN) != 0) {
NotifyReadReady();
}

Nan::MakeCallback(this->handle(), callback_v.As<Function>(), 2, argv);
if ((events & ZMQ_POLLOUT) != 0) {
NotifySendReady();
}
}

Expand Down Expand Up @@ -1234,6 +1250,7 @@ namespace zmq {
int events;
size_t events_size = sizeof(events);
bool checkPollOut = true;
bool readsReady = false;

int rc;

Expand All @@ -1253,6 +1270,10 @@ namespace zmq {
return Nan::ThrowError(ErrorMessage());
}

if ((events & ZMQ_POLLIN) != 0) {
readsReady = true;
}

if ((events & ZMQ_POLLOUT) == 0)
return info.GetReturnValue().Set(false);
}
Expand Down Expand Up @@ -1292,11 +1313,17 @@ namespace zmq {
}
}

if (checkPollOut) {
while (zmq_getsockopt(socket->socket_, ZMQ_EVENTS, &events, &events_size)) {
if (zmq_errno() != EINTR)
return Nan::ThrowError(ErrorMessage());
}
while (zmq_getsockopt(socket->socket_, ZMQ_EVENTS, &events, &events_size)) {
if (zmq_errno() != EINTR)
return Nan::ThrowError(ErrorMessage());
}

if ((events & ZMQ_POLLIN) != 0) {
readsReady = true;
}

if (readsReady) {
socket->NotifyReadReady();
}

return info.GetReturnValue().Set(true);
Expand Down
9 changes: 6 additions & 3 deletions lib/index.js
Expand Up @@ -293,9 +293,12 @@ exports.Socket = function (type) {
this._isFlushingWrites = false;
this._outgoing = new BatchList();

this._zmq.onReady = function(readable, writable) {
if (readable) self._flushReads();
if (writable) self._flushWrites();
this._zmq.onReadReady = function () {
self._flushReads();
};

this._zmq.onSendReady = function () {
self._flushWrites();
};
};

Expand Down

0 comments on commit 597cb72

Please sign in to comment.