【发布时间】:2010-11-21 23:00:32
【问题描述】:
我在 SMTP 邮件服务器 (LumiSoft Mail Server) 的代码中发现了以下代码。根据方法名,测试平台是否支持I/O Completion Ports。
/// <summary>
/// Gets if IO completion ports supported by OS.
/// </summary>
/// <returns></returns>
public static bool IsIoCompletionPortsSupported()
{
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
try{
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.SetBuffer(new byte[0],0,0);
e.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback,111);
s.SendToAsync(e)
return true;
}
catch(NotSupportedException nX){
string dummy = nX.Message;
return false;
}
finally{
s.Close();
}
}
它似乎工作正常,但在 Mono/Linux 上失败了。方法SendToAsync,正如它的名字所说,异步执行。它甚至在另一个线程中执行。但是,当它开始执行时,该方法的 finally 部分已经关闭了套接字,并在另一个线程中导致了ObjectDisposedException。
那么,测试 IOCP 支持的技术是否不正确?为什么它可以在 Windows 上运行?测试 IOCP 支持的正确方法是什么?
【问题讨论】:
-
I/O 完成端口与异步支持不同。 IOCP 是特定于 Windows 的。 Linux 使用
epoll(),而不是 IOCP。 -
Mono 模拟 IOCP。见bugzilla.novell.com/show_bug.cgi?id=644428