2

我有以下代码:

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...
}

我怎样才能做到这一点?

4

1 回答 1

3

要使用 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...
}
于 2017-02-07T08:26:25.227 回答