summaryrefslogtreecommitdiff
path: root/dht/src/Network/SocketLike.hs
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2019-10-16 07:58:55 +0000
committerJoe Crayne <joe@jerkface.net>2020-01-01 19:40:59 -0500
commit6894ac341a4e5691c72346bde9a26457d4e70460 (patch)
tree7ee2bf4b55b053bdbadfe65ad419e5f3199b4a76 /dht/src/Network/SocketLike.hs
parentc4dcf8374e218e741f4c3120d889c420d8ca2edb (diff)
Successful builds against newer network package
Built against both network-3.0.1.0 and network-3.1.0.0 Note, due to upstream issue, getting network-bsd-2.8.1.0 to cooperate with network-3.0.1.0 requires a little finessing. The network-bsd package expects a header(HsNetConfig.h) that network fails to provide. A simple work around is to copy the header after running a newer version of the network configure script from github. Similar for network-3.1.0.0, but this time the header is HsNetworkConfig.h.
Diffstat (limited to 'dht/src/Network/SocketLike.hs')
-rw-r--r--dht/src/Network/SocketLike.hs38
1 files changed, 25 insertions, 13 deletions
diff --git a/dht/src/Network/SocketLike.hs b/dht/src/Network/SocketLike.hs
index 27f6f492..2c97ddd4 100644
--- a/dht/src/Network/SocketLike.hs
+++ b/dht/src/Network/SocketLike.hs
@@ -1,4 +1,5 @@
1{-# LANGUAGE GeneralizedNewtypeDeriving #-} 1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE TupleSections #-}
2{-# LANGUAGE CPP #-} 3{-# LANGUAGE CPP #-}
3-- | 4-- |
4-- 5--
@@ -33,6 +34,7 @@ import Foreign.C.Types ( CUInt )
33 34
34import qualified Network.Socket as NS 35import qualified Network.Socket as NS
35import System.IO (Handle,hClose,hIsOpen) 36import System.IO (Handle,hClose,hIsOpen)
37import Control.Arrow
36 38
37-- | A safe (mostly read-only) interface to a 'NS.Socket'. Note that despite 39-- | A safe (mostly read-only) interface to a 'NS.Socket'. Note that despite
38-- how this class is named, it provides no access to typical 'NS.Socket' uses 40-- how this class is named, it provides no access to typical 'NS.Socket' uses
@@ -43,22 +45,36 @@ class SocketLike sock where
43 -- | See 'NS.getPeerName' 45 -- | See 'NS.getPeerName'
44 getPeerName :: sock -> IO SockAddr 46 getPeerName :: sock -> IO SockAddr
45 -- | See 'NS.getPeerCred' 47 -- | See 'NS.getPeerCred'
46 getPeerCred :: sock -> IO (CUInt, CUInt, CUInt) 48-- getPeerCred :: sock -> IO (CUInt, CUInt, CUInt)
47 49
48 -- | Is the socket still valid? Connected 50 -- | Is the socket still valid? Connected
49 sIsConnected :: sock -> IO Bool 51 --
52 -- In order to give the instance writer
53 -- the option to do book-keeping in a pure
54 -- type, a conceptually modified version of
55 -- the 'SocketLike' is returned.
56 --
57 isValidSocket :: sock -> IO (sock,Bool)
50 58
51 59
52instance SocketLike NS.Socket where 60instance SocketLike NS.Socket where
53 getSocketName = NS.getSocketName 61 getSocketName = NS.getSocketName
54 getPeerName = NS.getPeerName 62 getPeerName = NS.getPeerName
55 getPeerCred = NS.getPeerCred 63-- getPeerCred = NS.getPeerCred
64#if MIN_VERSION_network(3,1,0)
65 isValidSocket s = (s,) <$> NS.withFdSocket s (return . (/= (-1)))
66#else
67#if MIN_VERSION_network(3,0,0)
68 isValidSocket s = (s,) . (/= (-1)) <$> NS.fdSocket s
69#else
56#if MIN_VERSION_network(2,4,0) 70#if MIN_VERSION_network(2,4,0)
57 sIsConnected = NS.isConnected -- warning: this is always False if the socket 71 isValidSocket s = (s,) . NS.isConnected s -- warning: this is always False if the socket
58 -- was converted to a Handle 72 -- was converted to a Handle
59#else 73#else
60 sIsConnected = NS.sIsConnected -- warning: this is always False if the socket 74 isValidSocket = (s,) . NS.sIsConnected s -- warning: this is always False if the socket
61 -- was converted to a Handle 75 -- was converted to a Handle
76#endif
77#endif
62#endif 78#endif
63 79
64-- | An encapsulated socket. Data reads and writes are not possible. 80-- | An encapsulated socket. Data reads and writes are not possible.
@@ -67,12 +83,8 @@ data RestrictedSocket = Restricted (Maybe Handle) NS.Socket deriving Show
67instance SocketLike RestrictedSocket where 83instance SocketLike RestrictedSocket where
68 getSocketName (Restricted mb sock) = NS.getSocketName sock 84 getSocketName (Restricted mb sock) = NS.getSocketName sock
69 getPeerName (Restricted mb sock) = NS.getPeerName sock 85 getPeerName (Restricted mb sock) = NS.getPeerName sock
70 getPeerCred (Restricted mb sock) = NS.getPeerCred sock 86-- getPeerCred (Restricted mb sock) = NS.getPeerCred sock
71#if MIN_VERSION_network(2,4,0) 87 isValidSocket rs@(Restricted mb sock) = maybe (first (Restricted mb) <$> isValidSocket sock) (((rs,) <$>) . hIsOpen) mb
72 sIsConnected (Restricted mb sock) = maybe (NS.isConnected sock) (hIsOpen) mb
73#else
74 sIsConnected (Restricted mb sock) = maybe (NS.sIsConnected sock) (hIsOpen) mb
75#endif
76 88
77-- | Create a 'RestrictedSocket' that explicitly disallows sending or 89-- | Create a 'RestrictedSocket' that explicitly disallows sending or
78-- receiving data. 90-- receiving data.