Ejemplo n.º 1
0
VJSWebWorkerObject *VJSWebWorkerObject::_CreateWorker (XBOX::VJSContext &inParentContext, VJSWorker *inOutsideWorker,
        bool inReUseContext,
        const XBOX::VString &inUrl, bool inIsDedicated, const XBOX::VString &inName)
{
    xbox_assert(inOutsideWorker != NULL);

    bool		needToRun;
    VJSWorker	*insideWorker;

    if (inIsDedicated) {

        needToRun = true;
        insideWorker = new VJSWorker(inParentContext, inUrl, inReUseContext);

    } else

        insideWorker = VJSWorker::RetainSharedWorker(inParentContext, inUrl, inName, inReUseContext, &needToRun);

    xbox_assert(insideWorker != NULL);

    VJSMessagePort	*onMessagePort, *onErrorPort;

    onMessagePort = VJSMessagePort::Create(inOutsideWorker, insideWorker);
    onMessagePort->SetCallbackName(inOutsideWorker, "onmessage");
    onMessagePort->SetCallbackName(insideWorker, "onmessage");

    onErrorPort = VJSMessagePort::Create(inOutsideWorker, insideWorker);
    onErrorPort->SetCallbackName(inOutsideWorker, "onerror");
    onErrorPort->SetCallbackName(insideWorker, "onerror");

    inOutsideWorker->AddErrorPort(onErrorPort);
    insideWorker->AddErrorPort(onErrorPort);

    if (inIsDedicated)

        insideWorker->SetMessagePorts(onMessagePort, onErrorPort);

    else

        insideWorker->Connect(onMessagePort, onErrorPort);

    xbox_assert(!(inIsDedicated && !needToRun));
    if (needToRun)

        insideWorker->Run();

    XBOX::ReleaseRefCountable<VJSWorker>(&insideWorker);

    VJSWebWorkerObject	*webWorkerObject;

    webWorkerObject = new VJSWebWorkerObject(onMessagePort, onErrorPort);

    XBOX::ReleaseRefCountable<VJSMessagePort>(&onMessagePort);
    XBOX::ReleaseRefCountable<VJSMessagePort>(&onErrorPort);

    return webWorkerObject;
}
Ejemplo n.º 2
0
void VJSDedicatedWorkerClass::_terminate (XBOX::VJSParms_callStaticFunction &ioParms, VJSWebWorkerObject *inDedicatedWorker)
{
    xbox_assert(inDedicatedWorker != NULL);

    VJSWorker	*outsideWorker	= VJSWorker::RetainWorker(ioParms.GetContext());
    VJSWorker	*insideWorker	= inDedicatedWorker->fOnMessagePort->GetOther(outsideWorker);

    insideWorker->Terminate();
    XBOX::ReleaseRefCountable<VJSWorker>(&outsideWorker);
}
Ejemplo n.º 3
0
void VJSDedicatedWorkerClass::_terminate (XBOX::VJSParms_callStaticFunction &ioParms, VJSWebWorkerObject *inDedicatedWorker)
{
	xbox_assert(inDedicatedWorker != NULL);

	VJSWorker	*outsideWorker	= VJSWorker::RetainWorker(ioParms.GetContext());
	VJSWorker	*insideWorker	= inDedicatedWorker->fOnMessagePort->GetOther(outsideWorker);
	
//**

	// ACI0081079: 
	//
	// Defensive programming, check pointer is not NULL. Pending definitive fix (and architecture overall in WAK5).
	// insideWorker should never be NULL !

	if (insideWorker != NULL)

		insideWorker->Terminate();


//**

	XBOX::ReleaseRefCountable<VJSWorker>(&outsideWorker);
}
void VJSNetServerClass::_listen (XBOX::VJSParms_callStaticFunction &ioParms, VJSNetServerObject *inServer)
{
	xbox_assert(inServer != NULL);

	XBOX::VString	address;
	sLONG			port;

	if (!ioParms.CountParams()) {

		XBOX::vThrowError(XBOX::VE_JVSC_EXPECTING_PARAMETER, "net.Server.listen()");
		return;

	}

	if (!ioParms.IsNumberParam(1) || !ioParms.GetLongParam(1, &port)) {
		
		XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_NUMBER, "1");
		return;

	}

	XBOX::VString	hostname;
	XBOX::VJSObject	callback(ioParms.GetContext());
	bool			hasCallback;

	hostname = "";
	hasCallback = false;
	if (ioParms.CountParams() >= 2) {

		if (ioParms.IsStringParam(2)) {
			
			if (!ioParms.GetStringParam(2, hostname)) {

				XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_STRING, "2");
				return;

			}

		} else if (ioParms.IsObjectParam(2) && ioParms.GetParamFunc(2, callback)) {

			hasCallback = true;

		} else {

			XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_FUNCTION, "2");
			return;

		}

	} 

	if (ioParms.CountParams() == 3 ) {
	
		if ((hasCallback || !ioParms.IsObjectParam(3) || !ioParms.GetParamFunc(3, callback))) {

			XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_FUNCTION, "3");
			return;

		} else

			hasCallback = true;

	}

	// We are already listening, stop previous listener.

	if (inServer->fConnectionListener != NULL)

		inServer->Close();

	// Set up listener.
	
	if ((inServer->fConnectionListener = new XBOX::VTCPConnectionListener())== NULL) {

		XBOX::vThrowError(XBOX::VE_MEMORY_FULL);
		return;

	} 

	if (inServer->fIsSSL)

		inServer->fConnectionListener->SetSSLKeyAndCertificate(inServer->fCertificate, inServer->fKey);

	VJSNetSocketObject::sMutex.Lock();

	if (VJSNetSocketObject::sSelectIOPool == NULL)

		VJSNetSocketObject::sSelectIOPool = new XBOX::VTCPSelectIOPool();

	VJSNetSocketObject::sMutex.Unlock();

	inServer->fConnectionListener->AddSelectIOPool(VJSNetSocketObject::sSelectIOPool);

	VJSWorker	*worker;

	worker = VJSWorker::RetainWorker(ioParms.GetContext());

	VJSConnectionHandlerFactory	*connectionHandlerFactory;
	
	if ((connectionHandlerFactory = new VJSConnectionHandlerFactory(worker, inServer, inServer->fIsSSL)) == NULL) {

		XBOX::vThrowError(XBOX::VE_MEMORY_FULL);
		XBOX::ReleaseRefCountable<VJSWorker>(&worker);
		return;

	}

#if WITH_DEPRECATED_IPV4_API
	
	sLONG	resolvedAddress;

	if (hostname.GetLength()) {

		address = hostname;
		resolvedAddress = XBOX::ServerNetTools::ResolveAddress(hostname);

	} else {

		address = "0.0.0.0";

		std::vector<IP4>	localIPs;

		if (XBOX::ServerNetTools::GetLocalIPv4Addresses(localIPs))

			resolvedAddress = localIPs[0];

		else

			resolvedAddress = 0;	// Should be same as above.

	}

	connectionHandlerFactory->SetIP(resolvedAddress);

#else

	XBOX::VNetAddressList	addrList;
	bool				isIPv6;

	if (hostname.GetLength()) {

		XBOX::VNetAddress	addr(hostname, port);

		isIPv6 = addr.IsV6();
		addrList.FromDnsQuery(hostname, port);
		
	} else {

		addrList.FromLocalInterfaces();
		isIPv6 = false;

	}

	// Always take first matching type address.

	XBOX::VNetAddressList::const_iterator	it;

	for (it = addrList.begin(); it != addrList.end(); it++)

		if ((*it).IsV6() == isIPv6)

			break;

	address = (*it).GetIP();
		
	connectionHandlerFactory->SetIP(address);

#endif	

	connectionHandlerFactory->AddNewPort(port);

	XBOX::VError	error;

	error = inServer->fConnectionListener->AddConnectionHandlerFactory(connectionHandlerFactory);
	connectionHandlerFactory->Release();	// AddConnectionHandlerFactory() has done a retain().

	if (error != XBOX::VE_OK 
	|| (error = inServer->fConnectionListener->StartListening()) != XBOX::VE_OK) 
		
		XBOX::vThrowError(error);

	else {

		inServer->fAddress = address;
		inServer->fPort = port;
			
		if (hasCallback) 
			
			inServer->AddListener("listening", callback, false);
		
		worker->QueueEvent(VJSNetEvent::Create(inServer, "listening"));

	}
	XBOX::ReleaseRefCountable<VJSWorker>(&worker);
}
Ejemplo n.º 5
0
VJSWebWorkerObject *VJSWebWorkerObject::_CreateWorker (XBOX::VJSContext &inParentContext, VJSWorker *inOutsideWorker, 
														bool inReUseContext, VJSWebSocketObject *inWebSocket,
														const XBOX::VString &inUrl, bool inIsDedicated, const XBOX::VString &inName)
{
	xbox_assert(inOutsideWorker != NULL);
	
	bool		needToRun;
	VJSWorker	*insideWorker;
			
	if (inIsDedicated) {
	
		needToRun = true;
		insideWorker = new VJSWorker(inParentContext, inOutsideWorker, inUrl, inReUseContext, inWebSocket);
				
	} else 

		insideWorker = VJSWorker::RetainSharedWorker(inParentContext, inOutsideWorker, inUrl, inName, inReUseContext, &needToRun);

	xbox_assert(insideWorker != NULL);

	if (insideWorker == inOutsideWorker) {

		// Recursive SharedWorker constructor calls are forbidden.

		xbox_assert(!inIsDedicated);

		XBOX::ReleaseRefCountable<VJSWorker>(&insideWorker);
		XBOX::vThrowError(XBOX::VE_JVSC_RECURSIVE_SHARED_WORKER, inUrl);

		return NULL;

	}

	VJSMessagePort	*onMessagePort, *onErrorPort;
	
	onMessagePort = VJSMessagePort::Create(inOutsideWorker, insideWorker,inParentContext);
	onMessagePort->SetCallbackName(inOutsideWorker, "onmessage");	
	onMessagePort->SetCallbackName(insideWorker, "onmessage");
	
	onErrorPort = VJSMessagePort::Create(inOutsideWorker, insideWorker,inParentContext);
	onErrorPort->SetCallbackName(inOutsideWorker, "onerror");	
	onErrorPort->SetCallbackName(insideWorker, "onerror");

	inOutsideWorker->AddErrorPort(onErrorPort);
	insideWorker->AddErrorPort(onErrorPort);

	if (inIsDedicated)

		insideWorker->SetMessagePort(onMessagePort);

	VJSWebWorkerObject	*webWorkerObject;

	webWorkerObject = new VJSWebWorkerObject(onMessagePort, onErrorPort, insideWorker);

	xbox_assert(!(inIsDedicated && !needToRun));
	if (needToRun)

		insideWorker->Run();
	
	if (!inIsDedicated)

		insideWorker->Connect(onMessagePort, onErrorPort);

	XBOX::ReleaseRefCountable<VJSMessagePort>(&onMessagePort);
	XBOX::ReleaseRefCountable<VJSMessagePort>(&onErrorPort);
	XBOX::ReleaseRefCountable<VJSWorker>(&insideWorker);

	return webWorkerObject;
}