Skip to content

Commit

Permalink
implementing socketToFd
Browse files Browse the repository at this point in the history
  • Loading branch information
kazu-yamamoto committed Sep 4, 2019
1 parent 685cec5 commit e7312bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions Network/Socket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ module Network.Socket
, withFdSocket
, unsafeFdSocket
, touchSocket
, socketToFd
, fdSocket
, mkSocket
, socketToHandle
Expand Down
13 changes: 13 additions & 0 deletions Network/Socket/Types.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Network.Socket.Types (
, withFdSocket
, unsafeFdSocket
, touchSocket
, socketToFd
, fdSocket
, mkSocket
, invalidateSocket
Expand Down Expand Up @@ -162,6 +163,18 @@ withFdSocket (Socket ref _) f = do
touch ref
return r

-- | Socket is closed and a duplicated file descriptor is returned.
socketToFd :: Socket -> IO CInt
socketToFd s = do
fd <- unsafeFdSocket s
-- FIXME: throw error no if -1
fd2 <- c_dup fd
close s
return fd2

foreign import ccall unsafe "dup"
c_dup :: CInt -> IO CInt

-- | Creating a socket from a file descriptor.
mkSocket :: CInt -> IO Socket
mkSocket fd = do
Expand Down

0 comments on commit e7312bb

Please sign in to comment.