内存缓冲区Buffer作为最基础的内存数据包,只包含指针和长度!

Array作为变长内存数据包,可扩大
ByteArray直接继承自Array,脱离模板TArray
This commit is contained in:
nnhy 2016-03-06 11:01:01 +00:00
parent 243de19352
commit b72139033f
37 changed files with 433 additions and 253 deletions

View File

@ -64,12 +64,12 @@ const void* ConfigBlock::Data() const
uint ConfigBlock::CopyTo(Buffer& bs) const
{
if(Size == 0 || Size > bs.Capacity()) return 0;
if(Size == 0 || Size > bs.Length()) return 0;
bs.Copy(Data(), Size);
bs.SetLength(Size);
return bs.Copy(0, Data(), Size);
//bs.SetLength(Size);
return Size;
//return Size;
}
// 构造一个新的配置块
@ -436,7 +436,12 @@ HotConfig& HotConfig::Current()
// 查找配置数据,如果不存在,则清空
auto dat = cfg.Get("Hot");
if(!dat) dat = cfg.Set("Hot", ByteArray((byte)0, sizeof(HotConfig)));
if(!dat)
{
ByteArray bs(sizeof(HotConfig));
bs.Set(0, 0, bs.Length());
dat = cfg.Set("Hot", bs);
}
return *(HotConfig*)dat;
}

View File

@ -90,7 +90,7 @@ void IC74HC165MOR::ReaBit()
byte IC74HC165MOR::Read(byte *buf, byte count)
{
Open();
return _Bs.CopyTo(buf,count);
return _Bs.CopyTo(0, buf, count);
}
byte IC74HC165MOR::Read()

View File

@ -86,6 +86,6 @@ void BufferPort::OnReceive(const Buffer& bs, void* param)
if(Buf.Capacity() > 0)
{
Buf.SetLength(0);
Buf.Copy(bs);
Buf.Copy(0, bs, 0, -1);
}
}

View File

@ -16,7 +16,7 @@ public:
IDataPort* Led; // 指示灯
Buffer Buf; // 用于接收数据的缓冲区,外部在打开前设置大小
ByteArray Buf; // 用于接收数据的缓冲区,外部在打开前设置大小
BufferPort();
~BufferPort();

View File

@ -981,7 +981,7 @@ uint Enc28j60::OnRead(Buffer& bs)
// 从缓冲区中将数据包复制到packet中
ReadBuffer((byte*)bs.GetBuffer(), len);
}
bs.SetLength(len);
//bs.SetLength(len);
// 移动接收缓冲区 读指针
WriteReg(ERXRDPTL, (NextPacketPtr));
WriteReg(ERXRDPTH, (NextPacketPtr) >> 8);

View File

@ -788,7 +788,7 @@ uint NRF24L01::OnRead(Buffer& bs)
rs = 32;
if(DynPayload) rs = ReadReg(RX_PL_WID);
uint len = bs.Capacity();
uint len = bs.Length();
if(rs > len)
{
debug_printf("R24::Read 实际负载%d缓冲区大小%d为了稳定使用缓冲区大小\r\n", rs, len);
@ -796,6 +796,7 @@ uint NRF24L01::OnRead(Buffer& bs)
}
bs.SetLength(rs);
ReadBuf(RD_RX_PLOAD, bs); // 读取数据
//ReadBuf(RD_RX_PLOAD, bs.Sub(rs)); // 读取数据
}
}
@ -807,7 +808,11 @@ uint NRF24L01::OnRead(Buffer& bs)
if(rs && Led) Led->Write(1000);
// 微网指令特殊处理长度
if(FixData) FixData(&bs);
if(FixData)
{
rs = FixData(bs);
bs.SetLength(rs);
}
#if RF_DEBUG
/*debug_printf("R24::Read [%d]=", bs.Length());

View File

@ -169,11 +169,11 @@ bool ShunCom::OnWriteEx(const Buffer& bs, void* opt)
if(!AddrLength || !opt) return OnWrite(bs);
// 加入地址
ByteArray bs2;
bs2.Copy(opt, AddrLength);
bs2.Copy(0, opt, AddrLength);
//debug_printf("zigbee发送\r\n");
//bs2.Show();
bs2.Copy(bs, AddrLength);
bs2.Copy(AddrLength, bs, 0, -1);
bs2.Show();
return OnWrite(bs2);
}
@ -399,8 +399,6 @@ void ShunComMessage::Write(Stream& ms) const
}
ByteArray ShunComMessage::ToArray() const
{
//MemoryStream ms;
@ -422,7 +420,7 @@ ByteArray ShunComMessage::ToArray(Stream& ms)
void ShunComMessage::Set(ushort kind, const Buffer& bs)
{
Kind = kind;
bs.CopyTo(Data);
bs.CopyTo(0, Data, -1);
Length = 2 + 2 + bs.Length();
}

View File

@ -134,14 +134,14 @@ void UBlox::OnReceive(const Buffer& bs, void* param)
if(bs[0] == '$' && (Header == NULL || str.StartsWith(Header)))
{
Buf.SetLength(0);
Buf.Copy(bs, Buf.Length());
Buf.Copy(0, bs, 0, Buf.Length());
}
else
{
// 不合适的数据,可以直接附加在后面
if(Buf.Length() != 0)
{
Buf.Copy(bs, Buf.Length());
Buf.Copy(0, bs, 0, Buf.Length());
}
}
}

View File

@ -38,7 +38,8 @@ public:
// 电源等级变更(如进入低功耗模式)时调用
virtual void ChangePower(int level);
Action FixData;// 修正数据的委托
typedef int (*FuncBufInt)(const Buffer&);
FuncBufInt FixData;// 修正数据的委托
IDataPort* Led; // 数据灯
byte Status;

View File

@ -27,7 +27,7 @@ int DataStore::Write(uint offset, const Buffer& bs)
if(!OnHook(offset, size, 0)) return -1;
// 从数据区读取数据
uint rs = Data.Copy(bs, offset);
uint rs = Data.Copy(offset, bs, 0, -1);
// 执行钩子函数
if(!OnHook(offset, size, 1)) return -1;
@ -48,7 +48,9 @@ int DataStore::Read(uint offset, Buffer& bs)
if(!OnHook(offset, size, 2)) return -1;
// 从数据区读取数据
return bs.Copy(Data.GetBuffer() + offset, size);
//return bs.Copy(Data.GetBuffer() + offset, size);
//return Data.CopyTo(offset, bs, size);
return bs.Copy(0, Data, offset, size);
}
bool DataStore::OnHook(uint offset, uint size, int mode)

View File

@ -15,7 +15,7 @@ Message::Message(byte code)
void Message::SetData(const Array& bs, uint offset)
{
Length = bs.Length() + offset;
if(Length > 0 && bs.GetBuffer() != Data + offset) bs.CopyTo(Data + offset, Length);
if(Length > 0 && bs.GetBuffer() != Data + offset) bs.CopyTo(0, Data + offset, Length);
}
void Message::SetError(byte errorCode, const char* msg)

View File

@ -42,7 +42,7 @@ void WeakStore::Init()
debug_printf("初始化 0x%08X幻数 %s\r\n", Data.GetBuffer(), Magic);
Data.Clear();
Data.Copy((byte*)Magic, MagicLength);
Data.Copy(0, (byte*)Magic, MagicLength);
}
// 重载索引运算符[],定位到数据部分的索引。

View File

@ -349,7 +349,7 @@ IPAddress DNS::Query(const String& domain, int msTimeout)
Buffer bs(buf, ArrayLength(buf));
Buffer rs(buf, ArrayLength(buf));
// 同时作为响应缓冲区,别浪费了
rs.SetLength(0);
//rs.SetLength(0);
_Buffer = &rs;
dns_makequery(0, domain, bs);
@ -385,7 +385,7 @@ void DNS::Process(Buffer& bs, const IPEndPoint& server)
if(server.Address != Host.DNSServer) return;
if(_Buffer)
_Buffer->Copy(bs);
_Buffer->Copy(0, bs, 0, -1);
else
{
#if NET_DEBUG

View File

@ -371,7 +371,7 @@ typedef struct _DHCP_OPT
{
Option = option;
Length = bs.Length();
bs.CopyTo(&Data);
bs.CopyTo(0, &Data, -1);
return this;
}

View File

@ -9,7 +9,7 @@
IPAddress::IPAddress(const byte* ips)
{
Buffer((byte*)ips, 4).CopyTo(&Value, 4);
Buffer((byte*)ips, 4).CopyTo(0, &Value, 4);
}
IPAddress::IPAddress(byte ip1, byte ip2, byte ip3, byte ip4)
@ -19,7 +19,7 @@ IPAddress::IPAddress(byte ip1, byte ip2, byte ip3, byte ip4)
IPAddress::IPAddress(const Buffer& arr)
{
arr.CopyTo(&Value, 4);
arr.CopyTo(0, &Value, 4);
}
bool IPAddress::IsAny() const { return Value == 0; }
@ -45,14 +45,14 @@ const IPAddress& IPAddress::Broadcast()
IPAddress& IPAddress::operator=(const byte* v)
{
Buffer((byte*)v, 4).CopyTo(&Value, 4);
Buffer((byte*)v, 4).CopyTo(0, &Value, 4);
return *this;
}
IPAddress& IPAddress::operator=(const Buffer& arr)
{
arr.CopyTo(&Value, 4);
arr.CopyTo(0, &Value, 4);
return *this;
}
@ -74,7 +74,7 @@ ByteArray IPAddress::ToArray() const
void IPAddress::CopyTo(byte* ips) const
{
if(ips) Buffer((byte*)&Value, 4).CopyTo(ips, 4);
if(ips) Buffer((byte*)&Value, 4).CopyTo(0, ips, 4);
}
String& IPAddress::ToStr(String& str) const
@ -117,7 +117,7 @@ IPEndPoint::IPEndPoint(const Buffer& arr)
IPEndPoint& IPEndPoint::operator=(const Buffer& arr)
{
Address = arr;
arr.CopyTo(&Port, 2, 4);
arr.CopyTo(4, &Port, 2);
return *this;
}
@ -129,14 +129,14 @@ ByteArray IPEndPoint::ToArray() const
// 要复制数据,而不是直接使用指针,那样会导致外部修改内部数据
ByteArray bs(&Address.Value, 4, true);
bs.Copy(&Port, 2, 4);
bs.Copy(4, &Port, 2);
return bs;
}
void IPEndPoint::CopyTo(byte* ips) const
{
if(ips) ToArray().CopyTo(ips, 6);
if(ips) ToArray().CopyTo(0, ips, 6);
}
String& IPEndPoint::ToStr(String& str) const
@ -218,14 +218,14 @@ MacAddress& MacAddress::operator=(ulong v)
MacAddress& MacAddress::operator=(const byte* buf)
{
Buffer((byte*)buf, 6).CopyTo(&Value, 6);
Buffer((byte*)buf, 6).CopyTo(0, &Value, 6);
return *this;
}
MacAddress& MacAddress::operator=(const Buffer& arr)
{
arr.CopyTo(&Value, 6);
arr.CopyTo(0, &Value, 6);
return *this;
}
@ -247,7 +247,7 @@ ByteArray MacAddress::ToArray() const
void MacAddress::CopyTo(byte* macs) const
{
if(macs) Buffer((byte*)&Value, 6).CopyTo(macs, 6);
if(macs) Buffer((byte*)&Value, 6).CopyTo(0, macs, 6);
}
String& MacAddress::ToStr(String& str) const

View File

@ -66,36 +66,39 @@ uint Queue::Write(const Buffer& bs)
4
*/
byte* buf = (byte*)bs.GetBuffer();
//byte* buf = (byte*)bs.GetBuffer();
uint len = bs.Length();
uint rs = 0;
while(true)
{
// 计算这一个循环剩下的位置
uint remain = _s.Capacity() - _head;
uint remain = _s.Length() - _head;
// 如果要写入的数据足够存放
if(len <= remain)
{
_s.Copy(buf, len, _head);
//_s.Copy(buf, len, _head);
_s.Copy(_head, bs, rs, len);
rs += len;
_head += len;
if(_head >= _s.Capacity()) _head -= _s.Capacity();
if(_head >= _s.Length()) _head -= _s.Length();
break;
}
// 否则先写一段,指针回到开头
_s.Copy(buf, remain, _head);
buf += remain;
//_s.Copy(buf, remain, _head);
_s.Copy(_head, bs, rs, remain);
//buf += remain;
len -= remain;
rs += remain;
_head = 0;
}
SmartIRQ irq;
_size += rs;
{
SmartIRQ irq;
_size += rs;
}
return rs;
}
@ -113,7 +116,7 @@ uint Queue::Read(Buffer& bs)
uint len = bs.Length();
if(!len) return 0;
byte* buf = (byte*)bs.GetBuffer();
//byte* buf = (byte*)bs.GetBuffer();
if(len > _size) len = _size;
@ -125,7 +128,9 @@ uint Queue::Read(Buffer& bs)
// 如果要读取的数据都在这里
if(len <= remain)
{
_s.CopyTo(buf, len, _tail);
//_s.CopyTo(buf, len, _tail);
//_s.CopyTo(_tail, bs, rs, len);
bs.Copy(rs, _s, _tail, len);
rs += len;
_tail += len;
if(_tail >= _s.Capacity()) _tail -= _s.Capacity();
@ -134,17 +139,21 @@ uint Queue::Read(Buffer& bs)
}
// 否则先读一段,指针回到开头
_s.CopyTo(buf, remain, _tail);
buf += remain;
//_s.CopyTo(buf, remain, _tail);
//_s.CopyTo(_tail, bs, rs, remain);
bs.Copy(rs, _s, _tail, remain);
//buf += remain;
len -= remain;
rs += remain;
_tail = 0;
}
bs.SetLength(rs, false);
//bs.SetLength(rs, false);
SmartIRQ irq;
_size -= rs;
{
SmartIRQ irq;
_size -= rs;
}
//if(_size == 0) _tail = _head;
return rs;

View File

@ -458,7 +458,7 @@ void aesEncrypt(byte* buffer, byte* chainBlock )
CopyBytes( chainBlock, buffer, BLOCKSIZE );
}
ByteArray AES::Encrypt(const Array& data, const Array& pass)
ByteArray AES::Encrypt(const Buffer& data, const Buffer& pass)
{
byte buf[KeyLength];
ByteArray box(buf, KeyLength);
@ -478,7 +478,7 @@ ByteArray AES::Encrypt(const Array& data, const Array& pass)
ByteArray rs;
//rs.SetLength(data.Length());
rs.Copy(data);
rs.Copy(0, data, 0, -1);
// 加密
aesEncrypt(rs.GetBuffer(), box.GetBuffer());
@ -486,7 +486,7 @@ ByteArray AES::Encrypt(const Array& data, const Array& pass)
return rs;
}
ByteArray AES::Decrypt(const Array& data, const Array& pass)
ByteArray AES::Decrypt(const Buffer& data, const Buffer& pass)
{
byte buf[KeyLength];
ByteArray box(buf, KeyLength);
@ -509,7 +509,7 @@ ByteArray AES::Decrypt(const Array& data, const Array& pass)
ByteArray rs;
//rs.SetLength(data.Length());
rs.Copy(data);
rs.Copy(0, data, 0, -1);
// 解密
aesDecrypt(rs.GetBuffer(), box.GetBuffer());

View File

@ -8,8 +8,8 @@ class AES
{
public:
// 加解密
static ByteArray Encrypt(const Array& data, const Array& pass);
static ByteArray Decrypt(const Array& data, const Array& pass);
static ByteArray Encrypt(const Buffer& data, const Buffer& pass);
static ByteArray Decrypt(const Buffer& data, const Buffer& pass);
};
#endif

View File

@ -19,7 +19,7 @@ bool BlockStorage::Read(uint address, Buffer& bs) const
st_printf("BlockStorage::Read(0x%08x, %d, 0x%08x)\r\n", address, len, bs.GetBuffer());
#endif
bs.Copy((byte*)address);
bs.Copy(0, (byte*)address, -1);
return true;
}
@ -96,13 +96,13 @@ bool BlockStorage::Write(uint address, const Buffer& bs) const
uint blk = addr - offset;
uint size = Block - offset;
// 前段原始数据,中段来源数据,末段原始数据
ms.Copy((byte*)blk, offset, 0);
ms.Copy(0, (byte*)blk, offset);
if(size > remain) size = remain;
ms.Copy(pData, size, offset);
ms.Copy(offset, pData, size);
int offset2 = offset + size;
int last = Block - offset2;
if(last > 0) ms.Copy((byte*)(blk + offset2), last, offset2);
if(last > 0) ms.Copy(offset2, (byte*)(blk + offset2), last);
// 整块擦除,然后整体写入
//if(!IsErased(blk + offset, size)) Erase(blk, Block);
@ -130,8 +130,8 @@ bool BlockStorage::Write(uint address, const Buffer& bs) const
{
// 前段来源数据,末段原始数据
//ms.SetPosition(0);
ms.Copy(pData, remain, 0);
ms.Copy((byte*)(addr + remain), Block - remain, remain);
ms.Copy(0, pData, remain);
ms.Copy(remain, (byte*)(addr + remain), Block - remain);
//if(!IsErased(addr, remain)) Erase(addr, Block);
Erase(addr, remain);
@ -214,14 +214,14 @@ bool BlockStorage::IsErased(uint address, uint len) const
bool CharStorage::Read(uint address, Buffer& bs) const
{
bs.Copy((byte*)address);
bs.Copy(0, (byte*)address, -1);
return true;
}
bool CharStorage::Write(uint address, const Buffer& bs) const
{
bs.CopyTo((byte*)address);
bs.CopyTo(0, (byte*)address, -1);
return true;
}

View File

@ -220,25 +220,25 @@ uint Stream::ReadArray(Buffer& bs)
uint len = ReadEncodeInt();
if(!len) return 0;
if(len > bs.Capacity())
if(len > bs.Length())
{
// 在设计时,如果取得的长度超级大,可能是设计错误
if(len > 0x40)
{
debug_printf(" 读 %d > %d ", len, bs.Capacity());
debug_printf(" 读 %d > %d ", len, bs.Length());
//assert_param2(len <= bs.Capacity(), "缓冲区大小不足");
//bs.Set(0, 0, len);
/*// 即使缓冲区不够大,也不要随便去重置,否则会清空别人的数据
// 这里在缓冲区不够大时,有多少读取多少
len = bs.Capacity();*/
// 为了避免错误数据导致内存溢出,限定最大值
if(len > 0x400) len = bs.Capacity();
//if(len > 0x400) len = bs.Capacity();
}
// 如果不是设计错误,那么数组直接扩容
//bs.SetLength(len);
}
// 不管长度太大还是太小,都要设置一下长度,避免读取长度小于数组长度,导致得到一片空数据
bs.SetLength(len);
//bs.SetLength(len);
Read(bs.GetBuffer(), 0, len);

View File

@ -472,7 +472,7 @@ char& String::operator[](int index)
return buffer[index];
}
char String::operator[]( int index ) const
char String::operator[](int index) const
{
if (index >= len || !buffer) return 0;
return buffer[index];

View File

@ -134,7 +134,7 @@ public:
bool StartsWith(const char* str, int startIndex = 0) const;
bool EndsWith(const String& str) const;
bool EndsWith(const char* str) const;
typedef void (*StringItem)(const String& item);
int Split(const String& str, StringItem callback);
@ -165,6 +165,8 @@ protected:
void move(String& rhs);
};
#define R(str) String(str)
//String operator+(const char* str1, const char* str2);
//String operator+(const char* str, const Object& obj);
//String operator+(const Object& obj, const char* str);

View File

@ -20,7 +20,7 @@ bool DataMessage::ReadData(const DataStore& ds)
}
// 读取数据
bool DataMessage::ReadData(const Array& bs)
bool DataMessage::ReadData(const Buffer& bs)
{
if(!_Dest) return false;
@ -54,7 +54,7 @@ bool DataMessage::WriteData(DataStore& ds, bool withData)
Length = _Src.Remain();
if(_Dest && !Write(ds.Data.Length() - Offset)) return false;
Array dat(_Src.Current(), Length);
Buffer dat(_Src.Current(), Length);
ds.Write(Offset, dat);
// 如果携带数据,则把这一段数据附加到后面
@ -64,7 +64,7 @@ bool DataMessage::WriteData(DataStore& ds, bool withData)
}
// 写入数据
bool DataMessage::WriteData(Array bs, bool withData)
bool DataMessage::WriteData(Buffer& bs, bool withData)
{
TS("DataMessage::WriteData");
@ -72,8 +72,9 @@ bool DataMessage::WriteData(Array bs, bool withData)
Length = _Src.Remain();
if(_Dest && !Write(bs.Length() - Offset)) return false;
Array dat(_Src.Current(), Length);
bs.Copy(dat, Offset);
//Buffer dat(_Src.Current(), Length);
//bs.Copy(Offset, dat, 0, -1);
bs.Copy(Offset, _Src.Current(), Length);
// 如果携带数据,则把这一段数据附加到后面
if(_Dest && withData) _Dest->Write(bs.GetBuffer(), Offset, Length);

View File

@ -17,8 +17,8 @@ public:
bool ReadData(const DataStore& ds);
bool WriteData(DataStore& ds, bool withData);
bool ReadData(const Array& bs);
bool WriteData(Array bs, bool withData);
bool ReadData(const Buffer& bs);
bool WriteData(Buffer& bs, bool withData);
private:
Stream _Src;

View File

@ -3,7 +3,12 @@
/******************************** Device ********************************/
Device::Device()
Device::Device() :
HardID(_HardID, ArrayLength(HardID)),
Mac(_Mac, ArrayLength(_Mac)),
Name(_Name, ArrayLength(_Name)),
Pass(_Pass, ArrayLength(_Pass)),
Store(_Store, ArrayLength(_Store))
{
Address = 0;
Logined = false;
@ -22,10 +27,15 @@ Device::Device()
OfflineTime = 0;
SleepTime = 0;
ArrayZero(Mac);
/*ArrayZero(Mac);
ArrayZero(Name);
ArrayZero(Pass);
ArrayZero(Store);
ArrayZero(Store);*/
HardID.Clear();
Mac.Clear();
Name.Clear();
Pass.Clear();
Store.Clear();
Cfg = NULL;
@ -64,7 +74,7 @@ void Device::WriteMessage(Stream& ms) const
ms.Write(Address);
ms.Write(Kind);
ms.WriteArray(CArray(HardID));
ms.WriteArray(HardID);
ms.Write(LastTime);
ms.Write(Version);
@ -75,7 +85,7 @@ void Device::WriteMessage(Stream& ms) const
ms.Write(OfflineTime);
ms.Write(PingTime);
ms.WriteArray(CArray(Name));
ms.WriteString(Name);
// 计算并设置大小
byte size = ms.Position() - p;
@ -89,7 +99,8 @@ void Device::ReadMessage(Stream& ms)
Address = ms.ReadByte();
Kind = ms.ReadUInt16();
ms.ReadArray().CopyTo(HardID, ArrayLength(HardID));
//ms.ReadArray().CopyTo(0, HardID, ArrayLength(HardID));
HardID = ms.ReadArray();
LastTime= ms.ReadUInt32();
Version = ms.ReadUInt16();
@ -101,8 +112,9 @@ void Device::ReadMessage(Stream& ms)
PingTime = ms.ReadUInt16();
//ms.ReadString().CopyTo(Name, ArrayLength(Name));
String str(Name, ArrayLength(Name));
str = ms.ReadString();
//String str(Name, ArrayLength(Name));
//str = ms.ReadString();
Name = ms.ReadString();
// 最后位置
ms.SetPosition(p + size);
@ -126,7 +138,7 @@ String& Device::ToStr(String& str) const
str += " Hard=";
str.Concat(HardID[0], 16);
str.Concat(HardID[1], 16);
str = str + " Mac=" + ByteArray(Mac, 6);
str = str + " Mac=" + Mac;
DateTime dt;
dt.Parse(LastTime);
@ -137,11 +149,11 @@ String& Device::ToStr(String& str) const
if(len > 1 && len <= 32)
{
str = str + " ";
ByteArray(Store, len).ToStr(str);
ByteArray(Store.GetBuffer(), len).ToStr(str);
}
len = strlen(Name);
if(len)
//len = strlen(Name);
if(Name.Length() > 0)
{
str += "\t";
//String(Name, len).ToStr(str);
@ -161,7 +173,7 @@ bool operator!=(const Device& d1, const Device& d2)
return d1.Address != d2.Address;
}
Array Device::GetHardID() { return Array(HardID, ArrayLength(HardID)); }
/*Array Device::GetHardID() { return Array(HardID, ArrayLength(HardID)); }
Array Device::GetName() { return Array(Name, ArrayLength(Name)); }
Array Device::GetPass() { return Array(Pass, ArrayLength(Pass)); }
Array Device::GetStore() { return Array(Store, ArrayLength(Store)); }
@ -177,4 +189,4 @@ void Device::SetHardID(const Array& arr) { arr.CopyTo(HardID, ArrayLength(HardID
void Device::SetName(const Array& arr) { arr.CopyTo(Name, ArrayLength(Name)); }
void Device::SetPass(const Array& arr) { arr.CopyTo(Pass, ArrayLength(Pass)); }
void Device::SetStore(const Array& arr) { arr.CopyTo(Store, ArrayLength(Store)); }
void Device::SetConfig(const Array& arr) { arr.CopyTo(Cfg, sizeof(Cfg[0])); }
void Device::SetConfig(const Array& arr) { arr.CopyTo(Cfg, sizeof(Cfg[0])); }*/

View File

@ -16,7 +16,7 @@ public:
byte Address; // 节点地址
ushort Kind; // 类型
byte HardID[16]; // 硬件编码
byte _HardID[16]; // 硬件编码
uint LastTime; // 活跃时间。秒
uint RegTime; // 注册时间。秒
uint LoginTime; // 登录时间。秒
@ -30,12 +30,12 @@ public:
ushort OfflineTime;// 离线阀值时间。秒
ushort PingTime; // 心跳时间。秒
byte Mac[6]; // 无线物理地址
char Name[16]; // 名称
byte _Mac[6]; // 无线物理地址
char _Name[16]; // 名称
//String Name; //变长名称
byte Pass[8]; // 通信密码
byte _Pass[8]; // 通信密码
byte Store[32]; // 数据存储区
byte _Store[32]; // 数据存储区
// 以下字段不存Flash
TinyConfig* Cfg;
@ -43,6 +43,13 @@ public:
uint LastRead; // 最后读取数据的时间。秒
//uint LastWrite; // 最后写入数据的时间。毫秒
Buffer HardID;
Buffer Mac;
String Name;
Buffer Pass;
Buffer Store;
//Buffer Config;
Device();
// 序列化到消息数据流
@ -58,7 +65,7 @@ public:
bool CanSleep() const { return SleepTime > 0; }
bool Valid() const;
Array GetHardID();
/*Array GetHardID();
Array GetName();
Array GetPass();
Array GetStore();
@ -74,7 +81,7 @@ public:
void SetName(const Array& arr);
void SetPass(const Array& arr);
void SetStore(const Array& arr);
void SetConfig(const Array& arr);
void SetConfig(const Array& arr);*/
#if DEBUG
virtual String& ToStr(String& str) const;

View File

@ -20,25 +20,25 @@ void PingMessage::Write(Stream& ms) const
}
// 0x01 主数据
void PingMessage::ReadData(Stream& ms, Array& bs) const
void PingMessage::ReadData(Stream& ms, Buffer& bs) const
{
TS("PingMessage::ReadData");
byte offset = ms.ReadByte();
byte len = ms.ReadByte();
int remain = bs.Capacity() - offset;
int remain = bs.Length() - offset;
int len2 = len;
if(len2 > remain) len2 = remain;
// 保存一份到缓冲区
if(len2 > 0)
{
bs.Copy(ms.ReadBytes(len), len2, offset);
bs.Copy(offset, ms.ReadBytes(len), len2);
}
}
// 写入数据。同时写入头部大小,否则网关不知道数据区大小和配置区大小
void PingMessage::WriteData(Stream& ms, byte code, const Array& bs) const
void PingMessage::WriteData(Stream& ms, byte code, const Buffer& bs) const
{
TS("PingMessage::WriteData");
@ -52,19 +52,20 @@ void PingMessage::WriteData(Stream& ms, byte code, const Array& bs) const
ms.Write((byte)0x00); // 起始地址
ms.Write(len); // 长度
ms.Write(Array(bs.GetBuffer(), len));
ms.Write(bs.Sub(len));
}
// 0x03 硬件校验
bool PingMessage::ReadHardCrc(Stream& ms, const Device* dv, ushort& crc) const
bool PingMessage::ReadHardCrc(Stream& ms, const Device& dv, ushort& crc) const
{
crc = ms.ReadUInt16();
ushort crc1 = Crc::Hash16(dv->GetHardID());
ushort crc1 = Crc::Hash16(dv.HardID);
if(crc != crc1)
{
debug_printf("设备硬件Crc: %04X, 本地Crc%04X \r\n", crc, crc1);
debug_printf("设备硬件ID: ");
ByteArray(dv->HardID, ArrayLength(dv->HardID)).Show(true);
//ByteArray(dv->HardID, ArrayLength(dv->HardID)).Show(true);
dv.HardID.Show();
return false;
}

View File

@ -19,15 +19,15 @@ public:
virtual void Write(Stream& ms) const;
// 0x01 主数据
void ReadData(Stream& ms, Array& bs) const;
void WriteData(Stream& ms, byte code, const Array& bs) const;
void ReadData(Stream& ms, Buffer& bs) const;
void WriteData(Stream& ms, byte code, const Buffer& bs) const;
// 0x02 配置数据
/*void ReadConfig(Stream& ms, Array& bs);
void WriteConfig(Stream& ms, const Array& bs);*/
// 0x03 硬件校验
bool ReadHardCrc(Stream& ms, const Device* dv, ushort& crc) const;
bool ReadHardCrc(Stream& ms, const Device& dv, ushort& crc) const;
void WriteHardCrc(Stream& ms, ushort crc) const;
// 0x04 时间

View File

@ -56,16 +56,17 @@ void Setup(ushort code, const char* name, COM message, int baudRate)
Config::Current = &Config::CreateFlash();
}
void Fix2401(void* param)
int Fix2401(const Buffer& bs)
{
auto& bs = *(Buffer*)param;
//auto& bs = *(Buffer*)param;
// 微网指令特殊处理长度
uint rs = bs.Length();
if(rs >= 8)
{
rs = bs[5] + 8;
if(rs < bs.Length()) bs.SetLength(rs);
//if(rs < bs.Length()) bs.SetLength(rs);
}
return rs;
}
ITransport* Create2401(SPI spi_, Pin ce, Pin irq, Pin power, bool powerInvert, IDataPort* led)

View File

@ -282,7 +282,7 @@ void TinyClientTask(void* param)
client->Report(offset, bs[offset]);
else
{
auto bs2 = ByteArray(&bs[offset],len);
auto bs2 = ByteArray(&bs[offset], len);
client->Report(offset, bs2);
}
}
@ -319,7 +319,8 @@ void TinyClient::Join()
// 组网版本不是系统版本,而是为了做新旧版本组网消息兼容的版本号
//dm.Version = Sys.Version;
dm.Kind = Type;
dm.HardID.Copy(Sys.ID, 16);
//dm.HardID.Copy(Sys.ID, 16);
((Buffer&)dm.HardID) = Sys.ID;
dm.TranID = TranID;
dm.WriteMessage(msg);
//dm.Show(true);
@ -371,7 +372,7 @@ bool TinyClient::OnJoin(const TinyMessage& msg)
// 服务端组网密码,退网使用
//Cfg->Mac[0] = dm.HardID.Length();
//dm.HardID.Save(Cfg->Mac, ArrayLength(Cfg->Mac));
dm.HardID.CopyTo(Cfg->Mac);
dm.HardID.CopyTo(0, Cfg->Mac, -1);
#if DEBUG
debug_printf("组网成功!网关 0x%02X 分配 0x%02X ,频道:%d传输速率%dkbps密码", dm.Server, dm.Address, dm.Channel, Cfg->Speed);

View File

@ -49,7 +49,8 @@ bool TinyServer::Send(Message& msg) const
{
auto dv = FindDevice(((TinyMessage&)msg).Dest);
if(!dv) dv = Current;
if(dv) msg.State = dv->Mac;
//if(dv) msg.State = dv->Mac;
if(dv) dv->Mac.CopyTo(0, msg.State, -1);
}
return Control->Send(msg);
@ -263,13 +264,15 @@ bool TinyServer::OnJoin(const TinyMessage& msg)
// 节点注册
dv->RegTime = now;
dv->Kind = dm.Kind;
dv->SetHardID(dm.HardID);
//dv->SetHardID(dm.HardID);
dv->HardID = dm.HardID;
dv->Version = dm.Version;
dv->LoginTime = now;
// 生成随机密码。当前时间的MD5
auto bs = MD5::Hash(Array(&now, 8));
if(bs.Length() > 8) bs.SetLength(8);
dv->SetPass(bs);
//auto bs = MD5::Hash(Buffer(&now, 8));
//if(bs.Length() > 8) bs.SetLength(8);
//dv->SetPass(bs);
dv->Pass = MD5::Hash(Buffer(&now, 8));
// 保存无线物理地址
auto st = (byte*)msg.State;
@ -280,9 +283,11 @@ bool TinyServer::OnJoin(const TinyMessage& msg)
if(sum == 0 || sum == 0xFF * 5) st = NULL;
}
if(!st)
memcpy(dv->Mac, dv->HardID, 6);
//memcpy(dv->Mac, dv->HardID, 6);
dv->Mac = dv->HardID;
else
memcpy(dv->Mac, st, 6);
//memcpy(dv->Mac, st, 6);
dv->Mac = st;
if(dv->Valid())
{
@ -313,12 +318,14 @@ bool TinyServer::OnJoin(const TinyMessage& msg)
dm.Channel = Cfg->Channel;
dm.Speed = Cfg->Speed / 10;
dm.Address = dv->Address;
dm.Password.Copy(dv->GetPass());
//dm.Password.Copy(dv->GetPass());
((Buffer&)dm.Password) = dv->Pass;
dm.HardID.Set(Sys.ID, 6);
//dm.HardID.Set(Sys.ID, 6);
((Buffer&)dm.HardID) = Sys.ID;
dm.WriteMessage(rs);
rs.State = dv->Mac;
rs.State = dv->_Mac;
//Control->Send(rs);
// 组网消息属于广播消息很可能丢包重发3次
for(int i=0; i<3; i++)
@ -345,7 +352,7 @@ bool TinyServer::OnDisjoin(const TinyMessage& msg)
// 拿出来硬件ID的校验检查是否合法
auto ms = msg.ToStream();
ushort crc1 = ms.ReadUInt16();
ushort crc2 = Crc::Hash16(dv->GetHardID());
ushort crc2 = Crc::Hash16(dv->HardID);
if(crc1 == crc2)
{
debug_printf("TinyServer::OnDisjoin:0x%02X \r\n", dv->Address);
@ -382,7 +389,7 @@ bool TinyServer::Disjoin(byte id)
TS("TinyServer::Disjoin");
auto dv = FindDevice(id);
auto crc = Crc::Hash16(dv->GetHardID());
auto crc = Crc::Hash16(dv->HardID);
TinyMessage msg;
@ -419,8 +426,8 @@ bool TinyServer::OnPing(const TinyMessage& msg)
{
case 0x01:
{
auto bs = dv->GetStore();
pm.ReadData(ms, bs);
//auto bs = dv->GetStore();
pm.ReadData(ms, dv->Store);
// 更新读取时间
dv->LastRead = Sys.Seconds();
@ -429,14 +436,14 @@ bool TinyServer::OnPing(const TinyMessage& msg)
}
case 0x02:
{
auto bs = dv->GetConfig();
pm.ReadData(ms, bs);
//auto bs = dv->GetConfig();
//pm.ReadData(ms, bs);
break;
}
case 0x03:
{
ushort crc = 0;
if(!pm.ReadHardCrc(ms, dv, crc))
if(!pm.ReadHardCrc(ms, *dv, crc))
{
Disjoin(rs, crc);
return false;
@ -481,11 +488,11 @@ bool TinyServer::OnRead(const Message& msg, Message& rs, const Device& dv)
bool rt = true;
if(dm.Offset < 64)
rt = dm.ReadData(dv.GetStore());
rt = dm.ReadData(dv.Store);
else if(dm.Offset < 128)
{
dm.Offset -= 64;
rt = dm.ReadData(dv.GetConfig());
//rt = dm.ReadData(dv.GetConfig());
}
rs.Error = !rt;
@ -513,11 +520,11 @@ bool TinyServer::OnWrite(const Message& msg, Message& rs, Device& dv)
bool rt = true;
if(dm.Offset < 64)
rt = dm.WriteData(dv.GetStore(), false);
rt = dm.WriteData(dv.Store, false);
else if(dm.Offset < 128)
{
dm.Offset -= 64;
rt = dm.WriteData(dv.GetConfig(), false);
//rt = dm.WriteData(dv.GetConfig(), false);
}
rs.Error = !rt;
@ -548,11 +555,11 @@ bool TinyServer::OnWriteReply(const Message& msg, Device& dv)
DataMessage dm(msg, NULL);
if(dm.Offset < 64)
dm.WriteData(dv.GetStore(), false);
dm.WriteData(dv.Store, false);
else if(dm.Offset < 128)
{
dm.Offset -= 64;
dm.WriteData(dv.GetConfig(), false);
//dm.WriteData(dv.GetConfig(), false);
}
return true;
@ -601,13 +608,13 @@ void GetDeviceKey(byte scr,Array& key,void* param)
key.Copy(dv->Pass, 8);*/
}
Device* TinyServer::FindDevice(const Array& hardid) const
Device* TinyServer::FindDevice(const Buffer& hardid) const
{
if(hardid.Length() == 0) return NULL;
for(int i=0; i<Devices.Length(); i++)
{
if(Devices[i]!=NULL&&hardid == Devices[i]->GetHardID()) return Devices[i];
if(Devices[i] != NULL && hardid == Devices[i]->HardID) return Devices[i];
}
return NULL;
}
@ -756,7 +763,7 @@ void TinyServer::ClearDevices()
TinyMessage rs;
rs.Dest = dv->Address;
ushort crc = Crc::Hash16(dv->GetHardID());
ushort crc = Crc::Hash16(dv->HardID);
Disjoin(rs, crc);
}
}

View File

@ -35,7 +35,7 @@ public:
TArray<Device*> Devices;
Device* FindDevice(byte id) const;
Device* FindDevice(const Array& hardid) const;
Device* FindDevice(const Buffer& hardid) const;
bool DeleteDevice(byte id);
int LoadDevices();

View File

@ -60,8 +60,10 @@ void Gateway::Start()
dv->Kind = Sys.Code;
dv->LastTime = Sys.Seconds();
dv->SetHardID(Array(Sys.ID, 16));
dv->SetName(Array(Sys.Name, 0));
//dv->SetHardID(Array(Sys.ID, 16));
//dv->SetName(Array(Sys.Name, 0));
dv->HardID = Sys.ID;
dv->Name = Sys.Name;
Server->Devices.Push(dv);
Server->SaveDevices();
@ -420,7 +422,7 @@ bool Gateway::DeviceProcess(const Message& msg)
return false;
}
ushort crc = Crc::Hash16(dv->GetHardID());
ushort crc = Crc::Hash16(dv->HardID);
Server->Disjoin(id);
Sys.Sleep(300);
Server->Disjoin(id);

View File

@ -182,16 +182,17 @@ void Token::Setup(ushort code, const char* name, COM message, int baudRate)
Config::Current = &Config::CreateFlash();
}
void Fix2401(void* param)
int Fix2401(const Buffer& bs)
{
auto& bs = *(Buffer*)param;
//auto& bs = *(Buffer*)param;
// 微网指令特殊处理长度
uint rs = bs.Length();
if(rs >= 8)
{
rs = bs[5] + 8;
if(rs < bs.Length()) bs.SetLength(rs);
//if(rs < bs.Length()) bs.SetLength(rs);
}
return rs;
}
ITransport* Token::Create2401(SPI spi_, Pin ce, Pin irq, Pin power, bool powerInvert, IDataPort* led)

View File

@ -255,7 +255,7 @@ bool TokenClient::OnHello(TokenMessage& msg, Controller* ctrl)
// 使用系统ID作为Name
ext2.Name = TokenConfig::Current->User;
// 使用系统ID作为Key
ext2.Key.Copy(Sys.ID, 16);
ext2.Key.Copy(0, Sys.ID, 16);
//auto ctrl3 = dynamic_cast<TokenController*>(ctrl);
//if(ctrl3) ctrl3->Key = ext2.Key;

236
Type.cpp
View File

@ -65,59 +65,181 @@ const String Type::Name() const
/******************************** Buffer ********************************/
// 复制数组。深度克隆,拷贝数据,自动扩容
int Buffer::Copy(const void* data, int len, int index)
Buffer::Buffer(void* p, int len)
{
assert_param(len >= 0);
//assert_param2(_canWrite, "禁止CopyData修改");
assert_param2(data, "Buffer::Copy data Error");
//assert_param2(p || !p && len == 0, "Buffer空指针时不能指定长度");
assert_param2(p && len > 0, "Buffer构造指针不能为空");
//if(len < 0) len = MemLen(data);
_Arr = p;
_Length = len;
//_Capacity = len;
}
// 检查长度是否足够
int len2 = index + len;
//CheckCapacity(len2, index);
Buffer::Buffer(const Buffer& buf)
{
_Arr = buf._Arr;
_Length = buf._Length;
}
Buffer::Buffer(Buffer&& rval)
{
*this = rval;
}
Buffer& Buffer::operator = (const Buffer& rhs)
{
Copy(0, rhs, 0, -1);
return *this;
}
Buffer& Buffer::operator = (const void* p)
{
if(p) Copy(0, p, -1);
return *this;
}
Buffer& Buffer::operator = (Buffer&& rval)
{
_Arr = rval._Arr;
_Length = rval._Length;
rval._Arr = nullptr;
rval._Length = 0;
return *this;
}
// 重载索引运算符[],返回指定元素的第一个字节
byte Buffer::operator[](int i) const
{
assert_param2(i >= 0 && i < _Length, "下标越界");
byte* buf = (byte*)_Arr;
return buf[i];
}
byte& Buffer::operator[](int i)
{
assert_param2(i >= 0 && i < _Length, "下标越界");
byte* buf = (byte*)_Arr;
return buf[i];
}
/*byte* Buffer::GetBuffer() { return (byte*)_Arr; }
const byte* Buffer::GetBuffer() const { return (byte*)_Arr; }
int Buffer::Length() const { return _Length; }
int Buffer::Capacity() const { return _Capacity; }*/
//bool Buffer::Empty() const { return _Arr && _Length > 0; }
bool Buffer::Empty() const { return _Length == 0; }
bool Buffer::SetLength(int len, bool bak)
{
if(len > _Length) return false;
_Length = len;
return true;
}
// 拷贝数据,默认-1长度表示当前长度
int Buffer::Copy(int destIndex, const void* src, int len)
{
if(!src) return 0;
int remain = _Length - destIndex;
if(remain <= 0) return 0;
if(len < 0 || len > remain) len = remain;
// 拷贝数据
memcpy((byte*)_Arr + index, data, len);
// 扩大长度
if(len2 > _Length) _Length = len2;
if(len) memcpy((byte*)_Arr + destIndex, src, len);
return len;
}
// 复制数组。深度克隆,拷贝数据
int Buffer::Copy(const Buffer& arr, int index)
// 拷贝数据,默认-1长度表示两者最小长度
int Buffer::Copy(int destIndex, const Buffer& src, int srcIndex, int len)
{
//assert_param2(_canWrite, "禁止CopyArray修改数据");
if(&arr == this) return 0;
if(arr.Length() == 0) return 0;
if(&src == this) return _Length;
return Copy(arr._Arr, arr.Length(), index);
}
// 把当前数组复制到目标缓冲区。未指定长度len时复制全部
int Buffer::CopyTo(void* data, int len, int index) const
{
assert_param2(data, "Buffer::CopyTo data Error");
// 数据长度可能不足
if(_Length - index < len || len <= 0) len = _Length - index;
int remain = src._Length - srcIndex;
if(len > remain) len = remain;
if(len <= 0) return 0;
return Copy(destIndex, (byte*)src._Arr + srcIndex, src._Length);
}
// 把数据复制到目标缓冲区,默认-1长度表示当前长度
int Buffer::CopyTo(int srcIndex, void* data, int len) const
{
//assert_param2(data, "Buffer::CopyTo data Error");
if(!data) return 0;
int remain = _Length - srcIndex;
if(remain <= 0) return 0;
if(len < 0 || len > remain) len = remain;
// 拷贝数据
memcpy(data, (byte*)_Arr + index, len);
if(len) memcpy(data, (byte*)_Arr + srcIndex, len);
return len;
}
// 用指定字节设置初始化一个区域
void Buffer::Set(byte item, int index, int len)
{
int remain = _Length - index;
if(remain <= 0) return;
if(len < 0 || len > remain) len = remain;
if(len) memset((byte*)_Arr + index, item, len);
}
void Buffer::Clear()
{
Set(0, 0, _Length);
}
// 截取一个子缓冲区
Buffer Buffer::Sub(int len)
{
assert_param2(len <= _Length, "len <= _Length");
return Buffer(_Arr, len);
}
const Buffer Buffer::Sub(int len) const
{
assert_param2(len <= _Length, "len <= _Length");
return Buffer(_Arr, len);
}
bool operator == (const Buffer& bs1, const Buffer& bs2)
{
if(bs1.Length() != bs2.Length()) return false;
return memcmp(bs1._Arr, bs2._Arr, bs1.Length()) == 0;
}
bool operator != (const Buffer& bs1, const Buffer& bs2)
{
if(bs1.Length() != bs2.Length()) return true;
return memcmp(bs1._Arr, bs2._Arr, bs1.Length()) != 0;
}
/******************************** TArray ********************************/
// 数组长度
//int Array::Length() const { return _Length; }
// 数组最大容量。初始化时决定,后面不允许改变
//int Array::Capacity() const { return _Capacity; }
int Array::Capacity() const { return _Capacity; }
// 缓冲区。按字节指针返回
//byte* Array::GetBuffer() const { return (byte*)_Arr; }
@ -132,27 +254,27 @@ int MemLen(const void* data)
return len;
}
Array::Array(void* data, int len)
Array::Array(void* data, int len) : Buffer(data, len)
{
_Size = 1;
if(len < 0) len = MemLen(data);
//if(len < 0) len = MemLen(data);
_Arr = data;
_Length = len;
//_Arr = data;
//_Length = len;
_Capacity = len;
_needFree = false;
_canWrite = true;
}
Array::Array(const void* data, int len)
Array::Array(const void* data, int len) : Buffer((void*)data, len)
{
_Size = 1;
if(len < 0) len = MemLen(data);
//if(len < 0) len = MemLen(data);
_Arr = (void*)data;
_Length = len;
//_Arr = (void*)data;
//_Length = len;
_Capacity = len;
_needFree = false;
_canWrite = false;
@ -164,7 +286,7 @@ Array::~Array()
if(_needFree && _Arr) delete _Arr;
}
// 重载等号运算符,使用另一个固定数组来初始化
/*// 重载等号运算符,使用另一个固定数组来初始化
Array& Array::operator=(const Array& arr)
{
// 不要自己拷贝给自己
@ -172,10 +294,10 @@ Array& Array::operator=(const Array& arr)
_Length = 0;
Copy(arr);
Copy(0, arr, 0, -1);
return *this;
}
}*/
// 重载等号运算符,使用外部指针、内部长度,用户自己注意安全
/*Array& Array::operator=(const void* data)
@ -409,31 +531,31 @@ void* Array::Copy(void* dst, const void* src, int count)
/******************************** ByteArray ********************************/
ByteArray::ByteArray(const void* data, int length, bool copy) : TArray(0)
ByteArray::ByteArray(const void* data, int length, bool copy) : Array(Arr, ArrayLength(Arr))
{
if(copy)
Copy(data, length);
Copy(0, data, length);
else
Set(data, length);
}
ByteArray::ByteArray(void* data, int length, bool copy) : TArray(0)
ByteArray::ByteArray(void* data, int length, bool copy) : Array(Arr, ArrayLength(Arr))
{
if(copy)
Copy(data, length);
Copy(0, data, length);
else
Set(data, length);
}
// 字符串转为字节数组
ByteArray::ByteArray(String& str) : TArray(0)
ByteArray::ByteArray(String& str) : Array(Arr, ArrayLength(Arr))
{
char* p = str.GetBuffer();
Set((byte*)p, str.Length());
}
// 不允许修改,拷贝
ByteArray::ByteArray(const String& str) : TArray(0)
ByteArray::ByteArray(const String& str) : Array(Arr, ArrayLength(Arr))
{
const char* p = str.GetBuffer();
//Copy((const byte*)p, str.Length());
@ -451,7 +573,7 @@ ByteArray::ByteArray(const String& str) : TArray(0)
// 显示十六进制数据,指定分隔字符
String& ByteArray::ToHex(String& str, char sep, int newLine) const
{
byte* buf = GetBuffer();
auto buf = GetBuffer();
// 拼接在字符串后面
for(int i=0; i < Length(); i++, buf++)
@ -490,7 +612,7 @@ int ByteArray::Load(const void* data, int maxsize)
const byte* p = (const byte*)data;
_Length = p[0] <= maxsize ? p[0] : maxsize;
return Copy(p + 1, _Length);
return Copy(0, p + 1, _Length);
}
// 从普通字节数据组加载,首字节为长度
@ -502,7 +624,7 @@ int ByteArray::Save(void* data, int maxsize) const
int len = _Length <= maxsize ? _Length : maxsize;
p[0] = len;
return CopyTo(p + 1, len);
return CopyTo(0, p + 1, len);
}
String& ByteArray::ToStr(String& str) const
@ -527,7 +649,7 @@ void ByteArray::Show(bool newLine) const
ushort ByteArray::ToUInt16() const
{
byte* p = GetBuffer();
auto p = GetBuffer();
// 字节对齐时才能之前转为目标整数
if(((int)p & 0x01) == 0) return *(ushort*)p;
@ -536,7 +658,7 @@ ushort ByteArray::ToUInt16() const
uint ByteArray::ToUInt32() const
{
byte* p = GetBuffer();
auto p = GetBuffer();
// 字节对齐时才能之前转为目标整数
if(((int)p & 0x03) == 0) return *(uint*)p;
@ -545,7 +667,7 @@ uint ByteArray::ToUInt32() const
ulong ByteArray::ToUInt64() const
{
byte* p = GetBuffer();
auto p = GetBuffer();
// 字节对齐时才能之前转为目标整数
if(((int)p & 0x07) == 0) return *(ulong*)p;
@ -558,27 +680,27 @@ ulong ByteArray::ToUInt64() const
void ByteArray::Write(ushort value, int index)
{
Copy((byte*)&value, sizeof(ushort), index);
Copy(index, (byte*)&value, sizeof(ushort));
}
void ByteArray::Write(short value, int index)
{
Copy((byte*)&value, sizeof(short), index);
Copy(index, (byte*)&value, sizeof(short));
}
void ByteArray::Write(uint value, int index)
{
Copy((byte*)&value, sizeof(uint), index);
Copy(index, (byte*)&value, sizeof(uint));
}
void ByteArray::Write(int value, int index)
{
Copy((byte*)&value, sizeof(int), index);
Copy(index, (byte*)&value, sizeof(int));
}
void ByteArray::Write(ulong value, int index)
{
Copy((byte*)&value, sizeof(ulong), index);
Copy(index, (byte*)&value, sizeof(ulong));
}
/******************************** REV ********************************/

91
Type.h
View File

@ -60,63 +60,58 @@ public:
const String Name() const; // 名称
};
// 内存缓冲区。包装指针和长度
class Buffer : public Object
{
public:
Buffer(void* p = nullptr, int len = 0);
Buffer(const Buffer& buf);
Buffer(Buffer&& rval);
Buffer& operator = (const Buffer& rhs);
Buffer& operator = (const void* p);
Buffer& operator = (Buffer&& rval);
inline byte* GetBuffer() { return (byte*)_Arr; }
inline const byte* GetBuffer() const { return (byte*)_Arr; }
inline int Length() const { return _Length; }
virtual bool SetLength(int len, bool bak = false) { _Length = len; return true; }
inline int Capacity() const { return _Capacity; }
//inline int Capacity() const { return _Capacity; }
bool Empty() const;
Buffer(void* p = nullptr, int len = 0)
{
_Arr = p;
_Length = len;
_Capacity = len;
}
virtual bool SetLength(int len, bool bak = false);
// 重载索引运算符[],返回指定元素的第一个字节
byte& operator[](int i) const
{
assert_param2(_Arr && i >= 0 && i < _Length, "下标越界");
byte operator[](int i) const;
byte& operator[](int i);
byte* buf = (byte*)_Arr;
// 拷贝数据,默认-1长度表示当前长度
int Copy(int destIndex, const void* src, int len);
// 拷贝数据,默认-1长度表示两者最小长度
int Copy(int destIndex, const Buffer& src, int srcIndex, int len);
// 把数据复制到目标缓冲区,默认-1长度表示当前长度
int CopyTo(int srcIndex, void* dest, int len) const;
// 把数据复制到目标缓冲区,默认-1长度表示当前长度
//int CopyTo(int index, const Buffer& arr, int len) const;
return buf[i];
}
// 复制数组。深度克隆,拷贝数据,自动扩容
int Copy(const void* data, int len = -1, int index = 0);
// 复制数组。深度克隆,拷贝数据
int Copy(const Buffer& arr, int index = 0);
// 把当前数组复制到目标缓冲区。未指定长度len时复制全部
int CopyTo(void* data, int len = -1, int index = 0) const;
// 用指定字节设置初始化一个区域
void Set(byte item, int index, int len);
void Clear();
const Buffer Sub(int len) const
{
assert_param2(len <= _Length, "len <= _Length");
// 截取一个子缓冲区
Buffer Sub(int len);
const Buffer Sub(int len) const;
return Buffer(_Arr, len);
}
String ToHex();
friend bool operator == (const Buffer& bs1, const Buffer& bs2);
friend bool operator != (const Buffer& bs1, const Buffer& bs2);
protected:
void* _Arr; // 数据指针
int _Length; // 长度
uint _Capacity; // 最大容量
//uint _Capacity; // 最大容量
};
/*class Buffer : public IBuffer
{
public:
char* Ptr;
int Length;
Buffer(char* p = nullptr, int len = 0);
}*/
// 数组长度
#define ArrayLength(arr) (sizeof(arr)/sizeof(arr[0]))
// 数组清零,固定长度
@ -137,7 +132,7 @@ public:
// 数组长度
//int Length() const;
// 数组最大容量。初始化时决定,后面不允许改变
//int Capacity() const;
int Capacity() const;
// 缓冲区。按字节指针返回
//byte* GetBuffer() const;
@ -145,12 +140,14 @@ public:
Array(const void* data, int len);
// 重载等号运算符,使用另一个固定数组来初始化
Array& operator=(const Array& arr);
//Array& operator=(const Array& arr);
// 重载等号运算符,使用外部指针、内部长度,用户自己注意安全
//Array& operator=(const void* data);
virtual ~Array();
using Buffer::Set;
// 设置数组长度。容量足够则缩小Length否则扩容以确保数组容量足够大避免多次分配内存
virtual bool SetLength(int length, bool bak = false);
// 设置数组元素为指定值,自动扩容
@ -311,15 +308,16 @@ public:
};
// 字节数组
class ByteArray : public TArray<byte>
class ByteArray : public Array
{
public:
ByteArray(int length = 0) : TArray(length) { }
ByteArray(byte item, int length) : TArray(length) { Set(item, 0, length); }
ByteArray(int length = 0) : Array(Arr, length > 0 ? length : ArrayLength(Arr)) { }
ByteArray(byte item, int length) : Array(Arr, length) { Set(item, 0, length); }
// 因为使用外部指针,这里初始化时没必要分配内存造成浪费
ByteArray(const void* data, int length, bool copy = false);
ByteArray(void* data, int length, bool copy = false);
ByteArray(const Array& arr) : TArray(arr.Length()) { Copy(arr, 0); }
ByteArray(const Buffer& arr) : Array(Arr, arr.Length()) { Copy(0, arr, 0, -1); }
ByteArray(const ByteArray& arr) : Array(Arr, arr.Length()) { Copy(0, arr, 0, -1); }
ByteArray(String& str); // 直接引用数据缓冲区
ByteArray(const String& str); // 不允许修改,拷贝
@ -352,6 +350,11 @@ public:
//friend bool operator==(const ByteArray& bs1, const ByteArray& bs2);
//friend bool operator!=(const ByteArray& bs1, const ByteArray& bs2);
protected:
byte Arr[0x40]; // 内部缓冲区
virtual void* Alloc(int len) { return new byte[len]; }
};
// 从数组创建列表