什么是 NetMQ API 用于在自定义缓冲区中接收以避免在每个 frame/message 上创建新的字节数组?

What is the NetMQ API for receiving in a custom buffer in order to avoid creating a new byte array on each frame/message?

我有以下代码:

while (!isCancellationRequested)
{
    byte[] frameBytes = socket.ReceiveFrameBytes();
    MyData data = new MyData();
    data.Deserialize(frameBytes);
    // send the data somewhere somehow...
}

我想做的,但我在文档中找不到的是避免在每个 socket.Receive:

上创建新字节数组的东西
byte[] frameBytes = new byte[123];
while (!isCancellationRequested)
{
    socket.ReceiveFrameBytes(frameBytes);
    MyData data = new MyData();
    data.Deserialize(frameBytes);
    // send the data somewhere somehow...
}

我该怎么做?

要使用 NetMQ 获得高性能和零分配,您需要启用 BufferPool 并直接使用 Msg 结构。

BufferPool.SetBufferManagerBufferPool(1024 * 1024, 1024);    

while (!isCancellationRequested)
{
    Msg msg = new Msg();
    msg.InitEmpty();
    socket.Receive(ref msg);
    MyData data = new MyData();
    data.Deserialize(msg.Data, msg.Size);
    msg.Close();
    // send the data somewhere somehow...
}