Raised This Month: $32 Target: $400
 8% 

[INC] ByteBuffer


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 07-05-2015 , 01:49   [INC] ByteBuffer
Reply With Quote #1

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
__________________

Last edited by Dr. McKay; 07-05-2015 at 01:55.
Dr. McKay is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 07-05-2015 , 02:11   Re: [INC] ByteBuffer
Reply With Quote #2

Quote:
Originally Posted by Dr. McKay View Post
[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?
KyleS is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 07-06-2015 , 18:44   Re: [INC] ByteBuffer
Reply With Quote #3

Quote:
Originally Posted by KyleS View Post
Wouldn't using something like a DataPack be far more appropriate here?
afaik, DataPacks can only read/write entire cells at a time.
__________________

Last edited by Dr. McKay; 07-06-2015 at 18:44.
Dr. McKay is offline
d_a_parker
Junior Member
Join Date: Aug 2009
Location: Utica, NY (USA)
Old 04-01-2016 , 10:46   Re: [INC] ByteBuffer
Reply With Quote #4

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!
d_a_parker is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 04-01-2016 , 12:51   Re: [INC] ByteBuffer
Reply With Quote #5

Quote:
Originally Posted by d_a_parker View Post
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.
__________________
Dr. McKay is offline
d_a_parker
Junior Member
Join Date: Aug 2009
Location: Utica, NY (USA)
Old 04-01-2016 , 14:37   Re: [INC] ByteBuffer
Reply With Quote #6

I thought of that right after I posted the question. Makes sense. Thanks Doc!
d_a_parker is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:15.


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