108 lines
2.5 KiB
C++
Executable File
108 lines
2.5 KiB
C++
Executable File
/*
|
|
PIP - Platform Independent Primitives
|
|
Abstract input/output device
|
|
Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "piiodevice.h"
|
|
|
|
|
|
PIIODevice::PIIODevice(): PIThread() {
|
|
mode_ = ReadOnly;
|
|
opened_ = init_ = thread_started_ = false;
|
|
reopen_enabled_ = true;
|
|
reopen_timeout_ = 1000;
|
|
ret_func_ = 0;
|
|
ret_data_ = 0;
|
|
buffer_tr.resize(4096);
|
|
CONNECT2(void, void * , int, &timer, timeout, this, check_start);
|
|
init();
|
|
}
|
|
|
|
|
|
PIIODevice::PIIODevice(const PIString & path, PIIODevice::DeviceMode type, bool initNow): PIThread() {
|
|
path_ = path;
|
|
mode_ = type;
|
|
opened_ = init_ = thread_started_ = false;
|
|
reopen_enabled_ = true;
|
|
reopen_timeout_ = 1000;
|
|
ret_func_ = 0;
|
|
ret_data_ = 0;
|
|
buffer_tr.resize(4096);
|
|
CONNECT2(void, void * , int, &timer, timeout, this, check_start);
|
|
if (initNow) init();
|
|
}
|
|
|
|
|
|
void PIIODevice::check_start(void * data, int delim) {
|
|
//cout << "check " << tread_started_ << endl;
|
|
if (open()) {
|
|
thread_started_ = true;
|
|
timer.stop();
|
|
}
|
|
}
|
|
|
|
|
|
void PIIODevice::terminate() {
|
|
thread_started_ = false;
|
|
if (!isInitialized()) return;
|
|
if (isRunning()) {
|
|
stop();
|
|
PIThread::terminate();
|
|
}
|
|
}
|
|
|
|
|
|
void PIIODevice::begin() {
|
|
//cout << " begin\n";
|
|
thread_started_ = false;
|
|
if (!opened_) {
|
|
if (open()) {
|
|
thread_started_ = true;
|
|
//cout << " open && ok\n";
|
|
return;
|
|
}
|
|
} else {
|
|
thread_started_ = true;
|
|
//cout << " ok\n";
|
|
return;
|
|
}
|
|
//init();
|
|
if (!timer.isRunning() && reopen_enabled_) timer.start(reopen_timeout_);
|
|
}
|
|
|
|
|
|
void PIIODevice::run() {
|
|
if (!isReadable()) {
|
|
//cout << "not readable\n";
|
|
stop();
|
|
return;
|
|
}
|
|
if (!thread_started_) {
|
|
msleep(1);
|
|
//cout << "not started\n";
|
|
return;
|
|
}
|
|
|
|
readed_ = read(buffer_tr.data(), buffer_tr.size_s());
|
|
if (readed_ <= 0) {
|
|
msleep(10);
|
|
//cout << readed_ << ", " << errno << ", " << errorString() << endl;
|
|
return;
|
|
}
|
|
threadedRead(buffer_tr.data(), readed_);
|
|
}
|