使用缓冲区初始化数据流,支持自动扩容
This commit is contained in:
parent
3216b2a040
commit
39e944154c
|
@ -167,6 +167,8 @@ bool Controller::Send(Message& msg)
|
|||
|
||||
// ms需要在外面这里声明,否则离开大括号作用域以后变量被销毁,导致缓冲区不可用
|
||||
//Stream ms(len);
|
||||
byte buf[128];
|
||||
MemoryStream ms(buf, ArrayLength(buf));
|
||||
#if defined(STM32F0)
|
||||
byte buf[512]; // 0.5K
|
||||
#esle
|
||||
|
@ -175,10 +177,6 @@ bool Controller::Send(Message& msg)
|
|||
Stream ms(buf, ArrayLength(buf));
|
||||
// 带有负载数据,需要合并成为一段连续的内存
|
||||
msg.Write(ms);
|
||||
//assert_param2(len == ms.Position(), "消息标称大小和实际大小不符");
|
||||
/*uint len = ms.Position();
|
||||
// 内存流扩容以后,指针会改变
|
||||
byte* p = ms.GetBuffer();*/
|
||||
|
||||
Array bs(ms.GetBuffer(), ms.Position());
|
||||
return Port->Write(bs, msg.State);
|
||||
|
|
|
@ -338,6 +338,11 @@ MemoryStream::MemoryStream(uint len) : Stream(_Arr, ArrayLength(_Arr))
|
|||
}
|
||||
}
|
||||
|
||||
MemoryStream::MemoryStream(void* buf, uint len) : Stream(buf, len)
|
||||
{
|
||||
_needFree = false;
|
||||
}
|
||||
|
||||
// 销毁数据流
|
||||
MemoryStream::~MemoryStream()
|
||||
{
|
||||
|
|
2
Stream.h
2
Stream.h
|
@ -111,6 +111,8 @@ private:
|
|||
public:
|
||||
// 分配指定大小的数据流
|
||||
MemoryStream(uint len = 0);
|
||||
// 使用缓冲区初始化数据流,支持自动扩容
|
||||
MemoryStream(void* buf, uint len);
|
||||
// 销毁数据流
|
||||
~MemoryStream();
|
||||
};
|
||||
|
|
|
@ -295,7 +295,9 @@ bool Gateway::SendDevices(DeviceAtions act, const Device* dv)
|
|||
int count = Server->Devices.Length();
|
||||
if(dv) count = 1;
|
||||
|
||||
MemoryStream ms(1536);
|
||||
byte buf[1024];
|
||||
MemoryStream ms(buf, ArrayLength(buf));
|
||||
//MemoryStream ms(1536);
|
||||
ms.Write((byte)act);
|
||||
ms.Write((byte)count);
|
||||
//ms.WriteEncodeInt(count);
|
||||
|
|
Loading…
Reference in New Issue