diff options
Diffstat (limited to 'src/Network/DatagramServer.hs')
-rw-r--r-- | src/Network/DatagramServer.hs | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/src/Network/DatagramServer.hs b/src/Network/DatagramServer.hs index 2140e2cd..91efa443 100644 --- a/src/Network/DatagramServer.hs +++ b/src/Network/DatagramServer.hs | |||
@@ -190,13 +190,13 @@ type Handler h msg v = (QueryMethod msg, HandlerBody h msg v) | |||
190 | 190 | ||
191 | -- | Keep track pending queries made by /this/ node and handle queries | 191 | -- | Keep track pending queries made by /this/ node and handle queries |
192 | -- made by /remote/ nodes. | 192 | -- made by /remote/ nodes. |
193 | data Manager h raw msg = Manager | 193 | data Manager raw msg = Manager |
194 | { sock :: !Socket | 194 | { sock :: !Socket |
195 | , options :: !Options | 195 | , options :: !Options |
196 | , listenerThread :: !(MVar ThreadId) | 196 | , listenerThread :: !(MVar ThreadId) |
197 | , transactionCounter :: {-# UNPACK #-} !TransactionCounter | 197 | , transactionCounter :: {-# UNPACK #-} !TransactionCounter |
198 | , pendingCalls :: {-# UNPACK #-} !(PendingCalls msg raw) | 198 | , pendingCalls :: {-# UNPACK #-} !(PendingCalls msg raw) |
199 | , handlers :: [Handler h msg raw] -- TODO delete this, it's not used | 199 | -- , handlers :: [Handler h msg raw] -- TODO delete this, it's not used |
200 | , logMsg :: Char -> String -> T.Text -> IO () | 200 | , logMsg :: Char -> String -> T.Text -> IO () |
201 | } | 201 | } |
202 | 202 | ||
@@ -212,14 +212,14 @@ newManager :: Options -- ^ various protocol options; | |||
212 | -> (Char -> String -> T.Text -> IO ()) -- ^ loging function | 212 | -> (Char -> String -> T.Text -> IO ()) -- ^ loging function |
213 | -> SockAddr -- ^ address to listen on; | 213 | -> SockAddr -- ^ address to listen on; |
214 | -> [Handler h msg raw] -- ^ handlers to run on incoming queries. | 214 | -> [Handler h msg raw] -- ^ handlers to run on incoming queries. |
215 | -> IO (Manager h raw msg) -- ^ new rpc manager. | 215 | -> IO (Manager raw msg) -- ^ new rpc manager. |
216 | newManager opts @ Options {..} logmsg servAddr handlers = do | 216 | newManager opts @ Options {..} logmsg servAddr handlers = do |
217 | validateOptions opts | 217 | validateOptions opts |
218 | sock <- bindServ | 218 | sock <- bindServ |
219 | tref <- newEmptyMVar | 219 | tref <- newEmptyMVar |
220 | tran <- newIORef optSeedTransaction | 220 | tran <- newIORef optSeedTransaction |
221 | calls <- newIORef M.empty | 221 | calls <- newIORef M.empty |
222 | return $ Manager sock opts tref tran calls handlers logmsg | 222 | return $ Manager sock opts tref tran calls logmsg |
223 | where | 223 | where |
224 | bindServ = do | 224 | bindServ = do |
225 | let family = sockAddrFamily servAddr | 225 | let family = sockAddrFamily servAddr |
@@ -230,7 +230,7 @@ newManager opts @ Options {..} logmsg servAddr handlers = do | |||
230 | return sock | 230 | return sock |
231 | 231 | ||
232 | -- | Unblock all pending calls and close socket. | 232 | -- | Unblock all pending calls and close socket. |
233 | closeManager :: Manager m raw msg -> IO () | 233 | closeManager :: Manager raw msg -> IO () |
234 | closeManager Manager {..} = do | 234 | closeManager Manager {..} = do |
235 | maybe (return ()) killThread =<< tryTakeMVar listenerThread | 235 | maybe (return ()) killThread =<< tryTakeMVar listenerThread |
236 | -- TODO unblock calls | 236 | -- TODO unblock calls |
@@ -238,7 +238,7 @@ closeManager Manager {..} = do | |||
238 | 238 | ||
239 | -- | Check if the manager is still active. Manager becomes active | 239 | -- | Check if the manager is still active. Manager becomes active |
240 | -- until 'closeManager' called. | 240 | -- until 'closeManager' called. |
241 | isActive :: Manager m raw msg -> IO Bool | 241 | isActive :: Manager raw msg -> IO Bool |
242 | isActive Manager {..} = liftIO $ isBound sock | 242 | isActive Manager {..} = liftIO $ isBound sock |
243 | {-# INLINE isActive #-} | 243 | {-# INLINE isActive #-} |
244 | 244 | ||
@@ -246,7 +246,7 @@ isActive Manager {..} = liftIO $ isBound sock | |||
246 | -- | Normally you should use Control.Monad.Trans.Resource.allocate | 246 | -- | Normally you should use Control.Monad.Trans.Resource.allocate |
247 | -- function. | 247 | -- function. |
248 | withManager :: Options -> SockAddr -> [Handler h msg raw] | 248 | withManager :: Options -> SockAddr -> [Handler h msg raw] |
249 | -> (Manager h raw msg -> IO a) -> IO a | 249 | -> (Manager raw msg -> IO a) -> IO a |
250 | withManager opts addr hs = bracket (newManager opts addr hs) closeManager | 250 | withManager opts addr hs = bracket (newManager opts addr hs) closeManager |
251 | #endif | 251 | #endif |
252 | 252 | ||
@@ -289,7 +289,7 @@ genTransactionId ref = do | |||
289 | uniqueTransactionId cur | 289 | uniqueTransactionId cur |
290 | 290 | ||
291 | -- | How many times 'query' call have been performed. | 291 | -- | How many times 'query' call have been performed. |
292 | getQueryCount :: Manager h raw msg -> IO Int | 292 | getQueryCount :: Manager raw msg -> IO Int |
293 | getQueryCount mgr@Manager{..} = do | 293 | getQueryCount mgr@Manager{..} = do |
294 | curTrans <- readIORef transactionCounter | 294 | curTrans <- readIORef transactionCounter |
295 | return $ curTrans - optSeedTransaction options | 295 | return $ curTrans - optSeedTransaction options |
@@ -320,21 +320,21 @@ sendQuery sock addr q = handle sockError $ sendMessage sock addr q | |||
320 | -- This function should throw 'QueryFailure' exception if quered node | 320 | -- This function should throw 'QueryFailure' exception if quered node |
321 | -- respond with @error@ message or the query timeout expires. | 321 | -- respond with @error@ message or the query timeout expires. |
322 | -- | 322 | -- |
323 | query :: forall h a b raw msg. (SerializableTo raw b, Show (QueryMethod msg), Ord (TransactionID msg), Serialize (TransactionID msg), SerializableTo raw a, WireFormat raw msg) => Manager h raw msg -> QueryMethod msg -> SockAddr -> a -> IO b | 323 | query :: forall h a b raw msg. (SerializableTo raw b, Show (QueryMethod msg), Ord (TransactionID msg), Serialize (TransactionID msg), SerializableTo raw a, WireFormat raw msg) => Manager raw msg -> QueryMethod msg -> SockAddr -> a -> IO b |
324 | query mgr meth addr params = queryK mgr meth addr params (\_ x _ -> x) | 324 | query mgr meth addr params = queryK mgr meth addr params (\_ x _ -> x) |
325 | 325 | ||
326 | -- | Like 'query' but possibly returns your externally routable IP address. | 326 | -- | Like 'query' but possibly returns your externally routable IP address. |
327 | query' :: forall h a b raw msg. (SerializableTo raw b, Show (QueryMethod msg), Ord (TransactionID msg), Serialize (TransactionID msg), SerializableTo raw a, WireFormat raw msg) => Manager h raw msg -> QueryMethod msg -> SockAddr -> a -> IO (b, Maybe ReflectedIP) | 327 | query' :: forall h a b raw msg. (SerializableTo raw b, Show (QueryMethod msg), Ord (TransactionID msg), Serialize (TransactionID msg), SerializableTo raw a, WireFormat raw msg) => Manager raw msg -> QueryMethod msg -> SockAddr -> a -> IO (b, Maybe ReflectedIP) |
328 | query' mgr meth addr params = queryK mgr meth addr params (const (,)) | 328 | query' mgr meth addr params = queryK mgr meth addr params (const (,)) |
329 | 329 | ||
330 | -- | Enqueue a query, but give us the complete BEncoded content sent by the | 330 | -- | Enqueue a query, but give us the complete BEncoded content sent by the |
331 | -- remote Node. This is useful for handling extensions that this library does | 331 | -- remote Node. This is useful for handling extensions that this library does |
332 | -- not otherwise support. | 332 | -- not otherwise support. |
333 | queryRaw :: forall h a b raw msg. (SerializableTo raw b, Show (QueryMethod msg), Ord (TransactionID msg), Serialize (TransactionID msg), SerializableTo raw a, WireFormat raw msg) => Manager h raw msg -> QueryMethod msg -> SockAddr -> a -> IO (b, raw) | 333 | queryRaw :: forall h a b raw msg. (SerializableTo raw b, Show (QueryMethod msg), Ord (TransactionID msg), Serialize (TransactionID msg), SerializableTo raw a, WireFormat raw msg) => Manager raw msg -> QueryMethod msg -> SockAddr -> a -> IO (b, raw) |
334 | queryRaw mgr meth addr params = queryK mgr meth addr params (\raw x _ -> (x,raw)) | 334 | queryRaw mgr meth addr params = queryK mgr meth addr params (\raw x _ -> (x,raw)) |
335 | 335 | ||
336 | queryK :: forall h a b x raw msg. (SerializableTo raw b, Show (QueryMethod msg), Ord (TransactionID msg), Serialize (TransactionID msg), SerializableTo raw a, WireFormat raw msg) => | 336 | queryK :: forall h a b x raw msg. (SerializableTo raw b, Show (QueryMethod msg), Ord (TransactionID msg), Serialize (TransactionID msg), SerializableTo raw a, WireFormat raw msg) => |
337 | Manager h raw msg -> QueryMethod msg -> SockAddr -> a -> (raw -> b -> Maybe ReflectedIP -> x) -> IO x | 337 | Manager raw msg -> QueryMethod msg -> SockAddr -> a -> (raw -> b -> Maybe ReflectedIP -> x) -> IO x |
338 | queryK mgr@Manager{..} meth addr params kont = do | 338 | queryK mgr@Manager{..} meth addr params kont = do |
339 | tid <- liftIO $ genTransactionId transactionCounter | 339 | tid <- liftIO $ genTransactionId transactionCounter |
340 | -- let queryMethod = method :: Method a b | 340 | -- let queryMethod = method :: Method a b |
@@ -424,7 +424,7 @@ handler name body = (name, wrapper) | |||
424 | runHandler :: ( Envelope msg | 424 | runHandler :: ( Envelope msg |
425 | , Show (QueryMethod msg) | 425 | , Show (QueryMethod msg) |
426 | , Serialize (TransactionID msg)) | 426 | , Serialize (TransactionID msg)) |
427 | => Manager IO raw msg -> QueryMethod msg -> HandlerBody IO msg raw -> SockAddr -> msg raw -> IO (KResult msg raw) | 427 | => Manager raw msg -> QueryMethod msg -> HandlerBody IO msg raw -> SockAddr -> msg raw -> IO (KResult msg raw) |
428 | runHandler mgr@Manager{..} meth h addr m = Lifted.catches wrapper failbacks | 428 | runHandler mgr@Manager{..} meth h addr m = Lifted.catches wrapper failbacks |
429 | where | 429 | where |
430 | signature = querySignature meth (envelopeTransaction m) addr | 430 | signature = querySignature meth (envelopeTransaction m) addr |
@@ -462,7 +462,7 @@ dispatchHandler :: ( Eq (QueryMethod msg) | |||
462 | , Show (QueryMethod msg) | 462 | , Show (QueryMethod msg) |
463 | , Serialize (TransactionID msg) | 463 | , Serialize (TransactionID msg) |
464 | , Envelope msg | 464 | , Envelope msg |
465 | ) => Manager IO raw msg -> [Handler IO msg raw] -> QueryMethod msg -> msg raw -> SockAddr -> IO (KResult msg raw) | 465 | ) => Manager raw msg -> [Handler IO msg raw] -> QueryMethod msg -> msg raw -> SockAddr -> IO (KResult msg raw) |
466 | dispatchHandler mgr handlers meth q addr = do | 466 | dispatchHandler mgr handlers meth q addr = do |
467 | case L.lookup meth handlers of | 467 | case L.lookup meth handlers of |
468 | Nothing -> return $ Left $ KError MethodUnknown ("Unknown method " <> BC.pack (show meth)) (envelopeTransaction q) | 468 | Nothing -> return $ Left $ KError MethodUnknown ("Unknown method " <> BC.pack (show meth)) (envelopeTransaction q) |
@@ -483,7 +483,7 @@ handleQuery :: ( WireFormat raw msg | |||
483 | , Eq (QueryMethod msg) | 483 | , Eq (QueryMethod msg) |
484 | , Show (QueryMethod msg) | 484 | , Show (QueryMethod msg) |
485 | , Serialize (TransactionID msg) | 485 | , Serialize (TransactionID msg) |
486 | ) => Manager IO raw msg -> [Handler IO msg raw] -> QueryMethod msg -> raw -> msg raw -> SockAddr -> IO () | 486 | ) => Manager raw msg -> [Handler IO msg raw] -> QueryMethod msg -> raw -> msg raw -> SockAddr -> IO () |
487 | handleQuery mgr@Manager{..} hs meth raw q addr = void $ fork $ do | 487 | handleQuery mgr@Manager{..} hs meth raw q addr = void $ fork $ do |
488 | myThreadId >>= liftIO . flip labelThread "KRPC.handleQuery" | 488 | myThreadId >>= liftIO . flip labelThread "KRPC.handleQuery" |
489 | res <- dispatchHandler mgr hs meth q addr | 489 | res <- dispatchHandler mgr hs meth q addr |
@@ -501,7 +501,7 @@ handleQuery mgr@Manager{..} hs meth raw q addr = void $ fork $ do | |||
501 | 501 | ||
502 | handleResponse :: ( Ord (TransactionID msg) | 502 | handleResponse :: ( Ord (TransactionID msg) |
503 | , Envelope msg | 503 | , Envelope msg |
504 | ) => Manager IO raw msg -> raw -> KResult msg raw -> SockAddr -> IO () | 504 | ) => Manager raw msg -> raw -> KResult msg raw -> SockAddr -> IO () |
505 | handleResponse mgr@Manager{..} raw result addr = do | 505 | handleResponse mgr@Manager{..} raw result addr = do |
506 | liftIO $ do | 506 | liftIO $ do |
507 | let resultId = either errorId envelopeTransaction result | 507 | let resultId = either errorId envelopeTransaction result |
@@ -520,7 +520,7 @@ listener :: forall raw msg. | |||
520 | , Eq (QueryMethod msg) | 520 | , Eq (QueryMethod msg) |
521 | , Show (QueryMethod msg) | 521 | , Show (QueryMethod msg) |
522 | , Serialize (TransactionID msg) | 522 | , Serialize (TransactionID msg) |
523 | ) => Manager IO raw msg -> [Handler IO msg raw] -> Protocol raw msg -> IO () | 523 | ) => Manager raw msg -> [Handler IO msg raw] -> Protocol raw msg -> IO () |
524 | listener mgr@Manager{..} hs p = do | 524 | listener mgr@Manager{..} hs p = do |
525 | fix $ \again -> do | 525 | fix $ \again -> do |
526 | let ctx = error "TODO TOX ToxCipherContext or () for Mainline" | 526 | let ctx = error "TODO TOX ToxCipherContext or () for Mainline" |
@@ -551,7 +551,7 @@ listen :: ( WireFormat raw msg | |||
551 | , Eq (QueryMethod msg) | 551 | , Eq (QueryMethod msg) |
552 | , Show (QueryMethod msg) | 552 | , Show (QueryMethod msg) |
553 | , Serialize (TransactionID msg) | 553 | , Serialize (TransactionID msg) |
554 | ) => Manager IO raw msg -> [Handler IO msg raw] -> Protocol raw msg -> IO () | 554 | ) => Manager raw msg -> [Handler IO msg raw] -> Protocol raw msg -> IO () |
555 | listen mgr@Manager{..} hs p = do | 555 | listen mgr@Manager{..} hs p = do |
556 | tid <- fork $ do | 556 | tid <- fork $ do |
557 | myThreadId >>= liftIO . flip labelThread "KRPC.listen" | 557 | myThreadId >>= liftIO . flip labelThread "KRPC.listen" |