✅ Dynamic Buffer
Hello !
I want create a dynamic buffer and i didnt know how to proced.
I want Receive bytes of my client, and stock only the bytes receive.
Actualy use :
problem is i use lot of memory unnecessarily if receive little packet
Thanks for your help,
Slycex.
PS : sorry for my english :S
17 Replies
i have test this :
but it does not work
ArraySegment is just representing a portion of a larger backing array; using that isn't going to make you use less memory.
Just use a fixed sized buffer. In your example, 1KiB is not a lot of memory.
Yes in this exemple is not a lot of memory, but the server receive a lot of packet in second
Depending on what you are doing, you might make use of ArrayPool<T>
https://learn.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1?view=net-7.0
ArrayPool Class (System.Buffers)
Provides a resource pool that enables reusing instances of type T[].
I create an emulator for a mmorpg game, the server receive and send a lot packet at second
If they're received sequentially just keep re-using the same buffer.
Not sequentially, it's possible to receive a lot of packet at same time
(exemple if i have 1000players fighting in same time...) just an exemple
Don't packets being read from a socket have to be read sequentially? It's like a stream...
Or do you mean you have like many sockets open at once?
Anyway, I don't think what you are asking for is really possible. When dealing with APIs that operate on byte streams you usually provide a fixed size buffer.
Sorry my english is not good, is difficult for me to make me understand... ^^
Effectively, is sequential but i want a fastest solution to receive / send
using System;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
// ...
// Create a MemoryStream to store the received bytes.
MemoryStream memoryStream = new MemoryStream();
// Create a buffer for receiving data from the client.
byte[] buffer = new byte[1024];
while (true)
{
// Receive data from the client.
int bytesRead = await handler.ReceiveAsync(buffer, SocketFlags.None);
if (bytesRead > 0)
{
// Write the received data to the MemoryStream.
memoryStream.Write(buffer, 0, bytesRead);
}
else
{
// No more data is being received, exit the loop.
break;
}
}
// The MemoryStream now contains all the received bytes.
byte[] receivedBytes = memoryStream.ToArray();
// Do something with the receivedBytes array.
// For example, you can convert it to a string: string receivedData = Encoding.UTF8.GetString(receivedBytes);
thanks for your awnser !
but what is a difference to just keep packet in my buffer, or store my buffer in my memory stream ?
gain power of executing ?
@slycex take a look at https://github.com/microsoft/Microsoft.IO.RecyclableMemoryStream
It solves your problem
GitHub
GitHub - microsoft/Microsoft.IO.RecyclableMemoryStream: A library t...
A library to provide pooling for .NET MemoryStream objects to improve application performance. - GitHub - microsoft/Microsoft.IO.RecyclableMemoryStream: A library to provide pooling for .NET Memor...
I don't think it solves his problem
@mtreit He's using Socket.ReceiveAsync Method and look at the Usage section on GitHub page I sent...they describe such scenarios
it solves both his problems- dynamic buffer and memory allocation
I'm not convinced...I think he wants to avoid allocating any more memory than absolutely needed for a given payload
if that's what he wants, the solution will terribly inefficient
I agree