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

📄 opennetserviceinternal.cpp

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

#pragma warning (disable : 4275)
#pragma warning (disable : 4786)

#include <io.h>
#include <list>
#include <map>
#include <string>
#include <windows.h>

#include <boost/thread/thread.hpp>

#include "OpenNet.h"
#include "OpenNetExtensions.h"
#include "OpenNetServiceInternal.h"
#include "TransportSerial.h"
#include "TransportTCPIP.h"
#include "TransportTCPIPServer.h"
#include "TransportUDP.h"
#include "DefaultProtocol.h"
#include "ModemProtocol.h"

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

static const GUID GUID_INTERNALSERVICE = 
{ 0x34d2e766, 0xe166, 0x4b40, { 0xb9, 0xf6, 0x2a, 0xd5, 0xe9, 0x79, 0x4, 0x85 } };

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

class CInternalService : public IService {
public:
	CInternalService();
	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 CreateInstance(GUID family, GUID guid, void **object);
	virtual bool DLL_CALLCONV ObjectExists(GUID family, GUID guid);
	virtual bool DLL_CALLCONV RegisterComponent(GUID family, GUID guid, ObjectCreateProc proc);
	~CInternalService();

private :
	void AddComponent(ObjectDiscoverProc discover);

private :	
	unsigned int m_ref_count;
	boost::mutex m_mutex;
	_STL::list<ObjectInfo *> m_info_list;
};

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

CInternalService::CInternalService() :
m_ref_count(0),
m_mutex(),
m_info_list() {
	AddComponent(SerialTransportDiscover);
	AddComponent(TCPIPTransportDiscover);
	AddComponent(TCPIPServerTransportDiscover);
	AddComponent(UDPTransportDiscover);
	AddComponent(ModemProtocolDiscover);
	AddComponent(DefaultProtocolDiscover);
}

CInternalService::~CInternalService() {
	for (_STL::list<ObjectInfo *>::iterator it = m_info_list.begin(); it != m_info_list.end(); ++it)
		delete *it;
}

unsigned long DLL_CALLCONV
CInternalService::AddRef() {
	return ++m_ref_count;
}

unsigned long DLL_CALLCONV
CInternalService::Release() {
	int ref = --m_ref_count;

	if (ref == 0)
		delete this;

	return ref - 1;
}

HRESULT
CInternalService::QueryInterface(REFIID guid, void **iif) {
	if (IsEqualGUID(guid, GUID_INTERNALSERVICE)) {
		AddRef();
		*iif = this;
		return S_OK;
	} else if (IsEqualGUID(guid, CLSID_SERVICE_REGISTERABLE)) {
		AddRef();
		*iif = this;
		return S_OK;
	} else if (IsEqualGUID(guid, GUID_OBJECT)) {
		AddRef();
		*iif = this;
		return S_OK;
	}

	return E_NOINTERFACE;
}


HRESULT DLL_CALLCONV
CInternalService::CreateInstance(GUID family, GUID guid, void **object) {
	boost::mutex::scoped_lock scoped_lock(m_mutex);

	for (_STL::list<ObjectInfo *>::iterator it = m_info_list.begin(); it != m_info_list.end(); ++it)
		if (IsEqualGUID(guid, (*it)->guid))
			if (IsEqualGUID(family, (*it)->family))
				return (*it)->create(object);

	return E_FAIL;
}

bool DLL_CALLCONV
CInternalService::ObjectExists(GUID family, GUID guid) {
	boost::mutex::scoped_lock scoped_lock(m_mutex);

	for (_STL::list<ObjectInfo *>::iterator it = m_info_list.begin(); it != m_info_list.end(); ++it)
		if (IsEqualGUID(guid, (*it)->guid))
			if (IsEqualGUID(family, (*it)->family))
				return true;

	return false;
};

bool DLL_CALLCONV
CInternalService::RegisterComponent(GUID family, GUID guid, ObjectCreateProc create) {
	ObjectInfo *info = new ObjectInfo;
	info->family = family;
	info->guid = guid;
	info->create = create;
	info->extra = NULL;

	boost::mutex::scoped_lock scoped_lock(m_mutex);

	m_info_list.push_back(info);
	return true;
}

void
CInternalService::AddComponent(ObjectDiscoverProc discover) {
	boost::mutex::scoped_lock scoped_lock(m_mutex);

	m_info_list.push_back(new ObjectInfo);
	discover(0, m_info_list.back());
}

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

HRESULT DLL_CALLCONV
CInternalServiceCreate(void **iif) {
	CInternalService *object = new CInternalService;

	if (object) {
		object->AddRef();
		*iif = object;
		return S_OK;
	}

	return E_FAIL;
}

⌨️ 快捷键说明

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