sources/network/bytestream.cpp

changeset 13
09dcaeaa216b
parent 11
cffa2777d917
child 18
56a1ac7d931b
--- a/sources/network/bytestream.cpp	Sat Dec 13 04:32:15 2014 +0200
+++ b/sources/network/bytestream.cpp	Sat Dec 13 04:50:33 2014 +0200
@@ -31,8 +31,6 @@
 #include "bytestream.h"
 #include <string.h>
 
-bool Bytestream::sink;
-
 // -------------------------------------------------------------------------------------------------
 //
 Bytestream::Bytestream (unsigned long length) :
@@ -122,22 +120,29 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-char Bytestream::read_byte (bool* ok)
+METHOD
+Bytestream::ensure_read_space (unsigned int bytes) -> void
 {
-	*ok = bytes_left() > 0;
-	return *ok ? *m_cursor++ : -1;
+	if (bytes_left() < bytes)
+	{
+		throw IOError (format ("attempted to read %1 byte(s) past the end of bytestream",
+			bytes - bytes_left()));
+	}
 }
 
 // -------------------------------------------------------------------------------------------------
 //
-short int Bytestream::read_short (bool* ok)
+char Bytestream::read_byte()
 {
-	if (bytes_left() < 2)
-	{
-		*ok = false;
-		return false;
-	}
+	ensure_read_space (1);
+	return *m_cursor++;
+}
 
+// -------------------------------------------------------------------------------------------------
+//
+short int Bytestream::read_short()
+{
+	ensure_read_space (2);
 	short int result = 0;
 
 	for (int i = 0; i < 2; ++i)
@@ -149,14 +154,9 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-long int Bytestream::read_long (bool* ok)
+long int Bytestream::read_long()
 {
-	if (bytes_left() < 4)
-	{
-		*ok = false;
-		return -1;
-	}
-
+	ensure_read_space (4);
 	long int result = 0;
 
 	for (int i = 0; i < 4; ++i)
@@ -168,19 +168,15 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-float Bytestream::read_float (bool* ok)
+float Bytestream::read_float()
 {
-	int value = read_long (ok);
-
-	if (*ok == false)
-		return -1.0f;
-
+	int value = read_long();
 	return reinterpret_cast<float&> (value);
 }
 
 // -------------------------------------------------------------------------------------------------
 //
-String Bytestream::read_string (bool* ok)
+String Bytestream::read_string()
 {
 	// Zandronum sends strings of maximum 2048 characters, though it only
 	// reads 2047-character long ones so I guess we can follow up and do
@@ -194,11 +190,8 @@
 	for (stringEnd = m_cursor; *stringEnd != '\0'; ++stringEnd)
 	{
 		if (stringEnd == end)
-		{
 			// past the end of the buffer! Argh!
-			*ok = false;
-			return "";
-		}
+			throw IOError ("unterminated string in packet");
 	}
 
 	m_cursor = stringEnd + 1;
@@ -217,14 +210,9 @@
 // -------------------------------------------------------------------------------------------------
 //
 METHOD
-Bytestream::read (unsigned char* buffer, unsigned long length, bool* ok) -> void
+Bytestream::read (unsigned char* buffer, unsigned long length) -> void
 {
-	if (bytes_left() < length)
-	{
-		*ok = false;
-		return;
-	}
-
+	ensure_read_space (length);
 	memcpy (buffer, m_cursor, length);
 	m_cursor += length;
 }

mercurial