PIBinaryLog thread-safe and RealTime mode patching, now pause and seek available

This commit is contained in:
2021-03-17 22:51:44 +03:00
parent 7bbbd09f42
commit 6e4b808a66
3 changed files with 32 additions and 16 deletions

View File

@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0017 NEW) # need include() with .cmake
project(pip)
set(pip_MAJOR 2)
set(pip_MINOR 17)
set(pip_MINOR 18)
set(pip_REVISION 0)
set(pip_SUFFIX )
set(pip_COMPANY SHS)

View File

@@ -169,7 +169,9 @@ bool PIBinaryLog::threadedRead(uchar *readed, int size) {
// piCout << "binlog threaded read";
if (!canRead() || isEnd()) return PIIODevice::threadedRead(readed, size);
is_thread_ok = false;
PISystemTime pt;
logmutex.lock();
PISystemTime pt, lastrec_timestamp = lastrecord.timestamp;
logmutex.unlock();
double delay;
switch (play_mode) {
case PlayRealTime:
@@ -185,15 +187,15 @@ bool PIBinaryLog::threadedRead(uchar *readed, int size) {
pausemutex.unlock();
pt = PISystemTime::current() - startlogtime;
if (is_started) {
if (lastrecord.timestamp > pt)
(lastrecord.timestamp - pt).sleep();
if (lastrec_timestamp > pt)
(lastrec_timestamp - pt).sleep();
} else {
startlogtime = PISystemTime::current() - lastrecord.timestamp;
startlogtime = PISystemTime::current() - lastrec_timestamp;
is_started = true;
}
break;
case PlayVariableSpeed:
delay = lastrecord.timestamp.toMilliseconds() - play_time;
delay = lastrec_timestamp.toMilliseconds() - play_time;
//piCoutObj << "delay" << delay;
double cdelay;
int dtc;
@@ -218,7 +220,7 @@ bool PIBinaryLog::threadedRead(uchar *readed, int size) {
PISystemTime::fromMilliseconds(cdelay).sleep();
}
} else is_started = true;
play_time = lastrecord.timestamp.toMilliseconds();
play_time = lastrec_timestamp.toMilliseconds();
break;
case PlayStaticDelay:
if (is_started) {
@@ -279,7 +281,10 @@ void PIBinaryLog::setPause(bool pause) {
pausemutex.lock();
is_pause = pause;
if (pause) pause_time = PISystemTime::current();
else pause_time = PISystemTime::current() - pause_time;
else {
if (pause_time > PISystemTime())
pause_time = PISystemTime::current() - pause_time;
}
pausemutex.unlock();
}
@@ -334,15 +339,21 @@ int PIBinaryLog::writeBinLog_raw(int id, const PISystemTime &time, const void *d
}
PIByteArray PIBinaryLog::readBinLog(int id, PISystemTime * time) {
PIByteArray PIBinaryLog::readBinLog(int id, PISystemTime * time, int * readed_id) {
if (!canRead()) return PIByteArray();
logmutex.lock();
BinLogRecord br = readRecord();
logmutex.unlock();
if (br.id == -1) {
piCoutObj << "End of BinLog file";
fileEnd();
return PIByteArray();
}
if (id == 0 && br.id > 0) return br.data;
if (id == 0 && br.id > 0) {
if (time) *time = br.timestamp;
if (readed_id) *readed_id = br.id;
return br.data;
}
while (br.id != id && !isEnd()) br = readRecord();
if (br.id == -1) {
piCoutObj << "End of BinLog file";
@@ -350,8 +361,8 @@ PIByteArray PIBinaryLog::readBinLog(int id, PISystemTime * time) {
return PIByteArray();
}
if (br.id == id) {
if (time)
*time = br.timestamp;
if (time) *time = br.timestamp;
if (readed_id) *readed_id = br.id;
return br.data;
}
piCoutObj << "Can't find record with id =" << id;
@@ -359,9 +370,9 @@ PIByteArray PIBinaryLog::readBinLog(int id, PISystemTime * time) {
}
int PIBinaryLog::readBinLog(int id, void *read_to, int max_size, PISystemTime * time) {
int PIBinaryLog::readBinLog(int id, void *read_to, int max_size, PISystemTime * time, int * readed_id) {
if (max_size <= 0 || read_to == 0) return -1;
PIByteArray ba = readBinLog(id, time);
PIByteArray ba = readBinLog(id, time, readed_id);
if (ba.isEmpty()) return -1;
int sz = piMini(max_size, ba.size());
memcpy(read_to, ba.data(), sz);
@@ -385,6 +396,7 @@ PIByteArray PIBinaryLog::getHeader() {
int PIBinaryLog::readDevice(void *read_to, int max_size) {
PIMutexLocker _ml(logmutex);
if (lastrecord.id == -1 || isEnd()) return 0;
if(!is_thread_ok && lastrecord.id > 0) return lastrecord.data.size();
if (!canRead()) return -1;
@@ -656,8 +668,12 @@ void PIBinaryLog::seekTo(int rindex) {
if (rindex < index.size_s() && rindex >= 0) {
file.seek(index[rindex].pos);
moveIndex(index_pos.value(file.pos(), -1));
double prev_pt = play_time;
play_time = index[rindex].timestamp.toMilliseconds();
lastrecord.timestamp = index[rindex].timestamp;
if (play_mode == PlayRealTime) {
startlogtime = PISystemTime::current() - lastrecord.timestamp;
}
}
// piCoutObj << "seekTo done";
logmutex.unlock();

View File

@@ -175,10 +175,10 @@ public:
int writeCount() const {return write_count;}
//! Read one record from BinLog file, with ID = id, if id = 0 than any id will be readed
PIByteArray readBinLog(int id = 0, PISystemTime * time = 0);
PIByteArray readBinLog(int id = 0, PISystemTime * time = 0, int * readed_id = 0);
//! Read one record from BinLog file, with ID = id, if id = 0 than any id will be readed
int readBinLog(int id, void * read_to, int max_size, PISystemTime * time = 0);
int readBinLog(int id, void * read_to, int max_size, PISystemTime * time = 0, int * readed_id = 0);
//! Returns binary log file size
llong logSize() const {return log_size;}