Skip to content

Commit 9b4b3e7

Browse files
committed
definging CSocket
1 parent 5ef4f27 commit 9b4b3e7

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

Network/Socket/ByteString/Internal.hs

Lines changed: 6 additions & 6 deletions
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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ connect :: SocketAddress sa => Socket -> sa -> IO ()
141141
connect s sa = withSocketsDo $ withSocketAddress sa $ \p_sa sz ->
142142
connectLoop s p_sa (fromIntegral sz)
143143

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

226226
foreign import CALLCONV unsafe "socket"
227-
c_socket :: CInt -> CInt -> CInt -> IO CInt
227+
c_socket :: CInt -> CInt -> CInt -> IO CSocket
228228
foreign import CALLCONV unsafe "bind"
229-
c_bind :: CInt -> Ptr sa -> CInt{-CSockLen???-} -> IO CInt
229+
c_bind :: CSocket -> Ptr sa -> CInt{-CSockLen???-} -> IO CInt
230230
foreign import CALLCONV SAFE_ON_WIN "connect"
231-
c_connect :: CInt -> Ptr sa -> CInt{-CSockLen???-} -> IO CInt
231+
c_connect :: CSocket -> Ptr sa -> CInt{-CSockLen???-} -> IO CInt
232232
foreign import CALLCONV unsafe "listen"
233-
c_listen :: CInt -> CInt -> IO CInt
233+
c_listen :: CSocket -> CInt -> IO CInt
234234

235235
#ifdef HAVE_ADVANCED_SOCKET_FLAGS
236236
foreign import CALLCONV unsafe "accept4"
237-
c_accept4 :: CInt -> Ptr sa -> Ptr CInt{-CSockLen???-} -> CInt -> IO CInt
237+
c_accept4 :: CSocket -> Ptr sa -> Ptr CInt{-CSockLen???-} -> CInt -> IO CSocket
238238
#else
239239
foreign import CALLCONV unsafe "accept"
240-
c_accept :: CInt -> Ptr sa -> Ptr CInt{-CSockLen???-} -> IO CInt
240+
c_accept :: CSocket -> Ptr sa -> Ptr CInt{-CSockLen???-} -> IO CSocket
241241
#endif
242242

243243
#if defined(mingw32_HOST_OS)
244244
foreign import CALLCONV safe "accept"
245-
c_accept_safe :: CInt -> Ptr sa -> Ptr CInt{-CSockLen???-} -> IO CInt
245+
c_accept_safe :: CSocket -> Ptr sa -> Ptr CInt{-CSockLen???-} -> IO CSocket
246246
foreign import ccall unsafe "rtsSupportsBoundThreads"
247247
threaded :: Bool
248248
foreign import ccall unsafe "HsNet.h acceptNewSock"
249-
c_acceptNewSock :: Ptr () -> IO CInt
249+
c_acceptNewSock :: Ptr () -> IO CSocket
250250
foreign import ccall unsafe "HsNet.h newAcceptParams"
251-
c_newAcceptParams :: CInt -> CInt -> Ptr a -> IO (Ptr ())
251+
c_newAcceptParams :: CSocket -> CInt -> Ptr a -> IO (Ptr ())
252252
foreign import ccall unsafe "HsNet.h &acceptDoProc"
253253
c_acceptDoProc :: FunPtr (Ptr () -> IO Int)
254254
foreign import ccall unsafe "free"

Network/Socket/Types.hsc

Lines changed: 22 additions & 15 deletions
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
@@ -107,8 +108,14 @@ import Network.Socket.ReadShow
107108

108109
-----------------------------------------------------------------------------
109110

111+
#if defined(mingw32_HOST_OS)
112+
type CSocket = SOCKET
113+
#else
114+
type CSocket = CInt
115+
#endif
116+
110117
-- | Basic type for a socket.
111-
data Socket = Socket (IORef CInt) CInt {- for Show -}
118+
data Socket = Socket (IORef CSocket) CSocket {- for Show -}
112119

113120
instance Show Socket where
114121
show (Socket _ ofd) = "<socket: " ++ show ofd ++ ">"
@@ -118,7 +125,7 @@ instance Eq Socket where
118125

119126
{-# DEPRECATED fdSocket "Use withFdSocket or unsafeFdSocket instead" #-}
120127
-- | Currently, this is an alias of `unsafeFdSocket`.
121-
fdSocket :: Socket -> IO CInt
128+
fdSocket :: Socket -> IO CSocket
122129
fdSocket = unsafeFdSocket
123130

124131
-- | Getting a file descriptor from a socket.
@@ -143,7 +150,7 @@ fdSocket = unsafeFdSocket
143150
-- 'touchSocket' can be used for this purpose.
144151
--
145152
-- A safer option is to use 'withFdSocket' instead.
146-
unsafeFdSocket :: Socket -> IO CInt
153+
unsafeFdSocket :: Socket -> IO CSocket
147154
unsafeFdSocket (Socket ref _) = readIORef ref
148155

149156
-- | Ensure that the given 'Socket' stays alive (i.e. not garbage-collected)
@@ -175,7 +182,7 @@ touch (IORef (STRef mutVar)) =
175182
-- descriptor.
176183
--
177184
-- Since: 3.1.0.0
178-
withFdSocket :: Socket -> (CInt -> IO r) -> IO r
185+
withFdSocket :: Socket -> (CSocket -> IO r) -> IO r
179186
withFdSocket (Socket ref _) f = do
180187
fd <- readIORef ref
181188
-- Should we throw an exception if the socket is already invalid?
@@ -191,7 +198,7 @@ withFdSocket (Socket ref _) f = do
191198
-- of unexpectedly being closed if the socket is finalized. It is
192199
-- now the caller's responsibility to ultimately close the
193200
-- duplicated file descriptor.
194-
socketToFd :: Socket -> IO CInt
201+
socketToFd :: Socket -> IO CSocket
195202
socketToFd s = do
196203
#if defined(mingw32_HOST_OS)
197204
fd <- unsafeFdSocket s
@@ -201,7 +208,7 @@ socketToFd s = do
201208
return fd2
202209

203210
foreign import ccall unsafe "wsaDuplicate"
204-
c_wsaDuplicate :: CInt -> IO CInt
211+
c_wsaDuplicate :: CSocket -> IO CSocket
205212
#else
206213
fd <- unsafeFdSocket s
207214
-- FIXME: throw error no if -1
@@ -210,18 +217,18 @@ foreign import ccall unsafe "wsaDuplicate"
210217
return fd2
211218

212219
foreign import ccall unsafe "dup"
213-
c_dup :: CInt -> IO CInt
220+
c_dup :: CSocket -> IO CSocket
214221
#endif
215222

216223
-- | Creating a socket from a file descriptor.
217-
mkSocket :: CInt -> IO Socket
224+
mkSocket :: CSocket -> IO Socket
218225
mkSocket fd = do
219226
ref <- newIORef fd
220227
let s = Socket ref fd
221228
void $ mkWeakIORef ref $ close s
222229
return s
223230

224-
invalidSocket :: CInt
231+
invalidSocket :: CSocket
225232
#if defined(mingw32_HOST_OS)
226233
invalidSocket = #const INVALID_SOCKET
227234
#else
@@ -230,8 +237,8 @@ invalidSocket = -1
230237

231238
invalidateSocket ::
232239
Socket
233-
-> (CInt -> IO a)
234-
-> (CInt -> IO a)
240+
-> (CSocket -> IO a)
241+
-> (CSocket -> IO a)
235242
-> IO a
236243
invalidateSocket (Socket ref _) errorAction normalAction = do
237244
oldfd <- atomicModifyIORef' ref $ \cur -> (invalidSocket, cur)
@@ -250,7 +257,7 @@ close s = invalidateSocket s (\_ -> return ()) $ \oldfd -> do
250257
-- closeFdWith avoids the deadlock of IO manager.
251258
closeFdWith closeFd (toFd oldfd)
252259
where
253-
toFd :: CInt -> Fd
260+
toFd :: CSocket -> Fd
254261
toFd = fromIntegral
255262
-- closeFd ignores the return value of c_close and
256263
-- does not throw exceptions
@@ -264,7 +271,7 @@ close' s = invalidateSocket s (\_ -> return ()) $ \oldfd -> do
264271
-- closeFdWith avoids the deadlock of IO manager.
265272
closeFdWith closeFd (toFd oldfd)
266273
where
267-
toFd :: CInt -> Fd
274+
toFd :: CSocket -> Fd
268275
toFd = fromIntegral
269276
closeFd :: Fd -> IO ()
270277
closeFd fd = do
@@ -273,10 +280,10 @@ close' s = invalidateSocket s (\_ -> return ()) $ \oldfd -> do
273280

274281
#if defined(mingw32_HOST_OS)
275282
foreign import CALLCONV unsafe "closesocket"
276-
c_close :: CInt -> IO CInt
283+
c_close :: CSocket -> IO CInt
277284
#else
278285
foreign import ccall unsafe "close"
279-
c_close :: CInt -> IO CInt
286+
c_close :: CSocket -> IO CInt
280287
#endif
281288

282289
-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)