chriskohlhoff/asio — pull request #9 by VemundH (2014-01-06 21:23:12+00:00)
Title: IOCP - Use ConnectEx in async_connect when available

async_accept was used as motivation. (E.g. win_iocp_socket_connect_op is based on win_iocp_socket_accept_op)

Implementation details/considerations:

1. ConnectEx is available starting with Windows XP.
2. The ConnectEx function pointer must be obtained at run time. The pointer is stored in a static local variable as a potential optimization. The code does not prevent potential race as it does not really matter if multiple threads initialize the static variable (the code is safe).
3. ConnectEx can only be called with connection-oriented sockets. This is tested run-time, and sockets of type ASIO_OS_DEF(SOCK_STREAM) and ASIO_OS_DEF(SOCK_SEQPACKET) are considered connection-oriented. I did not find a simple solution to making this a compile-time test. (SOCK_RDM is also connection-oriented, but omitted as I could not find ASIO_OS_DEF(SOCK_RDM))
4. ConnectEx requires a bound socket. The implementation will always call bind. This is OK also in case the socket is already bound (see comment in code).

The code will fall back on using the reactor implementation if the code targets and earlier version of Windows than XP, the socket is not connection-oriented or in case of failure to obtain the ConnectEx function pointer.

A version without the “optimization” in (2) is available in this commit: https://github.com/VemundH/asio/commit/f89c15ad71f8f3299e5c63e51eeec4f338894c71