# HG changeset patch # User Atul Varma # Date 1245882778 25200 # Node ID e46bf8ff5dc5e4f3b488149b049cd9f6565b7852 # Parent 48dfabed15beeaf03606bd79bd2fd2e86a54dba0 implemented simple send(). diff -r 48dfabed15be -r e46bf8ff5dc5 server_socket.cpp --- a/server_socket.cpp Wed Jun 24 14:59:26 2009 -0700 +++ b/server_socket.cpp Wed Jun 24 15:32:58 2009 -0700 @@ -87,6 +87,41 @@ JSCLASS_NO_RESERVED_MEMBERS }; +static JSBool send(JSContext *cx, JSObject *obj, uintN argc, + jsval *argv, jsval *rval) +{ + JSString *data; + + if (!JS_ConvertArguments(cx, argc, argv, "S", &data)) + return JS_FALSE; + + char *dataBytes = JS_GetStringBytes(data); + size_t dataLength = JS_GetStringLength(data); + + if (dataLength > 0) { + PRFileDesc *fd; + if (!getSocket(cx, obj, &fd)) + return JS_FALSE; + + PRInt32 sent = PR_Send(fd, dataBytes, dataLength, 0, + PR_INTERVAL_NO_TIMEOUT); + + if (sent == -1) { + JS_ReportError(cx, "Send failed."); + return JS_FALSE; + } + } + + *rval = JSVAL_VOID; + return JS_TRUE; +} + +static JSBool recv(JSContext *cx, JSObject *obj, uintN argc, + jsval *argv, jsval *rval) +{ + +} + static JSBool listen(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { @@ -175,6 +210,8 @@ JS_FS("bind", bind, 2, 0, 0), JS_FS("listen", listen, 0, 0, 0), JS_FS("accept", accept, 0, 0, 0), + JS_FS("send", send, 1, 0, 0), + JS_FS("recv", recv, 1, 0, 0), JS_FS_END }; diff -r 48dfabed15be -r e46bf8ff5dc5 tcb.js --- a/tcb.js Wed Jun 24 14:59:26 2009 -0700 +++ b/tcb.js Wed Jun 24 15:32:58 2009 -0700 @@ -244,4 +244,14 @@ socket.listen(); var conn = socket.accept(); -print("connection: " + conn); + +if (conn) { + var toSend = "hello there."; + var headerLines = ["HTTP/1.0 200 OK", + "Content-Type: text/plain", + "Content-Length: " + toSend.length]; + var headers = headerLines.join("\r\n"); + conn.send(headers); + conn.send("\r\n"); + conn.send(toSend); +}