4.06.2013 - Version 0.3.4 - PIOBJECT() macro, ethernet improvement, documentation based on Doxygen
This commit is contained in:
@@ -20,6 +20,45 @@
|
||||
#include "piiodevice.h"
|
||||
|
||||
|
||||
/*! \class PIIODevice
|
||||
* \brief Base class for input/output classes
|
||||
*
|
||||
* \section PIIODevice_sec0 Synopsis
|
||||
* This class provide open/close logic, threaded read and virtual input/output
|
||||
* functions \a read() and \a write(). You should implement pure virtual
|
||||
* function \a openDevice() in your subclass.
|
||||
*
|
||||
* \section PIIODevice_sec1 Open and close
|
||||
* PIIODevice have boolean variable indicated open status. Returns of functions
|
||||
* \a openDevice() and \a closeDevice() change this variable.
|
||||
*
|
||||
* \section PIIODevice_sec2 Threaded read
|
||||
* PIIODevice based on PIThread, so it`s overload \a run() to exec \a read()
|
||||
* in background thread. If read is successful virtual function \a threadedRead()
|
||||
* is executed. Default implementation of this function execute external static
|
||||
* function set by \a setThreadedReadSlot() with data set by \a setThreadedReadData().
|
||||
* Extrenal static function should have format \n
|
||||
* bool func_name(void * Threaded_read_data, uchar * readed_data, int readed_size)\n
|
||||
* Threaded read starts with function \a startThreadedRead().
|
||||
*
|
||||
* \section PIIODevice_sec3 Internal buffer
|
||||
* PIIODevice have internal buffer for threaded read, and \a threadedRead() function
|
||||
* receive pointer to this buffer in first argument. You can adjust size of this buffer
|
||||
* by function \a setThreadedReadBufferSize() \n
|
||||
* Default size of this buffer is 4096 bytes
|
||||
*
|
||||
* \section PIIODevice_sec4 Reopen
|
||||
* When threaded read is begin its call \a open() if device is closed. While threaded
|
||||
* read running PIIODevice check if device opened every read and if not call \a open()
|
||||
* every reopen timeout if reopen enabled. Reopen timeout is set by \a setReopenTimeout(),
|
||||
* reopen enable is set by \a setReopenEnabled().
|
||||
*
|
||||
* \section PIIODevice_ex0 Example
|
||||
* \snippet piiodevice.cpp 0
|
||||
*/
|
||||
|
||||
|
||||
//! Constructs a empty PIIODevice
|
||||
PIIODevice::PIIODevice(): PIThread() {
|
||||
mode_ = ReadOnly;
|
||||
opened_ = init_ = thread_started_ = false;
|
||||
@@ -27,12 +66,18 @@ PIIODevice::PIIODevice(): PIThread() {
|
||||
reopen_timeout_ = 1000;
|
||||
ret_func_ = 0;
|
||||
ret_data_ = 0;
|
||||
tri = 0;
|
||||
buffer_tr.resize(4096);
|
||||
CONNECT2(void, void * , int, &timer, timeout, this, check_start);
|
||||
CONNECT(void, &write_thread, started, this, write_func);
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
/*! \brief Constructs a PIIODevice with path and mode
|
||||
* \param path path to device
|
||||
* \param type mode for open
|
||||
* \param initNow init or not in constructor */
|
||||
PIIODevice::PIIODevice(const PIString & path, PIIODevice::DeviceMode type, bool initNow): PIThread() {
|
||||
path_ = path;
|
||||
mode_ = type;
|
||||
@@ -41,8 +86,10 @@ PIIODevice::PIIODevice(const PIString & path, PIIODevice::DeviceMode type, bool
|
||||
reopen_timeout_ = 1000;
|
||||
ret_func_ = 0;
|
||||
ret_data_ = 0;
|
||||
tri = 0;
|
||||
buffer_tr.resize(4096);
|
||||
CONNECT2(void, void * , int, &timer, timeout, this, check_start);
|
||||
CONNECT(void, &write_thread, started, this, write_func);
|
||||
if (initNow) init();
|
||||
}
|
||||
|
||||
@@ -56,6 +103,21 @@ void PIIODevice::check_start(void * data, int delim) {
|
||||
}
|
||||
|
||||
|
||||
void PIIODevice::write_func() {
|
||||
while (!write_thread.isStopping()) {
|
||||
while (!write_queue.isEmpty()) {
|
||||
if (write_thread.isStopping()) return;
|
||||
write_thread.lock();
|
||||
PIPair<PIByteArray, ullong> item(write_queue.dequeue());
|
||||
write_thread.unlock();
|
||||
int ret = write(item.first);
|
||||
threadedWriteEvent(item.second, ret);
|
||||
}
|
||||
msleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PIIODevice::terminate() {
|
||||
thread_started_ = false;
|
||||
if (!isInitialized()) return;
|
||||
@@ -103,4 +165,14 @@ void PIIODevice::run() {
|
||||
return;
|
||||
}
|
||||
threadedRead(buffer_tr.data(), readed_);
|
||||
threadedReadEvent(buffer_tr.data(), readed_);
|
||||
}
|
||||
|
||||
|
||||
ullong PIIODevice::writeThreaded(const PIByteArray & data) {
|
||||
write_thread.lock();
|
||||
write_queue.enqueue(PIPair<PIByteArray, ullong>(data, tri));
|
||||
++tri;
|
||||
write_thread.unlock();
|
||||
return tri - 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user