【问题标题】:C# - Reuse IPEndPoint ObjectC# - 重用 IPEndPoint 对象
【发布时间】:2020-08-06 15:40:28
【问题描述】:

作为教育项目的一部分,我开始使用 C# 开发高度优化的 MMO 游戏服务器。

我尽量避免额外的内存分配,我想问一下这是否可能 在不分配内存的情况下重用IPEndPoint 对象。

我希望是这样的(我想要的函数是IPEndPoint.DeepCopy):

public void Send(IPEndPoint toIP, byte[] buffer, int numOfBytes)
    {
        // try aquire send event from free list
        SocketAsyncEventArgs e;
        bool successfullyTaken = this._writeEventArgsPool.TryTake(out e);

        // copy buffer
        // ....

        // set the outgoing IP
        IPEndPoint.DeepCopy(toIP, e.RemoteEndPoint);
        
        // send the packet
        //..
    }

谢谢。

【问题讨论】:

  • IPEndPoint 是连接完成时套接字类的属性。端点在连接关闭时被释放,因此无法重用。并且您无法在打开连接时更改端点。所以你不能重用端点。
  • @jdweng 这实际上不是真的。使用 UDP 协议,RecieveMessageFromAsync 会将内容放入 SocketAsyncEventArgs 对象(在 RemoreEndPoint 字段中),不会被释放。
  • 链接中贴出的例子是TCP。 SocketAysncEvent 获取一个套接字,当套接字被释放时,端点被释放。你说的和我说的没什么不同。
  • @jdweng 你又错了,这个对象也用在UDP中,不返回socket,只返回缓冲区中的消息。

标签: c# optimization network-programming memory-efficient


【解决方案1】:

目前无法避免来自 IPEndPoint 的分配。有a proposal!但它没有取得太大进展。可以使用自定义端点来摆弄 SocketAddress 序列化,以实现特定用途,如 No-gc-sockets 中所做的那样!但不能保证此策略适用于 .NET 实现。例如,由于内部实现细节,No-gc-sockets 中的 MutableIPEndPoint 似乎适用于 .NETFramework 4.7.x 但不适用于 .NETCore 3.x。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多