分离出来内存缓冲区Buffer,编译通过,未测试!

This commit is contained in:
Stone 2016-03-06 02:14:53 +00:00
parent 6c55562e70
commit 243de19352
55 changed files with 379 additions and 268 deletions

View File

@ -27,17 +27,17 @@ struct ConfigBlock
const ConfigBlock* Next() const; const ConfigBlock* Next() const;
const void* Data() const; const void* Data() const;
uint CopyTo(Array& bs) const; uint CopyTo(Buffer& bs) const;
bool Init(const char* name, const Array& bs); bool Init(const char* name, const Buffer& bs);
bool Write(const Storage& storage, uint addr, const Array& bs); bool Write(const Storage& storage, uint addr, const Buffer& bs);
bool Remove(const Storage& storage, uint addr); bool Remove(const Storage& storage, uint addr);
}; };
ushort ConfigBlock::GetHash() const ushort ConfigBlock::GetHash() const
{ {
// 计算头部 CRC。从数据CRC开始包括大小和名称 // 计算头部 CRC。从数据CRC开始包括大小和名称
return Crc::Hash16(Array(&Size, sizeof(*this) - offsetof(ConfigBlock, Size))); return Crc::Hash16(Buffer((byte*)&Size, sizeof(*this) - offsetof(ConfigBlock, Size)));
} }
bool ConfigBlock::Valid() const bool ConfigBlock::Valid() const
@ -62,7 +62,7 @@ const void* ConfigBlock::Data() const
return (const void*)&this[1]; return (const void*)&this[1];
} }
uint ConfigBlock::CopyTo(Array& bs) const uint ConfigBlock::CopyTo(Buffer& bs) const
{ {
if(Size == 0 || Size > bs.Capacity()) return 0; if(Size == 0 || Size > bs.Capacity()) return 0;
@ -73,7 +73,7 @@ uint ConfigBlock::CopyTo(Array& bs) const
} }
// 构造一个新的配置块 // 构造一个新的配置块
bool ConfigBlock::Init(const char* name, const Array& bs) bool ConfigBlock::Init(const char* name, const Buffer& bs)
{ {
if(name == NULL) return false; if(name == NULL) return false;
//assert_param2(name, "配置块名称不能为空"); //assert_param2(name, "配置块名称不能为空");
@ -100,7 +100,7 @@ bool ConfigBlock::Init(const char* name, const Array& bs)
} }
// 更新块 // 更新块
bool ConfigBlock::Write(const Storage& storage, uint addr, const Array& bs) bool ConfigBlock::Write(const Storage& storage, uint addr, const Buffer& bs)
{ {
TS("ConfigBlock::Write"); TS("ConfigBlock::Write");
@ -115,12 +115,12 @@ bool ConfigBlock::Write(const Storage& storage, uint addr, const Array& bs)
// 先写入头部,然后写入数据 // 先写入头部,然后写入数据
uint len = sizeof(ConfigBlock) - offsetof(ConfigBlock, Hash); uint len = sizeof(ConfigBlock) - offsetof(ConfigBlock, Hash);
if(!storage.Write(addr, Array(&Hash, len))) return false; if(!storage.Write(addr, Buffer(&Hash, len))) return false;
if(bs.Length() > 0) if(bs.Length() > 0)
{ {
uint len2 = bs.Length(); uint len2 = bs.Length();
if(len2 > Size) len2 = Size; if(len2 > Size) len2 = Size;
if(!storage.Write(addr + len, Array(bs.GetBuffer(), len2))) return false; if(!storage.Write(addr + len, bs.Sub(len2))) return false;
} }
return true; return true;
@ -135,7 +135,7 @@ bool ConfigBlock::Remove(const Storage& storage, uint addr)
// 写入头部 // 写入头部
uint len = sizeof(ConfigBlock) - offsetof(ConfigBlock, Hash); uint len = sizeof(ConfigBlock) - offsetof(ConfigBlock, Hash);
return storage.Write(addr, Array(&Hash, len)); return storage.Write(addr, Buffer(&Hash, len));
} }
/*================================ 配置 ================================*/ /*================================ 配置 ================================*/
@ -157,7 +157,7 @@ bool CheckSignature(const Storage& st, uint& addr, bool create)
{ {
if(!create) return false; if(!create) return false;
st.Write(addr, Array(&c_Version, sizeof(c_Version))); st.Write(addr, Buffer((byte*)&c_Version, sizeof(c_Version)));
} }
addr += sizeof(c_Version); addr += sizeof(c_Version);
@ -255,7 +255,7 @@ bool Config::Remove(const char* name) const
} }
// 根据名称更新块 // 根据名称更新块
const void* Config::Set(const char* name, const Array& bs) const const void* Config::Set(const char* name, const Buffer& bs) const
{ {
TS("Config::Set"); TS("Config::Set");
@ -276,7 +276,7 @@ const void* Config::Set(const char* name, const Array& bs) const
} }
// 获取配置数据 // 获取配置数据
bool Config::Get(const char* name, Array& bs) const bool Config::Get(const char* name, Buffer& bs) const
{ {
TS("Config::Get"); TS("Config::Get");
@ -303,7 +303,7 @@ const void* Config::Get(const char* name) const
} }
/*// 获取配置数据,如果不存在则覆盖 /*// 获取配置数据,如果不存在则覆盖
bool Config::GetOrSet(const char* name, Array& bs) const bool Config::GetOrSet(const char* name, Buffer& bs) const
{ {
TS("Config::GetOrSet"); TS("Config::GetOrSet");
@ -358,14 +358,14 @@ uint ConfigBase::Size() const
return (uint)_End - (uint)_Start; return (uint)_End - (uint)_Start;
} }
Array ConfigBase::ToArray() Buffer ConfigBase::ToArray()
{ {
return Array(_Start, Size()); return Buffer(_Start, Size());
} }
const Array ConfigBase::ToArray() const const Buffer ConfigBase::ToArray() const
{ {
return Array(_Start, Size()); return Buffer(_Start, Size());
} }
void ConfigBase::Init() void ConfigBase::Init()

View File

@ -25,11 +25,11 @@ public:
// 删除。仅清空名称,并不删除数据区 // 删除。仅清空名称,并不删除数据区
bool Remove(const char* name) const; bool Remove(const char* name) const;
// 设置配置数据 // 设置配置数据
const void* Set(const char* name, const Array& bs) const; const void* Set(const char* name, const Buffer& bs) const;
// 获取配置数据 // 获取配置数据
bool Get(const char* name, Array& bs) const; bool Get(const char* name, Buffer& bs) const;
// 获取配置数据,如果不存在则覆盖 // 获取配置数据,如果不存在则覆盖
//bool GetOrSet(const char* name, Array& bs) const; //bool GetOrSet(const char* name, Buffer& bs) const;
// 获取配置数据 // 获取配置数据
const void* Get(const char* name) const; const void* Get(const char* name) const;
@ -70,8 +70,8 @@ protected:
uint Size() const; uint Size() const;
Array ToArray(); Buffer ToArray();
const Array ToArray() const; const Buffer ToArray() const;
}; };
// 必须设定为1字节对齐否则offsetof会得到错误的位置 // 必须设定为1字节对齐否则offsetof会得到错误的位置

View File

@ -56,7 +56,7 @@ byte AT24CXX::Read(ushort addr)
return IIC->Read(addr & 0xFF); return IIC->Read(addr & 0xFF);
} }
bool AT24CXX::Write(uint addr, const Array& bs) const bool AT24CXX::Write(uint addr, const Buffer& bs) const
{ {
if(!IIC) return false; if(!IIC) return false;
@ -65,7 +65,7 @@ bool AT24CXX::Write(uint addr, const Array& bs) const
return IIC->Write((ushort)addr, bs); return IIC->Write((ushort)addr, bs);
} }
bool AT24CXX::Read(uint addr, Array& bs) const bool AT24CXX::Read(uint addr, Buffer& bs) const
{ {
if(!IIC) return false; if(!IIC) return false;

View File

@ -18,8 +18,8 @@ public:
bool Write(ushort addr, byte data); bool Write(ushort addr, byte data);
byte Read(ushort addr); byte Read(ushort addr);
virtual bool Write(uint addr, const Array& bs) const; virtual bool Write(uint addr, const Buffer& bs) const;
virtual bool Read(uint addr, Array& bs) const; virtual bool Read(uint addr, Buffer& bs) const;
}; };
#endif #endif

View File

@ -1,7 +1,7 @@
#include "BufferPort.h" #include "BufferPort.h"
#include "SerialPort.h" #include "SerialPort.h"
BufferPort::BufferPort() : Buffer((void*)NULL, 0) BufferPort::BufferPort() : Buf()
{ {
Name = "BufferPort"; Name = "BufferPort";
Port = NULL; Port = NULL;
@ -36,14 +36,14 @@ bool BufferPort::Open()
isNew = true; isNew = true;
} }
if(Buffer.Capacity() == 0) if(Buf.Capacity() == 0)
{ {
debug_printf("未指定缓冲区大小,默认分配 256 字节!\r\n"); debug_printf("未指定缓冲区大小,默认分配 256 字节!\r\n");
Buffer.SetLength(256); Buf.SetLength(256);
//return false; //return false;
} }
Buffer.SetLength(0); Buf.SetLength(0);
if(isNew && Com != COM_NONE) if(isNew && Com != COM_NONE)
{ {
@ -73,7 +73,7 @@ void BufferPort::Close()
Opened = false; Opened = false;
} }
uint BufferPort::OnReceive(ITransport* transport, Array& bs, void* param, void* param2) uint BufferPort::OnReceive(ITransport* transport, Buffer& bs, void* param, void* param2)
{ {
auto bp = (BufferPort*)param; auto bp = (BufferPort*)param;
if(bp) bp->OnReceive(bs, param2); if(bp) bp->OnReceive(bs, param2);
@ -81,11 +81,11 @@ uint BufferPort::OnReceive(ITransport* transport, Array& bs, void* param, void*
return 0; return 0;
} }
void BufferPort::OnReceive(const Array& bs, void* param) void BufferPort::OnReceive(const Buffer& bs, void* param)
{ {
if(Buffer.Capacity() > 0) if(Buf.Capacity() > 0)
{ {
Buffer.SetLength(0); Buf.SetLength(0);
Buffer.Copy(bs); Buf.Copy(bs);
} }
} }

View File

@ -16,7 +16,7 @@ public:
IDataPort* Led; // 指示灯 IDataPort* Led; // 指示灯
Array Buffer; // 用于接收数据的缓冲区,外部在打开前设置大小 Buffer Buf; // 用于接收数据的缓冲区,外部在打开前设置大小
BufferPort(); BufferPort();
~BufferPort(); ~BufferPort();
@ -26,10 +26,10 @@ public:
protected: protected:
virtual bool OnOpen(bool isNew); virtual bool OnOpen(bool isNew);
virtual void OnReceive(const Array& bs, void* param); virtual void OnReceive(const Buffer& bs, void* param);
private: private:
static uint OnReceive(ITransport* transport, Array& bs, void* param, void* param2); static uint OnReceive(ITransport* transport, Buffer& bs, void* param, void* param2);
}; };
#endif #endif

View File

@ -704,7 +704,7 @@ byte Enc28j60::GetRevision()
return ReadReg(EREVID); return ReadReg(EREVID);
} }
bool Enc28j60::OnWrite(const Array& bs) bool Enc28j60::OnWrite(const Buffer& bs)
{ {
uint len = bs.Length(); uint len = bs.Length();
assert_param2(len <= MAX_FRAMELEN, "以太网数据帧超大"); assert_param2(len <= MAX_FRAMELEN, "以太网数据帧超大");
@ -927,7 +927,7 @@ bool Enc28j60::OnWrite(const Array& bs)
// 从网络接收缓冲区获取一个数据包,该包开头是以太网头 // 从网络接收缓冲区获取一个数据包,该包开头是以太网头
// packet该包应该存储到的缓冲区maxlen可接受的最大数据长度 // packet该包应该存储到的缓冲区maxlen可接受的最大数据长度
uint Enc28j60::OnRead(Array& bs) uint Enc28j60::OnRead(Buffer& bs)
{ {
uint rxstat; uint rxstat;
uint len; uint len;

View File

@ -62,8 +62,8 @@ protected:
virtual bool OnOpen(); virtual bool OnOpen();
virtual void OnClose() { } virtual void OnClose() { }
virtual bool OnWrite(const Array& bs); virtual bool OnWrite(const Buffer& bs);
virtual uint OnRead(Array& bs); virtual uint OnRead(Buffer& bs);
}; };
#endif #endif

View File

@ -272,7 +272,7 @@ NRF24L01::~NRF24L01()
_spi = NULL; _spi = NULL;
} }
byte NRF24L01::WriteBuf(byte reg, const Array& bs) byte NRF24L01::WriteBuf(byte reg, const Buffer& bs)
{ {
SpiScope sc(_spi); SpiScope sc(_spi);
@ -283,7 +283,7 @@ byte NRF24L01::WriteBuf(byte reg, const Array& bs)
return status; return status;
} }
byte NRF24L01::ReadBuf(byte reg, Array& bs) byte NRF24L01::ReadBuf(byte reg, Buffer& bs)
{ {
SpiScope sc(_spi); SpiScope sc(_spi);
@ -321,7 +321,7 @@ bool NRF24L01::Check(void)
if(!Open()) return false; if(!Open()) return false;
byte buf[5] = {0xA5,0xA5,0xA5,0xA5,0xA5}; byte buf[5] = {0xA5,0xA5,0xA5,0xA5,0xA5};
Array src(buf, 5); Buffer src(buf, 5);
ByteArray bak(5); ByteArray bak(5);
ByteArray dst(5); ByteArray dst(5);
@ -531,7 +531,7 @@ bool NRF24L01::GetMode()
// 设置收发模式。 // 设置收发模式。
// 因为CE拉高需要延迟的原因整个函数大概耗时185us如果不延迟大概55us // 因为CE拉高需要延迟的原因整个函数大概耗时185us如果不延迟大概55us
bool NRF24L01::SetMode(bool isReceive, const Array& addr) bool NRF24L01::SetMode(bool isReceive, const Buffer& addr)
{ {
TS("R24::SetMode"); TS("R24::SetMode");
@ -628,8 +628,8 @@ void NRF24L01::SetAddress()
WriteReg(SETUP_AW, 5 - 2); WriteReg(SETUP_AW, 5 - 2);
// 发送地址为远程地址0通道为本地地址1通道为广播地址0xFF // 发送地址为远程地址0通道为本地地址1通道为广播地址0xFF
WriteBuf(TX_ADDR, Array(Remote, 5)); WriteBuf(TX_ADDR, Buffer(Remote, 5));
WriteBuf(RX_ADDR_P0, Array(Local, 5)); WriteBuf(RX_ADDR_P0, Buffer(Local, 5));
// 主节点再监听一个全0的地址 // 主节点再监听一个全0的地址
ByteArray bs((byte)0xFF, 5); ByteArray bs((byte)0xFF, 5);
@ -759,10 +759,10 @@ void NRF24L01::OnClose()
} }
// 从NRF的接收缓冲区中读出数据 // 从NRF的接收缓冲区中读出数据
uint NRF24L01::OnRead(Array& bs) uint NRF24L01::OnRead(Buffer& bs)
{ {
// 进入接收模式 // 进入接收模式
if(!SetMode(true, Array(Local, 5))) return false; if(!SetMode(true, Buffer(Local, 5))) return false;
TS("NRF24L01::OnRead"); TS("NRF24L01::OnRead");
@ -818,7 +818,7 @@ uint NRF24L01::OnRead(Array& bs)
} }
// 向NRF的发送缓冲区中写入数据 // 向NRF的发送缓冲区中写入数据
bool NRF24L01::SendTo(const Array& bs, const Array& addr) bool NRF24L01::SendTo(const Buffer& bs, const Buffer& addr)
{ {
TS("R24::SendTo"); TS("R24::SendTo");
@ -842,8 +842,9 @@ bool NRF24L01::SendTo(const Array& bs, const Array& addr)
uint len = bs.Length(); uint len = bs.Length();
byte pw = 32; byte pw = 32;
if(pw > 0) len = pw; if(pw > 0) len = pw;
Array bs2(bs.GetBuffer(), len); //Buffer bs2(bs.GetBuffer(), len);
WriteBuf(cmd, bs2); //WriteBuf(cmd, bs2);
WriteBuf(cmd, bs.Sub(len));
// 进入TX维持一段时间 // 进入TX维持一段时间
//_CE = false; //_CE = false;
@ -897,33 +898,33 @@ bool NRF24L01::SendTo(const Array& bs, const Array& addr)
//_CE = true; //_CE = true;
WriteReg(FLUSH_TX, NOP); WriteReg(FLUSH_TX, NOP);
SetMode(true, Array(Local, 5)); // 发送完成以后进入接收模式 SetMode(true, Buffer(Local, 5)); // 发送完成以后进入接收模式
return rs; return rs;
} }
/*// 引发数据到达事件 /*// 引发数据到达事件
uint NRF24L01::OnReceive(Array& bs, void* param) uint NRF24L01::OnReceive(Buffer& bs, void* param)
{ {
if(!Master) return ITransport::OnReceive(bs, param); if(!Master) return ITransport::OnReceive(bs, param);
// 取出地址 // 取出地址
//byte* addr = bs.GetBuffer(); //byte* addr = bs.GetBuffer();
//Array bs2(addr + 5, bs.Length() - 5); //Buffer bs2(addr + 5, bs.Length() - 5);
return ITransport::OnReceive(bs, param); return ITransport::OnReceive(bs, param);
}*/ }*/
bool NRF24L01::OnWrite(const Array& bs) bool NRF24L01::OnWrite(const Buffer& bs)
{ {
return SendTo(bs, Array(Remote, 5)); return SendTo(bs, Buffer(Remote, 5));
} }
bool NRF24L01::OnWriteEx(const Array& bs, void* opt) bool NRF24L01::OnWriteEx(const Buffer& bs, void* opt)
{ {
if(!Master || !opt) return OnWrite(bs); if(!Master || !opt) return OnWrite(bs);
// 加入地址 // 加入地址
return SendTo(bs, Array(opt, 5)); return SendTo(bs, Buffer(opt, 5));
} }
void NRF24L01::AddError() void NRF24L01::AddError()

View File

@ -56,7 +56,7 @@ public:
void Write(Stream& ms) const; void Write(Stream& ms) const;
ByteArray ToArray() const; ByteArray ToArray() const;
ByteArray ToArray(Stream& ms); ByteArray ToArray(Stream& ms);
void Set(ushort kind, const Array& bs); void Set(ushort kind, const Buffer& bs);
void Set(ushort kind, byte dat); void Set(ushort kind, byte dat);
void Set(ushort kind, ushort dat); void Set(ushort kind, ushort dat);
void Set(ushort kind, uint dat); void Set(ushort kind, uint dat);
@ -142,7 +142,7 @@ void ShunCom::ChangePower(int level)
} }
// 引发数据到达事件 // 引发数据到达事件
uint ShunCom::OnReceive(Array& bs, void* param) uint ShunCom::OnReceive(Buffer& bs, void* param)
{ {
if(Led) Led->Write(1000); if(Led) Led->Write(1000);
@ -154,14 +154,14 @@ uint ShunCom::OnReceive(Array& bs, void* param)
// 取出地址 // 取出地址
byte* addr = bs.GetBuffer(); byte* addr = bs.GetBuffer();
Array bs2(addr + AddrLength, bs.Length() - AddrLength); Buffer bs2(addr + AddrLength, bs.Length() - AddrLength);
//debug_printf("zigbee接收\r\n"); //debug_printf("zigbee接收\r\n");
//bs2.Show(true); //bs2.Show(true);
return ITransport::OnReceive(bs2, addr); return ITransport::OnReceive(bs2, addr);
} }
bool ShunCom::OnWriteEx(const Array& bs, void* opt) bool ShunCom::OnWriteEx(const Buffer& bs, void* opt)
{ {
//debug_printf("zigbee发送\r\n"); //debug_printf("zigbee发送\r\n");
//bs.Show(true); //bs.Show(true);
@ -419,7 +419,7 @@ ByteArray ShunComMessage::ToArray(Stream& ms)
ByteArray bs(ms.GetBuffer(), ms.Position()); ByteArray bs(ms.GetBuffer(), ms.Position());
return bs; return bs;
} }
void ShunComMessage::Set(ushort kind, const Array& bs) void ShunComMessage::Set(ushort kind, const Buffer& bs)
{ {
Kind = kind; Kind = kind;
bs.CopyTo(Data); bs.CopyTo(Data);

View File

@ -58,8 +58,8 @@ private:
virtual void ChangePower(int level); virtual void ChangePower(int level);
// 引发数据到达事件 // 引发数据到达事件
virtual uint OnReceive(Array& bs, void* param); virtual uint OnReceive(Buffer& bs, void* param);
virtual bool OnWriteEx(const Array& bs, void* opt); virtual bool OnWriteEx(const Buffer& bs, void* opt);
}; };
#endif #endif

View File

@ -52,8 +52,8 @@ void Sim900A::OnClose()
Port->Close(); Port->Close();
} }
bool Sim900A::OnWrite(const Array& bs) { return Send(bs); } bool Sim900A::OnWrite(const Buffer& bs) { return Send(bs); }
uint Sim900A::OnRead(Array& bs) { return 0; } uint Sim900A::OnRead(Buffer& bs) { return 0; }
String Sim900A::Send(const char* str, uint msTimeout) String Sim900A::Send(const char* str, uint msTimeout)
{ {
@ -166,7 +166,7 @@ void Sim900A::Init(uint msTimeout)
//以"AT"开头,以回车(<CR>)结尾 [AT+...\r] //以"AT"开头,以回车(<CR>)结尾 [AT+...\r]
//响应:<回车><换行><响应内容><回车><换行> [\r\n....\r\n] //响应:<回车><换行><响应内容><回车><换行> [\r\n....\r\n]
//使用最笨的任务方式进行进行处理 //使用最笨的任务方式进行进行处理
bool Sim900A::Send(const Array& bs) bool Sim900A::Send(const Buffer& bs)
{ {
if(!Inited) if(!Inited)
{ {

View File

@ -23,7 +23,7 @@ public:
Sim900A(); Sim900A();
virtual ~Sim900A(); virtual ~Sim900A();
bool Send(const Array& bs); bool Send(const Buffer& bs);
private: private:
void Init(uint msTimeout = 1000); void Init(uint msTimeout = 1000);
@ -31,8 +31,8 @@ private:
virtual bool OnOpen(); virtual bool OnOpen();
virtual void OnClose(); virtual void OnClose();
virtual bool OnWrite(const Array& bs); virtual bool OnWrite(const Buffer& bs);
virtual uint OnRead(Array& bs); virtual uint OnRead(Buffer& bs);
String Send(const char* str, uint msTimeout = 1000); String Send(const char* str, uint msTimeout = 1000);
bool SendCmd(const char* str, uint msTimeout = 1000, int times = 1); bool SendCmd(const char* str, uint msTimeout = 1000, int times = 1);

View File

@ -55,7 +55,7 @@ void UBlox::SetBaudRate(int baudRate)
//cmd[len - 1] = CK_B; //cmd[len - 1] = CK_B;
// 发送命令 // 发送命令
Array bs(cmd, len); Buffer bs(cmd, len);
bs.Show(true); bs.Show(true);
Port->Write(bs); Port->Write(bs);
} }
@ -69,8 +69,8 @@ void UBlox::SetRate()
0x01, 0x00, // navRate. Navigation Rate, in number of measurement cycles. This parameter cannot be changed, and must be set to 1. 0x01, 0x00, // navRate. Navigation Rate, in number of measurement cycles. This parameter cannot be changed, and must be set to 1.
0x01, 0x00, // timeRef. Alignment to reference time: 0 = UTC time, 1 = GPS time 0x01, 0x00, // timeRef. Alignment to reference time: 0 = UTC time, 1 = GPS time
0x01, 0x39 }; 0x01, 0x39 };
//Port->Write(Array(cmd, ArrayLength(cmd))); //Port->Write(Buffer(cmd, ArrayLength(cmd)));
Array bs(cmd, ArrayLength(cmd)); Buffer bs(cmd, ArrayLength(cmd));
bs.Show(true); bs.Show(true);
Port->Write(bs); Port->Write(bs);
} }
@ -92,8 +92,8 @@ void UBlox::SaveConfig()
0x00, 0x00, 0x00, 0x00, // loadMask 0x00, 0x00, 0x00, 0x00, // loadMask
0x17, // deviceMask. devBBR | devFlash | devEEPROM | 0 | devSpiFlash 0x17, // deviceMask. devBBR | devFlash | devEEPROM | 0 | devSpiFlash
0x31, 0xBF }; 0x31, 0xBF };
//Port->Write(Array(cmd, ArrayLength(cmd))); //Port->Write(Buffer(cmd, ArrayLength(cmd)));
Array bs(cmd, ArrayLength(cmd)); Buffer bs(cmd, ArrayLength(cmd));
bs.Show(true); bs.Show(true);
Port->Write(bs); Port->Write(bs);
} }
@ -120,7 +120,7 @@ void UBlox::SaveConfig()
return Open(); return Open();
}*/ }*/
void UBlox::OnReceive(const Array& bs, void* param) void UBlox::OnReceive(const Buffer& bs, void* param)
{ {
TS("UBlox::OnReceive"); TS("UBlox::OnReceive");
@ -128,20 +128,20 @@ void UBlox::OnReceive(const Array& bs, void* param)
String str((char*)bs.GetBuffer(), bs.Length()); String str((char*)bs.GetBuffer(), bs.Length());
//str.Show(true); //str.Show(true);
if(Buffer.Capacity() == 0) return; if(Buf.Capacity() == 0) return;
// 必须美元开头,可以指定头部识别符 // 必须美元开头,可以指定头部识别符
if(bs[0] == '$' && (Header == NULL || str.StartsWith(Header))) if(bs[0] == '$' && (Header == NULL || str.StartsWith(Header)))
{ {
Buffer.SetLength(0); Buf.SetLength(0);
Buffer.Copy(bs); Buf.Copy(bs, Buf.Length());
} }
else else
{ {
// 不合适的数据,可以直接附加在后面 // 不合适的数据,可以直接附加在后面
if(Buffer.Length() != 0) if(Buf.Length() != 0)
{ {
Buffer.Copy(bs, Buffer.Length()); Buf.Copy(bs, Buf.Length());
} }
} }
} }

View File

@ -22,7 +22,7 @@ public:
protected: protected:
virtual bool OnOpen(bool isNew); virtual bool OnOpen(bool isNew);
virtual void OnReceive(const Array& bs, void* param); virtual void OnReceive(const Buffer& bs, void* param);
}; };
#endif #endif

View File

@ -610,7 +610,7 @@ void W5500::SetAddress(ushort addr, byte rw, byte socket, byte block)
_spi->Write(cfg.ToByte()); _spi->Write(cfg.ToByte());
} }
bool W5500::WriteFrame(ushort addr, const Array& bs, byte socket, byte block) bool W5500::WriteFrame(ushort addr, const Buffer& bs, byte socket, byte block)
{ {
SpiScope sc(_spi); SpiScope sc(_spi);
@ -620,7 +620,7 @@ bool W5500::WriteFrame(ushort addr, const Array& bs, byte socket, byte block)
return true; return true;
} }
bool W5500::ReadFrame(ushort addr, Array& bs, byte socket, byte block) bool W5500::ReadFrame(ushort addr, Buffer& bs, byte socket, byte block)
{ {
SpiScope sc(_spi); SpiScope sc(_spi);
@ -1129,7 +1129,7 @@ void HardSocket::Change(const IPEndPoint& remote)
} }
// 接收数据 // 接收数据
uint HardSocket::Receive(Array& bs) uint HardSocket::Receive(Buffer& bs)
{ {
if(!Open()) return false; if(!Open()) return false;
@ -1166,7 +1166,7 @@ uint HardSocket::Receive(Array& bs)
} }
// 发送数据 // 发送数据
bool HardSocket::Send(const Array& bs) bool HardSocket::Send(const Buffer& bs)
{ {
if(!Open()) return false; if(!Open()) return false;
/*debug_printf("%s::Send [%d]=", Protocol == 0x01 ? "Tcp" : "Udp", bs.Length()); /*debug_printf("%s::Send [%d]=", Protocol == 0x01 ? "Tcp" : "Udp", bs.Length());
@ -1196,8 +1196,8 @@ bool HardSocket::Send(const Array& bs)
return true; return true;
} }
bool HardSocket::OnWrite(const Array& bs) { return Send(bs); } bool HardSocket::OnWrite(const Buffer& bs) { return Send(bs); }
uint HardSocket::OnRead(Array& bs) { return Receive(bs); } uint HardSocket::OnRead(Buffer& bs) { return Receive(bs); }
void HardSocket::ClearRX() void HardSocket::ClearRX()
{ {
@ -1364,7 +1364,7 @@ void TcpClient::OnProcess(byte reg)
void TcpClient::RaiseReceive() void TcpClient::RaiseReceive()
{ {
byte buf[1500]; byte buf[1500];
Array bs(buf, ArrayLength(buf)); Buffer bs(buf, ArrayLength(buf));
int size = Receive(bs); int size = Receive(bs);
if(size > 1500)return; if(size > 1500)return;
@ -1374,7 +1374,7 @@ void TcpClient::RaiseReceive()
/****************************** UdpClient ************************************/ /****************************** UdpClient ************************************/
bool UdpClient::SendTo(const Array& bs, const IPEndPoint& remote) bool UdpClient::SendTo(const Buffer& bs, const IPEndPoint& remote)
{ {
if(remote == Remote) return Send(bs); if(remote == Remote) return Send(bs);
@ -1392,7 +1392,7 @@ bool UdpClient::SendTo(const Array& bs, const IPEndPoint& remote)
return rs; return rs;
} }
bool UdpClient::OnWriteEx(const Array& bs, void* opt) bool UdpClient::OnWriteEx(const Buffer& bs, void* opt)
{ {
IPEndPoint* ep = (IPEndPoint*)opt; IPEndPoint* ep = (IPEndPoint*)opt;
if(!ep) return OnWrite(bs); if(!ep) return OnWrite(bs);
@ -1421,7 +1421,7 @@ void UdpClient::OnProcess(byte reg)
void UdpClient::RaiseReceive() void UdpClient::RaiseReceive()
{ {
byte buf[1500]; byte buf[1500];
Array bs(buf, ArrayLength(buf)); Buffer bs(buf, ArrayLength(buf));
ushort size = Receive(bs); ushort size = Receive(bs);
Stream ms(bs.GetBuffer(), size); Stream ms(bs.GetBuffer(), size);
@ -1440,7 +1440,7 @@ void UdpClient::RaiseReceive()
return; return;
} }
// 回调中断 // 回调中断
Array bs3(ms.ReadBytes(len), len); Buffer bs3(ms.ReadBytes(len), len);
OnReceive(bs3, &ep); OnReceive(bs3, &ep);
}; };
} }

View File

@ -42,8 +42,8 @@ public:
void ShowInfo(); void ShowInfo();
// 读写帧,帧本身由外部构造 (包括帧数据内部的读写标志) // 读写帧,帧本身由外部构造 (包括帧数据内部的读写标志)
bool WriteFrame(ushort addr, const Array& bs, byte socket = 0 ,byte block = 0); bool WriteFrame(ushort addr, const Buffer& bs, byte socket = 0 ,byte block = 0);
bool ReadFrame(ushort addr, Array& bs, byte socket = 0 ,byte block = 0); bool ReadFrame(ushort addr, Buffer& bs, byte socket = 0 ,byte block = 0);
// 复位 包含硬件复位和软件复位 // 复位 包含硬件复位和软件复位
void Reset(); void Reset();
@ -124,13 +124,13 @@ public:
// 应用配置,修改远程地址和端口 // 应用配置,修改远程地址和端口
void Change(const IPEndPoint& remote); void Change(const IPEndPoint& remote);
virtual bool OnWrite(const Array& bs); virtual bool OnWrite(const Buffer& bs);
virtual uint OnRead(Array& bs); virtual uint OnRead(Buffer& bs);
// 发送数据 // 发送数据
virtual bool Send(const Array& bs); virtual bool Send(const Buffer& bs);
// 接收数据 // 接收数据
virtual uint Receive(Array& bs); virtual uint Receive(Buffer& bs);
// 恢复配置 // 恢复配置
virtual void Recovery(); virtual void Recovery();
@ -173,7 +173,7 @@ class UdpClient : public HardSocket
public: public:
UdpClient(W5500& host) : HardSocket(host, ProtocolType::Udp) { } UdpClient(W5500& host) : HardSocket(host, ProtocolType::Udp) { }
virtual bool SendTo(const Array& bs, const IPEndPoint& remote); virtual bool SendTo(const Buffer& bs, const IPEndPoint& remote);
// 中断分发 维护状态 // 中断分发 维护状态
virtual void OnProcess(byte reg); virtual void OnProcess(byte reg);
@ -181,7 +181,7 @@ public:
virtual void RaiseReceive(); virtual void RaiseReceive();
private: private:
virtual bool OnWriteEx(const Array& bs, void* opt); virtual bool OnWriteEx(const Buffer& bs, void* opt);
}; };
#endif #endif

View File

@ -32,7 +32,7 @@ public:
bool GetPower(); // 获取当前电源状态 bool GetPower(); // 获取当前电源状态
bool SetPowerMode(bool on); // 设置当前电源状态。返回是否成功 bool SetPowerMode(bool on); // 设置当前电源状态。返回是否成功
bool GetMode(); // 获取当前模式是否接收模式 bool GetMode(); // 获取当前模式是否接收模式
bool SetMode(bool isReceive, const Array& addr); // 切换收发模式,不包含参数设定 bool SetMode(bool isReceive, const Buffer& addr); // 切换收发模式,不包含参数设定
void SetAddress(); // 设置地址 void SetAddress(); // 设置地址
// 电源等级变更(如进入低功耗模式)时调用 // 电源等级变更(如进入低功耗模式)时调用
@ -51,22 +51,22 @@ private:
virtual bool OnOpen(); virtual bool OnOpen();
virtual void OnClose(); virtual void OnClose();
virtual bool OnWrite(const Array& bs); virtual bool OnWrite(const Buffer& bs);
virtual uint OnRead(Array& bs); virtual uint OnRead(Buffer& bs);
// 引发数据到达事件 // 引发数据到达事件
//virtual uint OnReceive(Array& bs, void* param); //virtual uint OnReceive(Buffer& bs, void* param);
virtual bool OnWriteEx(const Array& bs, void* opt); virtual bool OnWriteEx(const Buffer& bs, void* opt);
bool SendTo(const Array& bs, const Array& addr); bool SendTo(const Buffer& bs, const Buffer& addr);
Spi* _spi; Spi* _spi;
OutputPort _CE; OutputPort _CE;
InputPort Irq; InputPort Irq;
OutputPort _Power; // 设置控制2401电源的引脚 直接进行对2401的通断电操作以免死机对setPower无效 OutputPort _Power; // 设置控制2401电源的引脚 直接进行对2401的通断电操作以免死机对setPower无效
byte WriteBuf(byte reg, const Array& bs); byte WriteBuf(byte reg, const Buffer& bs);
byte ReadBuf(byte reg, Array& bs); byte ReadBuf(byte reg, Buffer& bs);
byte ReadReg(byte reg); byte ReadReg(byte reg);
byte WriteReg(byte reg, byte dat); byte WriteReg(byte reg, byte dat);

View File

@ -114,7 +114,7 @@ bool I2C::SendSubAddr(int addr)
} }
// 新会话向指定地址写入多个字节 // 新会话向指定地址写入多个字节
bool I2C::Write(int addr, const Array& bs) bool I2C::Write(int addr, const Buffer& bs)
{ {
/*debug_printf("I2C::Write addr=0x%02X ", addr); /*debug_printf("I2C::Write addr=0x%02X ", addr);
bs.Show(true);*/ bs.Show(true);*/
@ -138,7 +138,7 @@ bool I2C::Write(int addr, const Array& bs)
} }
// 新会话从指定地址读取多个字节 // 新会话从指定地址读取多个字节
uint I2C::Read(int addr, Array& bs) uint I2C::Read(int addr, Buffer& bs)
{ {
Open(); Open();
@ -158,7 +158,7 @@ uint I2C::Read(int addr, Array& bs)
return rs; return rs;
} }
bool I2C::Write(int addr, byte data) { return Write(addr, Array(&data, 1)); } bool I2C::Write(int addr, byte data) { return Write(addr, Buffer(&data, 1)); }
byte I2C::Read(int addr) byte I2C::Read(int addr)
{ {
ByteArray bs(1); ByteArray bs(1);

4
I2C.h
View File

@ -37,10 +37,10 @@ public:
virtual bool WaitAck(int retry = 0) = 0; // 等待Ack默认0表示采用全局Retry virtual bool WaitAck(int retry = 0) = 0; // 等待Ack默认0表示采用全局Retry
// 新会话向指定地址写入 // 新会话向指定地址写入
virtual bool Write(int addr, const Array& bs); virtual bool Write(int addr, const Buffer& bs);
virtual bool Write(int addr, byte data); virtual bool Write(int addr, byte data);
// 新会话从指定地址读取 // 新会话从指定地址读取
virtual uint Read(int addr, Array& bs); virtual uint Read(int addr, Buffer& bs);
virtual byte Read(int addr); virtual byte Read(int addr);
virtual ushort Read2(int addr); virtual ushort Read2(int addr);
virtual uint Read4(int addr); virtual uint Read4(int addr);

View File

@ -51,7 +51,7 @@ void Controller::Close()
Opened = false; Opened = false;
} }
uint Controller::Dispatch(ITransport* port, Array& bs, void* param, void* param2) uint Controller::Dispatch(ITransport* port, Buffer& bs, void* param, void* param2)
{ {
TS("Controller::Dispatch"); TS("Controller::Dispatch");
@ -162,6 +162,6 @@ bool Controller::SendInternal(const Message& msg)
// 带有负载数据,需要合并成为一段连续的内存 // 带有负载数据,需要合并成为一段连续的内存
msg.Write(ms); msg.Write(ms);
Array bs(ms.GetBuffer(), ms.Position()); Buffer bs(ms.GetBuffer(), ms.Position());
return Port->Write(bs, msg.State); return Port->Write(bs, msg.State);
} }

View File

@ -13,7 +13,7 @@ typedef bool (*MessageHandler)(void* sender, Message& msg, void* param);
class Controller class Controller
{ {
private: private:
static uint Dispatch(ITransport* port, Array& bs, void* param, void* param2); static uint Dispatch(ITransport* port, Buffer& bs, void* param, void* param2);
protected: protected:

View File

@ -15,7 +15,7 @@ DataStore::DataStore() : Areas(0)
} }
// 写入数据 // 写入数据
int DataStore::Write(uint offset, const Array& bs) int DataStore::Write(uint offset, const Buffer& bs)
{ {
uint size = bs.Length(); uint size = bs.Length();
if(size == 0) return 0; if(size == 0) return 0;
@ -36,7 +36,7 @@ int DataStore::Write(uint offset, const Array& bs)
} }
// 读取数据 // 读取数据
int DataStore::Read(uint offset, Array& bs) int DataStore::Read(uint offset, Buffer& bs)
{ {
uint size = bs.Length(); uint size = bs.Length();
if(size == 0) return 0; if(size == 0) return 0;

View File

@ -17,9 +17,9 @@ public:
DataStore(); DataStore();
// 写入数据 // 写入数据
int Write(uint offset, const Array& bs); int Write(uint offset, const Buffer& bs);
// 读取数据 // 读取数据
int Read(uint offset, Array& bs); int Read(uint offset, Buffer& bs);
typedef bool (*Handler)(uint offset, uint size, int mode); typedef bool (*Handler)(uint offset, uint size, int mode);
// 注册某一块区域的读写钩子函数 // 注册某一块区域的读写钩子函数

View File

@ -218,7 +218,7 @@ bool dns_answer(Stream& ms, byte* ip_from_dns)
} }
// 分析响应 // 分析响应
bool parseDNSMSG(TDNS* hdr, const Array& bs, byte* ip_from_dns) bool parseDNSMSG(TDNS* hdr, const Buffer& bs, byte* ip_from_dns)
{ {
Stream ms(bs); Stream ms(bs);
ms.Little = false; ms.Little = false;
@ -268,7 +268,7 @@ bool parseDNSMSG(TDNS* hdr, const Array& bs, byte* ip_from_dns)
} }
// DNS查询消息 // DNS查询消息
short dns_makequery(short op, const String& name, Array& bs) short dns_makequery(short op, const String& name, Buffer& bs)
{ {
Stream ms(bs); Stream ms(bs);
ms.Little = false; ms.Little = false;
@ -346,8 +346,8 @@ IPAddress DNS::Query(const String& domain, int msTimeout)
#endif #endif
byte buf[1024]; byte buf[1024];
Array bs(buf, ArrayLength(buf)); Buffer bs(buf, ArrayLength(buf));
Array rs(buf, ArrayLength(buf)); Buffer rs(buf, ArrayLength(buf));
// 同时作为响应缓冲区,别浪费了 // 同时作为响应缓冲区,别浪费了
rs.SetLength(0); rs.SetLength(0);
_Buffer = &rs; _Buffer = &rs;
@ -372,14 +372,14 @@ IPAddress DNS::Query(const String& domain, int msTimeout)
return ip; return ip;
} }
uint DNS::OnReceive(ITransport* port, Array& bs, void* param, void* param2) uint DNS::OnReceive(ITransport* port, Buffer& bs, void* param, void* param2)
{ {
((DNS*)param)->Process(bs, *(const IPEndPoint*)param2); ((DNS*)param)->Process(bs, *(const IPEndPoint*)param2);
return 0; return 0;
} }
void DNS::Process(Array& bs, const IPEndPoint& server) void DNS::Process(Buffer& bs, const IPEndPoint& server)
{ {
// 只要来自服务器的 // 只要来自服务器的
if(server.Address != Host.DNSServer) return; if(server.Address != Host.DNSServer) return;

View File

@ -19,11 +19,11 @@ public:
static IPAddress Query(ISocketHost& host, const String& domain, int times = 5, int msTimeout = 1000); static IPAddress Query(ISocketHost& host, const String& domain, int times = 5, int msTimeout = 1000);
private: private:
static uint OnReceive(ITransport* port, Array& bs, void* param, void* param2); static uint OnReceive(ITransport* port, Buffer& bs, void* param, void* param2);
void Process(Array& bs, const IPEndPoint& server); void Process(Buffer& bs, const IPEndPoint& server);
ISocket* Socket; ISocket* Socket;
Array* _Buffer; Buffer* _Buffer;
}; };
#endif #endif

View File

@ -85,7 +85,7 @@ void Dhcp::SendDhcp(byte* buf, uint len)
}*/ }*/
//Send(*dhcp.Prev(), sizeof(DHCP_HEADER) + len, Remote.Address, Remote.Port, false); //Send(*dhcp.Prev(), sizeof(DHCP_HEADER) + len, Remote.Address, Remote.Port, false);
Array bs(dhcp, sizeof(DHCP_HEADER) + len); Buffer bs(dhcp, sizeof(DHCP_HEADER) + len);
Socket->Send(bs); Socket->Send(bs);
//if(isAny) Host->IP = bak; //if(isAny) Host->IP = bak;
@ -255,14 +255,14 @@ void Dhcp::PareOption(Stream& ms)
} }
} }
uint Dhcp::OnReceive(ITransport* port, Array& bs, void* param, void* param2) uint Dhcp::OnReceive(ITransport* port, Buffer& bs, void* param, void* param2)
{ {
((Dhcp*)param)->Process(bs, *(const IPEndPoint*)param2); ((Dhcp*)param)->Process(bs, *(const IPEndPoint*)param2);
return 0; return 0;
} }
void Dhcp::Process(Array& bs, const IPEndPoint& ep) void Dhcp::Process(Buffer& bs, const IPEndPoint& ep)
{ {
auto dhcp = (DHCP_HEADER*)bs.GetBuffer(); auto dhcp = (DHCP_HEADER*)bs.GetBuffer();
if(!dhcp->Valid()) return; if(!dhcp->Valid()) return;

View File

@ -38,8 +38,8 @@ public:
EventHandler OnStop; EventHandler OnStop;
private: private:
static uint OnReceive(ITransport* port, Array& bs, void* param, void* param2); static uint OnReceive(ITransport* port, Buffer& bs, void* param, void* param2);
void Process(Array& bs, const IPEndPoint& ep); void Process(Buffer& bs, const IPEndPoint& ep);
}; };
#endif #endif

View File

@ -53,7 +53,7 @@ void ITransport::Close()
} }
// 发送数据 // 发送数据
bool ITransport::Write(const Array& bs) bool ITransport::Write(const Buffer& bs)
{ {
// 特别是接口要检查this指针 // 特别是接口要检查this指针
assert_ptr(this); assert_ptr(this);
@ -64,7 +64,7 @@ bool ITransport::Write(const Array& bs)
} }
// 发送数据 // 发送数据
bool ITransport::Write(const Array& bs, void* opt) bool ITransport::Write(const Buffer& bs, void* opt)
{ {
// 特别是接口要检查this指针 // 特别是接口要检查this指针
assert_ptr(this); assert_ptr(this);
@ -75,7 +75,7 @@ bool ITransport::Write(const Array& bs, void* opt)
} }
// 接收数据 // 接收数据
uint ITransport::Read(Array& bs) uint ITransport::Read(Buffer& bs)
{ {
// 特别是接口要检查this指针 // 特别是接口要检查this指针
assert_ptr(this); assert_ptr(this);
@ -96,14 +96,14 @@ void ITransport::Register(TransportHandler handler, void* param)
} }
// 引发数据到达事件 // 引发数据到达事件
uint ITransport::OnReceive(Array& bs, void* param) uint ITransport::OnReceive(Buffer& bs, void* param)
{ {
if(_handler) return _handler(this, bs, _param, param); if(_handler) return _handler(this, bs, _param, param);
return 0; return 0;
} }
bool ITransport::OnWriteEx(const Array& bs, void* opt) bool ITransport::OnWriteEx(const Buffer& bs, void* opt)
{ {
return OnWrite(bs); return OnWrite(bs);
} }
@ -134,10 +134,10 @@ void PackPort::Set(ITransport* port)
bool PackPort::OnOpen() { return Port->Open(); } bool PackPort::OnOpen() { return Port->Open(); }
void PackPort::OnClose() { Port->Close(); } void PackPort::OnClose() { Port->Close(); }
bool PackPort::OnWrite(const Array& bs) { return Port->Write(bs); } bool PackPort::OnWrite(const Buffer& bs) { return Port->Write(bs); }
uint PackPort::OnRead(Array& bs) { return Port->Read(bs); } uint PackPort::OnRead(Buffer& bs) { return Port->Read(bs); }
uint PackPort::OnPortReceive(ITransport* sender, Array& bs, void* param, void* param2) uint PackPort::OnPortReceive(ITransport* sender, Buffer& bs, void* param, void* param2)
{ {
assert_ptr(param); assert_ptr(param);

View File

@ -6,7 +6,7 @@
class ITransport; class ITransport;
// 传输口数据到达委托。传入数据缓冲区地址和长度,如有反馈,仍使用该缓冲区,返回数据长度 // 传输口数据到达委托。传入数据缓冲区地址和长度,如有反馈,仍使用该缓冲区,返回数据长度
typedef uint (*TransportHandler)(ITransport* port, Array& bs, void* param, void* param2); typedef uint (*TransportHandler)(ITransport* port, Buffer& bs, void* param, void* param2);
// 帧数据传输接口 // 帧数据传输接口
// 实现者确保数据以包的形式传输,屏蔽数据的粘包和拆包 // 实现者确保数据以包的形式传输,屏蔽数据的粘包和拆包
@ -34,10 +34,10 @@ public:
void Close(); void Close();
// 发送数据 // 发送数据
bool Write(const Array& bs); bool Write(const Buffer& bs);
bool Write(const Array& bs, void* opt); bool Write(const Buffer& bs, void* opt);
// 接收数据 // 接收数据
uint Read(Array& bs); uint Read(Buffer& bs);
// 注册回调函数 // 注册回调函数
virtual void Register(TransportHandler handler, void* param = NULL); virtual void Register(TransportHandler handler, void* param = NULL);
@ -51,15 +51,15 @@ public:
protected: protected:
virtual bool OnOpen() { return true; } virtual bool OnOpen() { return true; }
virtual void OnClose() { } virtual void OnClose() { }
virtual bool OnWrite(const Array& bs) = 0; virtual bool OnWrite(const Buffer& bs) = 0;
virtual bool OnWriteEx(const Array& bs, void* opt); virtual bool OnWriteEx(const Buffer& bs, void* opt);
virtual uint OnRead(Array& bs) = 0; virtual uint OnRead(Buffer& bs) = 0;
// 是否有回调函数 // 是否有回调函数
bool HasHandler() { return _handler != NULL; } bool HasHandler() { return _handler != NULL; }
// 引发数据到达事件 // 引发数据到达事件
virtual uint OnReceive(Array& bs, void* param); virtual uint OnReceive(Buffer& bs, void* param);
}; };
// 数据口包装 // 数据口包装
@ -81,10 +81,10 @@ protected:
virtual bool OnOpen(); virtual bool OnOpen();
virtual void OnClose(); virtual void OnClose();
virtual bool OnWrite(const Array& bs); virtual bool OnWrite(const Buffer& bs);
virtual uint OnRead(Array& bs); virtual uint OnRead(Buffer& bs);
static uint OnPortReceive(ITransport* sender, Array& bs, void* param, void* param2); static uint OnPortReceive(ITransport* sender, Buffer& bs, void* param, void* param2);
}; };
#endif #endif

View File

@ -9,7 +9,7 @@
IPAddress::IPAddress(const byte* ips) IPAddress::IPAddress(const byte* ips)
{ {
Array(ips, 4).CopyTo(&Value, 4); Buffer((byte*)ips, 4).CopyTo(&Value, 4);
} }
IPAddress::IPAddress(byte ip1, byte ip2, byte ip3, byte ip4) IPAddress::IPAddress(byte ip1, byte ip2, byte ip3, byte ip4)
@ -17,7 +17,7 @@ IPAddress::IPAddress(byte ip1, byte ip2, byte ip3, byte ip4)
Value = (ip4 << 24) + (ip3 << 16) + (ip2 << 8) + ip1; Value = (ip4 << 24) + (ip3 << 16) + (ip2 << 8) + ip1;
} }
IPAddress::IPAddress(const Array& arr) IPAddress::IPAddress(const Buffer& arr)
{ {
arr.CopyTo(&Value, 4); arr.CopyTo(&Value, 4);
} }
@ -45,12 +45,12 @@ const IPAddress& IPAddress::Broadcast()
IPAddress& IPAddress::operator=(const byte* v) IPAddress& IPAddress::operator=(const byte* v)
{ {
Array(v, 4).CopyTo(&Value, 4); Buffer((byte*)v, 4).CopyTo(&Value, 4);
return *this; return *this;
} }
IPAddress& IPAddress::operator=(const Array& arr) IPAddress& IPAddress::operator=(const Buffer& arr)
{ {
arr.CopyTo(&Value, 4); arr.CopyTo(&Value, 4);
@ -74,7 +74,7 @@ ByteArray IPAddress::ToArray() const
void IPAddress::CopyTo(byte* ips) const void IPAddress::CopyTo(byte* ips) const
{ {
if(ips) Array(&Value, 4).CopyTo(ips, 4); if(ips) Buffer((byte*)&Value, 4).CopyTo(ips, 4);
} }
String& IPAddress::ToStr(String& str) const String& IPAddress::ToStr(String& str) const
@ -106,7 +106,7 @@ IPEndPoint::IPEndPoint(const IPAddress& addr, ushort port)
Port = port; Port = port;
} }
IPEndPoint::IPEndPoint(const Array& arr) IPEndPoint::IPEndPoint(const Buffer& arr)
{ {
/*byte* p = arr.GetBuffer(); /*byte* p = arr.GetBuffer();
Address = p; Address = p;
@ -114,7 +114,7 @@ IPEndPoint::IPEndPoint(const Array& arr)
*this = arr; *this = arr;
} }
IPEndPoint& IPEndPoint::operator=(const Array& arr) IPEndPoint& IPEndPoint::operator=(const Buffer& arr)
{ {
Address = arr; Address = arr;
arr.CopyTo(&Port, 2, 4); arr.CopyTo(&Port, 2, 4);
@ -187,7 +187,7 @@ MacAddress::MacAddress(const byte* macs)
memcpy(&Value, macs, 6); memcpy(&Value, macs, 6);
} }
MacAddress::MacAddress(const Array& arr) MacAddress::MacAddress(const Buffer& arr)
{ {
ByteArray bs(arr.GetBuffer(), arr.Length()); ByteArray bs(arr.GetBuffer(), arr.Length());
Value = bs.ToUInt64(); Value = bs.ToUInt64();
@ -218,12 +218,12 @@ MacAddress& MacAddress::operator=(ulong v)
MacAddress& MacAddress::operator=(const byte* buf) MacAddress& MacAddress::operator=(const byte* buf)
{ {
Array(buf, 6).CopyTo(&Value, 6); Buffer((byte*)buf, 6).CopyTo(&Value, 6);
return *this; return *this;
} }
MacAddress& MacAddress::operator=(const Array& arr) MacAddress& MacAddress::operator=(const Buffer& arr)
{ {
arr.CopyTo(&Value, 6); arr.CopyTo(&Value, 6);
@ -247,7 +247,7 @@ ByteArray MacAddress::ToArray() const
void MacAddress::CopyTo(byte* macs) const void MacAddress::CopyTo(byte* macs) const
{ {
if(macs) Array(&Value, 6).CopyTo(macs, 6); if(macs) Buffer((byte*)&Value, 6).CopyTo(macs, 6);
} }
String& MacAddress::ToStr(String& str) const String& MacAddress::ToStr(String& str) const
@ -287,7 +287,7 @@ bool ISocketHost::LoadConfig()
if(!Config::Current) return false; if(!Config::Current) return false;
NetConfig nc; NetConfig nc;
Array bs(&nc, sizeof(nc)); Buffer bs(&nc, sizeof(nc));
if(!Config::Current->Get("NET", bs)) return false; if(!Config::Current->Get("NET", bs)) return false;
IP = nc.IP; IP = nc.IP;
@ -314,7 +314,7 @@ bool ISocketHost::SaveConfig()
nc.DNSServer = DNSServer.Value; nc.DNSServer = DNSServer.Value;
nc.Gateway = Gateway.Value; nc.Gateway = Gateway.Value;
Array bs(&nc, sizeof(nc)); Buffer bs(&nc, sizeof(nc));
return Config::Current->Set("NET", bs); return Config::Current->Set("NET", bs);
} }

View File

@ -23,12 +23,12 @@ public:
IPAddress(uint value = 0) { Value = value; } IPAddress(uint value = 0) { Value = value; }
IPAddress(const byte* ips); IPAddress(const byte* ips);
IPAddress(byte ip1, byte ip2, byte ip3, byte ip4); IPAddress(byte ip1, byte ip2, byte ip3, byte ip4);
IPAddress(const Array& arr); IPAddress(const Buffer& arr);
IPAddress& operator=(int v) { Value = (uint)v; return *this; } IPAddress& operator=(int v) { Value = (uint)v; return *this; }
IPAddress& operator=(uint v) { Value = v; return *this; } IPAddress& operator=(uint v) { Value = v; return *this; }
IPAddress& operator=(const byte* v); IPAddress& operator=(const byte* v);
IPAddress& operator=(const Array& arr); IPAddress& operator=(const Buffer& arr);
// 重载索引运算符[],让它可以像数组一样使用下标索引。 // 重载索引运算符[],让它可以像数组一样使用下标索引。
byte& operator[](int i); byte& operator[](int i);
@ -60,9 +60,9 @@ public:
IPEndPoint(); IPEndPoint();
IPEndPoint(const IPAddress& addr, ushort port); IPEndPoint(const IPAddress& addr, ushort port);
IPEndPoint(const Array& arr); IPEndPoint(const Buffer& arr);
IPEndPoint& operator=(const Array& arr); IPEndPoint& operator=(const Buffer& arr);
// 字节数组 // 字节数组
ByteArray ToArray() const; ByteArray ToArray() const;
@ -86,14 +86,14 @@ public:
MacAddress(ulong v = 0); MacAddress(ulong v = 0);
MacAddress(const byte* macs); MacAddress(const byte* macs);
MacAddress(const Array& arr); MacAddress(const Buffer& arr);
// 是否广播地址全0或全1 // 是否广播地址全0或全1
bool IsBroadcast() const; bool IsBroadcast() const;
MacAddress& operator=(ulong v); MacAddress& operator=(ulong v);
MacAddress& operator=(const byte* buf); MacAddress& operator=(const byte* buf);
MacAddress& operator=(const Array& arr); MacAddress& operator=(const Buffer& arr);
// 重载索引运算符[],让它可以像数组一样使用下标索引。 // 重载索引运算符[],让它可以像数组一样使用下标索引。
byte& operator[](int i); byte& operator[](int i);
@ -159,10 +159,10 @@ public:
virtual ~ISocket() { } virtual ~ISocket() { }
// 发送数据 // 发送数据
virtual bool Send(const Array& bs) = 0; virtual bool Send(const Buffer& bs) = 0;
virtual bool SendTo(const Array& bs, const IPEndPoint& remote) { return Send(bs); } virtual bool SendTo(const Buffer& bs, const IPEndPoint& remote) { return Send(bs); }
// 接收数据 // 接收数据
virtual uint Receive(Array& bs) = 0; virtual uint Receive(Buffer& bs) = 0;
}; };
#endif #endif

View File

@ -57,7 +57,7 @@ byte Queue::Pop()
#pragma arm section code #pragma arm section code
uint Queue::Write(const Array& bs) uint Queue::Write(const Buffer& bs)
{ {
/* /*
1 1
@ -100,7 +100,7 @@ uint Queue::Write(const Array& bs)
return rs; return rs;
} }
uint Queue::Read(Array& bs) uint Queue::Read(Buffer& bs)
{ {
if(_size == 0) return 0; if(_size == 0) return 0;

View File

@ -27,8 +27,8 @@ public:
void Push(byte dat); void Push(byte dat);
byte Pop(); byte Pop();
uint Write(const Array& bs); // 批量写入 uint Write(const Buffer& bs); // 批量写入
uint Read(Array& bs); // 批量读取 uint Read(Buffer& bs); // 批量读取
}; };
#endif #endif

View File

@ -40,10 +40,10 @@ static const uint c_CRCTable[ 256 ] =
0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668, 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4 0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668, 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4
}; };
uint Crc::Hash(const Array& arr, uint crc) uint Crc::Hash(const Buffer& arr, uint crc)
{ {
const byte* ptr = arr.GetBuffer(); auto ptr = arr.GetBuffer();
int len = arr.Length(); int len = arr.Length();
while(len-- > 0) while(len-- > 0)
{ {
crc = c_CRCTable[ ((crc >> 24) ^ (*ptr++)) & 0xFF ] ^ (crc << 8); crc = c_CRCTable[ ((crc >> 24) ^ (*ptr++)) & 0xFF ] ^ (crc << 8);

View File

@ -9,8 +9,8 @@ class Crc
public: public:
// CRC32校验 // CRC32校验
//uint Crc(const void* buf, uint len); //uint Crc(const void* buf, uint len);
static uint Hash(const Array& arr, uint crc = 0); static uint Hash(const Buffer& arr, uint crc = 0);
static ushort Hash16(const Array& arr, ushort crc = 0xFFFF); static ushort Hash16(const Buffer& arr, ushort crc = 0xFFFF);
}; };
#endif #endif

View File

@ -6,10 +6,10 @@ static const ushort c_CRC16Table[] =
0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400,
}; };
ushort Crc::Hash16(const Array& arr, ushort crc) ushort Crc::Hash16(const Buffer& arr, ushort crc)
{ {
const byte* buf = arr.GetBuffer(); auto buf = arr.GetBuffer();
int len = arr.Length(); int len = arr.Length();
assert_param2(buf, "Crc16校验目标地址不能为空"); assert_param2(buf, "Crc16校验目标地址不能为空");
if (!buf || !len) return 0; if (!buf || !len) return 0;

View File

@ -226,11 +226,11 @@ void md5_finish(md5_context *ctx, byte digest[16] )
PUT_UINT32( ctx->state[3], digest, 12 ); PUT_UINT32( ctx->state[3], digest, 12 );
} }
ByteArray MD5::Hash(const Array& data) ByteArray MD5::Hash(const Buffer& data)
{ {
md5_context context; md5_context context;
md5_starts(&context); md5_starts(&context);
md5_update(&context, data.GetBuffer(), data.Length()); md5_update(&context, (byte*)data.GetBuffer(), data.Length());
ByteArray hash; ByteArray hash;
hash.SetLength(16); hash.SetLength(16);

View File

@ -8,7 +8,7 @@ class MD5
{ {
public: public:
// 散列 // 散列
static ByteArray Hash(const Array& data); static ByteArray Hash(const Buffer& data);
// 字符串散列。先取得字符串对应的字节码进行散列然后转为HEX编码字符串 // 字符串散列。先取得字符串对应的字节码进行散列然后转为HEX编码字符串
static String Hash(const String& str); static String Hash(const String& str);
}; };

View File

@ -218,7 +218,7 @@ uint SerialPort::SendData(byte data, uint times)
} }
// 向某个端口写入数据。如果size为0则把data当作字符串一直发送直到遇到\0为止 // 向某个端口写入数据。如果size为0则把data当作字符串一直发送直到遇到\0为止
bool SerialPort::OnWrite(const Array& bs) bool SerialPort::OnWrite(const Buffer& bs)
{ {
if(!bs.Length()) return true; if(!bs.Length()) return true;
/*#if defined(STM32F0) || defined(GD32F150) /*#if defined(STM32F0) || defined(GD32F150)
@ -281,7 +281,7 @@ void SerialPort::OnTxHandler()
#pragma arm section code #pragma arm section code
// 从某个端口读取数据 // 从某个端口读取数据
uint SerialPort::OnRead(Array& bs) uint SerialPort::OnRead(Buffer& bs)
{ {
uint count = 0; uint count = 0;
uint len = Rx.Length(); uint len = Rx.Length();
@ -353,7 +353,7 @@ void SerialPort::ReceiveTask(void* param)
// 从栈分配,节省内存 // 从栈分配,节省内存
byte buf[0x100]; byte buf[0x100];
Array bs(buf, ArrayLength(buf)); Buffer bs(buf, ArrayLength(buf));
int mx = sp->MaxSize; int mx = sp->MaxSize;
if(mx > 0 && mx > bs.Length()) bs.SetLength(mx); if(mx > 0 && mx > bs.Length()) bs.SetLength(mx);
@ -478,7 +478,7 @@ extern "C"
if(_printf_sp) if(_printf_sp)
{ {
byte b = ch; byte b = ch;
_printf_sp->Write(Array(&b, 1)); _printf_sp->Write(Buffer(&b, 1));
} }
isInFPutc = false; isInFPutc = false;

View File

@ -72,8 +72,8 @@ protected:
virtual bool OnOpen(); virtual bool OnOpen();
virtual void OnClose(); virtual void OnClose();
virtual bool OnWrite(const Array& bs); virtual bool OnWrite(const Buffer& bs);
virtual uint OnRead(Array& bs); virtual uint OnRead(Buffer& bs);
private: private:
static void OnHandler(ushort num, void* param); static void OnHandler(ushort num, void* param);

View File

@ -265,7 +265,7 @@ ushort Spi::Write16(ushort data)
} }
// 批量读写。以字节数组长度为准 // 批量读写。以字节数组长度为准
void Spi::Write(const Array& bs) void Spi::Write(const Buffer& bs)
{ {
for(int i=0; i<bs.Length(); i++) for(int i=0; i<bs.Length(); i++)
{ {
@ -273,7 +273,7 @@ void Spi::Write(const Array& bs)
} }
} }
void Spi::Read(Array& bs) void Spi::Read(Buffer& bs)
{ {
for(int i=0; i<bs.Length(); i++) for(int i=0; i<bs.Length(); i++)
{ {

4
Spi.h
View File

@ -41,8 +41,8 @@ public:
ushort Write16(ushort data); ushort Write16(ushort data);
// 批量读写。以字节数组长度为准 // 批量读写。以字节数组长度为准
void Write(const Array& bs); void Write(const Buffer& bs);
void Read(Array& bs); void Read(Buffer& bs);
void Start(); // 拉低NSS开始传输 void Start(); // 拉低NSS开始传输
void Stop(); // 拉高NSS停止传输 void Stop(); // 拉高NSS停止传输

View File

@ -8,7 +8,7 @@
#define st_printf(format, ...) #define st_printf(format, ...)
#endif #endif
bool BlockStorage::Read(uint address, Array& bs) const bool BlockStorage::Read(uint address, Buffer& bs) const
{ {
uint len = bs.Length(); uint len = bs.Length();
if (!len) return true; if (!len) return true;
@ -24,11 +24,11 @@ bool BlockStorage::Read(uint address, Array& bs) const
return true; return true;
} }
bool BlockStorage::Write(uint address, const Array& bs) const bool BlockStorage::Write(uint address, const Buffer& bs) const
{ {
assert_param2((address & 0x01) == 0x00, "Write起始地址必须是2字节对齐"); assert_param2((address & 0x01) == 0x00, "Write起始地址必须是2字节对齐");
byte* buf = bs.GetBuffer(); auto buf = bs.GetBuffer();
uint len = bs.Length(); uint len = bs.Length();
if (!len) return true; if (!len) return true;
@ -40,7 +40,7 @@ bool BlockStorage::Write(uint address, const Array& bs) const
if(len < len2) len2 = len; if(len < len2) len2 = len;
//st_printf(" Data: "); //st_printf(" Data: ");
//!!! 必须另起一个指针,否则移动原来的指针可能造成重大失误 //!!! 必须另起一个指针,否则移动原来的指针可能造成重大失误
byte* p = buf; auto p = buf;
for(int i=0; i<len2; i++) st_printf(" %02X", *p++); for(int i=0; i<len2; i++) st_printf(" %02X", *p++);
st_printf("\r\n"); st_printf("\r\n");
#endif #endif
@ -76,7 +76,7 @@ bool BlockStorage::Write(uint address, const Array& bs) const
// 从地址找到所在扇区 // 从地址找到所在扇区
int offset = address % Block; int offset = address % Block;
byte* pData = buf; // 指向要下一个写入的数据 auto pData = buf; // 指向要下一个写入的数据
int remain = len; // 剩下数据数量 int remain = len; // 剩下数据数量
int addr = address; // 下一个要写入数据的位置 int addr = address; // 下一个要写入数据的位置
@ -86,7 +86,7 @@ bool BlockStorage::Write(uint address, const Array& bs) const
#else #else
byte bb[0x800]; byte bb[0x800];
#endif #endif
Array ms(bb, ArrayLength(bb)); Buffer ms(bb, ArrayLength(bb));
ms.SetLength(Block); ms.SetLength(Block);
// 写入第一个半块 // 写入第一个半块
@ -212,14 +212,14 @@ bool BlockStorage::IsErased(uint address, uint len) const
return true; return true;
} }
bool CharStorage::Read(uint address, Array& bs) const bool CharStorage::Read(uint address, Buffer& bs) const
{ {
bs.Copy((byte*)address); bs.Copy((byte*)address);
return true; return true;
} }
bool CharStorage::Write(uint address, const Array& bs) const bool CharStorage::Write(uint address, const Buffer& bs) const
{ {
bs.CopyTo((byte*)address); bs.CopyTo((byte*)address);

View File

@ -8,9 +8,9 @@ class Storage
{ {
public: public:
// 读取 // 读取
virtual bool Read(uint address, Array& bs) const = 0; virtual bool Read(uint address, Buffer& bs) const = 0;
// 写入 // 写入
virtual bool Write(uint address, const Array& bs) const = 0; virtual bool Write(uint address, const Buffer& bs) const = 0;
}; };
// 块存储接口 // 块存储接口
@ -23,9 +23,9 @@ public:
bool ReadModifyWrite; // 是否读改写 bool ReadModifyWrite; // 是否读改写
// 读取 // 读取
virtual bool Read(uint address, Array& bs) const; virtual bool Read(uint address, Buffer& bs) const;
// 写入 // 写入
virtual bool Write(uint address, const Array& bs) const; virtual bool Write(uint address, const Buffer& bs) const;
// 清空 // 清空
virtual bool Memset(uint address, byte data, uint len) const; virtual bool Memset(uint address, byte data, uint len) const;
// 擦除 // 擦除
@ -45,9 +45,9 @@ class CharStorage : public Storage
{ {
public: public:
// 读取 // 读取
virtual bool Read(uint address, Array& bs) const; virtual bool Read(uint address, Buffer& bs) const;
// 写入 // 写入
virtual bool Write(uint address, const Array& bs) const; virtual bool Write(uint address, const Buffer& bs) const;
}; };
#endif #endif

View File

@ -14,12 +14,12 @@ Stream::Stream(const void* buf, uint len)
} }
// 使用字节数组初始化数据流。注意此时指针位于0而内容长度为缓冲区长度 // 使用字节数组初始化数据流。注意此时指针位于0而内容长度为缓冲区长度
Stream::Stream(Array& bs) Stream::Stream(Buffer& bs)
{ {
Init(bs.GetBuffer(), bs.Length()); Init(bs.GetBuffer(), bs.Length());
} }
Stream::Stream(const Array& bs) Stream::Stream(const Buffer& bs)
{ {
Init((void*)bs.GetBuffer(), bs.Length()); Init((void*)bs.GetBuffer(), bs.Length());
@ -128,7 +128,7 @@ uint Stream::ReadEncodeInt()
} }
// 读取数据到字节数组,由字节数组指定大小。不包含长度前缀 // 读取数据到字节数组,由字节数组指定大小。不包含长度前缀
uint Stream::Read(Array& bs) uint Stream::Read(Buffer& bs)
{ {
return Read(bs.GetBuffer(), 0, bs.Length()); return Read(bs.GetBuffer(), 0, bs.Length());
} }
@ -190,7 +190,7 @@ uint Stream::Write(const char* str)
} }
// 把字节数组的数据写入到数据流。不包含长度前缀 // 把字节数组的数据写入到数据流。不包含长度前缀
bool Stream::Write(const Array& bs) bool Stream::Write(const Buffer& bs)
{ {
return Write(bs.GetBuffer(), 0, bs.Length()); return Write(bs.GetBuffer(), 0, bs.Length());
} }
@ -215,7 +215,7 @@ int Stream::Peek() const
} }
// 从数据流读取变长数据到字节数组。以压缩整数开头表示长度 // 从数据流读取变长数据到字节数组。以压缩整数开头表示长度
uint Stream::ReadArray(Array& bs) uint Stream::ReadArray(Buffer& bs)
{ {
uint len = ReadEncodeInt(); uint len = ReadEncodeInt();
if(!len) return 0; if(!len) return 0;
@ -263,7 +263,7 @@ ByteArray Stream::ReadArray()
} }
// 把字节数组作为变长数据写入到数据流。以压缩整数开头表示长度 // 把字节数组作为变长数据写入到数据流。以压缩整数开头表示长度
bool Stream::WriteArray(const Array& bs) bool Stream::WriteArray(const Buffer& bs)
{ {
WriteEncodeInt(bs.Length()); WriteEncodeInt(bs.Length());
return Write(bs.GetBuffer(), 0, bs.Length()); return Write(bs.GetBuffer(), 0, bs.Length());

View File

@ -24,8 +24,8 @@ public:
Stream(void* buf, uint len); Stream(void* buf, uint len);
Stream(const void* buf, uint len); Stream(const void* buf, uint len);
// 使用字节数组初始化数据流。注意此时指针位于0而内容长度为缓冲区长度 // 使用字节数组初始化数据流。注意此时指针位于0而内容长度为缓冲区长度
Stream(Array& bs); Stream(Buffer& bs);
Stream(const Array& bs); Stream(const Buffer& bs);
// 数据流容量 // 数据流容量
uint Capacity() const; uint Capacity() const;
@ -49,7 +49,7 @@ public:
// 读取7位压缩编码整数 // 读取7位压缩编码整数
uint ReadEncodeInt(); uint ReadEncodeInt();
// 读取数据到字节数组,由字节数组指定大小。不包含长度前缀 // 读取数据到字节数组,由字节数组指定大小。不包含长度前缀
uint Read(Array& bs); uint Read(Buffer& bs);
// 把数据写入当前位置 // 把数据写入当前位置
bool Write(const void* buf, uint offset, uint count); bool Write(const void* buf, uint offset, uint count);
@ -58,13 +58,13 @@ public:
// 写入字符串,先写入压缩编码整数表示的长度 // 写入字符串,先写入压缩编码整数表示的长度
uint Write(const char* str); uint Write(const char* str);
// 把字节数组的数据写入到数据流。不包含长度前缀 // 把字节数组的数据写入到数据流。不包含长度前缀
bool Write(const Array& bs); bool Write(const Buffer& bs);
// 从数据流读取变长数据到字节数组。以压缩整数开头表示长度 // 从数据流读取变长数据到字节数组。以压缩整数开头表示长度
uint ReadArray(Array& bs); uint ReadArray(Buffer& bs);
ByteArray ReadArray(int count); ByteArray ReadArray(int count);
// 把字节数组作为变长数据写入到数据流。以压缩整数开头表示长度 // 把字节数组作为变长数据写入到数据流。以压缩整数开头表示长度
bool WriteArray(const Array& bs); bool WriteArray(const Buffer& bs);
ByteArray ReadArray(); ByteArray ReadArray();
String ReadString(); String ReadString();

View File

@ -205,7 +205,7 @@ void TcpSocket::OnDataReceive(TCP_HEADER& tcp, uint len)
byte* data = tcp.Next(); byte* data = tcp.Next();
// 触发ITransport接口事件 // 触发ITransport接口事件
Array bs(data, len); Buffer bs(data, len);
uint len2 = OnReceive(bs, NULL); uint len2 = OnReceive(bs, NULL);
// 如果有返回,说明有数据要回复出去 // 如果有返回,说明有数据要回复出去
if(len2) if(len2)
@ -365,7 +365,7 @@ bool TcpSocket::Disconnect()
return SendPacket(*tcp, 0, TCP_FLAGS_ACK | TCP_FLAGS_PUSH | TCP_FLAGS_FIN); return SendPacket(*tcp, 0, TCP_FLAGS_ACK | TCP_FLAGS_PUSH | TCP_FLAGS_FIN);
} }
bool TcpSocket::Send(const Array& bs) bool TcpSocket::Send(const Buffer& bs)
{ {
if(!Enable) if(!Enable)
{ {
@ -425,7 +425,7 @@ bool TcpSocket::Send(const Array& bs)
return wait; return wait;
} }
uint TcpSocket::Receive(Array& bs) uint TcpSocket::Receive(Buffer& bs)
{ {
return 0; return 0;
} }
@ -499,12 +499,12 @@ bool TcpSocket::Connect(IPAddress& ip, ushort port)
return false; return false;
} }
bool TcpSocket::OnWrite(const Array& bs) bool TcpSocket::OnWrite(const Buffer& bs)
{ {
return Send(bs); return Send(bs);
} }
uint TcpSocket::OnRead(Array& bs) uint TcpSocket::OnRead(Buffer& bs)
{ {
// 暂时不支持 // 暂时不支持
return 0; return 0;

View File

@ -39,9 +39,9 @@ public:
bool Disconnect(); // 关闭Socket bool Disconnect(); // 关闭Socket
// 发送数据 // 发送数据
virtual bool Send(const Array& bs); virtual bool Send(const Buffer& bs);
// 接收数据 // 接收数据
virtual uint Receive(Array& bs); virtual uint Receive(Buffer& bs);
// 收到Tcp数据时触发传递结构体和负载数据长度。返回值指示是否向对方发送数据包 // 收到Tcp数据时触发传递结构体和负载数据长度。返回值指示是否向对方发送数据包
typedef bool (*TcpHandler)(TcpSocket& socket, TCP_HEADER& tcp, byte* buf, uint len); typedef bool (*TcpHandler)(TcpSocket& socket, TCP_HEADER& tcp, byte* buf, uint len);
@ -67,8 +67,8 @@ protected:
virtual bool OnOpen(); virtual bool OnOpen();
virtual void OnClose(); virtual void OnClose();
virtual bool OnWrite(const Array& bs); virtual bool OnWrite(const Buffer& bs);
virtual uint OnRead(Array& bs); virtual uint OnRead(Buffer& bs);
}; };
#endif #endif

View File

@ -74,7 +74,7 @@ void UdpSocket::OnProcess(IP_HEADER& ip, UDP_HEADER& udp, Stream& ms)
assert_param2(len <= ms.Remain(), "UDP数据包不完整"); assert_param2(len <= ms.Remain(), "UDP数据包不完整");
// 触发ITransport接口事件 // 触发ITransport接口事件
Array bs(data, len); Buffer bs(data, len);
uint len2 = OnReceive(bs, &CurRemote); uint len2 = OnReceive(bs, &CurRemote);
// 如果有返回,说明有数据要回复出去 // 如果有返回,说明有数据要回复出去
//if(len2) Write(data, len2); //if(len2) Write(data, len2);
@ -130,7 +130,7 @@ void UdpSocket::SendPacket(UDP_HEADER& udp, uint len, IPAddress& ip, ushort port
} }
// 发送UDP数据到目标地址 // 发送UDP数据到目标地址
bool UdpSocket::Send(const Array& bs) bool UdpSocket::Send(const Buffer& bs)
{ {
//if(ip.IsAny()) ip = Remote.Address; //if(ip.IsAny()) ip = Remote.Address;
//if(!port) port = Remote.Port; //if(!port) port = Remote.Port;
@ -152,17 +152,17 @@ bool UdpSocket::Send(const Array& bs)
return true; return true;
} }
bool UdpSocket::OnWrite(const Array& bs) bool UdpSocket::OnWrite(const Buffer& bs)
{ {
return Send(bs); return Send(bs);
} }
uint UdpSocket::Receive(Array& bs) uint UdpSocket::Receive(Buffer& bs)
{ {
return 0; return 0;
} }
uint UdpSocket::OnRead(Array& bs) uint UdpSocket::OnRead(Buffer& bs)
{ {
// 暂时不支持 // 暂时不支持
return 0; return 0;

View File

@ -25,9 +25,9 @@ public:
UdpHandler OnReceived; UdpHandler OnReceived;
// 发送数据 // 发送数据
virtual bool Send(const Array& bs); virtual bool Send(const Buffer& bs);
// 接收数据 // 接收数据
virtual uint Receive(Array& bs); virtual uint Receive(Buffer& bs);
virtual const char* ToString() const; virtual const char* ToString() const;
@ -38,8 +38,8 @@ protected:
virtual bool OnOpen(); virtual bool OnOpen();
virtual void OnClose(); virtual void OnClose();
virtual bool OnWrite(const Array& bs); virtual bool OnWrite(const Buffer& bs);
virtual uint OnRead(Array& bs); virtual uint OnRead(Buffer& bs);
}; };
#endif #endif

View File

@ -12,7 +12,7 @@
#define ShunCom_SLAVE 0 #define ShunCom_SLAVE 0
//#define MSG_DEBUG 0 //#define MSG_DEBUG 0
uint OnSerial(ITransport* transport, Array& bs, void* param, void* param2) uint OnSerial(ITransport* transport, Buffer& bs, void* param, void* param2)
{ {
debug_printf("OnSerial len=%d \t", bs.Length()); debug_printf("OnSerial len=%d \t", bs.Length());
bs.Show(true); bs.Show(true);
@ -58,7 +58,7 @@ void Setup(ushort code, const char* name, COM message, int baudRate)
void Fix2401(void* param) void Fix2401(void* param)
{ {
auto& bs = *(Array*)param; auto& bs = *(Buffer*)param;
// 微网指令特殊处理长度 // 微网指令特殊处理长度
uint rs = bs.Length(); uint rs = bs.Length();
if(rs >= 8) if(rs >= 8)

View File

@ -129,7 +129,7 @@ TinyServer* Token::CreateServer(ITransport* port)
return &server; return &server;
} }
uint OnSerial(ITransport* transport, Array& bs, void* param, void* param2) uint OnSerial(ITransport* transport, Buffer& bs, void* param, void* param2)
{ {
debug_printf("OnSerial len=%d \t", bs.Length()); debug_printf("OnSerial len=%d \t", bs.Length());
bs.Show(true); bs.Show(true);
@ -184,7 +184,7 @@ void Token::Setup(ushort code, const char* name, COM message, int baudRate)
void Fix2401(void* param) void Fix2401(void* param)
{ {
auto& bs = *(Array*)param; auto& bs = *(Buffer*)param;
// 微网指令特殊处理长度 // 微网指令特殊处理长度
uint rs = bs.Length(); uint rs = bs.Length();
if(rs >= 8) if(rs >= 8)

View File

@ -63,13 +63,63 @@ const String Type::Name() const
return String(name); return String(name);
} }
/******************************** Buffer ********************************/
// 复制数组。深度克隆,拷贝数据,自动扩容
int Buffer::Copy(const void* data, int len, int index)
{
assert_param(len >= 0);
//assert_param2(_canWrite, "禁止CopyData修改");
assert_param2(data, "Buffer::Copy data Error");
//if(len < 0) len = MemLen(data);
// 检查长度是否足够
int len2 = index + len;
//CheckCapacity(len2, index);
// 拷贝数据
memcpy((byte*)_Arr + index, data, len);
// 扩大长度
if(len2 > _Length) _Length = len2;
return len;
}
// 复制数组。深度克隆,拷贝数据
int Buffer::Copy(const Buffer& arr, int index)
{
//assert_param2(_canWrite, "禁止CopyArray修改数据");
if(&arr == this) return 0;
if(arr.Length() == 0) return 0;
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;
if(len <= 0) return 0;
// 拷贝数据
memcpy(data, (byte*)_Arr + index, len);
return len;
}
/******************************** TArray ********************************/ /******************************** TArray ********************************/
// 数组长度 // 数组长度
int Array::Length() const { return _Length; } //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; } //byte* Array::GetBuffer() const { return (byte*)_Arr; }
int MemLen(const void* data) int MemLen(const void* data)
{ {
@ -211,7 +261,7 @@ bool Array::Set(const void* data, int len)
return true; return true;
} }
// 复制数组。深度克隆,拷贝数据,自动扩容 /*// 复制数组。深度克隆,拷贝数据,自动扩容
int Array::Copy(const void* data, int len, int index) int Array::Copy(const void* data, int len, int index)
{ {
assert_param2(_canWrite, "禁止CopyData修改"); assert_param2(_canWrite, "禁止CopyData修改");
@ -256,7 +306,7 @@ int Array::CopyTo(void* data, int len, int index) const
memcpy(data, (byte*)_Arr + _Size * index, _Size * len); memcpy(data, (byte*)_Arr + _Size * index, _Size * len);
return len; return len;
} }*/
// 清空已存储数据。 // 清空已存储数据。
void Array::Clear() void Array::Clear()

80
Type.h
View File

@ -14,6 +14,9 @@ typedef long long Int64;
#define UInt64_Max 0xFFFFFFFFFFFFFFFFull #define UInt64_Max 0xFFFFFFFFFFFFFFFFull
// 逐步使用char替代byte在返回类型中使用char*替代void*
// 因为格式化输出时,可以用%c输出char用%s输出char*
#include <typeinfo> #include <typeinfo>
using namespace ::std; using namespace ::std;
@ -57,17 +60,74 @@ public:
const String Name() const; // 名称 const String Name() const; // 名称
}; };
class Buffer : public Object
{
public:
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; }
Buffer(void* p = nullptr, int len = 0)
{
_Arr = p;
_Length = len;
_Capacity = len;
}
// 重载索引运算符[],返回指定元素的第一个字节
byte& operator[](int i) const
{
assert_param2(_Arr && i >= 0 && i < _Length, "下标越界");
byte* buf = (byte*)_Arr;
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;
const Buffer Sub(int len) const
{
assert_param2(len <= _Length, "len <= _Length");
return Buffer(_Arr, len);
}
protected:
void* _Arr; // 数据指针
int _Length; // 长度
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])) #define ArrayLength(arr) (sizeof(arr)/sizeof(arr[0]))
// 数组清零,固定长度 // 数组清零,固定长度
#define ArrayZero(arr) memset(arr, 0, ArrayLength(arr) * sizeof(arr[0])) #define ArrayZero(arr) memset(arr, 0, ArrayLength(arr) * sizeof(arr[0]))
// 数组。包括指针和最大长度,支持实际长度 // 数组。包括指针和最大长度,支持实际长度
class Array : public Object class Array : public Buffer
{ {
protected: protected:
void* _Arr; // 数据指针 //void* _Arr; // 数据指针
int _Length; // 元素个数。非字节数 //int _Length; // 元素个数。非字节数
uint _Capacity; // 最大个数。非字节数 uint _Capacity; // 最大个数。非字节数
bool _needFree; // 是否需要释放 bool _needFree; // 是否需要释放
bool _canWrite; // 是否可写 bool _canWrite; // 是否可写
@ -75,11 +135,11 @@ protected:
public: public:
// 数组长度 // 数组长度
int Length() const; //int Length() const;
// 数组最大容量。初始化时决定,后面不允许改变 // 数组最大容量。初始化时决定,后面不允许改变
int Capacity() const; //int Capacity() const;
// 缓冲区。按字节指针返回 // 缓冲区。按字节指针返回
byte* GetBuffer() const; //byte* GetBuffer() const;
Array(void* data, int len); Array(void* data, int len);
Array(const void* data, int len); Array(const void* data, int len);
@ -92,7 +152,7 @@ public:
virtual ~Array(); virtual ~Array();
// 设置数组长度。容量足够则缩小Length否则扩容以确保数组容量足够大避免多次分配内存 // 设置数组长度。容量足够则缩小Length否则扩容以确保数组容量足够大避免多次分配内存
bool SetLength(int length, bool bak = false); virtual bool SetLength(int length, bool bak = false);
// 设置数组元素为指定值,自动扩容 // 设置数组元素为指定值,自动扩容
bool SetItem(const void* data, int index, int count); bool SetItem(const void* data, int index, int count);
// 设置数组。直接使用指针,不拷贝数据 // 设置数组。直接使用指针,不拷贝数据
@ -100,11 +160,11 @@ public:
// 设置数组。直接使用指针,不拷贝数据 // 设置数组。直接使用指针,不拷贝数据
bool Set(const void* data, int len); bool Set(const void* data, int len);
// 复制数组。深度克隆,拷贝数据,自动扩容 // 复制数组。深度克隆,拷贝数据,自动扩容
int Copy(const void* data, int len = -1, int index = 0); //int Copy(const void* data, int len = -1, int index = 0);
// 复制数组。深度克隆,拷贝数据 // 复制数组。深度克隆,拷贝数据
int Copy(const Array& arr, int index = 0); //int Copy(const Array& arr, int index = 0);
// 把当前数组复制到目标缓冲区。未指定长度len时复制全部 // 把当前数组复制到目标缓冲区。未指定长度len时复制全部
int CopyTo(void* data, int len = -1, int index = 0) const; //int CopyTo(void* data, int len = -1, int index = 0) const;
// 清空已存储数据。 // 清空已存储数据。
virtual void Clear(); virtual void Clear();
// 设置指定位置的值,不足时自动扩容 // 设置指定位置的值,不足时自动扩容