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