|
| 1 | +#include "tcp.h" |
| 2 | + |
| 3 | +#include <oi_socket.h> |
| 4 | +#include <oi_async.h> |
| 5 | + |
| 6 | +#include <sys/types.h> |
| 7 | +#include <sys/socket.h> |
| 8 | +#include <netdb.h> |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +/* |
| 13 | + Target API |
| 14 | +
|
| 15 | + TCP.connect({ |
| 16 | +
|
| 17 | + host: "google.com", |
| 18 | +
|
| 19 | + port: 80, |
| 20 | +
|
| 21 | + connect: function () { |
| 22 | + this.write("GET /search?q=hello HTTP/1.0\r\n\r\n"); |
| 23 | + }, |
| 24 | +
|
| 25 | + read: function (data) { |
| 26 | +
|
| 27 | + request.respond("<table> <td>" + data + "</td> </table>"); |
| 28 | +
|
| 29 | + }, |
| 30 | +
|
| 31 | + drain: function () { |
| 32 | + }, |
| 33 | +
|
| 34 | + error: function () { |
| 35 | + } |
| 36 | + }); |
| 37 | +
|
| 38 | +*/ |
| 39 | + |
| 40 | +static oi_async thread_pool; |
| 41 | + |
| 42 | +static struct addrinfo tcp_hints = |
| 43 | +/* ai_flags */ { AI_PASSIVE |
| 44 | +/* ai_family */ , AF_UNSPEC |
| 45 | +/* ai_socktype */ , SOCK_STREAM |
| 46 | +/* ai_protocol */ , 0 |
| 47 | +/* ai_addrlen */ , 0 |
| 48 | +/* ai_addr */ , 0 |
| 49 | +/* ai_canonname */ , 0 |
| 50 | +/* ai_next */ , 0 |
| 51 | + }; |
| 52 | + |
| 53 | +static struct ev_loop *loop; |
| 54 | + |
| 55 | +class TCPClient { |
| 56 | +public: |
| 57 | + oi_task resolve_task; |
| 58 | + oi_socket socket; |
| 59 | + struct addrinfo *address; |
| 60 | + Persistent<Object> options; |
| 61 | +}; |
| 62 | + |
| 63 | +static void on_connect |
| 64 | + ( oi_socket *socket |
| 65 | + ) |
| 66 | +{ |
| 67 | + TCPClient *client = static_cast<TCPClient*> (socket->data); |
| 68 | + |
| 69 | + HandleScope scope; |
| 70 | + |
| 71 | + Handle<Value> connect_value = client->options->Get( String::NewSymbol("connect") ); |
| 72 | + if (!connect_value->IsFunction()) |
| 73 | + return; // error! |
| 74 | + Handle<Function> connect_cb = Handle<Function>::Cast(connect_value); |
| 75 | + |
| 76 | + TryCatch try_catch; |
| 77 | + Handle<Value> r = connect_cb->Call(client->options, 0, NULL); |
| 78 | + if (r.IsEmpty()) { |
| 79 | + String::Utf8Value error(try_catch.Exception()); |
| 80 | + printf("connect error: %s\n", *error); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +static void resolve_done |
| 85 | + ( oi_task *resolve_task |
| 86 | + , int result |
| 87 | + ) |
| 88 | +{ |
| 89 | + TCPClient *client = static_cast<TCPClient*> (resolve_task->data); |
| 90 | + |
| 91 | + if(result != 0) { |
| 92 | + printf("error. TODO make call options error callback\n"); |
| 93 | + client->options.Dispose(); |
| 94 | + delete client; |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // Got the address succesfully. Let's connect now. |
| 99 | + |
| 100 | + oi_socket_init(&client->socket, 30.0); // TODO adjustable timeout |
| 101 | + |
| 102 | + client->socket.on_connect = on_connect; |
| 103 | + client->socket.on_read = NULL; |
| 104 | + client->socket.on_drain = NULL; |
| 105 | + client->socket.on_error = NULL; |
| 106 | + client->socket.on_close = NULL; |
| 107 | + client->socket.on_timeout = NULL; |
| 108 | + client->socket.data = client; |
| 109 | + |
| 110 | + oi_socket_connect (&client->socket, client->address); |
| 111 | + oi_socket_attach (&client->socket, loop); |
| 112 | + |
| 113 | + |
| 114 | + freeaddrinfo(client->address); |
| 115 | + client->address = NULL; |
| 116 | +} |
| 117 | + |
| 118 | +static Handle<Value> Connect |
| 119 | + ( const Arguments& args |
| 120 | + ) |
| 121 | +{ |
| 122 | + if (args.Length() < 1) |
| 123 | + return Undefined(); |
| 124 | + |
| 125 | + HandleScope scope; |
| 126 | + |
| 127 | + Handle<Value> arg = args[0]; |
| 128 | + Handle<Object> options = arg->ToObject(); |
| 129 | + |
| 130 | + /* Make sure the user has provided at least host and port */ |
| 131 | + |
| 132 | + Handle<Value> host_value = options->Get( String::NewSymbol("host") ); |
| 133 | + |
| 134 | + if(host_value->IsUndefined()) |
| 135 | + return False(); |
| 136 | + |
| 137 | + Handle<Value> port_value = options->Get( String::NewSymbol("port") ); |
| 138 | + |
| 139 | + if(port_value->IsUndefined()) |
| 140 | + return False(); |
| 141 | + |
| 142 | + Handle<String> host = host_value->ToString(); |
| 143 | + Handle<String> port = port_value->ToString(); |
| 144 | + |
| 145 | + char host_s[host->Length()+1]; // + 1 for \0 |
| 146 | + char port_s[port->Length()+1]; |
| 147 | + |
| 148 | + host->WriteAscii(host_s, 0, host->Length()); |
| 149 | + port->WriteAscii(port_s, 0, port->Length()); |
| 150 | + |
| 151 | + printf("resolving host: %s, port: %s\n", host_s, port_s); |
| 152 | + |
| 153 | + TCPClient *client = new TCPClient; |
| 154 | + |
| 155 | + oi_task_init_getaddrinfo ( &client->resolve_task |
| 156 | + , resolve_done |
| 157 | + , host_s |
| 158 | + , port_s |
| 159 | + , &tcp_hints |
| 160 | + , &client->address |
| 161 | + ); |
| 162 | + client->options = Persistent<Object>::New(options); |
| 163 | + |
| 164 | + oi_async_submit (&thread_pool, &client->resolve_task); |
| 165 | +} |
| 166 | + |
| 167 | +Handle<Object> tcp_initialize |
| 168 | + ( struct ev_loop *_loop |
| 169 | + ) |
| 170 | +{ |
| 171 | + loop = _loop; |
| 172 | + |
| 173 | + oi_async_init(&thread_pool); |
| 174 | + oi_async_attach(loop, &thread_pool); |
| 175 | + |
| 176 | + HandleScope scope; |
| 177 | + |
| 178 | + Local<Object> t = Object::New(); |
| 179 | + |
| 180 | + t->Set(String::New("connect"), FunctionTemplate::New(Connect)->GetFunction()); |
| 181 | + |
| 182 | + return scope.Close(t); |
| 183 | +} |
| 184 | + |
0 commit comments