Skip to content

Commit ebd1cc6

Browse files
committed
definging CSocket
1 parent f9a4602 commit ebd1cc6

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

Network/Socket/ByteString/Internal.hs

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ mkInvalidRecvArgError loc = ioeSetErrorString (mkIOError
5353

5454
#if !defined(mingw32_HOST_OS)
5555
foreign import ccall unsafe "writev"
56-
c_writev :: CInt -> Ptr IOVec -> CInt -> IO CSsize
56+
c_writev :: CSocket -> Ptr IOVec -> CInt -> IO CSsize
5757

5858
foreign import ccall unsafe "sendmsg"
59-
c_sendmsg :: CInt -> Ptr (MsgHdr SockAddr) -> CInt -> IO CSsize
59+
c_sendmsg :: CSocket -> Ptr (MsgHdr SockAddr) -> CInt -> IO CSsize
6060

6161
foreign import ccall unsafe "recvmsg"
62-
c_recvmsg :: CInt -> Ptr (MsgHdr SockAddr) -> CInt -> IO CSsize
62+
c_recvmsg :: CSocket -> Ptr (MsgHdr SockAddr) -> CInt -> IO CSsize
6363
#else
6464
-- fixme Handle for SOCKET, see #426
6565
foreign import CALLCONV SAFE_ON_WIN "WSASend"
66-
c_wsasend :: CInt -> Ptr WSABuf -> DWORD -> LPDWORD -> DWORD -> Ptr () -> Ptr () -> IO CInt
66+
c_wsasend :: CSocket -> Ptr WSABuf -> DWORD -> LPDWORD -> DWORD -> Ptr () -> Ptr () -> IO CInt
6767
foreign import CALLCONV SAFE_ON_WIN "WSASendMsg"
68-
c_sendmsg :: CInt -> Ptr (MsgHdr SockAddr) -> DWORD -> LPDWORD -> Ptr () -> Ptr () -> IO CInt
68+
c_sendmsg :: CSocket -> Ptr (MsgHdr SockAddr) -> DWORD -> LPDWORD -> Ptr () -> Ptr () -> IO CInt
6969
foreign import CALLCONV SAFE_ON_WIN "WSARecvMsg"
70-
c_recvmsg :: CInt -> Ptr (MsgHdr SockAddr) -> LPDWORD -> Ptr () -> Ptr () -> IO CInt
70+
c_recvmsg :: CSocket -> Ptr (MsgHdr SockAddr) -> LPDWORD -> Ptr () -> Ptr () -> IO CInt
7171
#endif

Network/Socket/Syscall.hs

+10-10
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ connect :: SocketAddress sa => Socket -> sa -> IO ()
139139
connect s sa = withSocketsDo $ withSocketAddress sa $ \p_sa sz ->
140140
connectLoop s p_sa (fromIntegral sz)
141141

142-
connectLoop :: SocketAddress sa => Socket -> Ptr sa -> CInt -> IO ()
142+
connectLoop :: SocketAddress sa => Socket -> Ptr sa -> CSocket -> IO ()
143143
connectLoop s p_sa sz = withFdSocket s $ \fd -> loop fd
144144
where
145145
errLoc = "Network.Socket.connect: " ++ show s
@@ -223,31 +223,31 @@ accept listing_sock = withNewSocketAddress $ \new_sa sz ->
223223
#endif
224224

225225
foreign import CALLCONV unsafe "socket"
226-
c_socket :: CInt -> CInt -> CInt -> IO CInt
226+
c_socket :: CInt -> CInt -> CInt -> IO CSocket
227227
foreign import CALLCONV unsafe "bind"
228-
c_bind :: CInt -> Ptr sa -> CInt{-CSockLen???-} -> IO CInt
228+
c_bind :: CSocket -> Ptr sa -> CInt{-CSockLen???-} -> IO CInt
229229
foreign import CALLCONV SAFE_ON_WIN "connect"
230-
c_connect :: CInt -> Ptr sa -> CInt{-CSockLen???-} -> IO CInt
230+
c_connect :: CSocket -> Ptr sa -> CInt{-CSockLen???-} -> IO CInt
231231
foreign import CALLCONV unsafe "listen"
232-
c_listen :: CInt -> CInt -> IO CInt
232+
c_listen :: CSocket -> CInt -> IO CInt
233233

234234
#ifdef HAVE_ADVANCED_SOCKET_FLAGS
235235
foreign import CALLCONV unsafe "accept4"
236-
c_accept4 :: CInt -> Ptr sa -> Ptr CInt{-CSockLen???-} -> CInt -> IO CInt
236+
c_accept4 :: CSocket -> Ptr sa -> Ptr CInt{-CSockLen???-} -> CInt -> IO CSocket
237237
#else
238238
foreign import CALLCONV unsafe "accept"
239-
c_accept :: CInt -> Ptr sa -> Ptr CInt{-CSockLen???-} -> IO CInt
239+
c_accept :: CSocket -> Ptr sa -> Ptr CInt{-CSockLen???-} -> IO CSocket
240240
#endif
241241

242242
#if defined(mingw32_HOST_OS)
243243
foreign import CALLCONV safe "accept"
244-
c_accept_safe :: CInt -> Ptr sa -> Ptr CInt{-CSockLen???-} -> IO CInt
244+
c_accept_safe :: CSocket -> Ptr sa -> Ptr CInt{-CSockLen???-} -> IO CSocket
245245
foreign import ccall unsafe "rtsSupportsBoundThreads"
246246
threaded :: Bool
247247
foreign import ccall unsafe "HsNet.h acceptNewSock"
248-
c_acceptNewSock :: Ptr () -> IO CInt
248+
c_acceptNewSock :: Ptr () -> IO CSocket
249249
foreign import ccall unsafe "HsNet.h newAcceptParams"
250-
c_newAcceptParams :: CInt -> CInt -> Ptr a -> IO (Ptr ())
250+
c_newAcceptParams :: CSocket -> CInt -> Ptr a -> IO (Ptr ())
251251
foreign import ccall unsafe "HsNet.h &acceptDoProc"
252252
c_acceptDoProc :: FunPtr (Ptr () -> IO Int)
253253
foreign import ccall unsafe "free"

Network/Socket/Types.hsc

+22-15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
module 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

109116
instance 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
118125
fdSocket = 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
143150
unsafeFdSocket (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
175182
withFdSocket (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
191198
socketToFd s = do
192199
#if defined(mingw32_HOST_OS)
193200
fd <- unsafeFdSocket s
@@ -197,7 +204,7 @@ socketToFd s = do
197204
return fd2
198205

199206
foreign 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

208215
foreign 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
214221
mkSocket 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)
222229
invalidSocket = #const INVALID_SOCKET
223230
#else
@@ -226,8 +233,8 @@ invalidSocket = -1
226233

227234
invalidateSocket ::
228235
Socket
229-
-> (CInt -> IO a)
230-
-> (CInt -> IO a)
236+
-> (CSocket -> IO a)
237+
-> (CSocket -> IO a)
231238
-> IO a
232239
invalidateSocket (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)
271278
foreign import CALLCONV unsafe "closesocket"
272-
c_close :: CInt -> IO CInt
279+
c_close :: CSocket -> IO CInt
273280
#else
274281
foreign import ccall unsafe "close"
275-
c_close :: CInt -> IO CInt
282+
c_close :: CSocket -> IO CInt
276283
#endif
277284

278285
-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)