diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/KRPC.hs | 4 | ||||
-rw-r--r-- | src/Network/KRPC/Manager.hs | 12 |
2 files changed, 15 insertions, 1 deletions
diff --git a/src/Network/KRPC.hs b/src/Network/KRPC.hs index e10fcb58..10d2eb55 100644 --- a/src/Network/KRPC.hs +++ b/src/Network/KRPC.hs | |||
@@ -94,7 +94,6 @@ module Network.KRPC | |||
94 | -- * RPC | 94 | -- * RPC |
95 | , Handler | 95 | , Handler |
96 | , handler | 96 | , handler |
97 | , listen | ||
98 | , query | 97 | , query |
99 | 98 | ||
100 | -- * Manager | 99 | -- * Manager |
@@ -102,9 +101,12 @@ module Network.KRPC | |||
102 | , Manager | 101 | , Manager |
103 | , newManager | 102 | , newManager |
104 | , closeManager | 103 | , closeManager |
104 | , withManager | ||
105 | , listen | ||
105 | 106 | ||
106 | -- * Exceptions | 107 | -- * Exceptions |
107 | , KError (..) | 108 | , KError (..) |
109 | , ErrorCode (..) | ||
108 | ) where | 110 | ) where |
109 | 111 | ||
110 | import Network.KRPC.Message | 112 | import Network.KRPC.Message |
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 | ||
107 | withManager :: SockAddr -> [Handler h] -> (Manager h -> IO a) -> IO a | ||
108 | withManager addr hs = bracket (newManager addr hs) closeManager | ||
109 | |||
105 | sendMessage :: MonadIO m => BEncode a => Socket -> SockAddr -> a -> m () | 110 | sendMessage :: MonadIO m => BEncode a => Socket -> SockAddr -> a -> m () |
106 | sendMessage sock addr a = do | 111 | sendMessage 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 | -- | ||
139 | query :: forall h m a b. (MonadKRPC h m, KRPC a b) => SockAddr -> a -> m b | 149 | query :: forall h m a b. (MonadKRPC h m, KRPC a b) => SockAddr -> a -> m b |
140 | query addr params = do | 150 | query 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. | ||
164 | handler :: forall h a b. (KRPC a b, Monad h) | 176 | handler :: forall h a b. (KRPC a b, Monad h) |
165 | => (SockAddr -> a -> h b) -> Handler h | 177 | => (SockAddr -> a -> h b) -> Handler h |
166 | handler body = (name, wrapper) | 178 | handler body = (name, wrapper) |