From b1d4b3bb96629b3624e8328d7b1a0bce5333bb7d Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 21 Mar 2015 13:00:20 +0100 Subject: Unified cByteBuffer types. cByteBuffer now reads and writes any of the [U]Int types. --- src/ByteBuffer.cpp | 103 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 72 insertions(+), 31 deletions(-) (limited to 'src/ByteBuffer.cpp') diff --git a/src/ByteBuffer.cpp b/src/ByteBuffer.cpp index f3dc44d91..325b12dd1 100644 --- a/src/ByteBuffer.cpp +++ b/src/ByteBuffer.cpp @@ -101,8 +101,8 @@ public: assert_test(buf.Write("a", 1)); assert_test(buf.CanReadBytes(1)); assert_test(buf.GetReadableSpace() == 1); - unsigned char v = 0; - assert_test(buf.ReadByte(v)); + UInt8 v = 0; + assert_test(buf.ReadBEUInt8(v)); assert_test(v == 'a'); assert_test(buf.GetReadableSpace() == 0); buf.CommitRead(); @@ -317,7 +317,7 @@ bool cByteBuffer::CanWriteBytes(size_t a_Count) const -bool cByteBuffer::ReadChar(char & a_Value) +bool cByteBuffer::ReadBEInt8(Int8 & a_Value) { CHECK_THREAD CheckValid(); @@ -330,7 +330,7 @@ bool cByteBuffer::ReadChar(char & a_Value) -bool cByteBuffer::ReadByte(unsigned char & a_Value) +bool cByteBuffer::ReadBEUInt8(UInt8 & a_Value) { CHECK_THREAD CheckValid(); @@ -343,15 +343,15 @@ bool cByteBuffer::ReadByte(unsigned char & a_Value) -bool cByteBuffer::ReadBEShort(short & a_Value) +bool cByteBuffer::ReadBEInt16(Int16 & a_Value) { CHECK_THREAD CheckValid(); NEEDBYTES(2); - Int16 val; + UInt16 val; ReadBuf(&val, 2); val = ntohs(val); - a_Value = *(reinterpret_cast(&val)); + memcpy(&a_Value, &val, 2); return true; } @@ -373,13 +373,15 @@ bool cByteBuffer::ReadBEUInt16(UInt16 & a_Value) -bool cByteBuffer::ReadBEInt(int & a_Value) +bool cByteBuffer::ReadBEInt32(Int32 & a_Value) { CHECK_THREAD CheckValid(); NEEDBYTES(4); - ReadBuf(&a_Value, 4); - a_Value = (int)ntohl((u_long)a_Value); + UInt32 val; + ReadBuf(&val, 4); + val = ntohl(val); + memcpy(&a_Value, &val, 4); return true; } @@ -415,6 +417,20 @@ bool cByteBuffer::ReadBEInt64(Int64 & a_Value) +bool cByteBuffer::ReadBEUInt64(UInt64 & a_Value) +{ + CHECK_THREAD + CheckValid(); + NEEDBYTES(8); + ReadBuf(&a_Value, 8); + a_Value = NetworkToHostULong8(&a_Value); + return true; +} + + + + + bool cByteBuffer::ReadBEFloat(float & a_Value) { CHECK_THREAD @@ -448,7 +464,7 @@ bool cByteBuffer::ReadBool(bool & a_Value) CHECK_THREAD CheckValid(); NEEDBYTES(1); - char Value = 0; + UInt8 Value = 0; ReadBuf(&Value, 1); a_Value = (Value != 0); return true; @@ -462,17 +478,12 @@ bool cByteBuffer::ReadBEUTF16String16(AString & a_Value) { CHECK_THREAD CheckValid(); - short Length; - if (!ReadBEShort(Length)) + UInt16 Length; + if (!ReadBEUInt16(Length)) { return false; } - if (Length < 0) - { - ASSERT(!"Negative string length? Are you sure?"); - return true; - } - return ReadUTF16String(a_Value, (size_t)Length); + return ReadUTF16String(a_Value, Length); } @@ -565,7 +576,7 @@ bool cByteBuffer::ReadPosition(int & a_BlockX, int & a_BlockY, int & a_BlockZ) -bool cByteBuffer::WriteChar(char a_Value) +bool cByteBuffer::WriteBEInt8(Int8 a_Value) { CHECK_THREAD CheckValid(); @@ -577,7 +588,7 @@ bool cByteBuffer::WriteChar(char a_Value) -bool cByteBuffer::WriteByte(unsigned char a_Value) +bool cByteBuffer::WriteBEUInt8(UInt8 a_Value) { CHECK_THREAD CheckValid(); @@ -589,33 +600,48 @@ bool cByteBuffer::WriteByte(unsigned char a_Value) -bool cByteBuffer::WriteBEShort(short a_Value) +bool cByteBuffer::WriteBEInt16(Int16 a_Value) { CHECK_THREAD CheckValid(); PUTBYTES(2); - u_short Converted = htons((u_short)a_Value); - return WriteBuf(&Converted, 2); + UInt16 val; + memcpy(&val, &a_Value, 2); + val = htons(val); + return WriteBuf(&val, 2); } -bool cByteBuffer::WriteBEUShort(unsigned short a_Value) +bool cByteBuffer::WriteBEUInt16(UInt16 a_Value) { CHECK_THREAD CheckValid(); PUTBYTES(2); - u_short Converted = htons((u_short)a_Value); - return WriteBuf(&Converted, 2); + a_Value = htons(a_Value); + return WriteBuf(&a_Value, 2); +} + + + + + +bool cByteBuffer::WriteBEInt32(Int32 a_Value) +{ + CHECK_THREAD + CheckValid(); + PUTBYTES(4); + UInt32 Converted = HostToNetwork4(&a_Value); + return WriteBuf(&Converted, 4); } -bool cByteBuffer::WriteBEInt(int a_Value) +bool cByteBuffer::WriteBEUInt32(UInt32 a_Value) { CHECK_THREAD CheckValid(); @@ -641,6 +667,19 @@ bool cByteBuffer::WriteBEInt64(Int64 a_Value) +bool cByteBuffer::WriteBEUInt64(UInt64 a_Value) +{ + CHECK_THREAD + CheckValid(); + PUTBYTES(8); + UInt64 Converted = HostToNetwork8(&a_Value); + return WriteBuf(&Converted, 8); +} + + + + + bool cByteBuffer::WriteBEFloat(float a_Value) { CHECK_THREAD @@ -672,7 +711,8 @@ bool cByteBuffer::WriteBool(bool a_Value) { CHECK_THREAD CheckValid(); - return WriteChar(a_Value ? 1 : 0); + UInt8 val = a_Value ? 1 : 0; + return Write(&val, 1); } @@ -717,7 +757,7 @@ bool cByteBuffer::WriteVarUTF8String(const AString & a_Value) -bool cByteBuffer::WriteLEInt(int a_Value) +bool cByteBuffer::WriteLEInt32(Int32 a_Value) { CHECK_THREAD CheckValid(); @@ -733,9 +773,10 @@ bool cByteBuffer::WriteLEInt(int a_Value) -bool cByteBuffer::WritePosition(int a_BlockX, int a_BlockY, int a_BlockZ) +bool cByteBuffer::WritePosition(Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ) { CHECK_THREAD + CheckValid(); return WriteBEInt64(((Int64)a_BlockX & 0x3FFFFFF) << 38 | ((Int64)a_BlockY & 0xFFF) << 26 | ((Int64)a_BlockZ & 0x3FFFFFF)); } -- cgit v1.2.3