Share via


WSPDuplicateSocket (Windows Embedded CE 6.0)

1/6/2010

This function returns a structure that can be used to create a new socket descriptor for a shared socket.

Note

The Windows Sockets service provider included in Windows Embedded CE does not support socket sharing. You can, however, create a customized service provider that supports shared sockets.

Syntax

int WSPDuplicateSocket(
  SOCKET s,
  DWORD dwProcessId,
  LPWSAPROTOCOL_INFOW lpProtocolInfo,
  LPINT lpErrno 
);

Parameters

  • s
    [in] Specifies the local socket descriptor.
  • dwProcessId
    [in] Specifies the identifier of the target process for which the shared socket will be used.
  • lpProtocolInfo
    [out] Pointer to a buffer allocated by the client that is large enough to contain a WSAPROTOCOL_INFOW structure. The service provider copies the protocol information structure contents to this buffer.
  • lpErrno
    [out] Pointer to the error code.

Return Value

This function always returns SOCKET_ERROR and sets lpErrno to WSASYSCALLFAILURE on Windows Embedded CE.

Remarks

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

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 process context. The provider must then return a socket descriptor that references a common underlying socket. The dwProviderReserved member 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.

When new socket descriptors are allocated, non-IFS providers must call WPUCreateSocketHandle.

The following table shows one possible scenario for establishing and using a shared socket in a handoff mode.

Source process IPC Destination process

1. WSPSocket, WSPConnect.

2. Requests target process identifier.

...

3. Receives process identifier request and respond.

4. Receives process identifier.

5. Calls WSPDuplicateSocket to get a special WSAPROTOCOL_INFOW structure.

6. Sends WSAPROTOCOL_INFOW structure to target.

7. Receives WSAPROTOCOL_INFOW structure.

8. Calls WSPSocket to create shared socket descriptor.

10. WSPCloseSocket

9. Uses shared socket for data exchange.

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 that are responsible for information exchange.

Because what are duplicated are the socket descriptors and not the underlying socket, all the states associated with a socket are 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 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 also is a client of this function when it calls WSPDuplicateSocket of the next layer in the provider chain. Some special considerations apply to this function's lpProtocolInfo parameter as it is propagated down through the layers of the provider chain.

If the next layer in the provider 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 (that is, 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 provider chains. This same policy applies when propagating a WSAPROTOCOL_INFOW structure through a layered sequence of other functions such as WSPAddressToString, WSPStartup, WSPSocket, or WSPStringToAddress.

Requirements

Header ws2spi.h
Library Ws2.lib
Windows Embedded CE Windows CE .NET 4.0 and later

See Also

Reference

Winsock SPI Functions
WPUCreateSocketHandle