https://github.com/andrehrferreira/csharp-bytebuffer
Class for reading and writing binary communication and buffers in C#
https://github.com/andrehrferreira/csharp-bytebuffer
bytebuffer bytebufferpool bytes csharp
Last synced: 9 months ago
JSON representation
Class for reading and writing binary communication and buffers in C#
- Host: GitHub
- URL: https://github.com/andrehrferreira/csharp-bytebuffer
- Owner: andrehrferreira
- Created: 2024-07-01T06:10:17.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-23T19:48:16.000Z (over 1 year ago)
- Last Synced: 2025-03-26T10:21:25.554Z (10 months ago)
- Topics: bytebuffer, bytebufferpool, bytes, csharp
- Language: C#
- Homepage:
- Size: 28.3 KB
- Stars: 2
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Tales Of Shadowland - Byte Buffer C#
`@tos/bytebuffer` is a lightweight and efficient library for managing binary data in C#. It provides functionalities for reading, writing, queuing, and pooling buffers, along with support for generic network connectors (TCP, UDP, WebSocket).
## Features
- Efficient binary data manipulation with `ByteBuffer`.
- Buffer aggregation with `QueueBuffer` to reduce header overhead.
- Buffer pooling with `ByteBufferPool` to optimize memory allocation in high-frequency systems.
- Interface for generic network connectors supporting TCP, UDP, and WebSocket.
## Usage Example
Here is a basic example of how to use the API provided by the library for binary buffer manipulation:
```csharp
using Server;
class Program
{
static void Main()
{
// Initializing a ByteBuffer
ByteBuffer buffer = new ByteBuffer();
buffer.PutInt32(1234).PutString("Hello, ByteBuffer!");
int num = buffer.GetInt32();
string str = buffer.GetString();
Console.WriteLine(num); // 1234
Console.WriteLine(str); // "Hello, ByteBuffer!"
// Using ByteBufferPool to manage buffers
ByteBufferPool pool = new ByteBufferPool();
ByteBuffer pooledBuffer = ConcurrentByteBufferPool.Acquire();
pooledBuffer.PutFloat(3.14f);
ConcurrentByteBufferPool.Release(pooledBuffer);
// Aggregating buffers with QueueBuffer (if implemented)
// This would combine multiple ByteBuffers into one larger buffer
}
}
```
## API
### ByteBuffer
The main class for managing binary data in C#. It provides methods for reading and writing various types of binary data.
- **PutInt32(int value): ByteBuffer**: Inserts a 32-bit integer into the buffer.
- **GetInt32(): int**: Reads a 32-bit integer from the buffer.
- **PutString(string value): ByteBuffer**: Inserts a UTF-8 encoded string into the buffer.
- **GetString(): string**: Reads a UTF-8 encoded string from the buffer.
- **Reset(): void**: Resets the buffer's position for reuse and clears its contents.
### ByteBufferPool
Manages a pool of ByteBuffer instances to avoid frequent memory allocations, optimizing performance in high-load systems.
- **Acquire(): ByteBuffer**: Retrieves a buffer from the pool or creates a new one if necessary.
- **Release(ByteBuffer buffer): void**: Returns a buffer to the pool, making it available for reuse.
### ConcurrentByteBufferPool
A thread-local buffer pool that optimizes the use of ByteBuffer instances in multithreaded environments.
- **Acquire(): ByteBuffer**: Acquires a ByteBuffer from the pool, either locally or from the global pool.
- **Release(ByteBuffer buffer): void**: Releases a buffer to the thread-local pool.
- **Merge(): void**: Merges the local buffer pool back into the global pool.
- **Clear(): ByteBuffer**: Clears the global buffer pool and returns the removed buffers.
### QueueBuffer
Aggregates multiple binary buffers to reduce overhead in packet transmissions.
- **Enqueue(ByteBuffer buffer): void**: Adds a buffer to the queue.
- **Combine(): ByteBuffer**: Combines all enqueued buffers into a single buffer (not implemented in this example but could be added if needed).
## Contribution
Contributions are welcome! If you encounter any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.
## License
This project is licensed under the [MIT License](LICENSE).