Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade dup() for Windows #97

Open
zwetan opened this issue Jun 1, 2017 · 1 comment
Open

upgrade dup() for Windows #97

zwetan opened this issue Jun 1, 2017 · 1 comment

Comments

@zwetan
Copy link
Member

zwetan commented Jun 1, 2017

see _dup

dup() under POSIX can duplicate file and socket descriptor
but under WIN32 can only duplicate file descriptor.

see Winsock Programmer’s FAQ - BSD Sockets Compatibility

The Unix dup() function duplicates a file handle, and of course also works for sockets.
Under Winsock 2, you can do the same thing with WSADuplicateSocket().
It’s a bit more involved, but the WSADuplicateSocket() documentation in MSDN
has a good step-by-step example showing how to use this mechanism.

see WSADuplicateSocket()

@zwetan
Copy link
Member Author

zwetan commented Jun 1, 2017

in WinPortUtils2.cpp we have WIN32_is_socket()
which allow us to detect if a descriptor is actually a file or a socket.

scenario would be

move our definition from win32-platform2.h

#define VMPI_dup         ::_dup

to VMPI2.h

// ---- C.unistd ---- 
extern int VMPI_ dup(int fildes);

and in the the implementation (pseudo code)

int VMPI_dup(int fildes)
{
    SOCKET sd = FD_TO_SOCKET(fildes);
    
    if(  WIN32_is_socket( sd ) )
    {
        // use WSADuplicateSocket() to duplicate
    }

    return _dup( fildes );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant