增加配置类和数据存储区,编译通过

This commit is contained in:
nnhy 2015-08-17 02:37:17 +00:00
parent 7467a4be62
commit 8fdad9ff34
4 changed files with 195 additions and 0 deletions

9
Config.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "Config.h"
// 初始化各字段为0
Config::Config()
{
// 实际内存大小,减去头部大小
Length = sizeof(this) - (int)&Length - (int)this;
memset(&Length, 0, Length);
}

24
Config.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef __Config_H__
#define __Config_H__
#include "Sys.h"
#include "Stream.h"
// 必须设定为1字节对齐否则offsetof会得到错误的位置
#pragma pack(push) // 保存对齐状态
// 强制结构体紧凑分配空间
#pragma pack(1)
// 配置信息
class Config
{
public:
int Length; // 数据长度
// 初始化各字段为0
Config();
};
#pragma pack(pop) // 恢复对齐状态
#endif

120
Message/DataStore.cpp Normal file
View File

@ -0,0 +1,120 @@
#include "DataStore.h"
// 初始化
DataStore::DataStore()
{
Strict = true;
}
// 写入数据
int DataStore::Write(uint offset, ByteArray& bs)
{
uint size = bs.Length();
if(size == 0) return 0;
// 检查是否越界
if(Strict)
{
if(offset + size > Data.Length()) return -1;
}
// 执行钩子函数
for(int i=0; i<ArrayLength(Areas); i++)
{
Area& ar = Areas[i];
if(ar.Offset == 0 && ar.Size == 0) break;
if(ar.Writing)
{
if (ar.Offset <= offset && offset <= ar.Offset + ar.Size ||
ar.Offset >= offset && ar.Offset <= offset + size)
{
if(!ar.Writing(*this, offset, size, bs)) return -1;
}
}
}
// 从数据区读取数据
uint rs = Data.Copy(bs, offset);
// 执行钩子函数
for(int i=0; i<ArrayLength(Areas); i++)
{
Area& ar = Areas[i];
if(ar.Offset == 0 && ar.Size == 0) break;
if(ar.Wrote)
{
if (ar.Offset <= offset && offset <= ar.Offset + ar.Size ||
ar.Offset >= offset && ar.Offset <= offset + size)
{
ar.Wrote(*this, offset, size, bs);
}
}
}
return rs;
}
// 读取数据
int DataStore::Read(uint offset, ByteArray& bs)
{
uint size = bs.Length();
if(size == 0) return 0;
// 检查是否越界
if(Strict)
{
if(offset + size > Data.Length()) return -1;
}
// 执行钩子函数
for(int i=0; i<ArrayLength(Areas); i++)
{
Area& ar = Areas[i];
if(ar.Offset == 0 && ar.Size == 0) break;
if(ar.Read)
{
if (ar.Offset <= offset && offset <= ar.Offset + ar.Size ||
ar.Offset >= offset && ar.Offset <= offset + size)
{
if(!ar.Read(*this, offset, size, bs)) return -1;
}
}
}
// 从数据区读取数据
return bs.Copy(Data.GetBuffer() + offset, size);
}
// 注册某一块区域的读写钩子函数
void DataStore::Register(uint offset, uint size, Handler writing, Handler wrote, Handler read)
{
// 找一个空位
int i=0;
for(i=0; i<ArrayLength(Areas); i++)
{
if(Areas[i].Offset == 0 && Areas[i].Size == 0) break;
}
if(i >= ArrayLength(Areas))
{
debug_printf("数据存储区的读写钩子函数已满\r\n");
return;
}
Areas[i].Offset = offset;
Areas[i].Size = size;
Areas[i].Writing= writing;
Areas[i].Wrote = wrote;
Areas[i].Read = read;
}
DataStore::Area::Area()
{
Offset = 0;
Size = 0;
Writing = NULL;
Wrote = NULL;
Read = NULL;
}

42
Message/DataStore.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef __DataStore_H__
#define __DataStore_H__
#include "Sys.h"
#include "Stream.h"
// 数据存储适配器
class DataStore
{
public:
ByteArray Data; // 数据
bool Strict; // 是否严格限制存储区读写不许越界。默认true
// 初始化
DataStore();
// 写入数据
int Write(uint offset, ByteArray& bs);
// 读取数据
int Read(uint offset, ByteArray& bs);
typedef bool (*Handler)(DataStore& ds, uint offset, uint size, ByteArray& bs);
class Area
{
public:
uint Offset;
uint Size;
Handler Writing; // 写入之前
Handler Wrote; // 写入之后
Handler Read; // 读取之前
Area();
};
Area Areas[0x10];
// 注册某一块区域的读写钩子函数
void Register(uint offset, uint size, Handler writing, Handler wrote, Handler read);
};
#endif