diff options
author | madmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-09-02 21:38:37 +0200 |
---|---|---|
committer | madmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-09-02 21:38:37 +0200 |
commit | 38efe73154f30200fafb2631b17106bc6935ae05 (patch) | |
tree | e2524ff0d0a14241f84569b9df65e789cd31473e /source | |
parent | Fixed Bug #241: (diff) | |
download | cuberite-38efe73154f30200fafb2631b17106bc6935ae05.tar cuberite-38efe73154f30200fafb2631b17106bc6935ae05.tar.gz cuberite-38efe73154f30200fafb2631b17106bc6935ae05.tar.bz2 cuberite-38efe73154f30200fafb2631b17106bc6935ae05.tar.lz cuberite-38efe73154f30200fafb2631b17106bc6935ae05.tar.xz cuberite-38efe73154f30200fafb2631b17106bc6935ae05.tar.zst cuberite-38efe73154f30200fafb2631b17106bc6935ae05.zip |
Diffstat (limited to 'source')
-rw-r--r-- | source/ByteBuffer.cpp | 140 | ||||
-rw-r--r-- | source/ByteBuffer.h | 23 |
2 files changed, 157 insertions, 6 deletions
diff --git a/source/ByteBuffer.cpp b/source/ByteBuffer.cpp index af7151260..0f360a104 100644 --- a/source/ByteBuffer.cpp +++ b/source/ByteBuffer.cpp @@ -12,7 +12,8 @@ -#define NEEDBYTES(Num) if (!CanReadBytes(Num)) return false;
+#define NEEDBYTES(Num) if (!CanReadBytes(Num)) return false;
+#define PUTBYTES(Num) if (!CanWriteBytes(Num)) return false;
@@ -121,10 +122,19 @@ bool cByteBuffer::CanReadBytes(int a_Count) const +bool cByteBuffer::CanWriteBytes(int a_Count) const
+{
+ return (a_Count <= GetFreeSpace());
+}
+
+
+
+
+
bool cByteBuffer::ReadChar(char & a_Value)
{
NEEDBYTES(1);
- a_Value = m_Buffer[m_ReadPos++];
+ ReadBuf(&a_Value, 1);
return true;
}
@@ -135,7 +145,7 @@ bool cByteBuffer::ReadChar(char & a_Value) bool cByteBuffer::ReadByte(unsigned char & a_Value)
{
NEEDBYTES(1);
- a_Value = (unsigned char)(m_Buffer[m_ReadPos++]);
+ ReadBuf(&a_Value, 1);
return true;
}
@@ -228,6 +238,106 @@ bool cByteBuffer::ReadBEUTF16String16(AString & a_Value) +bool cByteBuffer::WriteChar(char a_Value)
+{
+ PUTBYTES(1);
+ return WriteBuf(&a_Value, 1);
+}
+
+
+
+
+
+bool cByteBuffer::WriteByte(unsigned char a_Value)
+{
+ PUTBYTES(1);
+ return WriteBuf(&a_Value, 1);
+}
+
+
+
+
+
+bool cByteBuffer::WriteBEShort(short a_Value)
+{
+ PUTBYTES(2);
+ short Converted = htons(a_Value);
+ return WriteBuf(&Converted, 2);
+}
+
+
+
+
+
+bool cByteBuffer::WriteBEInt(int a_Value)
+{
+ PUTBYTES(4);
+ int Converted = HostToNetwork4(&a_Value);
+ return WriteBuf(&Converted, 4);
+}
+
+
+
+
+
+bool cByteBuffer::WriteBEInt64(Int64 a_Value)
+{
+ PUTBYTES(8);
+ Int64 Converted = HostToNetwork8(&a_Value);
+ return WriteBuf(&Converted, 8);
+}
+
+
+
+
+
+bool cByteBuffer::WriteBEFloat(float a_Value)
+{
+ PUTBYTES(4);
+ int Converted = HostToNetwork4(&a_Value);
+ return WriteBuf(&Converted, 4);
+}
+
+
+
+
+
+bool cByteBuffer::WriteBEDouble(double a_Value)
+{
+ PUTBYTES(8);
+ Int64 Converted = HostToNetwork8(&a_Value);
+ return WriteBuf(&Converted, 8);
+}
+
+
+
+
+
+
+bool cByteBuffer::WriteBool(bool a_Value)
+{
+ return WriteChar(a_Value ? 1 : 0);
+}
+
+
+
+
+
+bool cByteBuffer::WriteBEUTF16String16(const AString & a_Value)
+{
+ PUTBYTES(2);
+ AString UTF16BE;
+ UTF8ToRawBEUTF16(a_Value.data(), a_Value.size(), UTF16BE);
+ WriteBEShort((short)(UTF16BE.size() / 2));
+ PUTBYTES(UTF16BE.size());
+ WriteBuf(UTF16BE.data(), UTF16BE.size());
+ return true;
+}
+
+
+
+
+
bool cByteBuffer::ReadBuf(void * a_Buffer, int a_Count)
{
NEEDBYTES(a_Count);
@@ -252,6 +362,30 @@ bool cByteBuffer::ReadBuf(void * a_Buffer, int a_Count) +bool cByteBuffer::WriteBuf(const void * a_Buffer, int a_Count)
+{
+ PUTBYTES(a_Count);
+ char * Src = (char *)a_Buffer; // So that we can do byte math
+ int BytesToEndOfBuffer = m_BufferSize - m_WritePos;
+ if (BytesToEndOfBuffer < a_Count)
+ {
+ // Reading across the ringbuffer end, read the first part and adjust parameters:
+ memcpy(m_Buffer + m_WritePos, Src, BytesToEndOfBuffer);
+ Src += BytesToEndOfBuffer;
+ a_Count -= BytesToEndOfBuffer;
+ m_WritePos = 0;
+ }
+
+ // Read the rest of the bytes in a single read (guaranteed to fit):
+ memcpy(m_Buffer + m_WritePos, Src, a_Count);
+ m_WritePos += a_Count;
+ return true;
+}
+
+
+
+
+
bool cByteBuffer::ReadString(AString & a_String, int a_Count)
{
NEEDBYTES(a_Count);
diff --git a/source/ByteBuffer.h b/source/ByteBuffer.h index 68dc48cd9..c0ffed43c 100644 --- a/source/ByteBuffer.h +++ b/source/ByteBuffer.h @@ -45,6 +45,9 @@ public: /// Returns true if the specified amount of bytes are available for reading
bool CanReadBytes(int a_Count) const;
+ /// Returns true if the specified amount of bytes are available for writing
+ bool CanWriteBytes(int a_Count) const;
+
// Read the specified datatype and advance the read pointer; return true if successfully read:
bool ReadChar (char & a_Value);
bool ReadByte (unsigned char & a_Value);
@@ -56,13 +59,27 @@ public: bool ReadBool (bool & a_Value);
bool ReadBEUTF16String16(AString & a_Value);
- /// Reads a_Count bytes into a_Buffer; return true if successful
+ // Write the specified datatype; return true if successfully written
+ bool WriteChar (char a_Value);
+ bool WriteByte (unsigned char a_Value);
+ bool WriteBEShort (short a_Value);
+ bool WriteBEInt (int a_Value);
+ bool WriteBEInt64 (Int64 a_Value);
+ bool WriteBEFloat (float a_Value);
+ bool WriteBEDouble (double a_Value);
+ bool WriteBool (bool a_Value);
+ bool WriteBEUTF16String16(const AString & a_Value);
+
+ /// Reads a_Count bytes into a_Buffer; returns true if successful
bool ReadBuf(void * a_Buffer, int a_Count);
- /// Reads a_Count bytes into a_String; return true if successful
+ /// Writes a_Count bytes into a_Buffer; returns true if successful
+ bool WriteBuf(const void * a_Buffer, int a_Count);
+
+ /// Reads a_Count bytes into a_String; returns true if successful
bool ReadString(AString & a_String, int a_Count);
- /// Reads 2 * a_NumChars bytes and interprets it as a UTF16 string, converting it into UTF8 string a_String
+ /// Reads 2 * a_NumChars bytes and interprets it as a UTF16-BE string, converting it into UTF8 string a_String
bool ReadUTF16String(AString & a_String, int a_NumChars);
/// Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer
|