Mercurial > spidermonkey-playground
changeset 47:e46bf8ff5dc5
implemented simple send().
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Wed, 24 Jun 2009 15:32:58 -0700 |
parents | 48dfabed15be |
children | aabc1dd92639 |
files | server_socket.cpp tcb.js |
diffstat | 2 files changed, 48 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- 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 };
--- 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); +}