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 cmake_policy(SET CMP0017 NEW) # need include() with .cmake
project(pip) project(pip)
set(pip_MAJOR 2) set(pip_MAJOR 2)
set(pip_MINOR 17) set(pip_MINOR 18)
set(pip_REVISION 0) set(pip_REVISION 0)
set(pip_SUFFIX ) set(pip_SUFFIX )
set(pip_COMPANY SHS) set(pip_COMPANY SHS)

View File

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

View File

@@ -175,10 +175,10 @@ public:
int writeCount() const {return write_count;} int writeCount() const {return write_count;}
//! Read one record from BinLog file, with ID = id, if id = 0 than any id will be readed //! 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 //! 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 //! Returns binary log file size
llong logSize() const {return log_size;} llong logSize() const {return log_size;}