The WSPDuplicateSocket function returns a WSAPROTOCOL_INFOW structure that can be used to create a new socket descriptor for a shared socket.
int WSPDuplicateSocket (
SOCKET s,
DWORD dwProcessId,
LPWSAPROTOCOL_INFOW lpProtocolInfo,
LPINT lpErrno
);
A source process calls WSPDuplicateSocket to obtain a special WSAPROTOCOL_INFOW structure. It uses some interprocess communications (IPC) mechanism to pass the contents of this structure to a target process, which in turn uses it in a call to WSPSocket to obtain a descriptor for the duplicated socket. Note that the special WSAPROTOCOL_INFOW structure can only be used once by the target process.
It is the service provider's responsibility to perform whatever operations are needed in the source process context and to create a WSAPROTOCOL_INFOW structure that will be recognized when it subsequently appears as a parameter to WSPSocket in the target processes' context. The provider must then return a socket descriptor that references a common underlying socket. The dwProviderReserved field of the WSAPROTOCOL_INFOW structure is available for the service provider's use, and can be used to store any useful context information, including a duplicated handle.
As is described in section 3.4.1. Descriptor Allocation, when new socket descriptors are allocated IFS providers must call WPUModifyIFSHandle and non-IFS providers must call WPUCreateSocketHandle.
One possible scenario for establishing and using a shared socket in a handoff mode is illustrated below:
Source Process | IPC | Destination Process |
---|---|---|
1) WSPSocket, WSPConnect | ||
2) Request target process ID | Þ | |
3) Receive process ID request and respond | ||
4) Receive process ID | Ü | |
5) Call WSPDuplicateSocket to get a special WSAPROTOCOL_INFOW structure | ||
6) Send WSAPROTOCOL_INFOW structure to target | ||
Þ | 7) Receive WSAPROTOCOL_INFOW structure | |
8) Call WSPSocket to create shared socket descriptor. | ||
10) WSPClosesocket | 9) Use shared socket for data exchange |
If no error occurs, WSPDuplicateSocket returns zero. Otherwise, the value of SOCKET_ERROR is returned, and a specific error number is available in lpErrno.
The descriptors that reference a shared socket can be used independently as far as I/O is concerned. However, the Windows Sockets interface does not implement any type of access control, so it is up to the processes involved to coordinate their operations on a shared socket. A typical use for shared sockets is to have one process that is responsible for creating sockets and establishing connections, hand off sockets to other processes which are responsible for information exchange.
Since what is duplicated are the socket descriptors and not the underlying socket, all of the state associated with a socket is held in common across all the descriptors. For example a WSPSetSockOpt operation performed using one descriptor is subsequently visible using a WSPGetSockOpt from any or all descriptors. A process can call WSPClosesocket on a duplicated socket and the descriptor will become deallocated. The underlying socket, however, will remain open until WSPClosesocket is called by the last remaining descriptor.
Notification on shared sockets is subject to the usual constraints of WSPAsyncSelect and WSPEventSelect. Issuing either of these calls using any of the shared descriptors cancels any previous event registration for the socket, regardless of which descriptor was used to make that registration. Thus, for example, a shared socket cannot deliver FD_READ events to process A and FD_WRITE events to process B. For situations when such tight coordination is required, it is suggested that developers use threads instead of separate processes.
A layered service provider supplies an implementation of this function, but it is also a client of this function if and when it calls WSPDuplicateSocket of the next layer in the protocol chain. Some special considerations apply to this function's lpProtocolInfo parameter as it is propagated down through the layers of the protocol chain.
If the next layer in the protocol chain is another layer then when the next layer's WSPDuplicateSocket is called, this layer must pass to the next layer a lpProtocolInfo that references the same unmodified WSAPROTOCOL_INFOW structure with the same unmodified chain information. However, if the next layer is the base protocol (i.e., the last element in the chain), this layer performs a substitution when calling the base provider's WSPDuplicateSocket. In this case, the base provider's WSAPROTOCOL_INFOW structure should be referenced by the lpProtocolInfo parameter.
One vital benefit of this policy is that base service providers do not have to be aware of protocol chains. This same propagation policy applies when propagating a WSAPROTOCOL_INFOW structure through a layered sequence of other functions such as WSPAddressToString, WSPStartup, WSPSocket, or WSPStringToAddress.
WSAENETDOWN | The network subsystem has failed. |
WSAEINVAL | Indicates that one of the specified parameters was invalid. |
WSAEINPROGRESS | A blocking Windows Sockets call is in progress, or the service provider is still processing a callback function. |
WSAEMFILE | No more socket descriptors are available. |
WSAENOBUFS | No buffer space is available. The socket cannot be created. |
WSAENOTSOCK | The descriptor is not a socket. |
Windows NT: Yes
Windows: Yes
Windows CE: Unsupported.
Header: Declared in ws2spi.h.
WPUCreateSocketHandle, WPUModifyIFSHandle