|
|
@ -3,6 +3,7 @@ |
|
|
|
#include <req_wrap.h> |
|
|
|
#include <handle_wrap.h> |
|
|
|
#include <stream_wrap.h> |
|
|
|
#include <tcp_wrap.h> |
|
|
|
|
|
|
|
// Temporary hack: libuv should provide uv_inet_pton and uv_inet_ntop.
|
|
|
|
#if defined(__MINGW32__) || defined(_MSC_VER) |
|
|
@ -45,8 +46,7 @@ using v8::Context; |
|
|
|
using v8::Arguments; |
|
|
|
using v8::Integer; |
|
|
|
|
|
|
|
Persistent<Function> tcpConstructor; |
|
|
|
|
|
|
|
static Persistent<Function> tcpConstructor; |
|
|
|
static Persistent<String> family_symbol; |
|
|
|
static Persistent<String> address_symbol; |
|
|
|
static Persistent<String> port_symbol; |
|
|
@ -55,10 +55,13 @@ static Persistent<String> port_symbol; |
|
|
|
typedef class ReqWrap<uv_connect_t> ConnectWrap; |
|
|
|
|
|
|
|
|
|
|
|
class TCPWrap : public StreamWrap { |
|
|
|
public: |
|
|
|
Local<Object> TCPWrap::Instantiate() { |
|
|
|
HandleScope scope; |
|
|
|
return scope.Close(tcpConstructor->NewInstance()); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void Initialize(Handle<Object> target) { |
|
|
|
void TCPWrap::Initialize(Handle<Object> target) { |
|
|
|
HandleWrap::Initialize(target); |
|
|
|
StreamWrap::Initialize(target); |
|
|
|
|
|
|
@ -91,10 +94,10 @@ class TCPWrap : public StreamWrap { |
|
|
|
port_symbol = NODE_PSYMBOL("port"); |
|
|
|
|
|
|
|
target->Set(String::NewSymbol("TCP"), tcpConstructor); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
static Handle<Value> New(const Arguments& args) { |
|
|
|
|
|
|
|
Handle<Value> TCPWrap::New(const Arguments& args) { |
|
|
|
// This constructor should not be exposed to public javascript.
|
|
|
|
// Therefore we assert that we are not trying to call this as a
|
|
|
|
// normal function.
|
|
|
@ -105,21 +108,24 @@ class TCPWrap : public StreamWrap { |
|
|
|
assert(wrap); |
|
|
|
|
|
|
|
return scope.Close(args.This()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TCPWrap(Handle<Object> object) : StreamWrap(object, |
|
|
|
(uv_stream_t*) &handle_) { |
|
|
|
TCPWrap::TCPWrap(Handle<Object> object) |
|
|
|
: StreamWrap(object, (uv_stream_t*) &handle_) { |
|
|
|
int r = uv_tcp_init(uv_default_loop(), &handle_); |
|
|
|
assert(r == 0); // How do we proxy this error up to javascript?
|
|
|
|
// Suggestion: uv_tcp_init() returns void.
|
|
|
|
UpdateWriteQueueSize(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
~TCPWrap() { |
|
|
|
TCPWrap::~TCPWrap() { |
|
|
|
assert(object_.IsEmpty()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static Handle<Value> GetSockName(const Arguments& args) { |
|
|
|
Handle<Value> TCPWrap::GetSockName(const Arguments& args) { |
|
|
|
HandleScope scope; |
|
|
|
struct sockaddr_storage address; |
|
|
|
int family; |
|
|
@ -154,10 +160,10 @@ class TCPWrap : public StreamWrap { |
|
|
|
} |
|
|
|
|
|
|
|
return scope.Close(sockname); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static Handle<Value> GetPeerName(const Arguments& args) { |
|
|
|
Handle<Value> TCPWrap::GetPeerName(const Arguments& args) { |
|
|
|
HandleScope scope; |
|
|
|
struct sockaddr_storage address; |
|
|
|
int family; |
|
|
@ -192,10 +198,10 @@ class TCPWrap : public StreamWrap { |
|
|
|
} |
|
|
|
|
|
|
|
return scope.Close(sockname); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static Handle<Value> Bind(const Arguments& args) { |
|
|
|
Handle<Value> TCPWrap::Bind(const Arguments& args) { |
|
|
|
HandleScope scope; |
|
|
|
|
|
|
|
UNWRAP |
|
|
@ -210,9 +216,10 @@ class TCPWrap : public StreamWrap { |
|
|
|
if (r) SetErrno(uv_last_error(uv_default_loop()).code); |
|
|
|
|
|
|
|
return scope.Close(Integer::New(r)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static Handle<Value> Bind6(const Arguments& args) { |
|
|
|
|
|
|
|
Handle<Value> TCPWrap::Bind6(const Arguments& args) { |
|
|
|
HandleScope scope; |
|
|
|
|
|
|
|
UNWRAP |
|
|
@ -227,9 +234,10 @@ class TCPWrap : public StreamWrap { |
|
|
|
if (r) SetErrno(uv_last_error(uv_default_loop()).code); |
|
|
|
|
|
|
|
return scope.Close(Integer::New(r)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static Handle<Value> Listen(const Arguments& args) { |
|
|
|
|
|
|
|
Handle<Value> TCPWrap::Listen(const Arguments& args) { |
|
|
|
HandleScope scope; |
|
|
|
|
|
|
|
UNWRAP |
|
|
@ -242,9 +250,10 @@ class TCPWrap : public StreamWrap { |
|
|
|
if (r) SetErrno(uv_last_error(uv_default_loop()).code); |
|
|
|
|
|
|
|
return scope.Close(Integer::New(r)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static void OnConnection(uv_stream_t* handle, int status) { |
|
|
|
|
|
|
|
void TCPWrap::OnConnection(uv_stream_t* handle, int status) { |
|
|
|
HandleScope scope; |
|
|
|
|
|
|
|
TCPWrap* wrap = static_cast<TCPWrap*>(handle->data); |
|
|
@ -258,7 +267,7 @@ class TCPWrap : public StreamWrap { |
|
|
|
|
|
|
|
if (status == 0) { |
|
|
|
// Instantiate the client javascript object and handle.
|
|
|
|
Local<Object> client_obj = tcpConstructor->NewInstance(); |
|
|
|
Local<Object> client_obj = Instantiate(); |
|
|
|
|
|
|
|
// Unwrap the client javascript object.
|
|
|
|
assert(client_obj->InternalFieldCount() > 0); |
|
|
@ -278,9 +287,10 @@ class TCPWrap : public StreamWrap { |
|
|
|
} |
|
|
|
|
|
|
|
MakeCallback(wrap->object_, "onconnection", 1, argv); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void AfterConnect(uv_connect_t* req, int status) { |
|
|
|
void TCPWrap::AfterConnect(uv_connect_t* req, int status) { |
|
|
|
ConnectWrap* req_wrap = (ConnectWrap*) req->data; |
|
|
|
TCPWrap* wrap = (TCPWrap*) req->handle->data; |
|
|
|
|
|
|
@ -303,9 +313,10 @@ class TCPWrap : public StreamWrap { |
|
|
|
MakeCallback(req_wrap->object_, "oncomplete", 3, argv); |
|
|
|
|
|
|
|
delete req_wrap; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static Handle<Value> Connect(const Arguments& args) { |
|
|
|
Handle<Value> TCPWrap::Connect(const Arguments& args) { |
|
|
|
HandleScope scope; |
|
|
|
|
|
|
|
UNWRAP |
|
|
@ -332,9 +343,10 @@ class TCPWrap : public StreamWrap { |
|
|
|
} else { |
|
|
|
return scope.Close(req_wrap->object_); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static Handle<Value> Connect6(const Arguments& args) { |
|
|
|
Handle<Value> TCPWrap::Connect6(const Arguments& args) { |
|
|
|
HandleScope scope; |
|
|
|
|
|
|
|
UNWRAP |
|
|
@ -358,11 +370,7 @@ class TCPWrap : public StreamWrap { |
|
|
|
} else { |
|
|
|
return scope.Close(req_wrap->object_); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uv_tcp_t handle_; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} // namespace node
|
|
|
|