14
14
module Network.Socket.Types (
15
15
-- * Socket type
16
16
Socket
17
+ , CSocket
17
18
, withFdSocket
18
19
, unsafeFdSocket
19
20
, touchSocket
@@ -103,8 +104,14 @@ import Network.Socket.ReadShow
103
104
104
105
-----------------------------------------------------------------------------
105
106
107
+ #if defined(mingw32_HOST_OS)
108
+ type CSocket = SOCKET
109
+ #else
110
+ type CSocket = CInt
111
+ #endif
112
+
106
113
-- | Basic type for a socket.
107
- data Socket = Socket (IORef CInt ) CInt {- for Show -}
114
+ data Socket = Socket (IORef CSocket ) CSocket {- for Show -}
108
115
109
116
instance Show Socket where
110
117
show (Socket _ ofd) = " <socket: " ++ show ofd ++ " >"
@@ -114,7 +121,7 @@ instance Eq Socket where
114
121
115
122
{-# DEPRECATED fdSocket "Use withFdSocket or unsafeFdSocket instead" #-}
116
123
-- | Currently, this is an alias of `unsafeFdSocket`.
117
- fdSocket :: Socket -> IO CInt
124
+ fdSocket :: Socket -> IO CSocket
118
125
fdSocket = unsafeFdSocket
119
126
120
127
-- | Getting a file descriptor from a socket.
@@ -139,7 +146,7 @@ fdSocket = unsafeFdSocket
139
146
-- 'touchSocket' can be used for this purpose.
140
147
--
141
148
-- A safer option is to use 'withFdSocket' instead.
142
- unsafeFdSocket :: Socket -> IO CInt
149
+ unsafeFdSocket :: Socket -> IO CSocket
143
150
unsafeFdSocket (Socket ref _) = readIORef ref
144
151
145
152
-- | Ensure that the given 'Socket' stays alive (i.e. not garbage-collected)
@@ -171,7 +178,7 @@ touch (IORef (STRef mutVar)) =
171
178
-- descriptor.
172
179
--
173
180
-- Since: 3.1.0.0
174
- withFdSocket :: Socket -> (CInt -> IO r ) -> IO r
181
+ withFdSocket :: Socket -> (CSocket -> IO r ) -> IO r
175
182
withFdSocket (Socket ref _) f = do
176
183
fd <- readIORef ref
177
184
-- Should we throw an exception if the socket is already invalid?
@@ -187,7 +194,7 @@ withFdSocket (Socket ref _) f = do
187
194
-- of unexpectedly being closed if the socket is finalized. It is
188
195
-- now the caller's responsibility to ultimately close the
189
196
-- duplicated file descriptor.
190
- socketToFd :: Socket -> IO CInt
197
+ socketToFd :: Socket -> IO CSocket
191
198
socketToFd s = do
192
199
#if defined(mingw32_HOST_OS)
193
200
fd <- unsafeFdSocket s
@@ -197,7 +204,7 @@ socketToFd s = do
197
204
return fd2
198
205
199
206
foreign import ccall unsafe " wsaDuplicate"
200
- c_wsaDuplicate :: CInt -> IO CInt
207
+ c_wsaDuplicate :: CSocket -> IO CSocket
201
208
#else
202
209
fd <- unsafeFdSocket s
203
210
-- FIXME: throw error no if -1
@@ -206,18 +213,18 @@ foreign import ccall unsafe "wsaDuplicate"
206
213
return fd2
207
214
208
215
foreign import ccall unsafe " dup"
209
- c_dup :: CInt -> IO CInt
216
+ c_dup :: CSocket -> IO CSocket
210
217
#endif
211
218
212
219
-- | Creating a socket from a file descriptor.
213
- mkSocket :: CInt -> IO Socket
220
+ mkSocket :: CSocket -> IO Socket
214
221
mkSocket fd = do
215
222
ref <- newIORef fd
216
223
let s = Socket ref fd
217
224
void $ mkWeakIORef ref $ close s
218
225
return s
219
226
220
- invalidSocket :: CInt
227
+ invalidSocket :: CSocket
221
228
#if defined(mingw32_HOST_OS)
222
229
invalidSocket = # const INVALID_SOCKET
223
230
#else
@@ -226,8 +233,8 @@ invalidSocket = -1
226
233
227
234
invalidateSocket ::
228
235
Socket
229
- -> (CInt -> IO a )
230
- -> (CInt -> IO a )
236
+ -> (CSocket -> IO a )
237
+ -> (CSocket -> IO a )
231
238
-> IO a
232
239
invalidateSocket (Socket ref _) errorAction normalAction = do
233
240
oldfd <- atomicModifyIORef' ref $ \ cur -> (invalidSocket, cur)
@@ -246,7 +253,7 @@ close s = invalidateSocket s (\_ -> return ()) $ \oldfd -> do
246
253
-- closeFdWith avoids the deadlock of IO manager.
247
254
closeFdWith closeFd (toFd oldfd)
248
255
where
249
- toFd :: CInt -> Fd
256
+ toFd :: CSocket -> Fd
250
257
toFd = fromIntegral
251
258
-- closeFd ignores the return value of c_close and
252
259
-- does not throw exceptions
@@ -260,7 +267,7 @@ close' s = invalidateSocket s (\_ -> return ()) $ \oldfd -> do
260
267
-- closeFdWith avoids the deadlock of IO manager.
261
268
closeFdWith closeFd (toFd oldfd)
262
269
where
263
- toFd :: CInt -> Fd
270
+ toFd :: CSocket -> Fd
264
271
toFd = fromIntegral
265
272
closeFd :: Fd -> IO ()
266
273
closeFd fd = do
@@ -269,10 +276,10 @@ close' s = invalidateSocket s (\_ -> return ()) $ \oldfd -> do
269
276
270
277
#if defined(mingw32_HOST_OS)
271
278
foreign import CALLCONV unsafe " closesocket"
272
- c_close :: CInt -> IO CInt
279
+ c_close :: CSocket -> IO CInt
273
280
#else
274
281
foreign import ccall unsafe " close"
275
- c_close :: CInt -> IO CInt
282
+ c_close :: CSocket -> IO CInt
276
283
#endif
277
284
278
285
-----------------------------------------------------------------------------
0 commit comments