using System; using System.Net.Sockets; namespace HuizhongLibrary.Network { /// /// Represents one of the new Socket xxxAsync methods in .NET 3.5. /// /// The SocketAsyncEventArgs for use with the method. /// Returns true if the operation completed asynchronously, false otherwise. public delegate Boolean SocketAsyncMethod(SocketAsyncEventArgs args); /// /// Holds helper methods for working with the new Socket xxxAsync methods in .NET 3.5. /// public static class ExtensionMethods { /// /// Extension method to simplyfiy the pattern required by the new Socket xxxAsync methods in .NET 3.5. /// See http://www.flawlesscode.com/post/2007/12/Extension-Methods-and-SocketAsyncEventArgs.aspx /// /// The socket this method acts on. /// The xxxAsync method to be invoked. /// The callback for the method. Note: The Completed event must already have been attached to the same. /// The SocketAsyncEventArgs to be used with this call. public static void InvokeAsyncMethod(this Socket socket, SocketAsyncMethod method, EventHandler callback, SocketAsyncEventArgs args) { if (!method(args)) { callback(socket, args); } } } }