⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectserver.cpp

📁 .net 方面的开发说明资料。
💻 CPP
字号:
// =====================================================
// Object Server
//
// Design and Implementation by Floris van den Berg
//
// Copyright (c) 2002 Pylon
// =====================================================

#pragma warning (disable : 4275)

#include <list>

#include "OpenObjects.h"
#include "ObjectServiceInternal.h"
#include "ObjectServerServiceDll.h"

// ------------------------------------------------------
// DLL Initialization
// ------------------------------------------------------

BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
	switch(fdwReason) {
		case DLL_PROCESS_ATTACH :
			break;

		case DLL_PROCESS_DETACH :
			break;
	};

	return TRUE;
}

// -----------------------------------------------------

struct ServiceEntry {
	GUID guid;
	IService *service;
};

// -----------------------------------------------------

class CServiceManager : public IServiceManager {
	typedef _STL::list<ServiceEntry> ServiceList;
	typedef _STL::list<ServiceEntry>::iterator ServiceListIt;

public :
	CServiceManager();
	virtual ~CServiceManager();
	virtual void Init();
	virtual unsigned long DLL_CALLCONV AddRef();
	virtual unsigned long DLL_CALLCONV Release();
	virtual HRESULT DLL_CALLCONV QueryInterface(REFIID guid, void **iif);
	virtual HRESULT DLL_CALLCONV AddService(GUID guid, IService *service);
	virtual HRESULT DLL_CALLCONV RemoveService(GUID guid);
	virtual HRESULT DLL_CALLCONV GetService(GUID guid, void **iif);
	virtual HRESULT DLL_CALLCONV CreateInstance(GUID family, GUID guid, void **object);
	virtual bool DLL_CALLCONV ObjectExists(GUID family, GUID guid);

private :
	LONG m_ref_count;
	ServiceList m_services;
	IService *m_registerable_service;
	IService *m_dll_service;
};

// -----------------------------------------------------

static CServiceManager *s_manager = NULL;

// -----------------------------------------------------

CServiceManager::CServiceManager() :
m_ref_count(0),
m_services(),
m_registerable_service(NULL),
m_dll_service(NULL) {
}

void
CServiceManager::Init() {
	if (CInternalServiceCreate((void **)&m_registerable_service) == S_OK)
		AddService(CLSID_SERVICE_REGISTERABLE, m_registerable_service);

	if (CDllServiceCreate((void **)&m_dll_service) == S_OK)
		AddService(GUID_SERVICE_DLL, m_dll_service);
}

CServiceManager::~CServiceManager() {
	for (ServiceListIt it = m_services.begin(); it != m_services.end(); ++it)
		(*it).service->Release();

	m_dll_service->Release();
	m_registerable_service->Release();
}

unsigned long DLL_CALLCONV
CServiceManager::AddRef() {
	return InterlockedIncrement(&m_ref_count);
}

unsigned long DLL_CALLCONV
CServiceManager::Release() {
	if (InterlockedDecrement(&m_ref_count) == 0) {
		delete this;
		return 0;
	}

	return m_ref_count;
}

HRESULT DLL_CALLCONV
CServiceManager::QueryInterface(REFIID guid, void **iif) {
	if (IsEqualGUID(guid, GUID_SERVICEMANAGER)) {
		AddRef();
		*iif = this;
		return S_OK;
	} else if (IsEqualGUID(guid, GUID_OBJECT)) {
		AddRef();
		*iif = this;
		return S_OK;
	}

	return E_NOINTERFACE;
}

HRESULT DLL_CALLCONV
CServiceManager::AddService(GUID guid, IService *service) {
	for (ServiceListIt it = m_services.begin(); it != m_services.end(); ++it)
		if (IsEqualGUID((*it).guid, guid))
			return E_FAIL;

	// the service manager references services, but it doesn't destroy them
	// this should be done by whoever created the manager.

	service->AddRef();

	// put the service + guid to locate the service in a structure

	ServiceEntry entry;
	entry.guid = guid;
	entry.service = service;

	// and add the structure to an internal list
	
	m_services.push_back(entry);
	return S_OK;
}

HRESULT DLL_CALLCONV
CServiceManager::RemoveService(GUID guid) {
	for (ServiceListIt it = m_services.begin(); it != m_services.end(); ++it)
		if (IsEqualGUID((*it).guid, guid)) {
			(*it).service->Release();
			m_services.erase(it);			
			return S_OK;
		}

	return E_FAIL;
}

HRESULT DLL_CALLCONV
CServiceManager::GetService(GUID guid, void **iif) {
	for (ServiceListIt it = m_services.begin(); it != m_services.end(); ++it)
		if (IsEqualGUID((*it).guid, guid)) {
			(*it).service->AddRef();
			*iif = (*it).service;
			return S_OK;
		}

	return E_FAIL;
}

HRESULT DLL_CALLCONV
CServiceManager::CreateInstance(GUID family, GUID guid, void **object) {
	for (ServiceListIt it = m_services.begin(); it != m_services.end(); ++it)
		if ((*it).service->CreateInstance(family, guid, object) == S_OK)
			return S_OK;

	return E_FAIL;
}

bool DLL_CALLCONV
CServiceManager::ObjectExists(GUID family, GUID guid) {
	for (ServiceListIt it = m_services.begin(); it != m_services.end(); ++it)
		if ((*it).service->ObjectExists(family, guid))
			return true;

	return false;
}

// -----------------------------------------------------

SERVER_API HRESULT DLL_CALLCONV
GetServiceManager(void **iif) {
	if (s_manager == NULL) {
		s_manager = new CServiceManager;
		s_manager->AddRef();
		s_manager->Init();
	} else {
		s_manager->AddRef();
	}

	*iif = s_manager;
	return S_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -