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

📄 opcdrvasynctransaction.cpp

📁 基于Intellution开发包的开发的OPC服务器
💻 CPP
字号:
//	OPCDrvAsynTransaction.cpp
//
//  This file contains the implementation of an asynchronous transaction object.
//
//
//	(c) COPYRIGHT 1996-1998, INTELLUTION INC.
// ALL RIGHTS RESERVED
//
//
//	Functions defined in this module:
//
//			COPCDrvAsyncTrans::COPCDrvAsyncTrans()
//			COPCDrvAsyncTrans::~COPCDrvAsyncTrans()
//			COPCDrvAsyncTrans::Init()
//			COPCDrvAsyncTrans::Cancel()
//
//
//
// Modification Log:
//	Vers	Date     By		Notes
//	----	-------- ---	-----
//	1.3		03/10/98 jra	Created
//
//

#define WIN32_LEAN_AND_MEAN

#include "OpcStdAfx.h"
#include "OPCDrv.h"
#include "OPC.h"


////////////////////////////////////////////////////////////////
// COPCDrvAsyncTrans::COPCDrvAsyncTrans()
//
// @desc	Standard constructor
//
// @parm	COPCDrvGroup * | pParent | Pointer to the parent group
//
// TODO:	Allocated memory, initialize members, etc.
//
////////////////////////////////////////////////////////////////
COPCDrvAsyncTrans::COPCDrvAsyncTrans(COPCDrvGroup *pParent)
{
	m_pHandleList		= NULL;
	m_pvWriteData		= NULL;
	m_dwTransactionID	= 0;
	m_PosInList			= NULL;
	m_pParentGroup		= pParent;

	// Record a reference to the parent object
	//
	if (m_pParentGroup)
	{
		m_pParentGroup->AddRef();
	}
}


////////////////////////////////////////////////////////////////
// COPCDrvAsyncTrans::~COPCDrvAsyncTrans()
//
// @desc	Standard destructor
//
// TODO:	Put any clean-up code here
//
////////////////////////////////////////////////////////////////
COPCDrvAsyncTrans::~COPCDrvAsyncTrans()
{
	// Free any allocated memory
	//
	if (m_pHandleList)
	{
		delete [] m_pHandleList;
	}
	if (m_pvWriteData)
	{
		for (DWORD x = 0; x < m_dwNumHandles; x++)
		{
			VariantClear(&m_pvWriteData[x]);
		}
		delete [] m_pvWriteData;
	}

	// Release or reference to the parent group
	//
	if (m_pParentGroup)
	{
		m_pParentGroup->Release();
	}
}


////////////////////////////////////////////////////////////////
// COPCDrvAsyncTrans::Init()
//
// @desc	Initializes the transaction
//
// @parm	IN			DWORD		| dwTransType	| Transaction type (read, write, etc)
// @parm	IN			DWORD		| dwReadType	| Read Type (device, cache)
// @parm	IN			DWORD		| dwID			| Transaction ID
// @parm	IN			DWORD		| dwNumHandles	| Pending handles to be written
// @parm	IN			OPCHANDLE *	| pHandles		| List of pending handles
// @parm	IN OPTIONAL	VARIANT   *	| pvData		| List of data to write
//
// @retval	S_OK			Success
// @retval	E_INVALIDARG	An invalid parameter was passed
// @retval	E_OUTOFMEMORY	A memory allocator failed
// @retval	E_POINTER		The group pointer passed into the constructor was invalid
//
////////////////////////////////////////////////////////////////
HRESULT COPCDrvAsyncTrans::Init(DWORD			dwTransType,
								OPCDATASOURCE	dwReadType,
								DWORD			dwNumHandles,
								OPCHANDLE		*pdwHandles,
								VARIANT			*pvData)
{
	// Sanity checks...
	//
	if ((NULL == pdwHandles) ||
		((NULL == pvData) && (TRANS_WRITE == dwTransType)))
	{
		return E_INVALIDARG;
	}

	// Make sure the parent group's pointer that was passed into the
	// constructor was good.
	//
	if (NULL == m_pParentGroup)
	{
		return E_POINTER;
	}

	// Save the paticulars
	//
	m_dwTransType	= dwTransType;
	m_dwReadType	= dwReadType;
	m_dwNumHandles	= dwNumHandles;

	// Allocate some memory for the lists
	//
	m_pHandleList = new OPCHANDLE [m_dwNumHandles];
	if (NULL == m_pHandleList)
	{
		return E_OUTOFMEMORY;
	}

	// Determine what type of transaction this is and the action to take
	//
	if (TRANS_WRITE == m_dwTransType)
	{
		m_pvWriteData = new VARIANT [m_dwNumHandles];
		if (NULL == m_pvWriteData)
		{
			delete [] m_pHandleList;
			return E_OUTOFMEMORY;
		}

		// Copy over the data and the handle list
		//
		for (DWORD x = 0; x < m_dwNumHandles; x++)
		{
			m_pHandleList[x] = pdwHandles[x];

			VariantInit(&m_pvWriteData[x]);
			VariantCopy(&m_pvWriteData[x], &pvData[x]);
		}
	}
	else
	{
		// Copy over the handle list
		//
		for (DWORD x = 0; x < m_dwNumHandles; x++)
		{
			m_pHandleList[x] = pdwHandles[x];
		}
	}

	// Get a new transaction id from the server
	//
	m_pParentGroup->m_pParentServer->GenerateTransaction(&m_dwTransactionID);
	return S_OK;
}


////////////////////////////////////////////////////////////////
// COPCDrvAsyncTrans::AddToQueue()
//
// @desc	Adds the write to the parent group's write queue
//
// @parm	none
//
// @retval	S_OK			Success
// @retval	E_INVALIDARG	An unknown transaction type was found
//
////////////////////////////////////////////////////////////////
HRESULT COPCDrvAsyncTrans::AddToQueue(void)
{
	// Add it to the corresponding queue
	//
	switch(m_dwTransType)
	{
	case TRANS_WRITE:
		m_PosInList = m_pParentGroup->m_AsyncWriteQueue.AddTail(this);
		break;

	case TRANS_READ:
	case TRANS_READTIME:
		m_PosInList = m_pParentGroup->m_AsyncReadQueue.AddTail(this);
		break;

	default:
		return E_FAIL;
		break;
	}

	return S_OK;
}


////////////////////////////////////////////////////////////////
// COPCDrvAsyncTrans::RemoveFromQueue()
//
// @desc	Removes the write from the parent group's write queue
//
// @parm	IN OPTIONAL BOOL	| bDelete	| Indicates if the transaction
//											  object should be deleted
//
// @retval	S_OK			Success
// @retval	E_FAIL			The transaction was not found in the queue
// @retval	E_INVALIDARG	An unknown transaction type was found
//
////////////////////////////////////////////////////////////////
HRESULT COPCDrvAsyncTrans::RemoveFromQueue(IN OPTIONAL BOOL		bDelete)	/* = FALSE */
{
	HRESULT hrRet = S_OK;


	// Remove it from the corresponding queue
	//
	switch(m_dwTransType)
	{
	case TRANS_WRITE:
		m_pParentGroup->m_AsyncWriteQueue.RemoveAt(m_PosInList);
		break;

	case TRANS_READ:
	case TRANS_READTIME:
		m_pParentGroup->m_AsyncReadQueue.RemoveAt(m_PosInList);
		break;

	default:
		hrRet = E_INVALIDARG;
		break;
	}

	if (bDelete)
	{
		delete this;
	}
	return hrRet;
}




////////////////////////////////////////////////////////////////
//
// inline methods. These methods are defined and coded in OPCDrv.h
// for speed purposes.
//
////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////
// COPCDrvAsyncTrans::GetTransactionID()
//
// @desc	Returns the generated transaction id
//
// @parm	none
//
// @retval	the tranaction ID
//
////////////////////////////////////////////////////////////////
//DWORD inline COPCDrvAsyncTrans::GetTransactionID(void)
//{
//	return m_dwTransactionID;
//}


////////////////////////////////////////////////////////////////
// COPCDrvAsyncTrans::GetHandleList()
//
// @desc	Returns the list of handles to write
//
// @parm	none
//
// @retval	the handle list
//
////////////////////////////////////////////////////////////////
//OPCHANDLE * inline COPCDrvAsyncTrans::GetHandleList(void)
//{
//	return m_pHandleList;
//}


////////////////////////////////////////////////////////////////
// COPCDrvAsyncTrans::GetDataList()
//
// @desc	Returns the list of data to write
//
// @parm	none
//
// @retval	the data list
//
////////////////////////////////////////////////////////////////
//VARIANT * inline COPCDrvAsyncTrans::GetDataList(void)
//{
//	return m_pvWriteData;
//}


////////////////////////////////////////////////////////////////
// COPCDrvAsyncTrans::GetNumHandles()
//
// @desc	Returns the number of pending handles
//
// @parm	none
//
// @retval	the handle count
//
////////////////////////////////////////////////////////////////
//DWORD inline COPCDrvAsyncTrans::GetNumHandles(void)
//{
//	return m_dwNumHandles;
//}


////////////////////////////////////////////////////////////////
// COPCDrvAsyncTrans::GetListInfo()
//
// @desc	Returns the information associated with the transaction
//
// @parm	DWORD		*  | dwNumHandles	| Returned count
// @parm	OPCHANDLE	** | ppdwHandleList | Returned handle list
// @parm	VARIANT		** | ppvDataList	| Returned data list
//
// @retval	S_OK		Success
//
////////////////////////////////////////////////////////////////
//HRESULT inline COPCDrvAsyncTrans::GetListInfo(DWORD		*dwNumHandles,
//											  OPCHANDLE	**ppdwHandleList,
//											  VARIANT		**ppvDataList)
//{
//	*dwNumHandles	= m_dwNumHandles;
//	*ppdwHandleList = m_pHandleList;
//	*ppvDataList	= m_pvWriteData;
//	return S_OK;
//}


////////////////////////////////////////////////////////////////
// COPCDrvAsyncTrans::Cancel()
//
// @desc	Cancels the pending write
//
// @parm	none
//
// @retval	S_OK			success
//
////////////////////////////////////////////////////////////////
//HRESULT COPCDrvAsyncTrans::Cancel(void)
//{
//	return RemoveFromQueue(TRUE);
//}

⌨️ 快捷键说明

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