Skip to content

Commit 72729e4

Browse files
committed
implementing socketToFd
1 parent f853afe commit 72729e4

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Network/Socket.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ module Network.Socket
141141
, withFdSocket
142142
, unsafeFdSocket
143143
, touchSocket
144+
, socketToFd
144145
, fdSocket
145146
, mkSocket
146147
, socketToHandle

Network/Socket/Types.hsc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Network.Socket.Types (
1414
, withFdSocket
1515
, unsafeFdSocket
1616
, touchSocket
17+
, socketToFd
1718
, fdSocket
1819
, mkSocket
1920
, invalidateSocket
@@ -166,6 +167,18 @@ withFdSocket (Socket ref _) f = do
166167
touch ref
167168
return r
168169

170+
-- | Socket is closed and a duplicated file descriptor is returned.
171+
socketToFd :: Socket -> IO CInt
172+
socketToFd s = do
173+
fd <- unsafeFdSocket s
174+
-- FIXME: throw error no if -1
175+
fd2 <- c_dup fd
176+
close s
177+
return fd2
178+
179+
foreign import ccall unsafe "dup"
180+
c_dup :: CInt -> IO CInt
181+
169182
-- | Creating a socket from a file descriptor.
170183
mkSocket :: CInt -> IO Socket
171184
mkSocket fd = do

0 commit comments

Comments
 (0)