SmartOS/Message/Message.cpp

83 lines
1.4 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;
Length = 0;
Data = NULL;
Reply = false;
Error = false;
}
// 设置数据。
void Message::SetData(const byte* buf, uint len, uint offset)
{
//assert_param(len <= ArrayLength(Data));
Length = len + offset;
if(len > 0 && buf != Data + offset)
{
assert_ptr(buf);
assert_ptr(Data);
memcpy(Data + offset, buf, len);
}
}
void Message::SetData(const ByteArray& bs, uint offset)
{
Length = bs.Length() + offset;
if(Length > 0 && bs.GetBuffer() != Data + offset) bs.CopyTo(Data + offset, Length);
}
void Message::SetError(byte errorCode, const char* msg)
{
/*byte* p = Data;
*p++ = errorCode;
while(msg) *p++ = (byte)*msg++;
Length = p - Data;*/
Error = true;
Stream ms(Data, MaxDataSize());
ms.Write(errorCode);
ms.Write(msg);
Length = ms.Position();
}
bool Message::Clone(const Message& msg)
{
Stream ms;
msg.Write(ms);
ms.SetPosition(0);
return Read(ms);
}
// 负载数据转数据流
Stream Message::ToStream()
{
Stream ms(Data, MaxDataSize());
ms.Length = Length;
return ms;
}
Stream Message::ToStream() const
{
Stream ms((const byte*)Data, MaxDataSize());
ms.Length = Length;
return ms;
}
/*// 负载数据转字节数组
ByteArray Message::ToArray()
{
return ByteArray(Data, Length);
}*/