sources/network/bytestream.cpp

Sun, 14 Dec 2014 19:38:47 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 14 Dec 2014 19:38:47 +0200
changeset 17
50341dec533e
parent 13
09dcaeaa216b
child 18
56a1ac7d931b
permissions
-rw-r--r--

- added command sending

/*
	Copyright 2014 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. Neither the name of the copyright holder nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
	PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
	OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "bytestream.h"
#include <string.h>

// -------------------------------------------------------------------------------------------------
//
Bytestream::Bytestream (unsigned long length) :
	m_data (nullptr)
{
	resize (length);
	clear();
}

// -------------------------------------------------------------------------------------------------
//
Bytestream::Bytestream (const unsigned char* data, unsigned long length) :
	m_data (nullptr)
{
	init (data, length);
}

// -------------------------------------------------------------------------------------------------
//
Bytestream::Bytestream (const Vector<unsigned char>& bytes) :
	m_data (nullptr)
{
	init (bytes.data(), bytes.size());
}

// -------------------------------------------------------------------------------------------------
//
Bytestream::Bytestream (const Bytestream& other) :
	m_data (nullptr)
{
	init (other.data(), other.written_length());
}

// -------------------------------------------------------------------------------------------------
METHOD Bytestream::operator= (const Bytestream& other) -> Bytestream&
{
	init (other.data(), other.written_length());
	return *this;
}

// -------------------------------------------------------------------------------------------------
//
Bytestream::~Bytestream()
{
	delete m_data;
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::resize (unsigned long newsize)
{
	Vector<unsigned char> olddata;
	unsigned long oldsize = 0L;

	if (m_data != nullptr)
	{
		oldsize = allocated_size();
		olddata.resize (oldsize);
		memcpy (olddata.data(), m_data, oldsize);
	}

	delete[] m_data;
	m_allocatedSize = newsize;
	m_data = new unsigned char[newsize];

	if (olddata > 0L)
		memcpy (m_data, olddata, min (oldsize, newsize));
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::init (const unsigned char* data, unsigned long length)
{
	resize (length);
	memcpy (m_data, data, length);
	m_cursor = &m_data[0];
	m_writtenLength = length;
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::clear()
{
	m_cursor = &m_data[0];
	m_writtenLength = 0;
}

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::ensure_read_space (unsigned int bytes) -> void
{
	if (bytes_left() < bytes)
	{
		throw IOError (format ("attempted to read %1 byte(s) past the end of bytestream",
			bytes - bytes_left()));
	}
}

// -------------------------------------------------------------------------------------------------
//
char Bytestream::read_byte()
{
	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)
		result |= m_cursor[i] << (i * 8);

	m_cursor += 2;
	return result;
}

// -------------------------------------------------------------------------------------------------
//
long int Bytestream::read_long()
{
	ensure_read_space (4);
	long int result = 0;

	for (int i = 0; i < 4; ++i)
		result |= m_cursor[i] << (i * 8);

	m_cursor += 4;
	return result;
}

// -------------------------------------------------------------------------------------------------
//
float Bytestream::read_float()
{
	int value = read_long();
	return reinterpret_cast<float&> (value);
}

// -------------------------------------------------------------------------------------------------
//
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
	// the same :-)
	static char buffer[MAX_NETWORK_STRING];
	unsigned char* stringEnd;
	unsigned char* stringBegin = m_cursor;
	unsigned char* end = m_data + allocated_size();

	// 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");
	}

	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);
}

// -------------------------------------------------------------------------------------------------
//
METHOD
Bytestream::read (unsigned char* buffer, unsigned long length) -> void
{
	ensure_read_space (length);
	memcpy (buffer, m_cursor, length);
	m_cursor += length;
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write (unsigned char val)
{
	*m_cursor++ = val;
	m_writtenLength++;
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write (const unsigned char* val, unsigned int length)
{
	grow_to_fit (length);
	memcpy (m_cursor, val, length);
	m_cursor += length;
	m_writtenLength += length;
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::grow_to_fit (unsigned long bytes)
{
	if (space_left() < bytes)
		resize (allocated_size() + bytes + 128);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_byte (char val)
{
	grow_to_fit (1);
	write (val);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_short (short int val)
{
	grow_to_fit (2);

	for (int i = 0; i < 2; ++i)
		write ((val >> (i * 8)) & 0xFF);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_long (long int val)
{
	grow_to_fit (4);

	for (int i = 0; i < 4; ++i)
		write ((val >> (i * 8)) & 0xFF);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_float (float val)
{
	// I know this is probably dangerous but this is what Zandronum does so yeah
	write_long (reinterpret_cast<int&> (val));
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_string (const String& val)
{
	grow_to_fit (val.length() + 1);
	write (reinterpret_cast<const unsigned char*> (val.chars()), val.length());
	write (0);
}

// -------------------------------------------------------------------------------------------------
//
void Bytestream::write_buffer (const Bytestream& other)
{
	write (other.data(), other.written_length());
}

mercurial