版本 3.5 中的套接字性能增强Socket Performance Enhancements in Version 3.5

版本 3.5 中增强了 System.Net.Sockets.Socket 类,以供使用异步网络 I/O 实现最高性能的应用程序使用。The System.Net.Sockets.Socket class has been enhanced in Version 3.5 for use by applications that use asynchronous network I/O to achieve the highest performance. 已添加了一系列新类,作为 Socket 类的一组增强功能的一部分,这些增强功能提供了一种可供专用高性能套接字应用程序使用的替代异步模式。A series of new classes have been added as part of a set of enhancements to the Socket class that provide an alternative asynchronous pattern that can be used by specialized high-performance socket applications. 这些增强功能专为需要高性能的网络服务器应用程序而设计。These enhancements were specifically designed for network server applications that require high performance. 应用程序可以独占方式使用增强型异步模式,或仅在其应用程序的目标热区域(例如,接收大量数据时)使用。An application can use the enhanced asynchronous pattern exclusively, or only in targeted hot areas of their application (when receiving large amounts of data, for example).

类增强功能Class Enhancements

这些增强功能的主要功能是避免在大容量异步套接字 I/O 期间重复分配和同步对象。The main feature of these enhancements is the avoidance of the repeated allocation and synchronization of objects during high-volume asynchronous socket I/O. 当前由异步套接字 I/O 的 Socket 类实现的 Begin/End 设计模式需要为每个异步套接字操作分配一个 System.IAsyncResult 对象。The Begin/End design pattern currently implemented by the Socket class for asynchronous socket I/O requires a System.IAsyncResult object be allocated for each asynchronous socket operation.

在新的 Socket 类增强功能中,异步套接字操作由应用程序分配和维护的可重用 System.Net.Sockets.SocketAsyncEventArgs 类对象描述。In the new Socket class enhancements, asynchronous socket operations are described by reusable System.Net.Sockets.SocketAsyncEventArgs class objects allocated and maintained by the application. 高性能套接字应用程序非常清楚必须维持的重叠套接字操作的数量。High-performance socket applications know best the amount of overlapped socket operations that must be sustained. 该应用程序可创建所需的 SocketAsyncEventArgs 对象数量。The application can create as many of the SocketAsyncEventArgs objects that it needs. 例如,如果服务器应用程序始终需要 15 个套接字接受操作,以支持传入的客户端连接速率,则可以预先为此分配 15 个可重用的 SocketAsyncEventArgs 对象。For example, if a server application needs to have 15 socket accept operations outstanding at all times to support incoming client connection rates, it can allocate 15 reusable SocketAsyncEventArgs objects in advance for that purpose.

使用此类执行异步套接字操作的模式包括以下步骤:The pattern for performing an asynchronous socket operation with this class consists of the following steps:

  1. 分配一个新的 SocketAsyncEventArgs 上下文对象,或从应用程序池中获取一个空闲对象。Allocate a new SocketAsyncEventArgs context object, or get a free one from an application pool.

  2. 将上下文对象的属性设置为要执行的操作(例如,回调代理方法和数据缓冲区)。Set properties on the context object to the operation about to be performed (the callback delegate method and data buffer, for example).

  3. 调用适当的套接字方法 (xxxAsync) 以启动异步操作。Call the appropriate socket method (xxxAsync) to initiate the asynchronous operation.

  4. 如果异步套接字方法 (xxxAsync) 在回调中返回 true,请查询完成状态的上下文属性。If the asynchronous socket method (xxxAsync) returns true in the callback, query the context properties for completion status.

  5. 如果异步套接字方法 (xxxAsync) 在回调中返回 false,则已同步完成该操作。If the asynchronous socket method (xxxAsync) returns false in the callback, the operation completed synchronously. 可查询上下文属性获取操作结果。The context properties may be queried for the operation result.

  6. 重新使用上下文进行另一项操作,将其放回池中,或放弃它。Reuse the context for another operation, put it back in the pool, or discard it.

新的异步套接字操作上下文对象的生存期由应用程序代码中的引用和异步 I/O 引用确定。The lifetime of the new asynchronous socket operation context object is determined by references in the application code and asynchronous I/O references. 作为参数提交给异步套接字操作方法之一后,应用程序不必保留对异步套接字操作上下文对象的引用。It is not necessary for the application to retain a reference to an asynchronous socket operation context object after it is submitted as a parameter to one of the asynchronous socket operation methods. 完成回调返回之前,应用程序会继续引用它。It will remain referenced until the completion callback returns. 然而,应用程序保留对上下文对象的引用是有利的,这样可将其重新用于将来的异步套接字操作。However it is advantageous for the application to retain the reference to the context object so that it can be reused for a future asynchronous socket operation.

请参阅See also