diff --git a/Config.cpp b/Config.cpp new file mode 100644 index 00000000..5de5144d --- /dev/null +++ b/Config.cpp @@ -0,0 +1,9 @@ +#include "Config.h" + +// 初始化,各字段为0 +Config::Config() +{ + // 实际内存大小,减去头部大小 + Length = sizeof(this) - (int)&Length - (int)this; + memset(&Length, 0, Length); +} diff --git a/Config.h b/Config.h new file mode 100644 index 00000000..2bfb1804 --- /dev/null +++ b/Config.h @@ -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 diff --git a/Message/DataStore.cpp b/Message/DataStore.cpp new file mode 100644 index 00000000..b30df191 --- /dev/null +++ b/Message/DataStore.cpp @@ -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= 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= 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= 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)) + { + 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; +} diff --git a/Message/DataStore.h b/Message/DataStore.h new file mode 100644 index 00000000..e8d1e120 --- /dev/null +++ b/Message/DataStore.h @@ -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