Skip to content

Commit 0ede619

Browse files
committed
style only
1 parent 176aa09 commit 0ede619

File tree

1 file changed

+104
-89
lines changed

1 file changed

+104
-89
lines changed

Network/Socket/Options.hsc

Lines changed: 104 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import Network.Socket.Internal
3838
import Network.Socket.Types
3939
import Network.Socket.ReadShow
4040

41-
-----------------------------------------------------------------------------
41+
----------------------------------------------------------------
4242
-- Socket Properties
4343

4444
-- | Socket options for use with 'setSocketOption' and 'getSocketOption'.
@@ -55,18 +55,75 @@ data SocketOption = SockOpt
5555
#endif
5656
deriving (Eq)
5757

58+
----------------------------------------------------------------
59+
60+
socketOptionBijection :: Bijection SocketOption String
61+
socketOptionBijection =
62+
[ (UnsupportedSocketOption, "UnsupportedSocketOption")
63+
, (Debug, "Debug")
64+
, (ReuseAddr, "ReuseAddr")
65+
, (SoDomain, "SoDomain")
66+
, (Type, "Type")
67+
, (SoProtocol, "SoProtocol")
68+
, (SoError, "SoError")
69+
, (DontRoute, "DontRoute")
70+
, (Broadcast, "Broadcast")
71+
, (SendBuffer, "SendBuffer")
72+
, (RecvBuffer, "RecvBuffer")
73+
, (KeepAlive, "KeepAlive")
74+
, (OOBInline, "OOBInline")
75+
, (Linger, "Linger")
76+
, (ReusePort, "ReusePort")
77+
, (RecvLowWater, "RecvLowWater")
78+
, (SendLowWater, "SendLowWater")
79+
, (RecvTimeOut, "RecvTimeOut")
80+
, (SendTimeOut, "SendTimeOut")
81+
, (UseLoopBack, "UseLoopBack")
82+
, (MaxSegment, "MaxSegment")
83+
, (NoDelay, "NoDelay")
84+
, (UserTimeout, "UserTimeout")
85+
, (Cork, "Cork")
86+
, (TimeToLive, "TimeToLive")
87+
, (RecvIPv4TTL, "RecvIPv4TTL")
88+
, (RecvIPv4TOS, "RecvIPv4TOS")
89+
, (RecvIPv4PktInfo, "RecvIPv4PktInfo")
90+
, (IPv6Only, "IPv6Only")
91+
, (RecvIPv6HopLimit, "RecvIPv6HopLimit")
92+
, (RecvIPv6TClass, "RecvIPv6TClass")
93+
, (RecvIPv6PktInfo, "RecvIPv6PktInfo")
94+
]
95+
96+
instance Show SocketOption where
97+
showsPrec = bijectiveShow socketOptionBijection def
98+
where
99+
defname = "SockOpt"
100+
unwrap = \(CustomSockOpt nm) -> nm
101+
def = defShow defname unwrap showIntInt
102+
103+
104+
instance Read SocketOption where
105+
readPrec = bijectiveRead socketOptionBijection def
106+
where
107+
defname = "SockOpt"
108+
def = defRead defname CustomSockOpt readIntInt
109+
110+
----------------------------------------------------------------
111+
112+
pattern UnsupportedSocketOption :: SocketOption
113+
pattern UnsupportedSocketOption = SockOpt (-1) (-1)
114+
58115
-- | Does the 'SocketOption' exist on this system?
59116
isSupportedSocketOption :: SocketOption -> Bool
60117
isSupportedSocketOption opt = opt /= SockOpt (-1) (-1)
61118

62-
-- | Get the 'SocketType' of an active socket.
63-
--
64-
-- Since: 3.0.1.0
65-
getSocketType :: Socket -> IO SocketType
66-
getSocketType s = unpackSocketType <$> getSockOpt s Type
119+
-- | Execute the given action only when the specified socket option is
120+
-- supported. Any return value is ignored.
121+
whenSupported :: SocketOption -> IO a -> IO ()
122+
whenSupported s action
123+
| isSupportedSocketOption s = action >> return ()
124+
| otherwise = return ()
67125

68-
pattern UnsupportedSocketOption :: SocketOption
69-
pattern UnsupportedSocketOption = SockOpt (-1) (-1)
126+
----------------------------------------------------------------
70127

71128
#ifdef SOL_SOCKET
72129
-- | SO_ACCEPTCONN, read-only
@@ -317,41 +374,7 @@ pattern CustomSockOpt xy <- ((\(SockOpt x y) -> (x, y)) -> xy)
317374
where
318375
CustomSockOpt (x, y) = SockOpt x y
319376

320-
#if __GLASGOW_HASKELL__ >= 806
321-
{-# COMPLETE CustomSockOpt #-}
322-
#endif
323-
#ifdef SO_LINGER
324-
-- | Low level 'SO_LINBER' option value, which can be used with 'setSockOpt'.
325-
--
326-
data StructLinger = StructLinger {
327-
-- | Set the linger option on.
328-
sl_onoff :: CInt,
329-
330-
-- | Linger timeout.
331-
sl_linger :: CInt
332-
}
333-
deriving (Eq, Ord, Show)
334-
335-
instance Storable StructLinger where
336-
sizeOf _ = (#const sizeof(struct linger))
337-
alignment _ = alignment (0 :: CInt)
338-
339-
peek p = do
340-
onoff <- (#peek struct linger, l_onoff) p
341-
linger <- (#peek struct linger, l_linger) p
342-
return $ StructLinger onoff linger
343-
344-
poke p (StructLinger onoff linger) = do
345-
(#poke struct linger, l_onoff) p onoff
346-
(#poke struct linger, l_linger) p linger
347-
#endif
348-
349-
-- | Execute the given action only when the specified socket option is
350-
-- supported. Any return value is ignored.
351-
whenSupported :: SocketOption -> IO a -> IO ()
352-
whenSupported s action
353-
| isSupportedSocketOption s = action >> return ()
354-
| otherwise = return ()
377+
----------------------------------------------------------------
355378

356379
-- | Set a socket option that expects an 'Int' value.
357380
setSocketOption :: Socket
@@ -378,6 +401,8 @@ setSockOpt s (SockOpt level opt) v = do
378401
throwSocketErrorIfMinus1_ "Network.Socket.setSockOpt" $
379402
c_setsockopt fd level opt ptr sz
380403

404+
----------------------------------------------------------------
405+
381406
-- | Get a socket option that gives an 'Int' value.
382407
getSocketOption :: Socket
383408
-> SocketOption -- Option Name
@@ -404,56 +429,46 @@ getSockOpt s (SockOpt level opt) = do
404429
c_getsockopt fd level opt ptr ptr_sz
405430
peek ptr
406431

432+
----------------------------------------------------------------
407433

408-
socketOptionBijection :: Bijection SocketOption String
409-
socketOptionBijection =
410-
[ (UnsupportedSocketOption, "UnsupportedSocketOption")
411-
, (Debug, "Debug")
412-
, (ReuseAddr, "ReuseAddr")
413-
, (SoDomain, "SoDomain")
414-
, (Type, "Type")
415-
, (SoProtocol, "SoProtocol")
416-
, (SoError, "SoError")
417-
, (DontRoute, "DontRoute")
418-
, (Broadcast, "Broadcast")
419-
, (SendBuffer, "SendBuffer")
420-
, (RecvBuffer, "RecvBuffer")
421-
, (KeepAlive, "KeepAlive")
422-
, (OOBInline, "OOBInline")
423-
, (Linger, "Linger")
424-
, (ReusePort, "ReusePort")
425-
, (RecvLowWater, "RecvLowWater")
426-
, (SendLowWater, "SendLowWater")
427-
, (RecvTimeOut, "RecvTimeOut")
428-
, (SendTimeOut, "SendTimeOut")
429-
, (UseLoopBack, "UseLoopBack")
430-
, (MaxSegment, "MaxSegment")
431-
, (NoDelay, "NoDelay")
432-
, (UserTimeout, "UserTimeout")
433-
, (Cork, "Cork")
434-
, (TimeToLive, "TimeToLive")
435-
, (RecvIPv4TTL, "RecvIPv4TTL")
436-
, (RecvIPv4TOS, "RecvIPv4TOS")
437-
, (RecvIPv4PktInfo, "RecvIPv4PktInfo")
438-
, (IPv6Only, "IPv6Only")
439-
, (RecvIPv6HopLimit, "RecvIPv6HopLimit")
440-
, (RecvIPv6TClass, "RecvIPv6TClass")
441-
, (RecvIPv6PktInfo, "RecvIPv6PktInfo")
442-
]
434+
-- | Get the 'SocketType' of an active socket.
435+
--
436+
-- Since: 3.0.1.0
437+
getSocketType :: Socket -> IO SocketType
438+
getSocketType s = unpackSocketType <$> getSockOpt s Type
443439

444-
instance Show SocketOption where
445-
showsPrec = bijectiveShow socketOptionBijection def
446-
where
447-
defname = "SockOpt"
448-
unwrap = \(CustomSockOpt nm) -> nm
449-
def = defShow defname unwrap showIntInt
440+
----------------------------------------------------------------
450441

442+
#if __GLASGOW_HASKELL__ >= 806
443+
{-# COMPLETE CustomSockOpt #-}
444+
#endif
445+
#ifdef SO_LINGER
446+
-- | Low level 'SO_LINBER' option value, which can be used with 'setSockOpt'.
447+
--
448+
data StructLinger = StructLinger {
449+
-- | Set the linger option on.
450+
sl_onoff :: CInt,
451451

452-
instance Read SocketOption where
453-
readPrec = bijectiveRead socketOptionBijection def
454-
where
455-
defname = "SockOpt"
456-
def = defRead defname CustomSockOpt readIntInt
452+
-- | Linger timeout.
453+
sl_linger :: CInt
454+
}
455+
deriving (Eq, Ord, Show)
456+
457+
instance Storable StructLinger where
458+
sizeOf _ = (#const sizeof(struct linger))
459+
alignment _ = alignment (0 :: CInt)
460+
461+
peek p = do
462+
onoff <- (#peek struct linger, l_onoff) p
463+
linger <- (#peek struct linger, l_linger) p
464+
return $ StructLinger onoff linger
465+
466+
poke p (StructLinger onoff linger) = do
467+
(#poke struct linger, l_onoff) p onoff
468+
(#poke struct linger, l_linger) p linger
469+
#endif
470+
471+
----------------------------------------------------------------
457472

458473
foreign import CALLCONV unsafe "getsockopt"
459474
c_getsockopt :: CInt -> CInt -> CInt -> Ptr a -> Ptr CInt -> IO CInt

0 commit comments

Comments
 (0)