| Dr. McKay |
07-05-2015 01:49 |
[INC] ByteBuffer
Version 1
Best used with networking (Socket extension, etc.), this allows you to create byte-array buffers in SourceMod plugins. Transitional syntax only.
On load, a plugin implementing bytebuffer.inc will allocate MAX_BUFFERS * MAX_BUFFER_LENGTH bytes of memory (default for MAX_BUFFERS is 32, MAX_BUFFER_LENGTH is 1024 for a total of 32 KiB of memory). You can define MAX_BUFFERS and MAX_BUFFER_LENGTH to your own values prior to including bytebuffer.inc if you wish.
MAX_BUFFERS controls how many buffers can be open at one time. MAX_BUFFER_LENGTH controls how much data can be written to any particular buffer.
API:
PHP Code:
/** * Creates a new ByteBuffer and returns a handle to it * * @param bool littleEndian true if numbers should be treated as little-endian, false for big-endian (default true for little-endian) * @param char data A string containing data to initialize this buffer with (for reading) * @param int dataSize The size in bytes of the input data * @return ByteBuffer An integer between 0 and MAX_BUFFERS - 1 which refers to this ByteBuffer */ ByteBuffer CreateByteBuffer(bool littleEndian = true, const char data[] = "", int dataSize = 0)
/** * @property int The current read/write offset of this buffer (starting at 0) */ ByteBuffer.Cursor
/** * @property bool true if this buffer is treated as little-endian */ ByteBuffer.LittleEndian
/** * @property bool true if this buffer is treated as big-endian */ ByteBuffer.BigEndian
/** * Writes an unsigned byte of data (0 - 255 inclusive) and increments the Cursor by 1 * * @param int data The data to write * @noreturn */ void ByteBuffer.WriteByte(int data)
/** * Writes an unsigned short (two bytes of data, 0 - 65535 inclusive) and increments the Cursor by 2 * * @param int data The data to write * @noreturn */ void ByteBuffer.WriteShort(int data)
/** * Writes a 4-byte integer and increments the Cursor by 4 * * @param int data The data to write * @noreturn */ void ByteBuffer.WriteInt(int data)
/** * Writes a null-terminated string and increments the Cursor by strlen(input) + 1 * * @param char[] input The string to write * @noreturn */ void ByteBuffer.WriteString(const char[] input)
/** * Reads an unsigned byte of data (0 - 255 inclusive) and increments the Cursor by 1 * * @return int Data at current byte */ int ByteBuffer.ReadByte()
/** * Reads an unsigned short (two bytes of data, 0 - 65535 inclusive) and increments the Cursor by 2 * * @return int Data at current two bytes */ int ByteBuffer.ReadShort()
/** * Reads a 4-byte integer and increments the Cursor by 4 * * @return int Data at current four bytes */ int ByteBuffer.ReadInt()
/** * Reads a null-terminated string and increments the Cursor by strlen(output) + 1 * * @param char[] output Buffer to which we will write the string * @param int maxlen Maximum length of output buffer * @return int Number of bytes written to output buffer */ int ByteBuffer.ReadString(char[] output, int maxlen)
/** * Dumps the contents of this buffer from first byte to Cursor * * @param char[] output Buffer to which we will write this buffer's contents * @param int maxlen Maximum length of output buffer * @return int Number of bytes written to output buffer */ int ByteBuffer.Dump(char[] output, int maxlen)
/** * Appends the contents of another ByteBuffer to the current one. Data will be written to current buffer starting at Cursor. * * @param ByteBuffer buffer Buffer whose contents will be appended to this one. Data will be read from buffer from 0 to buffer's Cursor. * @return int Number of bytes written to current buffer */ int ByteBuffer.Append(ByteBuffer buffer)
/** * Extracts the contents of the current buffer to a new ByteBuffer, starting at the Cursor. * * @param int length Maximum number of bytes that will be extracted and written to the new buffer. * @return ByteBuffer A new ByteBuffer containing the extracted contents (new buffer's Cursor will be 0). */ ByteBuffer ByteBuffer.Extract(int length)
/** * Resets the buffer's Cursor to 0 * * @noreturn */ void ByteBuffer.Reset()
/** * Closes the current buffer. All buffers must be closed when no longer in use. * This simply marks the buffer as no longer in use. Memory for all possible buffer (MAX_BUFFERS) will be * allocated on plugin load, and will only be freed on plugin unload. Any data currently in the buffer * will be retained, but will be overwritten when this buffer index is reused. Any attempts to read/write * from a closed buffer will throw an error. * * @noreturn */ void ByteBuffer.Close()
Download
View Source
|