summaryrefslogtreecommitdiff
path: root/src/Network/KRPC
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/KRPC')
-rw-r--r--src/Network/KRPC/Manager.hs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/Network/KRPC/Manager.hs b/src/Network/KRPC/Manager.hs
index 304f43f2..9d8688d3 100644
--- a/src/Network/KRPC/Manager.hs
+++ b/src/Network/KRPC/Manager.hs
@@ -9,6 +9,8 @@ module Network.KRPC.Manager
9 , Manager 9 , Manager
10 , newManager 10 , newManager
11 , closeManager 11 , closeManager
12 , withManager
13
12 , query 14 , query
13 15
14 , Handler 16 , Handler
@@ -102,6 +104,9 @@ closeManager Manager {..} = do
102 -- TODO unblock calls 104 -- TODO unblock calls
103 close sock 105 close sock
104 106
107withManager :: SockAddr -> [Handler h] -> (Manager h -> IO a) -> IO a
108withManager addr hs = bracket (newManager addr hs) closeManager
109
105sendMessage :: MonadIO m => BEncode a => Socket -> SockAddr -> a -> m () 110sendMessage :: MonadIO m => BEncode a => Socket -> SockAddr -> a -> m ()
106sendMessage sock addr a = do 111sendMessage sock addr a = do
107 liftIO $ sendManyTo sock (BL.toChunks (BE.encode a)) addr 112 liftIO $ sendManyTo sock (BL.toChunks (BE.encode a)) addr
@@ -136,6 +141,11 @@ queryResponse ares = do
136 Right r -> pure r 141 Right r -> pure r
137 Left e -> throwIO $ decodeError e respId 142 Left e -> throwIO $ decodeError e respId
138 143
144-- |
145--
146-- This function will throw exception if quered node respond with
147-- @error@ message or timeout expires.
148--
139query :: forall h m a b. (MonadKRPC h m, KRPC a b) => SockAddr -> a -> m b 149query :: forall h m a b. (MonadKRPC h m, KRPC a b) => SockAddr -> a -> m b
140query addr params = do 150query addr params = do
141 Manager {..} <- getManager 151 Manager {..} <- getManager
@@ -161,6 +171,8 @@ query addr params = do
161-- Handlers 171-- Handlers
162-----------------------------------------------------------------------} 172-----------------------------------------------------------------------}
163 173
174-- | Any thrown exception will be supressed and send over wire back to
175-- the quering node.
164handler :: forall h a b. (KRPC a b, Monad h) 176handler :: forall h a b. (KRPC a b, Monad h)
165 => (SockAddr -> a -> h b) -> Handler h 177 => (SockAddr -> a -> h b) -> Handler h
166handler body = (name, wrapper) 178handler body = (name, wrapper)