修正数据流读写字符串的BUG
This commit is contained in:
parent
19ac9be732
commit
03cc00c88a
53
Stream.cpp
53
Stream.cpp
|
@ -130,7 +130,10 @@ uint Stream::ReadEncodeInt()
|
||||||
// 读取数据到字节数组,由字节数组指定大小。不包含长度前缀
|
// 读取数据到字节数组,由字节数组指定大小。不包含长度前缀
|
||||||
uint Stream::Read(Buffer& bs)
|
uint Stream::Read(Buffer& bs)
|
||||||
{
|
{
|
||||||
return Read(bs.GetBuffer(), 0, bs.Length());
|
uint rs = Read(bs.GetBuffer(), 0, bs.Length());
|
||||||
|
bs.SetLength(rs);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 把数据写入当前位置
|
// 把数据写入当前位置
|
||||||
|
@ -176,12 +179,7 @@ uint Stream::Write(const char* str)
|
||||||
{
|
{
|
||||||
if(!CanWrite) return false;
|
if(!CanWrite) return false;
|
||||||
|
|
||||||
int len = 0;
|
int len = strlen(str);
|
||||||
if(str)
|
|
||||||
{
|
|
||||||
auto p = str;
|
|
||||||
while(*p++) len++;
|
|
||||||
}
|
|
||||||
|
|
||||||
WriteEncodeInt(len);
|
WriteEncodeInt(len);
|
||||||
if(len) Write((byte*)str, 0, len);
|
if(len) Write((byte*)str, 0, len);
|
||||||
|
@ -218,36 +216,31 @@ int Stream::Peek() const
|
||||||
uint Stream::ReadArray(Buffer& bs)
|
uint Stream::ReadArray(Buffer& bs)
|
||||||
{
|
{
|
||||||
uint len = ReadEncodeInt();
|
uint len = ReadEncodeInt();
|
||||||
if(!len) return 0;
|
if(!len)
|
||||||
|
{
|
||||||
|
bs.SetLength(0);
|
||||||
|
|
||||||
if(len > bs.Length())
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(len > bs.Length() && !bs.SetLength(len))
|
||||||
{
|
{
|
||||||
// 在设计时,如果取得的长度超级大,可能是设计错误
|
// 在设计时,如果取得的长度超级大,可能是设计错误
|
||||||
if(len > 0x40)
|
//if(len > 0x40)
|
||||||
{
|
{
|
||||||
debug_printf(" 读 %d > %d ", len, bs.Length());
|
debug_printf("Stream::ReadArray 缓冲区大小不足 读 %d > %d ", len, bs.Length());
|
||||||
//assert_param2(len <= bs.Capacity(), "缓冲区大小不足");
|
//assert_param2(len <= bs.Capacity(), "缓冲区大小不足");
|
||||||
//bs.Set(0, 0, len);
|
|
||||||
/*// 即使缓冲区不够大,也不要随便去重置,否则会清空别人的数据
|
|
||||||
// 这里在缓冲区不够大时,有多少读取多少
|
|
||||||
len = bs.Capacity();*/
|
|
||||||
// 为了避免错误数据导致内存溢出,限定最大值
|
|
||||||
//if(len > 0x400) len = bs.Capacity();
|
|
||||||
}
|
}
|
||||||
// 如果不是设计错误,那么数组直接扩容
|
return 0;
|
||||||
//bs.SetLength(len);
|
|
||||||
}
|
}
|
||||||
// 不管长度太大还是太小,都要设置一下长度,避免读取长度小于数组长度,导致得到一片空数据
|
|
||||||
//bs.SetLength(len);
|
|
||||||
|
|
||||||
Read(bs.GetBuffer(), 0, len);
|
return Read(bs.GetBuffer(), 0, len);
|
||||||
|
|
||||||
return len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteArray Stream::ReadArray(int count)
|
ByteArray Stream::ReadArray(int count)
|
||||||
{
|
{
|
||||||
ByteArray bs(count);
|
ByteArray bs;
|
||||||
|
bs.SetLength(count);
|
||||||
Read(bs.GetBuffer(), 0, bs.Length());
|
Read(bs.GetBuffer(), 0, bs.Length());
|
||||||
|
|
||||||
return bs;
|
return bs;
|
||||||
|
@ -273,20 +266,20 @@ String Stream::ReadString()
|
||||||
{
|
{
|
||||||
String str;
|
String str;
|
||||||
|
|
||||||
//ReadArray(str);
|
ReadArray(str);
|
||||||
|
|
||||||
uint len = ReadEncodeInt();
|
/*uint len = ReadEncodeInt();
|
||||||
if(!len) return 0;
|
if(!len) return 0;
|
||||||
|
|
||||||
str = (char*)ReadBytes(len);
|
str = (char*)ReadBytes(len);*/
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Stream::WriteString(const String& str)
|
/*bool Stream::WriteString(const String& str)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
byte Stream::ReadByte()
|
byte Stream::ReadByte()
|
||||||
{
|
{
|
||||||
|
|
2
Stream.h
2
Stream.h
|
@ -68,7 +68,7 @@ public:
|
||||||
|
|
||||||
ByteArray ReadArray();
|
ByteArray ReadArray();
|
||||||
String ReadString();
|
String ReadString();
|
||||||
bool WriteString(const String& str);
|
//bool WriteString(const String& str);
|
||||||
|
|
||||||
byte ReadByte();
|
byte ReadByte();
|
||||||
ushort ReadUInt16();
|
ushort ReadUInt16();
|
||||||
|
|
|
@ -81,7 +81,7 @@ void Device::WriteMessage(Stream& ms) const
|
||||||
ms.Write(OfflineTime);
|
ms.Write(OfflineTime);
|
||||||
ms.Write(PingTime);
|
ms.Write(PingTime);
|
||||||
|
|
||||||
ms.WriteString(Name);
|
ms.WriteArray(Name);
|
||||||
|
|
||||||
// 计算并设置大小
|
// 计算并设置大小
|
||||||
byte size = ms.Position() - p;
|
byte size = ms.Position() - p;
|
||||||
|
|
|
@ -89,8 +89,8 @@ bool HelloMessage::Read(Stream& ms)
|
||||||
void HelloMessage::Write(Stream& ms) const
|
void HelloMessage::Write(Stream& ms) const
|
||||||
{
|
{
|
||||||
ms.Write(Version);
|
ms.Write(Version);
|
||||||
//ms.WriteArray(Type);
|
ms.WriteArray(Type);
|
||||||
//ms.WriteArray(Name);
|
ms.WriteArray(Name);
|
||||||
ms.Write(LocalTime);
|
ms.Write(LocalTime);
|
||||||
ms.Write(EndPoint.ToArray());
|
ms.Write(EndPoint.ToArray());
|
||||||
|
|
||||||
|
|
|
@ -30,8 +30,8 @@ void LoginMessage::Write(Stream& ms) const
|
||||||
{
|
{
|
||||||
if(!Reply)
|
if(!Reply)
|
||||||
{
|
{
|
||||||
ms.WriteString(User);
|
ms.WriteArray(User);
|
||||||
ms.WriteString(Pass);
|
ms.WriteArray(Pass);
|
||||||
|
|
||||||
if(Salt.Length() > 0)
|
if(Salt.Length() > 0)
|
||||||
ms.WriteArray(Salt);
|
ms.WriteArray(Salt);
|
||||||
|
@ -44,7 +44,7 @@ void LoginMessage::Write(Stream& ms) const
|
||||||
else if(!Error)
|
else if(!Error)
|
||||||
{
|
{
|
||||||
ms.Write(Token);
|
ms.Write(Token);
|
||||||
ms.WriteString(Pass);
|
ms.WriteArray(Pass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,8 +25,8 @@ void RegisterMessage::Write(Stream& ms) const
|
||||||
{
|
{
|
||||||
if(!Error)
|
if(!Error)
|
||||||
{
|
{
|
||||||
ms.WriteString(User);
|
ms.WriteArray(User);
|
||||||
ms.WriteString(Pass);
|
ms.WriteArray(Pass);
|
||||||
|
|
||||||
if(Salt.Length() > 0)
|
if(Salt.Length() > 0)
|
||||||
ms.WriteArray(Salt);
|
ms.WriteArray(Salt);
|
||||||
|
|
Loading…
Reference in New Issue