-
Notifications
You must be signed in to change notification settings - Fork 188
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
implementing socketToFd #424
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ module Network.Socket.Types ( | |
, withFdSocket | ||
, unsafeFdSocket | ||
, touchSocket | ||
, socketToFd | ||
, fdSocket | ||
, mkSocket | ||
, invalidateSocket | ||
|
@@ -166,6 +167,33 @@ withFdSocket (Socket ref _) f = do | |
touch ref | ||
return r | ||
|
||
-- | Socket is closed and a duplicated file descriptor is returned. | ||
-- The duplicated descriptor is no longer subject to the possibility | ||
-- of unexpectedly being closed if the socket is finalized. It is | ||
-- now the caller's responsibility to ultimately close the | ||
-- duplicated file descriptor. | ||
socketToFd :: Socket -> IO CInt | ||
socketToFd s = do | ||
#if defined(mingw32_HOST_OS) | ||
fd <- unsafeFdSocket s | ||
fd2 <- c_wsaDuplicate fd | ||
-- FIXME: throw error no if -1 | ||
close s | ||
return fd2 | ||
|
||
foreign import ccall unsafe "wsaDuplicate" | ||
c_wsaDuplicate :: CInt -> IO CInt | ||
#else | ||
fd <- unsafeFdSocket s | ||
-- FIXME: throw error no if -1 | ||
fd2 <- c_dup fd | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ IIRC, on windows INVALID_SOCKET is not That said, yes, this is the general idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mysteriously, AppVeyor can build this: https://ci.appveyor.com/project/kazu-yamamoto/network/builds/27135892 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Mistuke Would you suggest a proper way on Windows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually think this may work fine.
and also on the docs for Socket handles https://docs.microsoft.com/en-us/windows/win32/winsock/socket-handles-2. But reason I think this may work is because on that same page it says
So a So easiest way is to test it and see. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean we should merge this PR and release a new version and see what will happen? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, I mean write a simple test to see if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can do both for you if you want but it'll have to wait for the weekend :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem. I can wait. Thanks in advance! |
||
close s | ||
return fd2 | ||
|
||
foreign import ccall unsafe "dup" | ||
c_dup :: CInt -> IO CInt | ||
#endif | ||
|
||
-- | Creating a socket from a file descriptor. | ||
mkSocket :: CInt -> IO Socket | ||
mkSocket fd = do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation can then say, that the duplicated descriptor is no longer subject to the possibility of unexpectedly being closed if the socket is finalized. It is now the caller's responsibility to ultimately close the duplicated file descriptor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks and done.