Skip to content

Commit 4975027

Browse files
committed
refactoring
1 parent 729482c commit 4975027

1 file changed

Lines changed: 46 additions & 40 deletions

File tree

Network/Socket/Internal.hs

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,26 @@ import Network.Socket.Types
8484
-- | Throw an 'IOError' corresponding to the current socket error.
8585
throwSocketError :: String -- ^ textual description of the error location
8686
-> IO a
87+
#if defined(mingw32_HOST_OS)
88+
throwSocketError name =
89+
c_getLastError >>= throwSocketErrorCode name
90+
#else
91+
throwSocketError = throwErrno
92+
#endif
8793

8894
-- | Like 'throwSocketError', but the error code is supplied as an argument.
8995
--
9096
-- On Windows, do not use errno. Use a system error code instead.
9197
throwSocketErrorCode :: String -> CInt -> IO a
98+
#if defined(mingw32_HOST_OS)
99+
throwSocketErrorCode name rc = do
100+
pstr <- c_getWSError rc
101+
str <- peekCString pstr
102+
ioError (ioeSetErrorString (mkIOError OtherError name Nothing Nothing) str)
103+
#else
104+
throwSocketErrorCode loc errno =
105+
ioError (errnoToIOError loc (Errno errno) Nothing Nothing)
106+
#endif
92107

93108
-- | Throw an 'IOError' corresponding to the current socket error if
94109
-- the IO action returns a result of @-1@. Discards the result of the
@@ -98,6 +113,13 @@ throwSocketErrorIfMinus1_
98113
=> String -- ^ textual description of the location
99114
-> IO a -- ^ the 'IO' operation to be executed
100115
-> IO ()
116+
#if defined(mingw32_HOST_OS)
117+
throwSocketErrorIfMinus1_ name act = do
118+
_ <- throwSocketErrorIfMinus1Retry name act
119+
return ()
120+
#else
121+
throwSocketErrorIfMinus1Retry = throwErrnoIfMinus1Retry
122+
#endif
101123

102124
{-# SPECIALIZE throwSocketErrorIfMinus1_ :: String -> IO CInt -> IO () #-}
103125

@@ -109,6 +131,12 @@ throwSocketErrorIfMinus1Retry
109131
=> String -- ^ textual description of the location
110132
-> IO a -- ^ the 'IO' operation to be executed
111133
-> IO a
134+
#if defined(mingw32_HOST_OS)
135+
throwSocketErrorIfMinus1Retry
136+
= throwSocketErrorIfMinus1ButRetry (const False)
137+
#else
138+
throwSocketErrorIfMinus1_ = throwErrnoIfMinus1_
139+
#endif
112140

113141
{-# SPECIALIZE throwSocketErrorIfMinus1Retry :: String -> IO CInt -> IO CInt #-}
114142

@@ -136,6 +164,13 @@ throwSocketErrorIfMinus1RetryMayBlock
136164
-- immediate retry would block
137165
-> IO a -- ^ the 'IO' operation to be executed
138166
-> IO a
167+
#if defined(mingw32_HOST_OS)
168+
throwSocketErrorIfMinus1RetryMayBlock name _ act
169+
= throwSocketErrorIfMinus1Retry name act
170+
#else
171+
throwSocketErrorIfMinus1RetryMayBlock name on_block act =
172+
throwErrnoIfMinus1RetryMayBlock name act on_block
173+
#endif
139174

140175
{-# SPECIALIZE throwSocketErrorIfMinus1RetryMayBlock
141176
:: String -> IO b -> IO CInt -> IO CInt #-}
@@ -154,22 +189,20 @@ throwSocketErrorIfMinus1RetryMayBlockBut
154189
-- immediate retry would block
155190
-> IO a -- ^ the 'IO' operation to be executed
156191
-> IO a
157-
158-
{-# SPECIALIZE throwSocketErrorIfMinus1RetryMayBlock
159-
:: String -> IO b -> IO CInt -> IO CInt #-}
160-
161192
#if defined(mingw32_HOST_OS)
162-
163-
throwSocketErrorIfMinus1RetryMayBlock name _ act
164-
= throwSocketErrorIfMinus1Retry name act
165-
166193
throwSocketErrorIfMinus1RetryMayBlockBut exempt name _ act
167194
= throwSocketErrorIfMinus1ButRetry exempt name act
195+
#else
196+
throwSocketErrorIfMinus1RetryMayBlockBut _exempt name on_block act =
197+
throwErrnoIfMinus1RetryMayBlock name act on_block
198+
#endif
168199

169-
throwSocketErrorIfMinus1_ name act = do
170-
_ <- throwSocketErrorIfMinus1Retry name act
171-
return ()
200+
{-# SPECIALIZE throwSocketErrorIfMinus1RetryMayBlock
201+
:: String -> IO b -> IO CInt -> IO CInt #-}
202+
203+
-- ---------------------------------------------------------------------
172204

205+
#if defined(mingw32_HOST_OS)
173206
throwSocketErrorIfMinus1ButRetry :: (Eq a, Num a) =>
174207
(CInt -> Bool) -> String -> IO a -> IO a
175208
throwSocketErrorIfMinus1ButRetry exempt name act = do
@@ -189,42 +222,15 @@ throwSocketErrorIfMinus1ButRetry exempt name act = do
189222
else throwSocketError name
190223
else return r
191224

192-
throwSocketErrorIfMinus1Retry
193-
= throwSocketErrorIfMinus1ButRetry (const False)
194-
195-
throwSocketErrorCode name rc = do
196-
pstr <- c_getWSError rc
197-
str <- peekCString pstr
198-
ioError (ioeSetErrorString (mkIOError OtherError name Nothing Nothing) str)
199-
200-
throwSocketError name =
201-
c_getLastError >>= throwSocketErrorCode name
202-
203225
foreign import CALLCONV unsafe "WSAGetLastError"
204226
c_getLastError :: IO CInt
205227

206228
foreign import ccall unsafe "getWSErrorDescr"
207229
c_getWSError :: CInt -> IO (Ptr CChar)
208-
209-
#else
210-
211-
throwSocketErrorIfMinus1RetryMayBlock name on_block act =
212-
throwErrnoIfMinus1RetryMayBlock name act on_block
213-
214-
throwSocketErrorIfMinus1RetryMayBlockBut _exempt name on_block act =
215-
throwErrnoIfMinus1RetryMayBlock name act on_block
216-
217-
throwSocketErrorIfMinus1Retry = throwErrnoIfMinus1Retry
218-
219-
throwSocketErrorIfMinus1_ = throwErrnoIfMinus1_
220-
221-
throwSocketError = throwErrno
222-
223-
throwSocketErrorCode loc errno =
224-
ioError (errnoToIOError loc (Errno errno) Nothing Nothing)
225-
226230
#endif
227231

232+
-- ---------------------------------------------------------------------
233+
228234
-- | Like 'throwSocketErrorIfMinus1Retry', but if the action fails with
229235
-- @EWOULDBLOCK@ or similar, wait for the socket to be read-ready,
230236
-- and try again.

0 commit comments

Comments
 (0)