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

📄 opcdrv.h

📁 基于Intellution开发包的开发的OPC服务器
💻 H
📖 第 1 页 / 共 2 页
字号:
// OPCDrv.h
//
// This file contains OBJECT definitions to the OPC server 
// for the XXXOPC.DLL.
// (The OPC.H file contains common interface base classes that
//  each object derives from)
//
//
//	(C) COPYRIGHT 1996-1998, INTELLUTION INC.
// ALL RIGHTS RESERVED
//
//
// Modification Log:
//	Vers	Date     By		Notes
//	----	-------- ---	-----
//	1.0		08/26/97 jra	Created
//	1.3		03/10/98 jra	Modified to be wizard generated and driver specific.
//
//


#ifndef OPCDrv__H__
#define OPCDrv__H__

#include <afxtempl.h>
#include <afxcoll.h>

#include "OPC.h"

// Required Elwood headers
#include <NioDrv.h>
#include <InterfaceDef.h>


/////////////////////////////////////////////////////////////////////////////
// Cached Data Change Flags
//
#define OPC_ODC_DO				1		// Data Object without time
#define OPC_ODC_DOT				2		// Data Object with time
#define OPC_ODC_ANY				OPC_ODC_DO | OPC_ODC_DOT


/////////////////////////////////////////////////////////////////////////////
// Used for the Browse interface.
// these indicate what level of browsing we are on
//
#define DEVICE_LEVEL			0
#define DATABLOCK_LEVEL			1


/////////////////////////////////////////////////////////////////////////////
// Async transaction stuff
//
#define TRANS_WRITE				0
#define TRANS_READ				1
#define TRANS_READTIME			2


/////////////////////////////////////////////////////////////////////////////
// Types of messages capable of being sent to the driver's event window
//
#define MSG_ERROR				0
#define	MSG_WARNING				1
#define	MSG_INFO				2
#define	MSG_DEBUG				3


/////////////////////////////////////////////////////////////////////////////
// misc...
//
#define	MAX_STRING_LENGTH		256
#define THREAD_SHUTDOWN_TIME	2000


/////////////////////////////////////////////////////////////////////////////
// Forward type definitions of the classes
//
class	CImpIOPCBrowseServer;				// browse address space (optional)
class	CImpIBrowseServer;					// server browsing
class	CImpIEnumString;					// String enumeration implementation
class	COPCDrvServer;						// The server object itself
class	COPCDrvGroup;						// The Group Object itself
class	COPCDrvItem;						// A 'local' Item Class
class	COPCDrvAsyncTrans;					// Used for read/write async stuff


/////////////////////////////////////////////////////////////////////////////
// Global server variables.
// These variables keep track of state information which is global to the server process.
// The common memory allocator is required so that the server
//   can allocate items to be returned to the client.
// The client must be able to access and to free these items 
//   so they need to be in common memory.
/////////////////////////////////////////////////////////////////////////////
extern IMalloc		*pIMalloc;		// Common memory allocator


/////////////////////////////////////////////////////////////////////////////
// From OPCDrvUtility.cpp
// Utility and convenience functions
//
#define WCHAR	wchar_t

BSTR	BSTRFromWSTR(const WCHAR *s, IMalloc *pmem);	// Aloc BSTR and copy WSTR into it
void	BSTRFree(BSTR bstr, IMalloc *pmem);				// Free a BSTR
WCHAR*	WSTRFromBSTR(const BSTR bstr, IMalloc *pmem);	// Aloc a WSTR and copy a BSTR into it
WCHAR*	WSTRClone(const WCHAR *oldstr, IMalloc *pmem);
void	WSTRFree(WCHAR *c, IMalloc *pmem);
BOOL	LogEventMessage(LPCTSTR ptcMessage, LPVOID pData = NULL,
						DWORD dwDataSize = 0, DWORD dwEventID = 0, 
						WORD wType = EVENTLOG_INFORMATION_TYPE);// Send a message to event Viewer
void	RegisterEventService(void);
void	DeRegisterEventService(void);
void	IAClone(OPCITEMATTRIBUTES * pNewIA, const OPCITEMATTRIBUTES*pOldIA, IMalloc *pmem);
void	IAFree(OPCITEMATTRIBUTES *pIA, IMalloc *pmem);


/////////////////////////////////////////////////////////////////////////////
// Items shared by the class factory and the DLL or EXE mainline
//
extern	void		OPCServerUnload(void);


/////////////////////////////////////////////////////////////////////////////
// Class Factory implementation
//
/////////////////////////////////////////////////////////////////////////////
class CImpIClassFactory : public IClassFactory
{
public:
    CImpIClassFactory(REFCLSID classID);

	BOOL Register(
		DWORD				regCls = REGCLS_MULTIPLEUSE
		);
	
	BOOL Unregister(
		void
		);

    STDMETHODIMP         QueryInterface(
		REFIID				iid,
		LPVOID				* ppInterface
		);
    
	STDMETHODIMP_(ULONG) AddRef(
		void
		);
    
	STDMETHODIMP_(ULONG) Release(
		void
		);

    STDMETHODIMP		CreateInstance(
		LPUNKNOWN			pUnkOuter, 
		REFIID				riid, 
		LPVOID				*ppvObject
		);

    STDMETHODIMP		LockServer(
		BOOL				bLock
		);

protected:
    long				m_lRefCount;
};


/////////////////////////////////////////////////////////////////////////////
// OPCDrvServer object
//
// This class is an implementation of a specific version of the OPCServer class.
// There is no standard OPC Server 'object', only vendor specific objects
// so this derives from IUnKnown, but we also derive from CObject to make use
// of the MFC lists and maps.
//
/////////////////////////////////////////////////////////////////////////////
class COPCDrvServer :	public IUnknown,
						public CObject
{
	friend class CImpIOPCServer;
	friend class CImpIPersistFile;
	friend class CImpIOPCGroupStateMgt;
	friend class CImpIOPCBrowseServer;
	friend class CImpIEnumString;

	friend class COPCDrvGroup;
	friend class COPCDrvItem;

public:
	COPCDrvServer(LPUNKNOWN, void (*pfn)(void));
	~COPCDrvServer(void);

	// the IUnknown Functions
	STDMETHODIMP         QueryInterface(
		REFIID				iid, 
		LPVOID				*ppInterface
		);
	
	STDMETHODIMP_(ULONG) AddRef(
		void
		);
	
	STDMETHODIMP_(ULONG) Release(
		void
		);


	//
	// Public (non-OPC) Functions
	//

	HRESULT				Init(
		void
		);

	void				UpdateDataCache(
		DWORD				dwTics
		);
	
	void				GenerateTransaction(
		DWORD				*pTransactionID
		);

	HRESULT				GetDriverInfo(
		void
		);

	BOOL				IsDataTypeSupported(
		VARTYPE				vDataType
		);

	HRESULT				SendMessageToDriver(
		CString				szMessage,
		unsigned short		nType		= MSG_ERROR
		);

private:
	// Group List Management Functions (NOT an OPC interface).
	BOOL				IsGroupValid(
		OPCHANDLE			hGroup
		);									// Validate Group Handle
	
	COPCDrvGroup		*GetGroupPtr(
		OPCHANDLE			hGroup
		);									// Given handle, return pointer
	
	int					GetNumGroupHandles(
		void
		);									// Total # Group handles
	
	HRESULT				GroupAlloc(
		OPCHANDLE			*hGroup, 
		COPCDrvGroup		**ppGroup,
		LPUNKNOWN			pUnkOuter
		);									// Alloc next avaialable Handle and 
											// get the memory for it
	HRESULT				GroupSet(
		COPCDrvGroup		*pGroup
		);									// Assign an Group to a slot
	
	HRESULT				GroupFree(
		OPCHANDLE			hGroup, 
		BOOL				bFreeMemFlag=FALSE
		);									// Remove a Group from the map

	POSITION			GetFirstGroupPosition(
		void
		);									// Returns the position of the first Group

	void				GetNextGroup(
		POSITION			&ItemPosition, 
		OPCHANDLE			&hGroup, 
		COPCDrvGroup		*&pGroup
		);									// Returns the next Group in the 
											// server relative to POSITION

	HRESULT				FreeAllGroups(
		void
		);									// free all of the groups


	// Some private (non-opc) utility functions to help with Enumeration
	void 				GetUnkList  (
		OPCENUMSCOPE		scope, 
		LPUNKNOWN			**ppGroupList, 
		int					*nGroupCount
		);

	void 				GetNameList (
		OPCENUMSCOPE		scope, 
		LPOLESTR			**ppGroupList,
		int					*nGroupCount
		);

	void				FreeNameList(
		LPOLESTR			*pGroupList,
		int					nGroupCount
		);

	void				FreeUnkList (
		LPUNKNOWN			*pGroupList,
		int					nGroupCount
		);

	// for thread safety (just in case)
	void			Lock  (void) { EnterCriticalSection(&this->m_Lock); }
	
	void			UnLock(void) { LeaveCriticalSection(&this->m_Lock); }
  
	// Public properties
	OPCSERVERSTATE				m_ServerState;				// Sate of the server
		
public:
	// Public Member Variables
	DWORD						m_dwBandwidth;				// Server's bandwidth
	DWORD						m_dwNumGroupsProcessed;		// These parameters are used to
	DWORD						m_dwTotalPasses;			// calculate the % Bandwidth of the server

	DWORD						m_dwPollThreadId;			// Poll thread ID

private:
	//
	// Private Member Variables
	//

	CImpIOPCServer				*m_pCImpIOPCServer;			// tear-off IOPCServer
	CImpIOPCBrowseServer		*m_pCImpIOPCBrowseServer;	// tear-off browse address space (optional)
	CImpIPersistFile			*m_pCImpIPersistFile;		// tear-off persist file (optional)

	long						m_lRefCount;
	FILETIME					m_ftLastUpdate;
	FILETIME					m_ftServerStartTime;
	void						(*m_pfnDestroy)(void);

	long						m_lTransactionID;			// transaction ID
	int							m_Slot;						// Asnyc helper thread slot

	DWORD						m_dwLastTickCount;			// Tick count at last process

	DWORD						m_dwNumGroups;				// Number of groups

	BOOL						m_bKeepRunning;				// Used to shutdown threads

	// I/O Driver's OLE Automation interface stuff
	//
	IDriver						*m_pIDriver;
	IDriverMessage				*m_pIDriverMessage;
	IDriverMessage				*m_pIDriverMessageMarshalled;
	IOPCConnections				*m_pIOpcConnections;
	LPSTREAM					m_pIDriverMessageStream;

	// NIO hardware options stuff
	//
	int							m_nOptionsSize;				// Size of the buffer needed
	int							m_nNumOptions;				// Number of options
	VARTYPE						*m_pvtOptionValues;			// Pointer to list of option values
	char						*m_pszOptions;				// Pointer to the buffer

	// Server thread used to perform asynchronous back-ground stuff.
	//
	HANDLE	m_hPollThread;
	static	UINT __stdcall PollThread(void *pParam);

	CRITICAL_SECTION			m_Lock;						// Critical section

	// List of groups
	CMap<OPCHANDLE, OPCHANDLE, COPCDrvGroup*, COPCDrvGroup*&> m_mapGroup;
};


/////////////////////////////////////////////////////////////////////////////
// COPCDrvGroup object
//
// This class is an implementation of a specific version of the OPCGroup class.
// We derive both from IUnknown and CObject.
//
/////////////////////////////////////////////////////////////////////////////
class COPCDrvGroup :	public IUnknown,
						public CObject
{
	friend class COPCDrvServer;
	friend class COPCDrvItem;
	friend class CImpIOPCServer;
	friend class CImpIOPCItemMgt;
	friend class CImpIOPCSyncIO;
	friend class CImpIOPCAsyncIO;
	friend class CImpIOPCGroupStateMgt;
	friend class CImpIDataObject;
	friend class CImpIEnumOPCItemAttributes;
	friend class COPCDrvAsyncTrans;


public:
	COPCDrvGroup(LPUNKNOWN);
	~COPCDrvGroup(void);

	// the IUnknown Functions
	STDMETHODIMP         QueryInterface(
		REFIID					iid, 
		LPVOID					*ppInterface
		);
	
	STDMETHODIMP_(ULONG) AddRef(
		void
		);
	
	STDMETHODIMP_(ULONG) Release(
		void
		);

	// Get/Set Group active state
	void				SetActive(
		BOOL					bActive
		);

	inline BOOL			GetActive(void)	
		{ return this->m_bActive; }

	// Get/Set Group update state
	void				SetUpdateRate(
		DWORD					dwRequestedRate,
		DWORD					*pReviseRate = NULL
		);

	DWORD				GetUpdateRate(void)	
		{ return this->m_dwRevisedRate; }

	// Overloaded operators
	COPCDrvGroup &		operator=(
		const COPCDrvGroup &OtherGroup
		);


private:
	// Some private (non-opc) utility functions to help with Enumeration
	void				GetItemList (
		OPCITEMATTRIBUTES		**AttrList, 
		int						*nItemCount
		);
	
	void				FreeItemList(
		OPCITEMATTRIBUTES		*AttrList, 
		int						nItemCount
		);

	// Item List Management Functions (NOT a required OPC interface)
	BOOL				IsItemValid(
		OPCHANDLE				hItem
		);										// See if a handle is in use
	
	COPCDrvItem	*		GetItemPtr(
		OPCHANDLE				hItem
		);										// Given handle, return pointer
	
	int					GetNumItemHandles(
		void
		);										// Total # item handles
	
	HRESULT				ItemAlloc(
		OPCHANDLE				*hItem, 
		COPCDrvItem				**ppNewItem
		);										// Alloc next available Handle and 
												// add it to the map
	HRESULT				ItemSet(
		COPCDrvItem				*pItem
		);										// Assign an item to a slot
	
	HRESULT				ItemFree(
		OPCHANDLE				hItem, 
		BOOL					bFreeMemFlag=FALSE
		);										// Remove an Item from the map

	POSITION			GetFirstItemPosition(
		);										// Returns the position of the 
												// first Group in the map

⌨️ 快捷键说明

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