|
@ -2,7 +2,7 @@ |
|
|
#include "node.h" |
|
|
#include "node.h" |
|
|
|
|
|
|
|
|
#include <oi_socket.h> |
|
|
#include <oi_socket.h> |
|
|
#include <oi_async.h> |
|
|
#include <oi_buf.h> |
|
|
|
|
|
|
|
|
#include <sys/types.h> |
|
|
#include <sys/types.h> |
|
|
#include <sys/socket.h> |
|
|
#include <sys/socket.h> |
|
@ -10,36 +10,42 @@ |
|
|
|
|
|
|
|
|
using namespace v8; |
|
|
using namespace v8; |
|
|
|
|
|
|
|
|
|
|
|
static Persistent<String> readyState_str; |
|
|
|
|
|
|
|
|
/*
|
|
|
static Persistent<Integer> readyState_CONNECTING; |
|
|
Target API |
|
|
static Persistent<Integer> readyState_OPEN; |
|
|
|
|
|
static Persistent<Integer> readyState_CLOSED; |
|
|
|
|
|
|
|
|
TCP.connect({ |
|
|
enum readyState { READY_STATE_CONNECTING = 0 |
|
|
|
|
|
, READY_STATE_OPEN = 1 |
|
|
host: "google.com", |
|
|
, READY_STATE_CLOSED = 2 |
|
|
|
|
|
} ; |
|
|
port: 80, |
|
|
|
|
|
|
|
|
|
|
|
connect: function () { |
|
|
class TCPClient { |
|
|
this.write("GET /search?q=hello HTTP/1.0\r\n\r\n"); |
|
|
public: |
|
|
}, |
|
|
TCPClient(Handle<Object> obj); |
|
|
|
|
|
~TCPClient(); |
|
|
|
|
|
|
|
|
read: function (data) { |
|
|
int Connect(char *host, char *port); |
|
|
|
|
|
void Write (Handle<Value> arg); |
|
|
|
|
|
void Disconnect(); |
|
|
|
|
|
|
|
|
request.respond("<table> <td>" + data + "</td> </table>"); |
|
|
void OnOpen(); |
|
|
|
|
|
|
|
|
}, |
|
|
private: |
|
|
|
|
|
oi_socket socket; |
|
|
|
|
|
struct addrinfo *address; |
|
|
|
|
|
Persistent<Object> js_client; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
drain: function () { |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
error: function () { |
|
|
static void on_connect |
|
|
|
|
|
( oi_socket *socket |
|
|
|
|
|
) |
|
|
|
|
|
{ |
|
|
|
|
|
TCPClient *client = static_cast<TCPClient*> (socket->data); |
|
|
|
|
|
client->OnOpen(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
static oi_async thread_pool; |
|
|
|
|
|
|
|
|
|
|
|
static struct addrinfo tcp_hints = |
|
|
static struct addrinfo tcp_hints = |
|
|
/* ai_flags */ { AI_PASSIVE |
|
|
/* ai_flags */ { AI_PASSIVE |
|
@ -52,130 +58,203 @@ static struct addrinfo tcp_hints = |
|
|
/* ai_next */ , 0 |
|
|
/* ai_next */ , 0 |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
class TCPClient { |
|
|
static Handle<Value> newTCPClient |
|
|
public: |
|
|
( const Arguments& args |
|
|
oi_task resolve_task; |
|
|
|
|
|
oi_socket socket; |
|
|
|
|
|
struct addrinfo *address; |
|
|
|
|
|
Persistent<Object> options; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static void on_connect |
|
|
|
|
|
( oi_socket *socket |
|
|
|
|
|
) |
|
|
) |
|
|
{ |
|
|
{ |
|
|
TCPClient *client = static_cast<TCPClient*> (socket->data); |
|
|
if (args.Length() < 1) |
|
|
|
|
|
return Undefined(); |
|
|
|
|
|
|
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
|
|
|
|
|
|
Handle<Value> connect_value = client->options->Get( String::NewSymbol("connect") ); |
|
|
String::AsciiValue host(args[0]); |
|
|
if (!connect_value->IsFunction()) |
|
|
String::AsciiValue port(args[1]); |
|
|
return; // error!
|
|
|
|
|
|
Handle<Function> connect_cb = Handle<Function>::Cast(connect_value); |
|
|
|
|
|
|
|
|
|
|
|
TryCatch try_catch; |
|
|
TCPClient *client = new TCPClient(args.This()); |
|
|
Handle<Value> r = connect_cb->Call(client->options, 0, NULL); |
|
|
if(client == NULL) |
|
|
if (r.IsEmpty()) { |
|
|
return Undefined(); // XXX raise error?
|
|
|
String::Utf8Value error(try_catch.Exception()); |
|
|
|
|
|
printf("connect error: %s\n", *error); |
|
|
int r = client->Connect(*host, *port); |
|
|
|
|
|
if (r != 0) |
|
|
|
|
|
return Undefined(); // XXX raise error?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return args.This(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static TCPClient* |
|
|
|
|
|
UnwrapClient (Handle<Object> obj) |
|
|
|
|
|
{ |
|
|
|
|
|
HandleScope scope; |
|
|
|
|
|
Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0)); |
|
|
|
|
|
TCPClient* client = static_cast<TCPClient*>(field->Value()); |
|
|
|
|
|
return client; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static void resolve_done |
|
|
static Handle<Value> |
|
|
( oi_task *resolve_task |
|
|
WriteCallback (const Arguments& args) |
|
|
, int result |
|
|
|
|
|
) |
|
|
|
|
|
{ |
|
|
{ |
|
|
TCPClient *client = static_cast<TCPClient*> (resolve_task->data); |
|
|
HandleScope scope; |
|
|
|
|
|
TCPClient *client = UnwrapClient(args.Holder()); |
|
|
|
|
|
client->Write(args[0]); |
|
|
|
|
|
return Undefined(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
printf("hello world\n"); |
|
|
static Handle<Value> |
|
|
|
|
|
DisconnectCallback (const Arguments& args) |
|
|
|
|
|
{ |
|
|
|
|
|
HandleScope scope; |
|
|
|
|
|
TCPClient *client = UnwrapClient(args.Holder()); |
|
|
|
|
|
client->Disconnect(); |
|
|
|
|
|
return Undefined(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if(result != 0) { |
|
|
static void |
|
|
printf("error. TODO make call options error callback\n"); |
|
|
client_destroy (Persistent<Value> _, void *data) |
|
|
client->options.Dispose(); |
|
|
{ |
|
|
|
|
|
TCPClient *client = static_cast<TCPClient *> (data); |
|
|
delete client; |
|
|
delete client; |
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
printf("Got the address succesfully. Let's connect now. \n"); |
|
|
TCPClient::TCPClient(Handle<Object> _js_client) |
|
|
|
|
|
{ |
|
|
oi_socket_init(&client->socket, 30.0); // TODO adjustable timeout
|
|
|
oi_socket_init(&socket, 30.0); // TODO adjustable timeout
|
|
|
|
|
|
socket.on_connect = on_connect; |
|
|
client->socket.on_connect = on_connect; |
|
|
socket.on_read = NULL; |
|
|
client->socket.on_read = NULL; |
|
|
socket.on_drain = NULL; |
|
|
client->socket.on_drain = NULL; |
|
|
socket.on_error = NULL; |
|
|
client->socket.on_error = NULL; |
|
|
socket.on_close = NULL; |
|
|
client->socket.on_close = NULL; |
|
|
socket.on_timeout = NULL; |
|
|
client->socket.on_timeout = NULL; |
|
|
socket.data = this; |
|
|
client->socket.data = client; |
|
|
|
|
|
|
|
|
|
|
|
oi_socket_connect (&client->socket, client->address); |
|
|
HandleScope scope; |
|
|
oi_socket_attach (&client->socket, node_loop()); |
|
|
js_client = Persistent<Object>::New(_js_client); |
|
|
|
|
|
js_client->SetInternalField (0, External::New(this)); |
|
|
|
|
|
js_client.MakeWeak (this, client_destroy); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
freeaddrinfo(client->address); |
|
|
TCPClient::~TCPClient () |
|
|
client->address = NULL; |
|
|
{ |
|
|
|
|
|
Disconnect(); |
|
|
|
|
|
oi_socket_detach (&socket); |
|
|
|
|
|
js_client.Dispose(); |
|
|
|
|
|
js_client.Clear(); // necessary?
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static Handle<Value> Connect |
|
|
int |
|
|
( const Arguments& args |
|
|
TCPClient::Connect(char *host, char *port) |
|
|
) |
|
|
|
|
|
{ |
|
|
{ |
|
|
if (args.Length() < 1) |
|
|
int r; |
|
|
return Undefined(); |
|
|
|
|
|
|
|
|
|
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
|
|
|
|
|
|
Handle<Value> arg = args[0]; |
|
|
js_client->Set(readyState_str, readyState_CONNECTING); |
|
|
Handle<Object> options = arg->ToObject(); |
|
|
|
|
|
|
|
|
|
|
|
/* Make sure the user has provided at least host and port */ |
|
|
/* FIXME Blocking DNS resolution. Use oi_async. */ |
|
|
|
|
|
printf("resolving host: %s, port: %s\n", host, port); |
|
|
|
|
|
r = getaddrinfo (host, port, &tcp_hints, &address); |
|
|
|
|
|
if(r != 0) { |
|
|
|
|
|
perror("getaddrinfo"); |
|
|
|
|
|
return r; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Handle<Value> host_value = options->Get( String::NewSymbol("host") ); |
|
|
r = oi_socket_connect (&socket, address); |
|
|
|
|
|
if(r != 0) { |
|
|
|
|
|
perror("oi_socket_connect"); |
|
|
|
|
|
return r; |
|
|
|
|
|
} |
|
|
|
|
|
oi_socket_attach (&socket, node_loop()); |
|
|
|
|
|
|
|
|
if(host_value->IsUndefined()) |
|
|
freeaddrinfo(address); |
|
|
return False(); |
|
|
address = NULL; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Handle<Value> port_value = options->Get( String::NewSymbol("port") ); |
|
|
void TCPClient::Write (Handle<Value> arg) |
|
|
|
|
|
{ |
|
|
|
|
|
HandleScope scope; |
|
|
|
|
|
|
|
|
if(port_value->IsUndefined()) |
|
|
if(arg == Null()) { |
|
|
return False(); |
|
|
|
|
|
|
|
|
|
|
|
Handle<String> host = host_value->ToString(); |
|
|
oi_socket_write_eof(&socket); |
|
|
Handle<String> port = port_value->ToString(); |
|
|
|
|
|
|
|
|
|
|
|
char host_s[host->Length()+1]; // + 1 for \0
|
|
|
} else { |
|
|
char port_s[port->Length()+1]; |
|
|
Local<String> s = arg->ToString(); |
|
|
|
|
|
|
|
|
host->WriteAscii(host_s); |
|
|
oi_buf *buf = oi_buf_new2(s->Length()); |
|
|
port->WriteAscii(port_s); |
|
|
s->WriteAscii(buf->base, 0, s->Length()); |
|
|
|
|
|
|
|
|
printf("resolving host: %s, port: %s\n", host_s, port_s); |
|
|
oi_socket_write(&socket, buf); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
TCPClient *client = new TCPClient; |
|
|
void |
|
|
|
|
|
TCPClient::Disconnect() |
|
|
|
|
|
{ |
|
|
|
|
|
oi_socket_close(&socket); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
oi_task_init_getaddrinfo ( &client->resolve_task |
|
|
void TCPClient::OnOpen() |
|
|
, resolve_done |
|
|
{ |
|
|
, host_s |
|
|
HandleScope scope; |
|
|
, port_s |
|
|
|
|
|
, &tcp_hints |
|
|
js_client->Set(readyState_str, readyState_OPEN); |
|
|
, &client->address |
|
|
|
|
|
); |
|
|
|
|
|
client->options = Persistent<Object>::New(options); |
|
|
|
|
|
|
|
|
|
|
|
oi_async_submit (&thread_pool, &client->resolve_task); |
|
|
Handle<Value> onopen_value = js_client->Get( String::NewSymbol("onopen") ); |
|
|
|
|
|
if (!onopen_value->IsFunction()) |
|
|
|
|
|
return; |
|
|
|
|
|
Handle<Function> onopen = Handle<Function>::Cast(onopen_value); |
|
|
|
|
|
|
|
|
|
|
|
TryCatch try_catch; |
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> r = onopen->Call(js_client, 0, NULL); |
|
|
|
|
|
|
|
|
|
|
|
if(try_catch.HasCaught()) |
|
|
|
|
|
node_fatal_exception(try_catch); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
Init_tcp (Handle<Object> target) |
|
|
Init_tcp (Handle<Object> target) |
|
|
{ |
|
|
{ |
|
|
oi_async_init(&thread_pool); |
|
|
|
|
|
oi_async_attach(node_loop(), &thread_pool); |
|
|
|
|
|
|
|
|
|
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
|
|
|
readyState_str = Persistent<String>::New(String::NewSymbol("readyState")); |
|
|
|
|
|
|
|
|
|
|
|
Local<FunctionTemplate> client_t = FunctionTemplate::New(newTCPClient); |
|
|
|
|
|
|
|
|
|
|
|
client_t->InstanceTemplate()->SetInternalFieldCount(1); |
|
|
|
|
|
|
|
|
|
|
|
/* readyState constants */ |
|
|
|
|
|
|
|
|
|
|
|
readyState_CONNECTING = Persistent<Integer>::New(Integer::New(READY_STATE_CONNECTING)); |
|
|
|
|
|
client_t->InstanceTemplate()->Set ( String::NewSymbol("CONNECTING") |
|
|
|
|
|
, readyState_CONNECTING |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
Local<Object> tcp = Object::New(); |
|
|
readyState_OPEN = Persistent<Integer>::New(Integer::New(READY_STATE_OPEN)); |
|
|
target->Set(String::NewSymbol("TCP"), tcp); |
|
|
client_t->InstanceTemplate()->Set ( String::NewSymbol("OPEN") |
|
|
|
|
|
, readyState_OPEN |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
readyState_CLOSED = Persistent<Integer>::New(Integer::New(READY_STATE_CLOSED)); |
|
|
|
|
|
client_t->InstanceTemplate()->Set ( String::NewSymbol("CLOSED") |
|
|
|
|
|
, readyState_CLOSED |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
/* write callback */ |
|
|
|
|
|
|
|
|
|
|
|
Local<FunctionTemplate> write_t = FunctionTemplate::New(WriteCallback); |
|
|
|
|
|
|
|
|
|
|
|
client_t->InstanceTemplate()->Set ( String::NewSymbol("write") |
|
|
|
|
|
, write_t->GetFunction() |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
/* disconnect callback */ |
|
|
|
|
|
|
|
|
|
|
|
Local<FunctionTemplate> disconnect_t = FunctionTemplate::New(DisconnectCallback); |
|
|
|
|
|
|
|
|
|
|
|
client_t->InstanceTemplate()->Set ( String::NewSymbol("disconnect") |
|
|
|
|
|
, disconnect_t->GetFunction() |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
tcp->Set(String::NewSymbol("connect"), FunctionTemplate::New(Connect)->GetFunction()); |
|
|
target->Set(String::NewSymbol("TCPClient"), client_t->GetFunction()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|