C
C#15mo ago
Slycex

✅ 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 :
byte buffer = new byte[1024]
var received = await handler.ReceiveAsync(buffer, SocketFlags.None)
byte buffer = new byte[1024]
var received = await handler.ReceiveAsync(buffer, SocketFlags.None)
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
Slycex
SlycexOP15mo ago
i have test this :
ArraySegment<byte> buffer = new ArraySegment<byte>();
var received = await handler.ReceiveAsync(buffer, SocketFlags.None);
ArraySegment<byte> buffer = new ArraySegment<byte>();
var received = await handler.ReceiveAsync(buffer, SocketFlags.None);
but it does not work
mtreit
mtreit15mo ago
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.
Slycex
SlycexOP15mo ago
Yes in this exemple is not a lot of memory, but the server receive a lot of packet in second
mtreit
mtreit15mo ago
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[].
Slycex
SlycexOP15mo ago
I create an emulator for a mmorpg game, the server receive and send a lot packet at second
mtreit
mtreit15mo ago
If they're received sequentially just keep re-using the same buffer.
Slycex
SlycexOP15mo ago
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
mtreit
mtreit15mo ago
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.
Slycex
SlycexOP15mo ago
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
softmek
softmek15mo ago
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);
Slycex
SlycexOP15mo ago
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 ?
David_F
David_F15mo ago
@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...
mtreit
mtreit15mo ago
I don't think it solves his problem
David_F
David_F15mo ago
@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
mtreit
mtreit15mo ago
I'm not convinced...I think he wants to avoid allocating any more memory than absolutely needed for a given payload
David_F
David_F15mo ago
if that's what he wants, the solution will terribly inefficient
mtreit
mtreit15mo ago
I agree
Want results from more Discord servers?
Add your server