1414module Network.Socket.Types (
1515 -- * Socket type
1616 Socket
17+ , CSocket
1718 , withFdSocket
1819 , unsafeFdSocket
1920 , touchSocket
@@ -103,8 +104,14 @@ import Network.Socket.ReadShow
103104
104105-----------------------------------------------------------------------------
105106
107+ #if defined(mingw32_HOST_OS)
108+ type CSocket = SOCKET
109+ #else
110+ type CSocket = CInt
111+ #endif
112+
106113-- | Basic type for a socket.
107- data Socket = Socket (IORef CInt ) CInt {- for Show -}
114+ data Socket = Socket (IORef CSocket ) CSocket {- for Show -}
108115
109116instance Show Socket where
110117 show (Socket _ ofd) = " <socket: " ++ show ofd ++ " >"
@@ -114,7 +121,7 @@ instance Eq Socket where
114121
115122{-# DEPRECATED fdSocket "Use withFdSocket or unsafeFdSocket instead" #-}
116123-- | Currently, this is an alias of `unsafeFdSocket`.
117- fdSocket :: Socket -> IO CInt
124+ fdSocket :: Socket -> IO CSocket
118125fdSocket = unsafeFdSocket
119126
120127-- | Getting a file descriptor from a socket.
@@ -139,7 +146,7 @@ fdSocket = unsafeFdSocket
139146-- 'touchSocket' can be used for this purpose.
140147--
141148-- A safer option is to use 'withFdSocket' instead.
142- unsafeFdSocket :: Socket -> IO CInt
149+ unsafeFdSocket :: Socket -> IO CSocket
143150unsafeFdSocket (Socket ref _) = readIORef ref
144151
145152-- | Ensure that the given 'Socket' stays alive (i.e. not garbage-collected)
@@ -171,7 +178,7 @@ touch (IORef (STRef mutVar)) =
171178-- descriptor.
172179--
173180-- Since: 3.1.0.0
174- withFdSocket :: Socket -> (CInt -> IO r ) -> IO r
181+ withFdSocket :: Socket -> (CSocket -> IO r ) -> IO r
175182withFdSocket (Socket ref _) f = do
176183 fd <- readIORef ref
177184 -- Should we throw an exception if the socket is already invalid?
@@ -187,7 +194,7 @@ withFdSocket (Socket ref _) f = do
187194-- of unexpectedly being closed if the socket is finalized. It is
188195-- now the caller's responsibility to ultimately close the
189196-- duplicated file descriptor.
190- socketToFd :: Socket -> IO CInt
197+ socketToFd :: Socket -> IO CSocket
191198socketToFd s = do
192199#if defined(mingw32_HOST_OS)
193200 fd <- unsafeFdSocket s
@@ -197,7 +204,7 @@ socketToFd s = do
197204 return fd2
198205
199206foreign import ccall unsafe " wsaDuplicate"
200- c_wsaDuplicate :: CInt -> IO CInt
207+ c_wsaDuplicate :: CSocket -> IO CSocket
201208#else
202209 fd <- unsafeFdSocket s
203210 -- FIXME: throw error no if -1
@@ -206,18 +213,18 @@ foreign import ccall unsafe "wsaDuplicate"
206213 return fd2
207214
208215foreign import ccall unsafe " dup"
209- c_dup :: CInt -> IO CInt
216+ c_dup :: CSocket -> IO CSocket
210217#endif
211218
212219-- | Creating a socket from a file descriptor.
213- mkSocket :: CInt -> IO Socket
220+ mkSocket :: CSocket -> IO Socket
214221mkSocket fd = do
215222 ref <- newIORef fd
216223 let s = Socket ref fd
217224 void $ mkWeakIORef ref $ close s
218225 return s
219226
220- invalidSocket :: CInt
227+ invalidSocket :: CSocket
221228#if defined(mingw32_HOST_OS)
222229invalidSocket = # const INVALID_SOCKET
223230#else
@@ -226,8 +233,8 @@ invalidSocket = -1
226233
227234invalidateSocket ::
228235 Socket
229- -> (CInt -> IO a )
230- -> (CInt -> IO a )
236+ -> (CSocket -> IO a )
237+ -> (CSocket -> IO a )
231238 -> IO a
232239invalidateSocket (Socket ref _) errorAction normalAction = do
233240 oldfd <- atomicModifyIORef' ref $ \ cur -> (invalidSocket, cur)
@@ -246,7 +253,7 @@ close s = invalidateSocket s (\_ -> return ()) $ \oldfd -> do
246253 -- closeFdWith avoids the deadlock of IO manager.
247254 closeFdWith closeFd (toFd oldfd)
248255 where
249- toFd :: CInt -> Fd
256+ toFd :: CSocket -> Fd
250257 toFd = fromIntegral
251258 -- closeFd ignores the return value of c_close and
252259 -- does not throw exceptions
@@ -260,7 +267,7 @@ close' s = invalidateSocket s (\_ -> return ()) $ \oldfd -> do
260267 -- closeFdWith avoids the deadlock of IO manager.
261268 closeFdWith closeFd (toFd oldfd)
262269 where
263- toFd :: CInt -> Fd
270+ toFd :: CSocket -> Fd
264271 toFd = fromIntegral
265272 closeFd :: Fd -> IO ()
266273 closeFd fd = do
@@ -269,10 +276,10 @@ close' s = invalidateSocket s (\_ -> return ()) $ \oldfd -> do
269276
270277#if defined(mingw32_HOST_OS)
271278foreign import CALLCONV unsafe " closesocket"
272- c_close :: CInt -> IO CInt
279+ c_close :: CSocket -> IO CInt
273280#else
274281foreign import ccall unsafe " close"
275- c_close :: CInt -> IO CInt
282+ c_close :: CSocket -> IO CInt
276283#endif
277284
278285-----------------------------------------------------------------------------
0 commit comments