📄 asyncwriter.cpp
字号:
/************************************************************************
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
* PURPOSE.
************************************************************************/
/************************************************************************
*
* Module: AsyncWriter.h
* Long name: CAsyncWriter class implementation
* Description: Simple class that continuously transmit data using
* asynchronous WRITE requests (based on CVhpdAsyncWriter).
*
* Runtime Env.: Win32
* Author(s): Frank Senf
* Company: Thesycon GmbH, Ilmenau
************************************************************************/
#include "AsyncWriter.h"
// constructor
CAsyncWriter::CAsyncWriter()
{
// init members
m_File = NULL;
m_Pattern = 0x0;
m_ThreadExited = false;
m_BufferError = 0;
m_LastError = VHPD_STATUS_SUCCESS;
} // ctor
// destructor
CAsyncWriter::~CAsyncWriter()
{
CloseFile();
} // dtor
// open the source file
unsigned long CAsyncWriter::OpenFile(const char *Filename)
{
unsigned long retval;
if ( m_File == NULL ) {
// open the file, file must exist
m_File = ::CreateFile(
Filename, // name of file
GENERIC_READ, // access mode
FILE_SHARE_READ,// share mode (0=not shared)
NULL, // security descriptor
OPEN_EXISTING, // how to create
0, // file flags and attributes
NULL // handle to template file
);
if ( m_File == INVALID_HANDLE_VALUE ) {
// opening input file failed
retval = ::GetLastError();
m_File = NULL;
} else {
// file successfully opened
retval = 0;
}
} else {
// an input file was already opened
retval = 1;
}
return retval;
} // OpenFile
// close the source file
void CAsyncWriter::CloseFile()
{
if ( m_File!=NULL ) {
::CloseHandle(m_File);
m_File = NULL;
}
} // CloseFile
// overloaded CVhpdThread::TerminateThread() function
// NOTE 1: called in the context of the main thread
// NOTE 2: TerminateThread() is overloaded to ensure that the
// thread will exit. This is done by trying to terminate the
// thread until it really terminates. For future version of VHPDLib
// this will be implemented by the base class
void CAsyncWriter::TerminateThread()
{
// cancel all pending notification buffers, this forces the worker thread
// to resume from a wait state and check for termination
AbortIoBuffers();
// now wait for the thread to terminate
if ( mThreadHandle != NULL ) {
// loop until thread has terminated
for (;;) {
// wait on thread handle, 40 ms timeout
DWORD err = WaitForSingleObject(mThreadHandle,40);
if ( err==WAIT_OBJECT_0 ) {
// handle is signaled, done
break;
}
if ( err==WAIT_TIMEOUT ) {
// wait timed out
// cancel all pending notification buffers again, should force the worker thread
// to resume from the wait state and check for termination
AbortIoBuffers();
// loop and wait again
continue;
}
// neither WAIT_OBJECT_0 nor WAIT_TIMEOUT returned -> error during wait
break;
} // for()
}
} // TerminateThread
// overloaded CVhpdThread::OnThreadExit() function
// NOTE: It is called in the context of the worker thread.
void CAsyncWriter::OnThreadExit()
{
// store new thread state
m_ThreadExited = true;
} // OnThreadExit
// overloaded CVhpdAsyncWriter::ProcessBuffer() function
// NOTE: It is called in the context of the worker thread.
BOOL CAsyncWriter::ProcessBuffer(CVhpdBuf* Buf)
{
if ( m_File == NULL ) {
// no input file, fill buffer with fixed pattern
memset(Buf->Buffer(),m_Pattern,Buf->Size());
// we submit the whole buffer
Buf->mNumberOfBytesToTransfer = Buf->Size();
return TRUE;
} else {
// read data from file
unsigned long Bytecount = 0;
if ( !(::ReadFile(m_File, Buf->Buffer(), Buf->Size(), &Bytecount, NULL)) ) {
// serious error while reading from file
// stop data transfer
return FALSE;
}
// ReadFile returns success (0) and sets number of bytes read to zero if the
// file pointer was beyond EOF
if ( Bytecount == 0 ) {
// end of file reached, stop data transfer
return FALSE;
}
// data successfully read from file, update buffer fill
Buf->mNumberOfBytesToTransfer = Bytecount;
// submit this buffer
return TRUE;
}
} // ProcessBuffer
// overloaded CVhpdAsyncWriter::BufErrorHandler() function
void CAsyncWriter::BufErrorHandler(CVhpdBuf* Buf)
{
m_BufferError++;
m_LastError = Buf->mStatus;
} // BufErrorHandler
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -