sources/network/bytestream.h

Thu, 11 Dec 2014 07:18:11 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Thu, 11 Dec 2014 07:18:11 +0200
changeset 8
8b697d30c49f
parent 5
146825d63b9a
child 9
e7a09ceb4505
permissions
-rw-r--r--

- added huffman lib, now capable of initializing an rcon connection!

#pragma once
#include "../main.h"

class String;

enum { MAX_NETWORK_STRING = 0x800 };

// TODO: Make able to handle big-endian too
class Bytestream
{
public:
	static bool sink;

	Bytestream (unsigned long length = 0x800);
	Bytestream (const unsigned char* data, unsigned long length);
	Bytestream (const Vector<unsigned char>& bytes);
	~Bytestream();

	inline METHOD allocated_size() const -> unsigned long;
	inline METHOD bytes_left() const -> unsigned long;
	       METHOD clear() -> void;
	inline METHOD data() -> unsigned char*;
	inline METHOD data() const -> const unsigned char*;
	       METHOD grow_to_fit (unsigned long bytes) -> void;
	inline METHOD position() const -> unsigned long;
	       METHOD read (unsigned char* buffer, unsigned long length, bool* ok = &sink) -> void;
	       METHOD read_byte (bool* ok = &sink) -> char;
	       METHOD read_short (bool* ok = &sink) -> short int;
	       METHOD read_long (bool* ok = &sink) -> long int;
	       METHOD read_string (bool* ok = &sink) -> String;
	       METHOD read_float (bool* ok = &sink) -> float;
	       METHOD resize (unsigned long length) -> void;
	inline METHOD rewind() -> void;
	inline METHOD seek (unsigned long pos) -> void;
	       METHOD write (const unsigned char* val, unsigned int length) -> void;
	       METHOD write_buffer (const Bytestream& other) -> void;
	       METHOD write_buffer (const Vector<unsigned char>& other) -> void;
	       METHOD write_byte (char val) -> void;
	       METHOD write_double (double val) -> void;
	       METHOD write_float (float val) -> void;
	       METHOD write_long (long int val) -> void;
	       METHOD write_short (short int val) -> void;
	       METHOD write_string (const String& val) -> void;
	inline METHOD written_length() const -> unsigned long;

	inline METHOD operator[] (unsigned long idx) -> unsigned char&;
	inline METHOD operator[] (unsigned long idx) const -> unsigned char;

private:
	unsigned char* m_data;
	unsigned char* m_cursor;
	unsigned long m_allocatedSize;
	unsigned long m_writtenLength;

	METHOD init (const unsigned char* data, unsigned long length) -> void;
	METHOD write (unsigned char val) -> void;
	inline METHOD space_left() const -> unsigned long;
};

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::allocated_size() const -> unsigned long
{
	return m_allocatedSize;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::written_length() const -> unsigned long
{
	return m_writtenLength;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::operator[] (unsigned long idx) -> unsigned char&
{
	return m_data[idx];
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::operator[] (unsigned long idx) const -> unsigned char
{
	return m_data[idx];
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::position() const -> unsigned long
{
	return m_cursor - m_data;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::seek (unsigned long pos) -> void
{
	m_cursor = m_data + pos;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::rewind() -> void
{
	m_cursor = m_data;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::bytes_left() const -> unsigned long
{
	return (m_writtenLength - (m_cursor - &m_data[0]));
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::space_left() const -> unsigned long
{
	return (m_allocatedSize - m_writtenLength);
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::data() -> unsigned char*
{
	return m_data;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::data() const -> const unsigned char*
{
	return m_data;
}

mercurial