fast/Plugin/Plc/CEXMemFileQueue.h
2025-01-20 10:30:01 +08:00

121 lines
2.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <vector>
#include <string>
#define DATA_FIRST_LINE_SIZE (16) //第一行记录当前文件实际长度
//////////////////////////////////////////////////////////////////////////
// char acSeq[10];
// char acTime[12];
// unsigned char cIndex; // 0或1
// unsigned char acTxRx; // 0:send, 1:recv
// char acID[10];
// unsigned char acFrame;//0:data, 1:remote
// unsigned char acType; //0:standard, 1:extend
// unsigned char cDlc;
// char acData[32];
#define DATA_FILE_SIZE (89*10000) //单个文件字节数
class CEX_QUEUE_FILE_ITEM
{
public:
CEX_QUEUE_FILE_ITEM()
{
lFileSize = DATA_FIRST_LINE_SIZE;
lBeindIndex = 0;
lEndIndex = 0;
hFile = INVALID_HANDLE_VALUE;
hMapFile = NULL;
pBuf = NULL;
}
~CEX_QUEUE_FILE_ITEM()
{
if (NULL != pBuf)
{
FlushViewOfFile(pBuf, lFileSize);
UnmapViewOfFile(pBuf);
}
if (NULL != hMapFile)
{
CloseHandle(hMapFile);
}
if (INVALID_HANDLE_VALUE != hFile)
{
CloseHandle(hFile);
}
}
std::string strFileName;
HANDLE hFile;//文件句柄
HANDLE hMapFile;//文件映射对象
void* pBuf;//文件内容
int lFileSize;//当前文件字节数,包含文件首行的摘要信息(文件实际有效长度)
int lBeindIndex;//当前文件第一个数据在所有数据中的序号
int lEndIndex;//当前文件最后一个数据在所有数据中的序号
};
class CEXMemFileQueue
{
class ThreadLock {
public:
// 构造函数:初始化锁
ThreadLock(CRITICAL_SECTION* cs)
{
m_cs = cs;
EnterCriticalSection(m_cs);
}
~ThreadLock()
{
LeaveCriticalSection(m_cs);
}
private:
CRITICAL_SECTION* m_cs; // 临界区对象
};
public:
CEXMemFileQueue(std::string strDataDir);
~CEXMemFileQueue();
//读取一条数据长度固定为DATA_LINE_SIZE
int ReadItem(void* pData, int lIndex = -1);
//写入一条数据长度固定位DATA_LINE_SIZE
int WriteItem(void* pData);
//强制刷新内存数据到文件中
void FlushWriteFile();
//获取当前消息总条数
int GetItemCount();
int GetAutoSn();
//切换当前日期
void ChangeDataDate(SYSTEMTIME stDate);
//获取有数据的日期列表
std::vector<std::string> GetDataList();
//自动清理历史数据lRecvDay为保留的天数
void AutoCleanData(int lRecvDay);
private:
//数据分文件存储
std::vector<CEX_QUEUE_FILE_ITEM*> m_vecFile;
//当前正在写入的文件节点
CEX_QUEUE_FILE_ITEM* m_pWriteFileItem;
//当前正在读取的文件节点
CEX_QUEUE_FILE_ITEM* m_pReadFileItem;
int m_lReadIndex;//当前读取索引值
int m_lItemCount;//当前总数据条数
std::string m_strBaseDir;//数据文件路径
std::string m_strDataDir;//数据文件路径
CRITICAL_SECTION m_cs;
int m_lAutoSn;
SYSTEMTIME m_stDataDate;
};