From f349e9427db4a1b35d0af6801f6ad00b8a17991e Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Thu, 19 Dec 2013 01:50:38 +0400 Subject: Remove useless type synonyms --- src/Network/KRPC.hs | 41 +++++++++++------------------------------ src/Network/KRPC/Protocol.hs | 20 ++++++++------------ 2 files changed, 19 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/Network/KRPC.hs b/src/Network/KRPC.hs index b6e14bb0..8cc3fcab 100644 --- a/src/Network/KRPC.hs +++ b/src/Network/KRPC.hs @@ -101,8 +101,6 @@ module Network.KRPC , idM -- * Client - , RemoteAddr - , RPCException(..) , call -- * Server @@ -128,6 +126,7 @@ import Data.List as L import Data.Monoid import Data.Typeable import Network +import Network.Socket import GHC.Generics import Network.KRPC.Protocol @@ -253,46 +252,28 @@ invalidParamList pl be = error $ "KRPC invalid parameter list: " ++ show pl ++ "\n" ++ "while procedure args are: " ++ show be --- | Alias to Socket, through might change in future. -type Remote = Socket - --- | Represent any error mentioned by protocol specification that --- 'call', 'await' might throw. --- For more details see 'Remote.KRPC.Protocol'. --- -data RPCException = RPCException KError - deriving (Show, Eq, Typeable) - -instance Exception RPCException - --- | Address of remote can be called by client. -type RemoteAddr = KRemoteAddr - -queryCall :: BEncode param - => KRemote -> KRemoteAddr +queryCall :: BEncode param => Socket -> SockAddr -> Method param result -> param -> IO () queryCall sock addr m arg = sendMessage q addr sock where q = kquery (methodName m) (injectVals (methodParams m) (toBEncode arg)) -getResult :: BEncode result - => KRemote - -> Method param result -> IO result +getResult :: BEncode result => Socket -> Method param result -> IO result getResult sock m = do resp <- recvResponse sock case resp of - Left e -> throw (RPCException e) + Left e -> throw e Right (respVals -> dict) -> do case fromBEncode =<< extractArgs (methodVals m) dict of Right vals -> return vals - Left e -> throw (RPCException (ProtocolError (BC.pack e))) + Left e -> throw (ProtocolError (BC.pack e)) -- | Makes remote procedure call. Throws RPCException on any error -- occurred. call :: (MonadBaseControl IO host, MonadIO host) => (BEncode param, BEncode result) - => RemoteAddr -- ^ Address of callee. + => SockAddr -- ^ Address of callee. -> Method param result -- ^ Procedure to call. -> param -- ^ Arguments passed by callee to procedure. -> host result -- ^ Values returned by callee from the procedure. @@ -301,8 +282,8 @@ call addr m arg = liftIO $ withRemote $ \sock -> do call_ sock addr m arg -- | The same as 'call' but use already opened socket. call_ :: (MonadBaseControl IO host, MonadIO host) => (BEncode param, BEncode result) - => Remote -- ^ Socket to use - -> RemoteAddr -- ^ Address of callee. + => Socket -- ^ Socket to use + -> SockAddr -- ^ Address of callee. -> Method param result -- ^ Procedure to call. -> param -- ^ Arguments passed by callee to procedure. -> host result -- ^ Values returned by callee from the procedure. @@ -311,7 +292,7 @@ call_ sock addr m arg = liftIO $ do getResult sock m -type HandlerBody remote = KRemoteAddr -> KQuery -> remote (Either KError KResponse) +type HandlerBody remote = SockAddr -> KQuery -> remote (Either KError KResponse) -- | Procedure signature and implementation binded up. type MethodHandler remote = (MethodName, HandlerBody remote) @@ -333,7 +314,7 @@ infix 1 ==> (BEncode param, BEncode result) => Monad remote => Method param result -- ^ Signature. - -> (KRemoteAddr -> param -> remote result) -- ^ Implementation. + -> (SockAddr -> param -> remote result) -- ^ Implementation. -> MethodHandler remote -- ^ Handler used by server. {-# INLINE (==>@) #-} m ==>@ body = (methodName m, newbody) @@ -353,7 +334,7 @@ infix 1 ==>@ -- it will not create new thread for each connection. -- server :: (MonadBaseControl IO remote, MonadIO remote) - => KRemoteAddr -- ^ Port used to accept incoming connections. + => SockAddr -- ^ Port used to accept incoming connections. -> [MethodHandler remote] -- ^ Method table. -> remote () server servAddr handlers = do diff --git a/src/Network/KRPC/Protocol.hs b/src/Network/KRPC/Protocol.hs index 16027362..adc02b5f 100644 --- a/src/Network/KRPC/Protocol.hs +++ b/src/Network/KRPC/Protocol.hs @@ -40,8 +40,6 @@ module Network.KRPC.Protocol , recvResponse -- * Remote - , KRemote - , KRemoteAddr , withRemote , remoteServer ) where @@ -102,6 +100,8 @@ instance BEncode KError where fromBEncode _ = decodingError "KError" +instance Exception KError + type ErrorCode = Int errorCode :: KError -> ErrorCode @@ -194,29 +194,26 @@ kresponse :: BDict -> KResponse kresponse = KResponse {-# INLINE kresponse #-} -type KRemoteAddr = SockAddr -type KRemote = Socket - sockAddrFamily :: SockAddr -> Family sockAddrFamily (SockAddrInet _ _ ) = AF_INET sockAddrFamily (SockAddrInet6 _ _ _ _) = AF_INET6 sockAddrFamily (SockAddrUnix _ ) = AF_UNIX -withRemote :: (MonadBaseControl IO m, MonadIO m) => (KRemote -> m a) -> m a +withRemote :: (MonadBaseControl IO m, MonadIO m) => (Socket -> m a) -> m a withRemote = bracket (liftIO (socket AF_INET6 Datagram defaultProtocol)) (liftIO . sClose) -{-# SPECIALIZE withRemote :: (KRemote -> IO a) -> IO a #-} +{-# SPECIALIZE withRemote :: (Socket -> IO a) -> IO a #-} maxMsgSize :: Int --maxMsgSize = 512 -- release: size of payload of one udp packet maxMsgSize = 64 * 1024 -- bench: max UDP MTU {-# INLINE maxMsgSize #-} -sendMessage :: BEncode msg => msg -> KRemoteAddr -> KRemote -> IO () +sendMessage :: BEncode msg => msg -> SockAddr -> Socket -> IO () sendMessage msg addr sock = sendManyTo sock (LB.toChunks (encode msg)) addr {-# INLINE sendMessage #-} -recvResponse :: KRemote -> IO (Either KError KResponse) +recvResponse :: Socket -> IO (Either KError KResponse) recvResponse sock = do (raw, _) <- recvFrom sock maxMsgSize return $ case decode raw of @@ -227,9 +224,8 @@ recvResponse sock = do -- | Run server using a given port. Method invocation should be done manually. remoteServer :: (MonadBaseControl IO remote, MonadIO remote) - => KRemoteAddr -- ^ Port number to listen. - -> (KRemoteAddr -> KQuery -> remote (Either KError KResponse)) - -- ^ Handler. + => SockAddr -- ^ Port number to listen. + -> (SockAddr -> KQuery -> remote (Either KError KResponse)) -> remote () remoteServer servAddr action = bracket (liftIO bindServ) (liftIO . sClose) loop where -- cgit v1.2.3