Example #1
0
void OOCore::UserSession::start()
{
#if defined(NDEBUG)
	OOBase::Timeout timeout(15,0);
#else
	OOBase::Timeout timeout;
#endif

	OOBase::StackAllocator<256> allocator;
	OOBase::LocalString strPipe(allocator);
	discover_server_port(strPipe,timeout);

	// Connect up to the user process...
	int err = 0;
	while (!timeout.has_expired())
	{
		m_stream = OOBase::Socket::connect_local(strPipe.c_str(),err,timeout);
		if (!err || (err != ENOENT && err != ECONNREFUSED))
			break;

		// We ignore the error, and try again until we timeout
	}
	if (err)
		OMEGA_THROW(err);

	// Send version information
	uint32_t version = (OOCORE_MAJOR_VERSION << 24) | (OOCORE_MINOR_VERSION << 16) | OOCORE_PATCH_VERSION;
	if ((err = m_stream->send(version)) != 0)
		OMEGA_THROW(err);

	// Read our channel id
	if ((err = m_stream->recv(m_channel_id)) != 0)
		OMEGA_THROW(err);

	// Create the zero compartment
	OOBase::SmartPtr<Compartment> ptrZeroCompt = new (OOCore::throwing) Compartment(this);
	ptrZeroCompt->set_id(0);

	// Register our local channel factory
	OOBase::Guard<OOBase::RWMutex> guard(m_lock);
	m_rot_cookies.push(OTL::GetModule()->RegisterAutoObjectFactory<OOCore::ChannelMarshalFactory>());

	// Create a new object manager for the user channel on the zero compartment
	if ((err = m_mapCompartments.force_insert(0,ptrZeroCompt)) != 0)
		OMEGA_THROW(err);

	guard.release();

	// Spawn off the io worker thread
	m_worker_thread.run(io_worker_fn,this);

	// Create a proxy to the server interface
	IObject* pIPS = NULL;
	ObjectPtr<Remoting::IObjectManager> ptrOM = ptrZeroCompt->get_channel_om(m_channel_id & 0xFF000000);
	ptrOM->GetRemoteInstance(OID_InterProcessService,Activation::Library | Activation::DontLaunch,OMEGA_GUIDOF(IInterProcessService),pIPS);
	ObjectPtr<IInterProcessService> ptrIPS = static_cast<IInterProcessService*>(pIPS);

	// Register the IPS...
	OTL::GetModule()->RegisterIPS(ptrIPS,false);

	// And register the Registry
	ObjectPtr<Registry::IKey> ptrReg;
	ptrReg.GetObject(Registry::OID_Registry_Instance);
	if (ptrReg)
	{
		// Re-register the proxy locally.. it saves a lot of time!
		ObjectPtr<Activation::IRunningObjectTable> ptrROT;
		ptrROT.GetObject(Activation::OID_RunningObjectTable_Instance);

		guard.acquire();

		m_rot_cookies.push(ptrROT->RegisterObject(Registry::OID_Registry_Instance,ptrReg,Activation::ProcessScope));
		m_rot_cookies.push(ptrROT->RegisterObject(string_t::constant("Omega.Registry"),ptrReg,Activation::ProcessScope));
	}
}