-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fixes #151 * clean up callback explicitly * replace nanomsg_socket_t with NanoMsgPollCtx * clean up ipc files in case tests don't * properly free NanoMsgPollCtx * clean up fcb * remove duplicate code by combining PollSendSocket and PollReceiveSocket into PollSocket * move more logic into PollCtx * everything is awe^H^H^Hconst * prefer static_cast to reinterpret_cast * avoid allocating a Nan::Callback, thanks @kkoopa * more const * clean up WrapPointer a little * use sizeof instead literal value * clean up handle->data * remove a lot of unused WrapPointer code, the ultimate goal is to remove as much black magic as possible * move PollCtx to its own compilation unit * move UV_READABLE check back into NanomsgReadable * consistent use of using keyword * fix race condition in test/shutdown.js It had assumed that a 5 publisher-1 subscriber topology would receive messages in order (which on Travis CI seems to not occur) so had unsafe shutdown code that would clean up prematurely in the case of late deliverance. This would mess up the count of subscribers since the messages effectly race from publishers to subscriber. As an example: before this patch, messages could be delivered in this order: ``` p1 x10 p5 <shutdown occurs> p2 <error, count is all messed up> ``` Now, each socket checks to see that the last one out hits the lights. * Revert "clean up handle->data" @m-ohuchi points out with valgrind that this is an invalid memory write. This reverts commit 4a0d012. * delete node_pointer.h, move pointer wrapping logic into PollCtx * remove unused includes and then sort them * change the line of contributors table to insert my info * remove offset in UnwrapPointer; document wrap_pointer_callback * don't deref an unwrapped PollCtx if it's NULL
- Loading branch information
1 parent
a271345
commit 0461f5c
Showing
10 changed files
with
163 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "nn.h" | ||
#include "poll_ctx.h" | ||
|
||
using v8::Function; | ||
using v8::Local; | ||
using v8::Number; | ||
using v8::Value; | ||
|
||
static void NanomsgReadable(uv_poll_t* req, int /* status */, int events) { | ||
const PollCtx* const context = static_cast<PollCtx*>(req->data); | ||
if (events & UV_READABLE) { | ||
context->invoke_callback(events); | ||
} | ||
} | ||
|
||
void PollCtx::begin_poll (const int s, const bool is_sender) { | ||
size_t siz = sizeof(uv_os_sock_t); | ||
nn_getsockopt(s, NN_SOL_SOCKET, is_sender ? NN_SNDFD : NN_RCVFD, &sockfd, | ||
&siz); | ||
if (sockfd != 0) { | ||
uv_poll_init_socket(uv_default_loop(), &poll_handle, sockfd); | ||
uv_poll_start(&poll_handle, UV_READABLE, NanomsgReadable); | ||
} | ||
} | ||
|
||
PollCtx::PollCtx (const int s, const bool is_sender, | ||
const Local<Function> cb): callback(cb) { | ||
// TODO: maybe container_of can be used instead? | ||
// that would save us this assignment, and ugly static_cast hacks. | ||
poll_handle.data = this; | ||
begin_poll(s, is_sender); | ||
} | ||
|
||
void PollCtx::invoke_callback (const int events) const { | ||
Nan::HandleScope scope; | ||
Local<Value> argv[] = { Nan::New<Number>(events) }; | ||
callback.Call(1, argv); | ||
} | ||
|
||
// Nan will invoke this once it's done with the Buffer, in case we wanted to | ||
// free ptr. In this case, ptr is a PollCtx that we're not done with and don't | ||
// want to free yet (not until PollStop is invoked), so we do nothing. | ||
static void wrap_pointer_cb(char * /* data */, void * /* hint */) {} | ||
|
||
Local<Value> PollCtx::WrapPointer (void* ptr, size_t length) { | ||
return Nan::NewBuffer(static_cast<char *>(ptr), length, wrap_pointer_cb, 0) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
.ToLocalChecked(); | ||
} | ||
|
||
PollCtx* PollCtx::UnwrapPointer (v8::Local<v8::Value> buffer) { | ||
return reinterpret_cast<PollCtx*>(node::Buffer::HasInstance(buffer) ? | ||
node::Buffer::Data(buffer.As<v8::Object>()) : NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include <nan.h> | ||
|
||
class PollCtx { | ||
private: | ||
const Nan::Callback callback; | ||
uv_os_sock_t sockfd; // for libnanomsg | ||
void begin_poll (const int s, const bool is_sender); | ||
public: | ||
uv_poll_t poll_handle; // for libuv | ||
PollCtx (const int s, const bool is_sender, | ||
const v8::Local<v8::Function> cb); | ||
void invoke_callback (const int events) const; | ||
static v8::Local<v8::Value> WrapPointer (void* ptr, size_t length); | ||
static PollCtx* UnwrapPointer (v8::Local<v8::Value> buffer); | ||
}; |
Oops, something went wrong.
we don't need to pass length, move
sizeof
here