diff server_socket.cpp @ 47:e46bf8ff5dc5

implemented simple send().
author Atul Varma <varmaa@toolness.com>
date Wed, 24 Jun 2009 15:32:58 -0700
parents 48dfabed15be
children aabc1dd92639
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
 };