From 1afe6d26dbcf76de15df7e2c8fc3aadbbb8b117d Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 2 Mar 2009 15:49:24 +0100 Subject: [PATCH] begin work on the TCP.connect interface --- Makefile | 8 ++- README | 18 +++++- example.js | 40 +++++++------ server.cc | 35 ++++++----- tcp.cc | 184 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tcp.h | 11 ++++ tcp_example.js | 9 +++ 7 files changed, 268 insertions(+), 37 deletions(-) create mode 100644 tcp.cc create mode 100644 tcp.h create mode 100644 tcp_example.js diff --git a/Makefile b/Makefile index 8396309..6d7086c 100644 --- a/Makefile +++ b/Makefile @@ -10,11 +10,14 @@ ifdef EVDIR LDFLAGS += -L$(EVDIR)/lib endif -server: server.o oi_socket.o ebb_request_parser.o oi_buf.o +server: server.o tcp.o oi_socket.o oi_async.o ebb_request_parser.o oi_buf.o g++ -o server $^ $(LDFLAGS) $(V8LIB) server.o: server.cc g++ $(CFLAGS) -c $< + +tcp.o: tcp.cc + g++ $(CFLAGS) -c $< ebb_request_parser.o: ebb_request_parser.c deps/ebb/ebb_request_parser.h g++ $(CFLAGS) -c $< @@ -25,6 +28,9 @@ ebb_request_parser.c: deps/ebb/ebb_request_parser.rl oi_socket.o: deps/oi/oi_socket.c deps/oi/oi_socket.h gcc $(CFLAGS) -c $< +oi_async.o: deps/oi/oi_async.c deps/oi/oi_async.h + gcc $(CFLAGS) -c $< + oi_buf.o: deps/oi/oi_buf.c deps/oi/oi_buf.h gcc $(CFLAGS) -c $< diff --git a/README b/README index b177f94..58c89a8 100644 --- a/README +++ b/README @@ -1,4 +1,16 @@ +WHEREAS, The usage of threads has complicated computer programming; and -git submodule init -git submodule update -make +WHEREAS, V8 javascript comes free of I/O and threads; and + +WHEREAS, Most operating systems do not provide asynchonous file system +access. + +Now, therefore: + +This set server and client libraries were made to build simple but fast +servers. They are provided free of charge under a permissive simple license. + +Submitted by +Ryah Dahl, Programmer +Tim Becker, Programmer +March 1, 2009 diff --git a/example.js b/example.js index 4befa8e..515c598 100644 --- a/example.js +++ b/example.js @@ -1,22 +1,26 @@ - function encode(data) { - var chunk = data.toString(); - return chunk.length.toString(16) + "\r\n" + chunk + "\r\n"; - } +function encode(data) { + var chunk = data.toString(); + return chunk.length.toString(16) + "\r\n" + chunk + "\r\n"; +} + +function Process(request) { + + log( "path: " + request.path ); + log( "query string: " + request.query_string ); - function Process(request) { - // onBody sends null on the last chunk. - request.onBody = function (chunk) { - if(chunk) { - this.respond(encode(chunk)); - } else { - this.respond(encode("\n")); - this.respond("0\r\n\r\n"); - this.respond(null); // signals end-of-request - } + // onBody sends null on the last chunk. + request.onBody = function (chunk) { + if(chunk) { + this.respond(encode(chunk)); + } else { + this.respond(encode("\n")); + this.respond("0\r\n\r\n"); + this.respond(null); // signals end-of-request } - request.respond("HTTP/1.0 200 OK\r\n"); - request.respond("Content-Type: text-plain\r\n"); - request.respond("Transfer-Encoding: chunked\r\n"); - request.respond("\r\n"); } + request.respond("HTTP/1.0 200 OK\r\n"); + request.respond("Content-Type: text/plain\r\n"); + request.respond("Transfer-Encoding: chunked\r\n"); + request.respond("\r\n"); +} diff --git a/server.cc b/server.cc index ef45738..def6287 100644 --- a/server.cc +++ b/server.cc @@ -1,6 +1,8 @@ #include #include +#include "tcp.h" + #include #include #include @@ -17,7 +19,7 @@ using namespace std; static oi_server server; static struct ev_loop *loop; -static Persistent context_; +static Persistent context; static Persistent process_; static Persistent request_template_; @@ -320,7 +322,7 @@ static void on_headers_complete // Enter this processor's context so all the remaining operations // take place there - Context::Scope context_scope(context_); + Context::Scope context_scope(context); // Set up an exception handler before calling the Process function TryCatch try_catch; @@ -329,7 +331,7 @@ static void on_headers_complete // and one argument, the request. const int argc = 1; Handle argv[argc] = { request->js_object }; - Handle r = process_->Call(context_->Global(), argc, argv); + Handle r = process_->Call(context->Global(), argc, argv); if (r.IsEmpty()) { String::Utf8Value error(try_catch.Exception()); printf("error: %s\n", *error); @@ -495,9 +497,13 @@ static bool compile // Compile the script and check for errors. Handle