Skip to content

Commit 85a4b71

Browse files
Add SocketOptValue and setSocketOptValue
See docs for `setSocketOptValue`
1 parent a7b15f8 commit 85a4b71

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Network/Socket.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ module Network.Socket (
181181
setSocketOption,
182182
getSockOpt,
183183
setSockOpt,
184+
SocketOptValue (..),
185+
setSocketOptValue,
184186

185187
-- * Socket
186188
Socket,

Network/Socket/Options.hsc

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE ScopedTypeVariables #-}
44
{-# LANGUAGE RecordWildCards #-}
55
{-# LANGUAGE ViewPatterns #-}
6+
{-# LANGUAGE GADTs #-}
67

78
#include "HsNet.h"
89
##include "HsNetDef.h"
@@ -25,6 +26,8 @@ module Network.Socket.Options (
2526
, setSocketOption
2627
, getSockOpt
2728
, setSockOpt
29+
, SocketOptValue (..)
30+
, setSocketOptValue
2831
, StructLinger (..)
2932
, SocketTimeout (..)
3033
) where
@@ -408,6 +411,22 @@ setSockOpt s (SockOpt level opt) v = do
408411
throwSocketErrorIfMinus1_ "Network.Socket.setSockOpt" $
409412
c_setsockopt fd level opt ptr sz
410413

414+
-- | Set a socket option value
415+
--
416+
-- The existential 'SocketOptValue' enables things like:
417+
--
418+
-- @
419+
-- mapM_ (uncurry $ 'setSocketOptValue' sock) [
420+
-- ('NoDelay', 'SocketOptValue' @Int 1)
421+
-- , ('Linger', 'SocketOptValue' ('StructLinger' 1 0))
422+
-- ]
423+
-- @
424+
setSocketOptValue :: Socket
425+
-> SocketOption
426+
-> SocketOptValue
427+
-> IO ()
428+
setSocketOptValue s opt (SocketOptValue v) = setSockOpt s opt v
429+
411430
----------------------------------------------------------------
412431

413432
-- | Get a socket option that gives an 'Int' value.
@@ -456,8 +475,8 @@ getSocketType s = unpackSocketType <$> getSockOpt s Type
456475
{-# COMPLETE CustomSockOpt #-}
457476
#endif
458477
#ifdef SO_LINGER
459-
-- | Low level 'SO_LINBER' option value, which can be used with 'setSockOpt'.
460-
--
478+
-- | Low level @SO_LINGER@ option value, which can be used with 'setSockOpt' or
479+
-- @'setSocketOptValue' . 'SocketOptValue'@.
461480
data StructLinger = StructLinger {
462481
-- | Set the linger option on.
463482
sl_onoff :: CInt,
@@ -481,6 +500,13 @@ instance Storable StructLinger where
481500
(#poke struct linger, l_linger) p linger
482501
#endif
483502

503+
-- | A type that can hold any 'Storable' socket option value (e.g.
504+
-- 'StructLinger' and 'CInt')
505+
--
506+
-- See 'setSocketOptValue'
507+
data SocketOptValue where
508+
SocketOptValue :: Storable a => a -> SocketOptValue
509+
484510
----------------------------------------------------------------
485511

486512
-- | Timeout in microseconds.

0 commit comments

Comments
 (0)