?? cudalpipe.cpp
字號:
#include <Cudal.h>
#include "UsbIoPipe.h"
/** \brief Protected constructor (to prevent user instantiation)
*
* The constructor initializes the CudalPipe object by storing parameters passed from
* \ref CudalDongle::CreatePipe(). The pipe is then reset.
*
* \note The constructor should only be called by \ref CudalDongle::CreatePipe().
*
* \param[in] *pUsbIoPipe
* The USBIO pipe object created by \ref CudalDongle::CreatePipe()
* \param[in] endpointIndex
* The same value that was passed to \ref CudalDongle::CreatePipe()
* \param[in] directionIsIn
* The same value that was passed to \ref CudalDongle::CreatePipe()
*/
CudalPipe::CudalPipe(CUsbIoPipe *pUsbIoPipe, BYTE endpointIndex, BOOL directionIsIn) {
// Store the parameters
m_pUsbIoPipe = pUsbIoPipe;
m_endpointIndex = endpointIndex;
m_directionIsIn = directionIsIn;
// Reset the pipe
m_pUsbIoPipe->ResetPipe();
} // CudalPipe
/** \brief Destroys the pipe object
*/
CudalPipe::~CudalPipe() {
m_pUsbIoPipe->AbortPipe();
m_pUsbIoPipe->ResetPipe();
m_pUsbIoPipe->Unbind();
delete m_pUsbIoPipe;
} // ~CudalPipe
/** \brief Submits a read request on the pipe, and waits for it to complete
*
* To achieve high data rates, the size of each transfer should be maximized (up to 4 kB).
* If the operation fails, the pipe is automatically reset. Note that data may be lost upon a timeout!
*
* \param[out] *pData
* Pointer to a caller-provided buffer that receives the data
* \param[in,out] *pLength
* Pointer to a caller-provided varible that initially specifies the size of the data buffer
* (1-4096). If the function returns \c USBIO_ERR_SUCCESS, it indicates the number of valid bytes in
* the data buffer.
* \param[in] timeout
* Specifies a timeout interval, in milliseconds. The default value is 5 seconds. The value
* \c INFINITE makes the interval never elapse. In that case the \ref AbortAll() function
* must be run from another thread to cancel the read request.
*
* \return
* A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be
* \c USBIO_ERR_SUCCESS = 0. If the specified timeout interval is reached, \c USBIO_ERR_TIMEOUT
* is returned.
*
* \sa WriteSync(), CudalPipe::AbortAll
*/
USBIOERR CudalPipe::ReadSync(void *pData, DWORD *pLength, DWORD timeout) {
if (m_directionIsIn) {
USBIOERR result = m_pUsbIoPipe->ReadSync(pData, *pLength, timeout);
if (result != USBIO_ERR_SUCCESS) {
m_pUsbIoPipe->ResetPipe();
}
return result;
} else {
return USBIO_ERR_INVALID_INBUFFER;
}
} // ReadSync
/** \brief Submits a write request on the pipe, and waits for it to complete
*
* To achieve high data rates, the size of each transfer should be maximized (up to 4 kB).
* If the operation fails, the pipe is automatically reset.
*
* \param[out] *pData
* Pointer to the buffer containing the data to be written
* \param[in,out] *pLength
* Pointer to a caller-provided varible that initially specifies the number of bytes to be
* transferred (1-4096). If the function returns \c USBIO_ERR_SUCCESS, it indicates the number of
* bytes actually transferred.
* \param[in] timeout
* Specifies a timeout interval, in milliseconds. The default value is 5 seconds. The value
* \c INFINITE makes the interval never elapse. In that case the \ref AbortAll() function
* must be run from another thread to cancel the write request.
*
* \return
* A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be
* \c USBIO_ERR_SUCCESS = 0. If the specified timeout interval is reached, \c USBIO_ERR_TIMEOUT
* is returned.
*
* \sa CudalPipe::ReadSync, CudalPipe::AbortAll
*/
USBIOERR CudalPipe::WriteSync(void *pData, DWORD *pLength, DWORD timeout) {
if (!m_directionIsIn) {
USBIOERR result = m_pUsbIoPipe->WriteSync(pData, *pLength, timeout);
if (result != USBIO_ERR_SUCCESS) {
m_pUsbIoPipe->ResetPipe();
}
return result;
} else {
return USBIO_ERR_INVALID_OUTBUFFER;
}
} // WriteSync
/** \brief Submits a read request on the pipe, and returns immediately
*
* To achieve high data rates, the size of each transfer should be maximized (up to 4 kB).
*
* \param[in] *pInfo
* Pointer to a caller-provided \ref CudalAsyncInfo object that contains information about the
* asynchronous transfer. Note that each transfer must have it's own \ref CudalAsyncInfo object.
* \param[out] *pData
* Pointer to a caller-provided buffer that receives the data
* \param[in] maxLength
* The maximum number of bytes to be read (1-4096).
*
* \return
* \c TRUE if the operation succeeds, otherwise FALSE
*
* \sa CudalPipe::WaitAsync, CudalPipe::WaitAsyncTermination, CudalPipe::AbortAll
*/
// Submits a read request on the pipe, and returns immediately
BOOL CudalPipe::ReadAsync(CudalAsyncInfo *pInfo, void *pData, DWORD maxLength) {
// Sanity checks
if (maxLength > 4096) return NULL;
// Initialize the transfer structure
pInfo->m_pUsbIoBuf->NumberOfBytesToTransfer = maxLength;
pInfo->m_length = maxLength;
pInfo->m_pData = pData;
// Submit the read request
return m_pUsbIoPipe->Read(pInfo->m_pUsbIoBuf);
} // ReadAsync
/** \brief Submits a write request on the pipe, and returns immediately
*
* To achieve high data rates, the size of each transfer should be maximized (up to 4 kB).
*
* \param[in] *pInfo
* Pointer to a caller-provided \ref CudalAsyncInfo object that contains information about the
* asynchronous transfer. Note that each transfer must have it's own \ref CudalAsyncInfo object.
* \param[out] *pData
* Pointer to the first byte to be written. The data is copied into the \ref CudalAsyncInfo
* structure, so \c *pData can be deleted or modified after this function returns.
* \param[in] length
* The number of bytes to be written (1-4096)
*
* \return
* \c TRUE if the operation succeeds, otherwise FALSE
*
* \sa CudalPipe::WaitAsync, CudalPipe::WaitAsyncTermination, CudalPipe::AbortAll
*/
BOOL CudalPipe::WriteAsync(CudalAsyncInfo *pInfo, void *pData, DWORD length) {
// Sanity checks
if (length > 4096) return NULL;
// Initialize the info
pInfo->m_length = length;
pInfo->m_pData = NULL;
pInfo->m_pUsbIoBuf->NumberOfBytesToTransfer = length;
memcpy(pInfo->m_pUsbIoBuf->Buffer(), pData, length);
// Submit the read request
return m_pUsbIoPipe->Write(pInfo->m_pUsbIoBuf);
} // WriteAsync
/** \brief Waits for an asynchronous read/write operation to complete
*
* This function should be called to check whether or not an asynchronous read/write request has
* finished. If it has not, the function simply returns \c USBIO_ERR_TIMEOUT, and the request remains
* active. To terminate read/write requests, the application must call \ref AbortAll(), followed by a
* call to \ref WaitAsyncTermination() for each pending request.
*
* \param[in] *pInfo
* Pointer to a caller-provided \ref CudalAsyncInfo object that contains information about the
* asynchronous transfer
* \param[out] *pActualLength
* Pointer to a variable that receives the number of bytes actually transferred, provided that the
* return value is \c USBIO_ERR_SUCCESS.
* \param[in] timeout
* Specifies a timeout interval, in milliseconds. The default value is 5 seconds. The value
* \c INFINITE makes the interval never elapse.
*
* \return
* A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be
* \c USBIO_ERR_SUCCESS = 0. For a timeout it is \c USBIO_ERR_TIMEOUT.
*
* \sa CudalPipe::ReadAsync, CudalPipe::WriteAsync, CudalPipe::WaitAsyncTermination, CudalPipe::AbortAll
*/
USBIOERR CudalPipe::WaitAsync(CudalAsyncInfo *pInfo, DWORD *pActualLength, DWORD timeout) {
// Wait for the transfer to complete
USBIOERR result = m_pUsbIoPipe->WaitForCompletion(pInfo->m_pUsbIoBuf, timeout);
// Examine the result
switch (result) {
// Success -> Delete the buffers and return the actual length
case USBIO_ERR_SUCCESS:
if (pActualLength) *pActualLength = pInfo->m_pUsbIoBuf->BytesTransferred;
if (pInfo->m_pData) {
memcpy(pInfo->m_pData, pInfo->m_pUsbIoBuf->Buffer(), pInfo->m_pUsbIoBuf->BytesTransferred);
}
break;
// Timeout -> Nothing happens...
case USBIO_ERR_TIMEOUT:
break;
// If the operation fails, the pipe is reset
default:
m_pUsbIoPipe->ResetPipe();
if (pActualLength) *pActualLength = 0;
break;
}
return result;
} // WaitAsync
/** \brief Aborts all transfers on the pipe
*
* Aborted transfer functions will return \c USBIO_ERR_CANCELED.
*
* \return
* A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be
* \c USBIO_ERR_SUCCESS = 0.
*
* \sa CudalPipe::ReadSync, CudalPipe::WriteSync, CudalPipe::WaitAsync, CudalPipe::WaitAsyncTermination
*/
USBIOERR CudalPipe::AbortAll() {
return m_pUsbIoPipe->AbortPipe();
} // Abort
/** \brief Initialize the USBIO buffer and blanks all other information
*
* The USBIO buffer size is always set to 4096 bytes, which the maximum number for CUDAL transfers
*/
CudalAsyncInfo::CudalAsyncInfo() {
m_pUsbIoBuf = new CUsbIoBuf(4096);
m_pData = NULL;
m_length = 0;
} // CudalAsyncInfo
/** \brief Destroys the USBIO buffer and blanks all other information
*
* Note that a transfer must have been properly terminated before destroying the CudalAsyncInfo object!
*/
CudalAsyncInfo::~CudalAsyncInfo() {
delete m_pUsbIoBuf;
} // ~CudalAsyncInfo
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -