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

📄 opcdrvserverlist.cpp

📁 基于Intellution开发包的开发的OPC服务器
💻 CPP
字号:
// OPCDrvServerList.cpp
//
//  This file contains the implementation of 
//  the COPCDrv Server Object GROUP LIST management functions:
//
//  (c) COPYRIGHT 1996-1998, INTELLUTION INC.
//  ALL RIGHTS RESERVED
//
//
//	Functions defined in this module:
//
//			COPCDrvServer::IsGroupValid()
//			COPCDrvServer::GetGroupPtr()
//			COPCDrvServer::GroupHandles()
//			COPCDrvServer::GroupAlloc()
//			COPCDrvServer::GroupSet()
//			COPCDrvServer::GroupFree()
//			COPCDrvServer::FreeAllGroups()
//			COPCDrvServer::GetFirstGroup()
//			COPCDrvServer::GetFirstGroupPosition()
//
//
//
// 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.
//
//

#define WIN32_LEAN_AND_MEAN

#define NOCOMM

#include <afxtempl.h>

#include "OPCDrv.h"


////////////////////////////////////////////////////////////////
// COPCDrvServer::IsGroupValid()
//
// Verifies that an Group is valid.
//
// Returns TRUE if the Group handle is found.
// Returns FALSE if it is not found.
//
////////////////////////////////////////////////////////////////
BOOL COPCDrvServer::IsGroupValid(OPCHANDLE OPCHandle)
{
	COPCDrvGroup	*pGroup;


	if (m_mapGroup.Lookup(OPCHandle, pGroup) == TRUE)
	{
		// Group handle found
		return TRUE;
	}

	return FALSE;
}


////////////////////////////////////////////////////////////////
// COPCDrvServer::GetGroupPtr()
//
// Accepts an OPCHANDLE and returns a pointer to the 
//	corresponding Group in the map.
//
// Returns a pointer to the Group.
// Returns NULL if the handle was not found in the map.
//
////////////////////////////////////////////////////////////////
COPCDrvGroup* COPCDrvServer::GetGroupPtr(OPCHANDLE OPCHandle)
{
	COPCDrvGroup	*pGroup;


	if (m_mapGroup.Lookup(OPCHandle, pGroup) == TRUE)
	{
		// Group handle found
		return pGroup;
	}

	return NULL;
}


////////////////////////////////////////////////////////////////
// COPCDrvServer::GetNumGroupHandles()
//
// Return # Group handles (for use in loops)
//
////////////////////////////////////////////////////////////////
int COPCDrvServer::GetNumGroupHandles(void)
{
	return (m_mapGroup.GetCount());
}


////////////////////////////////////////////////////////////////
// COPCDrvServer::GroupAlloc
// Add an Group to the list.
//
// Alocates the memory and assigns a handle for a new Group
// object. The caller should check for failure and add the
// group to the map using GroupSet() if everything worked.
//
// Returns the new Group's handle and a pointer to it.
//
////////////////////////////////////////////////////////////////
HRESULT COPCDrvServer::GroupAlloc(OPCHANDLE		*OPCHandle, 
								  COPCDrvGroup	**ppGroup,
								  LPUNKNOWN		pUnkOuter)
{
	// Make sure that we have valid pointers
	//
	if ((NULL == ppGroup)   ||
		(NULL == OPCHandle) ||
		(NULL == pUnkOuter))
	{
		*OPCHandle = 0;
		return E_INVALIDARG;
	}

	// Create the new group and verify it
	//
	COPCDrvGroup *pNewGroup = new COPCDrvGroup (pUnkOuter);
	if (NULL == pNewGroup)
	{
		*OPCHandle = 0;
		*ppGroup = NULL;
		return E_OUTOFMEMORY;
	}

	// Return a pointer to the caller as well as the new handle
	//
	*ppGroup	= pNewGroup;
	*OPCHandle	= (OPCHANDLE)pNewGroup;
	return S_OK;
}


////////////////////////////////////////////////////////////////
// COPCDrvServer::GroupSet()
//
// Assign an Group to a specific handle.
//
// Returns E_INVALIDARG if an argument is invalid (NULL pointer, etc)
// Returns S_OK otherwise.
//
////////////////////////////////////////////////////////////////
HRESULT COPCDrvServer::GroupSet(COPCDrvGroup	*pGroup)
{
	// Make sure that we have a valid Group pointer
	//
	if (NULL == pGroup)
	{
		return E_INVALIDARG;
	}

	// Save the OPC handle
	//
	pGroup->m_ServerGroupHandle = (OPCHANDLE)pGroup;

	// Add Group to map	
	//
	Lock();
	m_mapGroup.SetAt(pGroup->m_ServerGroupHandle, pGroup);
	m_dwNumGroups++;
	UnLock();

	return S_OK;
}


////////////////////////////////////////////////////////////////
// COPCDrvServer::GroupFree()
//
// This function will remove the Group key from the Group map,
// but the caller is expected to free the memory associated with
// the object (can be done by calling Release()). However, with the
// bForce set to TRUE, the function will free the memory
// regardless as to how many connections are referencing it. This
// is used when an Group is removed by force.
//
// Note: We don't perform a Lock()/UnLock() here because the 
//		 parent server is locked out by the caller.
//
// Returns E_INVALIDARG if an argument is invalid (NULL pointer, etc)
// Returns S_OK otherwise.
//
////////////////////////////////////////////////////////////////
HRESULT COPCDrvServer::GroupFree(OPCHANDLE		OPCHandle, 
								 BOOL			bForce)
{
	COPCDrvGroup		*pTempGroup;
	DWORD				dwCurrentCount;


	if (m_mapGroup.Lookup(OPCHandle, pTempGroup) != TRUE)
	{
		// Group key not found
		return E_INVALIDARG;
	}

	// Remove the group first, then release it
	//
	m_mapGroup.RemoveKey(OPCHandle);
	dwCurrentCount = pTempGroup->Release();

	if ((dwCurrentCount > 0) && (TRUE == bForce))
	{
		// The force flag is set, so delete it regardless
		//
		delete pTempGroup;
	}

	m_dwNumGroups--;
	return S_OK;
}


////////////////////////////////////////////////////////////////
// COPCDrvServer::FreeAllGroups()
//
// This function will remove all of the Groups in a server.
//
// Note: We don't perform a Lock()/UnLock() here because the 
//		 parent server is locked out by the caller.
//
// Returns E_FAIL if GroupFree() fails
// Returns S_OK otherwise.
//
////////////////////////////////////////////////////////////////
HRESULT COPCDrvServer::FreeAllGroups(void)
{
	POSITION		posGroup	= GetFirstGroupPosition();
	DWORD			dwNumGroups	= GetNumGroupHandles();
	HRESULT			hrRet		= S_OK;
	COPCDrvGroup	*pGroup		= NULL;
	OPCHANDLE		OPCHandle	= 0;


	if (posGroup)
	{
		// Free each element in the map
		//
		for (DWORD j = 0; j < dwNumGroups; j++)
		{
			GetNextGroup(posGroup, OPCHandle, pGroup);
			if (FAILED(GroupFree(OPCHandle, TRUE)))
			{
				hrRet = E_FAIL;
			}
		}
	}

	return hrRet;
}


////////////////////////////////////////////////////////////////
// COPCDrvServer::GetFirstGroupPosition()
//
// Returns a POSITION structure indicating where the first
// Group is located in the map
//
////////////////////////////////////////////////////////////////
POSITION COPCDrvServer::GetFirstGroupPosition()
{
	return (m_mapGroup.GetStartPosition());
}


////////////////////////////////////////////////////////////////
// COPCDrvServer::GetNextGroup()
//
// Gets the next Group in the map, relative to the GroupPosition.
//
////////////////////////////////////////////////////////////////
void COPCDrvServer::GetNextGroup(POSITION		&GroupPosition, 
								 OPCHANDLE		&OPCHandle, 
								 COPCDrvGroup	*&pGroup)
{
	m_mapGroup.GetNextAssoc(GroupPosition, OPCHandle, pGroup);
}


⌨️ 快捷键说明

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