0

I have video data and am generating new frames for many clients regularly. I'm getting the frames from the library as an IntPtr and an int representing the size of the byte array. Currently I'm turning that into a "Memory" and sending it over a NetworkStream achieving zero-copying of the rather large buffer once I have it.

I would like to try to do the same thing using SocketAsyncEventArgs to see if it performs better and try those UserTokens for multiple clients. However, I can't figure out how to tell SocketAsyncEventArgs to send a chunk of memory pointed to by an IntPtr with a int representing a known size while maintaining zero-copy. Seems like some clever Marshalling or "MemoryMarshal" should do the trick but I'm not seeing it.

Do you see a way to accomplish this?

mczarnek
  • 1,305
  • 2
  • 11
  • 24
  • You need to copy the IntPtr to a byte array and then send the array : byte[] data = new byte[1024]; Marshal.Copy(source, data, 0, data.Length); Where source is the IntPtr. – jdweng Jan 07 '20 at 08:20
  • The memory copy is not going to be the bottleneck. The network will be. – David Heffernan Jan 07 '20 at 09:36

1 Answers1

1

Use .Net Core 2.1+

You can set a buffer of Memory using SetBuffer(Memory<byte>) see here

public void SetBuffer (Memory<byte> buffer);

In .Net Framework you're going to need to copy it to a byte[]. But as the comments above state, this isn't going to be your bottle neck.

Rowan Smith
  • 1,815
  • 15
  • 29