【发布时间】: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