AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [INC] ByteBuffer (https://forums.alliedmods.net/showthread.php?t=265789)

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[] outputint 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[] outputint 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

KyleS 07-05-2015 02:11

Re: [INC] ByteBuffer
 
Quote:

Originally Posted by Dr. McKay (Post 2315750)
[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.

Wouldn't using something like a DataPack be far more appropriate here?

Dr. McKay 07-06-2015 18:44

Re: [INC] ByteBuffer
 
Quote:

Originally Posted by KyleS (Post 2315754)
Wouldn't using something like a DataPack be far more appropriate here?

afaik, DataPacks can only read/write entire cells at a time.

d_a_parker 04-01-2016 10:46

Re: [INC] ByteBuffer
 
Hello!

Just a quick question, is it possible to get the length of a ByteBuffer? I thought maybe you could call ReadByte() in a loop with a counter until you reach the end of the buffer, but what exactly will happen when ReadByte() is called beyond the end?

Thanks!

Dr. McKay 04-01-2016 12:51

Re: [INC] ByteBuffer
 
Quote:

Originally Posted by d_a_parker (Post 2407330)
Hello!

Just a quick question, is it possible to get the length of a ByteBuffer? I thought maybe you could call ReadByte() in a loop with a counter until you reach the end of the buffer, but what exactly will happen when ReadByte() is called beyond the end?

Thanks!

All buffers have the same length, MAX_BUFFER_LENGTH, which defaults to 1024 bytes. You're responsible for keeping track of the maximum cursor position that holds any meaningful data.

d_a_parker 04-01-2016 14:37

Re: [INC] ByteBuffer
 
I thought of that right after I posted the question. Makes sense. Thanks Doc!


All times are GMT -4. The time now is 00:46.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.