Corsaair / redtamarin

AS3 running on the command line / server side
http://redtamarin.com
Other
119 stars 23 forks source link

upgrade dup() for Windows #97

Open zwetan opened 7 years ago

zwetan commented 7 years ago

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 commented 7 years ago

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 );
}