From f1f5ef36b221638b574fea51e52f24636c52ce4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sat, 15 Apr 2017 14:30:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=B8=8A=E6=8A=A5=E5=92=8C?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E6=9E=B6=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Message/HistoryStore.cpp | 85 +++++++++++++++++----------------------- Message/HistoryStore.h | 10 +++-- 2 files changed, 42 insertions(+), 53 deletions(-) diff --git a/Message/HistoryStore.cpp b/Message/HistoryStore.cpp index 45621fa8..84c1d6cd 100644 --- a/Message/HistoryStore.cpp +++ b/Message/HistoryStore.cpp @@ -2,8 +2,8 @@ #include "HistoryStore.h" -//#define DS_DEBUG DEBUG -#define DS_DEBUG 0 +#define DS_DEBUG DEBUG +//#define DS_DEBUG 0 #if DS_DEBUG #define ds_printf debug_printf #else @@ -17,13 +17,13 @@ HistoryStore::HistoryStore() RenderPeriod = 30; ReportPeriod = 300; - FlashPeriod = 600; + StorePeriod = 600; MaxCache = 16 * 1024; MaxReport = 1024; OnReport = nullptr; - OnFlash = nullptr; + OnStore = nullptr; _task = 0; } @@ -44,7 +44,7 @@ bool HistoryStore::Open() if (Opened) return true; _Report = 0; - _Flash = 0; + _Store = 0; // 定时生成历史数据 30s int p = RenderPeriod * 1000; @@ -93,10 +93,10 @@ void HistoryStore::Reader() _Report -= RenderPeriod; if (_Report <= 0) Report(); - // 自动写入Flash - if (_Flash <= 0) _Flash = FlashPeriod; - _Flash -= RenderPeriod; - if (_Flash <= 0) Flash(); + // 自动写入Store + if (_Store <= 0) _Store = StorePeriod; + _Store -= RenderPeriod; + if (_Store <= 0) Store(); } void HistoryStore::Report() @@ -104,63 +104,50 @@ void HistoryStore::Report() if (!OnReport) return; auto& s = Cache; + int len = s.Length - s.Position(); // 没有数据 - if (s.Position() == s.Length) return; + if (!len) return; // 根据每次最大上报长度,计算可以上报多少条历史记录 int n = MaxReport / Size; - int len = n * Size; + int len2 = n * Size; + if (len2 > len) len2 = len; - ByteArray bs(len); - len = s.Read(bs); - bs.SetLength(len); + ds_printf("HistoryStore::Report %d/%d", len2, len); - // 游标先移回去,成功以后再删除 - s.Seek(-len); - - if (len) { - int len2 = OnReport(this, bs, nullptr); - if (len2) { - s.Seek(len2); - - // 如果缓存里面没有了数据,则从头开始使用 - if (s.Position() == s.Length) { - s.SetPosition(0); - s.Length = 0; - } - } - } + Process(len2, OnReport); } -void HistoryStore::Flash() +void HistoryStore::Store() { - if (!OnFlash) return; + if (!OnStore) return; auto& s = Cache; + int len = s.Length - s.Position(); // 没有数据 - if (s.Position() == s.Length) return; + if (!len) return; - // 根据每次最大上报长度,计算可以上报多少条历史记录 - int n = MaxReport / Size; - int len = n * Size; + ds_printf("HistoryStore::Store %d", len); - ByteArray bs(len); - len = s.Read(bs); - bs.SetLength(len); + Process(len, OnStore); +} - // 游标先移回去,成功以后再删除 - s.Seek(-len); +void HistoryStore::Process(int len, DataHandler handler) +{ + if (!len || !handler) return; - if (len) { - int len2 = OnFlash(this, bs, nullptr); - if (len2) { - s.Seek(len2); + auto& s = Cache; - // 如果缓存里面没有了数据,则从头开始使用 - if (s.Position() == s.Length) { - s.SetPosition(0); - s.Length = 0; - } + Buffer bs(s.GetBuffer() + s.Position(), len); + + int rs = handler(this, bs, nullptr); + if (rs) { + s.Seek(rs); + + // 如果缓存里面没有了数据,则从头开始使用 + if (s.Position() == s.Length) { + s.SetPosition(0); + s.Length = 0; } } } diff --git a/Message/HistoryStore.h b/Message/HistoryStore.h index bfe0f8af..a6443390 100644 --- a/Message/HistoryStore.h +++ b/Message/HistoryStore.h @@ -10,7 +10,7 @@ public: MemoryStream Cache; // 数据 short RenderPeriod; // 生成历史数据周期。默认30s short ReportPeriod; // 上报数据周期。默认300s - short FlashPeriod; // 写入Flash周期。默认600s + short StorePeriod; // 写入Store周期。默认600s short MaxCache; // 缓存最大长度。默认16 * 1024 short MaxReport; // 每次最大上报长度。默认1024 @@ -20,7 +20,7 @@ public: // 数据上报句柄 DataHandler OnReport; // 数据存储句柄 - DataHandler OnFlash; + DataHandler OnStore; // 初始化 HistoryStore(); @@ -39,14 +39,16 @@ private: int Size; short _Report; - short _Flash; + short _Store; uint _task; static void RenderTask(void* param); void Reader(); void Report(); - void Flash(); + void Store(); + + void Process(int len, DataHandler handler); }; /*