SmartOS/Message/Message.cpp

69 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Message.h"
// 初始化消息各字段为0
Message::Message(byte code)
{
Code = code;
Reply = false;
Error = false;
OneWay = false;
Length = 0;
Data = nullptr;
State = nullptr;
}
// 设置数据。
void Message::SetData(const Buffer& bs, int offset)
{
Length = bs.Length() + offset;
if(Length > 0) bs.CopyTo(0, Data + offset, Length);
}
void Message::SetError(byte errorCode, cstring msg)
{
Error = true;
Stream ms(Data, MaxDataSize());
ms.Write(errorCode);
ms.WriteArray(String(msg));
Length = ms.Position();
}
bool Message::Clone(const Message& msg)
{
MemoryStream ms;
msg.Write(ms);
ms.SetPosition(0);
return Read(ms);
}
// 负载数据转数据流
Stream Message::ToStream()
{
Stream ms(Data, MaxDataSize());
ms.Length = Length;
ms.CanResize = false;
return ms;
}
Stream Message::ToStream() const
{
Stream ms((const byte*)Data, MaxDataSize());
ms.Length = Length;
ms.CanResize = false;
return ms;
}
/*// 负载数据转字节数组
ByteArray Message::ToArray()
{
return ByteArray(Data, Length);
}*/