view server_socket.cpp @ 43:cdfbd4b71027

Added listen() method to ServerSocket().
author Atul Varma <varmaa@toolness.com>
date Wed, 24 Jun 2009 14:12:59 -0700
parents 68d2a675a33a
children 1fd58132b533
line wrap: on
line source

/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Ubiquity.
 *
 * The Initial Developer of the Original Code is Mozilla.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Atul Varma <atul@mozilla.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "server_socket.h"

#include "nspr.h"

static JSBool getSocket(JSContext *cx, JSObject *obj, PRFileDesc **fd)
{
  *fd = (PRFileDesc *) JS_GetInstancePrivate(
    cx,
    obj,
    &sServerSocket_JSClass.base,
    NULL
    );
  if (*fd == NULL) {
    JS_ReportError(cx, "ServerSocket method called on "
                   "non-ServerSocket object.");
    return JS_FALSE;
  }
  return JS_TRUE;
}

static void finalize(JSContext *cx, JSObject *obj)
{
  PRFileDesc *fd = (PRFileDesc *) JS_GetPrivate(cx, obj);
  // Just in case, ensure we don't have a dangling pointer.
  JS_SetPrivate(cx, obj, NULL);
  PR_Close(fd);
}

JSExtendedClass sServerSocket_JSClass = {
  // JSClass (JSExtendedClass.base) initialization
  { "FlexibleWrapper",
    JSCLASS_IS_EXTENDED | JSCLASS_HAS_PRIVATE |
    JSCLASS_HAS_RESERVED_SLOTS(0),
    JS_PropertyStub,          JS_PropertyStub,
    JS_PropertyStub,          JS_PropertyStub,
    JS_EnumerateStub,         JS_ResolveStub,
    JS_ConvertStub,           finalize,
    NULL,                     NULL,
    NULL,                     NULL,
    NULL,                     NULL,
    NULL,                     NULL
  },
  // JSExtendedClass initialization
  NULL, // equality
  NULL, // outerObject
  NULL, // innerObject
  NULL, // iterator
  NULL, // wrapper
  JSCLASS_NO_RESERVED_MEMBERS
};

JSBool listen(JSContext *cx, JSObject *obj, uintN argc,
              jsval *argv, jsval *rval)
{
  PRInt32 backlog;

  if (!JS_ConvertArguments(cx, argc, argv, "i", &backlog))
    return JS_FALSE;

  PRFileDesc *fd;
  if (!getSocket(cx, obj, &fd))
    return JS_FALSE;

  PRStatus result = PR_Listen(fd, backlog);

  if (result != PR_SUCCESS) {
    JS_ReportError(cx, "Listen failed.");
    return JS_FALSE;
  }

  *rval = JSVAL_VOID;
  return JS_TRUE;
}

JSBool bind(JSContext *cx, JSObject *obj, uintN argc,
            jsval *argv, jsval *rval)
{
  const char *addrStr;
  PRUint16 port;

  if (!JS_ConvertArguments(cx, argc, argv, "sc", &addrStr, &port))
    return JS_FALSE;

  PRNetAddr addr;
  PRStatus result = PR_StringToNetAddr(addrStr, &addr);

  if (result != PR_SUCCESS) {
    JS_ReportError(cx, "Invalid address.");
    return JS_FALSE;
  }

  PRFileDesc *fd;
  if (!getSocket(cx, obj, &fd))
    return JS_FALSE;

  addr.inet.port = port;
  result = PR_Bind(fd, &addr);

  if (result != PR_SUCCESS) {
    JS_ReportError(cx, "Bind failed.");
    return JS_FALSE;
  }

  *rval = JSVAL_VOID;
  return JS_TRUE;
}

static JSFunctionSpec methods[] = {
  JS_FS("bind",          bind,        2, 0, 0),
  JS_FS("listen",        listen,      1, 0, 0),
  JS_FS_END
};

JSBool createServerSocket(JSContext *cx, JSObject *obj, uintN argc,
                          jsval *argv, jsval *rval)
{
  JSObject *object = JS_NewObject(
    cx,
    &sServerSocket_JSClass.base,
    NULL,
    JS_GetScopeChain(cx)
    );

  if (!JS_DefineFunctions(cx, object, methods))
    return JS_FALSE;

  PRFileDesc *fd = PR_OpenTCPSocket(PR_AF_INET);
  if (fd == NULL) {
    JS_ReportError(cx, "Creation of TCP socket failed.");
    return JS_FALSE;
  }

  if (!JS_SetPrivate(cx, object, fd)) {
    PR_Close(fd);
    return JS_FALSE;
  }

  *rval = OBJECT_TO_JSVAL(object);
  return JS_TRUE;
}