sources/network/bytestream.cpp

branch
protocol5
changeset 106
7b156b764d11
parent 99
f9f73eeba3b7
child 109
e4966d7e615d
--- a/sources/network/bytestream.cpp	Sat Jan 09 02:35:00 2016 +0200
+++ b/sources/network/bytestream.cpp	Sat Jan 09 17:41:21 2016 +0200
@@ -30,6 +30,7 @@
 
 #include "bytestream.h"
 #include <string.h>
+BEGIN_ZFC_NAMESPACE
 
 // -------------------------------------------------------------------------------------------------
 //
@@ -72,8 +73,7 @@
 }
 
 // -------------------------------------------------------------------------------------------------
-METHOD
-Bytestream::operator= (const Bytestream& other) -> Bytestream&
+Bytestream& Bytestream::operator= (const Bytestream& other)
 {
 	init (other.data(), other.written_length());
 	return *this;
@@ -81,8 +81,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::resize (unsigned long newsize) -> void
+void Bytestream::resize (unsigned long newsize)
 {
 	Vector<unsigned char> olddata;
 	unsigned long oldsize = 0L;
@@ -98,14 +97,13 @@
 	m_allocatedSize = newsize;
 	m_data = new unsigned char[newsize];
 
-	if (olddata > 0L)
+	if (oldsize > 0L)
 		memcpy (m_data, olddata, min (oldsize, newsize));
 }
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::init (const unsigned char* data, unsigned long length) -> void
+void Bytestream::init (const unsigned char* data, unsigned long length)
 {
 	resize (length);
 	memcpy (m_data, data, length);
@@ -115,8 +113,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::clear() -> void
+void Bytestream::clear()
 {
 	m_cursor = &m_data[0];
 	m_writtenLength = 0;
@@ -124,8 +121,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::ensure_read_space (unsigned int bytes) -> void
+void Bytestream::ensure_read_space (unsigned int bytes)
 {
 	if (bytes_left() < bytes)
 	{
@@ -140,8 +136,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::read_byte() -> char
+int8_t Bytestream::read_byte()
 {
 	ensure_read_space (1);
 	return *m_cursor++;
@@ -149,8 +144,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::read_short() -> short int
+int16_t Bytestream::read_short()
 {
 	ensure_read_space (2);
 	short int result = 0;
@@ -164,8 +158,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::read_long() -> long int
+int32_t Bytestream::read_long()
 {
 	ensure_read_space (4);
 	long int result = 0;
@@ -179,8 +172,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::read_float() -> float
+float Bytestream::read_float()
 {
 	float value;
 	int intvalue = read_long();
@@ -190,8 +182,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::read_string() -> String
+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
@@ -201,22 +192,18 @@
 	unsigned char* stringBegin = m_cursor;
 	unsigned char* end = m_data + allocated_size();
 
-	// where's the end of the string?
+	// Where's the end of the string?
 	for (stringEnd = m_cursor; *stringEnd != '\0'; ++stringEnd)
 	{
 		if (stringEnd == end)
-			// past the end of the buffer! Argh!
-			throw IOError ("unterminated string in packet");
+		{
+			// Past the end of the buffer
+			throw IOError ("unterminated or too long string in packet");
+		}
 	}
 
+	unsigned int length = stringEnd - m_cursor;
 	m_cursor = stringEnd + 1;
-	unsigned int length = stringEnd - m_cursor;
-
-	// ensure we won't write past the buffer (note: we still moved
-	// past the excess bytes in the above statement, those are ignored)
-	if (length >= MAX_NETWORK_STRING)
-		length = MAX_NETWORK_STRING - 1;
-
 	memcpy (buffer, stringBegin, length);
 	buffer[length] = '\0';
 	return String (buffer);
@@ -224,8 +211,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::read (unsigned char* buffer, unsigned long length) -> void
+void Bytestream::read (unsigned char* buffer, unsigned long length)
 {
 	ensure_read_space (length);
 	memcpy (buffer, m_cursor, length);
@@ -234,8 +220,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::write (unsigned char val) -> void
+void Bytestream::write (unsigned char val)
 {
 	*m_cursor++ = val;
 	m_writtenLength++;
@@ -243,8 +228,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::write (const unsigned char* val, unsigned int length) -> void
+void Bytestream::write (const unsigned char* val, unsigned int length)
 {
 	grow_to_fit (length);
 	memcpy (m_cursor, val, length);
@@ -254,8 +238,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::grow_to_fit (unsigned long bytes) -> void
+void Bytestream::grow_to_fit (unsigned long bytes)
 {
 	if (space_left() < bytes)
 		resize (allocated_size() + bytes + 128);
@@ -263,8 +246,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::write_byte (char val) -> void
+void Bytestream::write_byte (int8_t val)
 {
 	grow_to_fit (1);
 	write (val);
@@ -272,8 +254,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::write_short (short int val) -> void
+void Bytestream::write_short (int16_t val)
 {
 	grow_to_fit (2);
 
@@ -283,8 +264,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::write_long (long int val) -> void
+void Bytestream::write_long (int32_t val)
 {
 	grow_to_fit (4);
 
@@ -294,8 +274,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::write_float (float val) -> void
+void Bytestream::write_float (float val)
 {
 	// I know this is probably dangerous but this is what Zandronum does so yeah
 	int intvalue;
@@ -305,8 +284,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::write_string (const String& val) -> void
+void Bytestream::write_string (const String& val)
 {
 	grow_to_fit (val.length() + 1);
 	write (reinterpret_cast<const unsigned char*> (val.chars()), val.length());
@@ -315,8 +293,9 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-Bytestream::write_buffer (const Bytestream& other) -> void
+void Bytestream::write_buffer (const Bytestream& other)
 {
 	write (other.data(), other.written_length());
 }
+
+END_ZFC_NAMESPACE
\ No newline at end of file

mercurial