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

📄 opennet.cpp

📁 .net 方面的开发说明资料。
💻 CPP
字号:
// ======================================================
// Eplug User Interface Implementation
// ======================================================

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

// --------------------------------------------------------
// Regular include stuff
// --------------------------------------------------------

#include <crtdbg.h>
#include <io.h>
#include <stdio.h>

#ifdef WIN32
#include <windows.h>
#endif

#include "Agent.h"
#include "OpenNet.h"
#include "OpenNetExtensions.h"
#include "EventDispatcher.h"
#include "ThreadBalancer.h"
#include "TimeoutManager.h"
#include "TransportSerial.h"
#include "TransportTCPIP.h"
#include "TransportTCPIPServer.h"
#include "TransportUDP.h"
#include "DefaultProtocol.h"
#include "ModemProtocol.h"

// --------------------------------------------------------
// Global typedefs
// --------------------------------------------------------

typedef CLSID (DLL_CALLCONV *InitProtocolProc)(unsigned int count, Protocol *protocol);
typedef void (DLL_CALLCONV *NewPluginProc)(HMODULE module, unsigned int  count, FARPROC proc);

// --------------------------------------------------------
// Global Structures
// --------------------------------------------------------

struct InsensitiveCompare {
	bool operator()(const std::string &a, const std::string &b) const {
		return stricmp(a.c_str(), b.c_str()) < 0;
	}
};

struct ProtocolEntry {
	CLSID id;
	HMODULE module;
	Protocol protocol;
};

// --------------------------------------------------------
// Global data
// --------------------------------------------------------

static LONG s_init_ref_count = 0;
static std::list<ProtocolEntry *> s_protocols;
static char s_version[] = "1.0.0.46";

// --------------------------------------------------------
// Platform dependent global data
// --------------------------------------------------------

#ifdef WIN32
static WSADATA s_wsa_data;
#endif

// ------------------------------------------------------
// Grid container... hidden from the user since it
// serves no particular purpose except an internal one.
// ------------------------------------------------------

IAgent *s_agent_container = NULL;

// ------------------------------------------------------
// 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;
}

// ------------------------------------------------------
// Load various plugins
// ------------------------------------------------------

void DLL_CALLCONV
LoadPlugins(const char *files, const char *function, NewPluginProc proc) {
	// add user protocols

	_finddata_t find_data;
	long find_handle;

	if ((find_handle = _findfirst(files, &find_data)) != -1) {
		do {
			HMODULE module = LoadLibrary(find_data.name);

			if (module) {
				FARPROC plugin_proc = GetProcAddress(module, function);

				if (plugin_proc)
					proc(module, 0, plugin_proc);
				else
					FreeLibrary(module);
			}
		} while(_findnext(find_handle, &find_data) == 0);

		_findclose(find_handle);
	}
}

void DLL_CALLCONV
NewProtocolEntry(HMODULE module, unsigned int count, FARPROC proc) {
	ProtocolEntry *entry = new ProtocolEntry;
	memset(entry, 0, sizeof(ProtocolEntry));

	entry->module = module;
	entry->id = ((InitProtocolProc)proc)(count, &entry->protocol);

	s_protocols.push_back(entry);
}

// --------------------------------------------------------
// Init / DeInit functions
// --------------------------------------------------------

DLL_API bool DLL_CALLCONV
EpInit() {
	if (s_init_ref_count == 0) {
		// set up winsock

#ifdef WIN32
		WSAStartup(MAKEWORD(2, 2), &s_wsa_data);
#endif
		// create the grid container that manages and creates all plugs

		AgentCreate((void **)&s_agent_container);

		// load the plugins

		if (s_agent_container) {
			NewProtocolEntry(NULL, 0, (FARPROC)SystemProtocolInit);
			NewProtocolEntry(NULL, 0, (FARPROC)ModemProtocolInit);

			LoadPlugins("*.epp", "_EpProtocolInit@8", NewProtocolEntry);

			++s_init_ref_count;
			return true;
		}

		return false;
	}

	++s_init_ref_count;
	return true;
}

DLL_API void DLL_CALLCONV
EpEnableDebugEvents(TRANSPORT_HANDLE transport, bool enable) {
	if (s_init_ref_count > 0)
		s_agent_container->enableDebugEvents((ITransport *)transport, enable);
}

DLL_API bool DLL_CALLCONV
EpHeartBeat() {
	if (s_init_ref_count > 0)
		return EventDispatcher::getInstance()->heartBeat();

	return false;
}

DLL_API void DLL_CALLCONV
EpFinalizeWin32() {
	WSACleanup();
}

DLL_API void DLL_CALLCONV
EpFinalize() {
	if (--s_init_ref_count == 0) {
		// destroy the created agent container...
		// the agent will make sure all events from
		// the event dispatcher are deleted.

		delete s_agent_container;
		s_agent_container = NULL;

		// remove the singleton classes

		ThreadBalancer::destroyInstance();
		EventDispatcher::destroyInstance();
		TimeOutManager::destroyInstance();

		// remove all known protocols

		for (std::list<ProtocolEntry *>::iterator j = s_protocols.begin(); j != s_protocols.end(); ++j) {
			if ((*j)->module)
				FreeLibrary((*j)->module);

			delete *j;
		}

		s_protocols.clear();

		// and be gone with winsock

#ifdef WIN32
		EpFinalizeWin32();
#endif	
	}
}

DLL_API int DLL_CALLCONV
EpGetCpuCount() {
	SYSTEM_INFO system_info;

	GetSystemInfo(&system_info);

	return system_info.dwNumberOfProcessors;
}

DLL_API const char * DLL_CALLCONV
EpGetVersion() {
	return s_version;
}

// --------------------------------------------------------
// Utility functions
// --------------------------------------------------------

DLL_API int DLL_CALLCONV
EpCLSIDToString(CLSID clsid, char *result) {
	LPOLESTR s = (LPOLESTR)CoTaskMemAlloc(256);

	StringFromCLSID(clsid, &s);

	int temp = WideCharToMultiByte(CP_ACP, 0, s, -1, result, result ? 256 : 0, NULL, NULL); 

	strcpy(result, result);

	CoTaskMemFree(s);

	return temp;
}

// ------------------------------------------------------
// Component registering
// ------------------------------------------------------

DLL_API bool DLL_CALLCONV
EpRegisterProtocol(CLSID id, Protocol *protocol) {
	if (protocol) {
		// protocol doesn't exist already?

		for (std::list<ProtocolEntry *>::iterator i = s_protocols.begin(); i != s_protocols.end(); ++i)
			if (IsEqualGUID(id, (*i)->id))
				return false;

		// if we come here we succeeded

		ProtocolEntry *entry = new ProtocolEntry;

		if (entry) {
			entry->module = NULL;
			entry->id = id;
			entry->protocol.create = protocol->create;
			entry->protocol.send = protocol->send;
			entry->protocol.receive = protocol->receive;
			entry->protocol.destroy = protocol->destroy;

			s_protocols.push_back(entry);
			return true;
		}
	}

	return false;
}

DLL_API Protocol *DLL_CALLCONV
EpProtocolHandle(CLSID id) {
	for (std::list<ProtocolEntry *>::iterator i = s_protocols.begin(); i != s_protocols.end(); ++i)
		if (IsEqualGUID(id, (*i)->id))
			return &(*i)->protocol;

	return NULL;
}

// --------------------------------------------------------
// Transport managing
// --------------------------------------------------------

DLL_API TRANSPORT_HANDLE DLL_CALLCONV
EpCreateTransport(const char *properties, CallbackProc callback, bool enable_debug_events, void *data) {
	if (s_init_ref_count > 0)
		return s_agent_container->createTransport(properties, callback, enable_debug_events, data);

	return NULL;
}

DLL_API void DLL_CALLCONV
EpDestroyTransport(TRANSPORT_HANDLE transport) {
	if (s_init_ref_count > 0)
		s_agent_container->destroyTransport((ITransport *)transport);
}

DLL_API bool DLL_CALLCONV
EpGetOption(TRANSPORT_HANDLE transport, int option, void *value, int *size) {
	if (s_init_ref_count > 0)
		return s_agent_container->getOption((ITransport *)transport, option, value, size);

	return false;
}

DLL_API bool DLL_CALLCONV
EpSetOption(TRANSPORT_HANDLE transport, int option, void *value) {
	if (s_init_ref_count > 0)
		return s_agent_container->setOption((ITransport *)transport, option, value);

	return false;
}

// ------------------------------------------------------
// protocol management
// ------------------------------------------------------

DLL_API bool DLL_CALLCONV
EpAddProtocol(TRANSPORT_HANDLE transport, GUID protocol_id) {
	if (s_init_ref_count > 0)
		return s_agent_container->addProtocol((ITransport *)transport, &protocol_id);

	return false;
}

DLL_API void DLL_CALLCONV
EpResetProtocol(TRANSPORT_HANDLE handle, GUID protocol) {
	if (s_init_ref_count > 0)
		s_agent_container->resetProtocol((ITransport *)handle, protocol);
}

// --------------------------------------------------------
// communication
// --------------------------------------------------------

DLL_API void DLL_CALLCONV
EpConnect(TRANSPORT_HANDLE transport, const char *host, int port, int timeout) {
	if (s_init_ref_count > 0)
		s_agent_container->connect((ITransport *)transport, host, port, timeout);
}

DLL_API void DLL_CALLCONV
EpDisconnect(TRANSPORT_HANDLE transport) {
	if (s_init_ref_count > 0)
		s_agent_container->disconnect((ITransport *)transport);
}

DLL_API bool DLL_CALLCONV
EpSendAction(TRANSPORT_HANDLE transport, EpAction *action) {
	if (s_init_ref_count > 0)
		return s_agent_container->sendAction((ITransport *)transport, action);

	return false;
}

DLL_API void DLL_CALLCONV
EpCompleteAction(TRANSPORT_HANDLE transport, unsigned char *data, int size) {
	if (s_init_ref_count > 0)
		s_agent_container->completeAction((ITransport *)transport, data, size);
}

DLL_API Action *DLL_CALLCONV
EpGetNextAction(TRANSPORT_HANDLE transport) {
	if (s_init_ref_count > 0)
		return s_agent_container->getNextAction((ITransport *)transport);

	return NULL;
}

DLL_API bool DLL_CALLCONV
EpSendRawData(TRANSPORT_HANDLE transport, unsigned char *data, int size, GUID reply_protocol, int reply_msg, int timeout) {
	if (s_init_ref_count > 0)
		return s_agent_container->sendRawData((ITransport *)transport, data, size, reply_protocol, reply_msg, timeout);

	return false;
}

DLL_API bool DLL_CALLCONV
EpCancelTimeout(TRANSPORT_HANDLE transport, GUID protocol, int msg) {
	if (s_init_ref_count > 0)
		return TimeOutManager::getInstance()->removeItem((ITransport *)transport, protocol, msg);

	return false;
}

DLL_API bool DLL_CALLCONV
EpCancelProtocolTimeouts(TRANSPORT_HANDLE transport, GUID protocol) {
	if (s_init_ref_count > 0)
		return TimeOutManager::getInstance()->removeItems((ITransport *)transport, protocol);

	return false;
}

DLL_API bool DLL_CALLCONV
EpCancelAllTimeouts(TRANSPORT_HANDLE transport) {
	if (s_init_ref_count > 0)
		return TimeOutManager::getInstance()->removeItems((ITransport *)transport);

	return false;
}

DLL_API void DLL_CALLCONV
EpDispatchEvent(TRANSPORT_HANDLE transport, EpEvent *event) {
	if (s_init_ref_count > 0)
		EventDispatcher::getInstance()->pushEventEntry(EventDispatcher::getInstance()->createEventEntry(transport, event));
}

DLL_API void *DLL_CALLCONV
EpGetEventRefData(TRANSPORT_HANDLE transport, int reference) {
	if (s_init_ref_count > 0)
		return s_agent_container->getEventRefData((ITransport *)transport, reference);

	return NULL;
}

DLL_API void DLL_CALLCONV
EpInheritedRecv(TRANSPORT_HANDLE transport, unsigned char *data, int size) {
	if (s_init_ref_count > 0)
		s_agent_container->inheritedRecv((ITransport *)transport, data, size);
}

DLL_API void DLL_CALLCONV
EpPacketSent(TRANSPORT_HANDLE transport, bool succeeded) {
	if (s_init_ref_count > 0)
		s_agent_container->dispatchPacketSent((ITransport *)transport, succeeded);
}

DLL_API int DLL_CALLCONV
EpGetProtocolName(TRANSPORT_HANDLE transport, GUID protocol, char *name, int size) {
	if ((s_init_ref_count > 0) && (name) && (size > 0))
		return s_agent_container->getProtocolName((ITransport *)transport, protocol, name, size);

	return 0;
}

DLL_API int DLL_CALLCONV
EpGetProtocolMsgName(TRANSPORT_HANDLE transport, GUID protocol, int msg, char *name, int size) {
	if ((s_init_ref_count > 0) && (name) && (size > 0))
		return s_agent_container->getProtocolMsgName((ITransport *)transport, protocol, msg, name, size);

	return 0;
}

⌨️ 快捷键说明

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