summaryrefslogtreecommitdiff
path: root/src/Network/KRPC/Protocol.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/KRPC/Protocol.hs')
-rw-r--r--src/Network/KRPC/Protocol.hs20
1 files changed, 8 insertions, 12 deletions
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
40 , recvResponse 40 , recvResponse
41 41
42 -- * Remote 42 -- * Remote
43 , KRemote
44 , KRemoteAddr
45 , withRemote 43 , withRemote
46 , remoteServer 44 , remoteServer
47 ) where 45 ) where
@@ -102,6 +100,8 @@ instance BEncode KError where
102 100
103 fromBEncode _ = decodingError "KError" 101 fromBEncode _ = decodingError "KError"
104 102
103instance Exception KError
104
105type ErrorCode = Int 105type ErrorCode = Int
106 106
107errorCode :: KError -> ErrorCode 107errorCode :: KError -> ErrorCode
@@ -194,29 +194,26 @@ kresponse :: BDict -> KResponse
194kresponse = KResponse 194kresponse = KResponse
195{-# INLINE kresponse #-} 195{-# INLINE kresponse #-}
196 196
197type KRemoteAddr = SockAddr
198type KRemote = Socket
199
200sockAddrFamily :: SockAddr -> Family 197sockAddrFamily :: SockAddr -> Family
201sockAddrFamily (SockAddrInet _ _ ) = AF_INET 198sockAddrFamily (SockAddrInet _ _ ) = AF_INET
202sockAddrFamily (SockAddrInet6 _ _ _ _) = AF_INET6 199sockAddrFamily (SockAddrInet6 _ _ _ _) = AF_INET6
203sockAddrFamily (SockAddrUnix _ ) = AF_UNIX 200sockAddrFamily (SockAddrUnix _ ) = AF_UNIX
204 201
205withRemote :: (MonadBaseControl IO m, MonadIO m) => (KRemote -> m a) -> m a 202withRemote :: (MonadBaseControl IO m, MonadIO m) => (Socket -> m a) -> m a
206withRemote = bracket (liftIO (socket AF_INET6 Datagram defaultProtocol)) 203withRemote = bracket (liftIO (socket AF_INET6 Datagram defaultProtocol))
207 (liftIO . sClose) 204 (liftIO . sClose)
208{-# SPECIALIZE withRemote :: (KRemote -> IO a) -> IO a #-} 205{-# SPECIALIZE withRemote :: (Socket -> IO a) -> IO a #-}
209 206
210maxMsgSize :: Int 207maxMsgSize :: Int
211--maxMsgSize = 512 -- release: size of payload of one udp packet 208--maxMsgSize = 512 -- release: size of payload of one udp packet
212maxMsgSize = 64 * 1024 -- bench: max UDP MTU 209maxMsgSize = 64 * 1024 -- bench: max UDP MTU
213{-# INLINE maxMsgSize #-} 210{-# INLINE maxMsgSize #-}
214 211
215sendMessage :: BEncode msg => msg -> KRemoteAddr -> KRemote -> IO () 212sendMessage :: BEncode msg => msg -> SockAddr -> Socket -> IO ()
216sendMessage msg addr sock = sendManyTo sock (LB.toChunks (encode msg)) addr 213sendMessage msg addr sock = sendManyTo sock (LB.toChunks (encode msg)) addr
217{-# INLINE sendMessage #-} 214{-# INLINE sendMessage #-}
218 215
219recvResponse :: KRemote -> IO (Either KError KResponse) 216recvResponse :: Socket -> IO (Either KError KResponse)
220recvResponse sock = do 217recvResponse sock = do
221 (raw, _) <- recvFrom sock maxMsgSize 218 (raw, _) <- recvFrom sock maxMsgSize
222 return $ case decode raw of 219 return $ case decode raw of
@@ -227,9 +224,8 @@ recvResponse sock = do
227 224
228-- | Run server using a given port. Method invocation should be done manually. 225-- | Run server using a given port. Method invocation should be done manually.
229remoteServer :: (MonadBaseControl IO remote, MonadIO remote) 226remoteServer :: (MonadBaseControl IO remote, MonadIO remote)
230 => KRemoteAddr -- ^ Port number to listen. 227 => SockAddr -- ^ Port number to listen.
231 -> (KRemoteAddr -> KQuery -> remote (Either KError KResponse)) 228 -> (SockAddr -> KQuery -> remote (Either KError KResponse))
232 -- ^ Handler.
233 -> remote () 229 -> remote ()
234remoteServer servAddr action = bracket (liftIO bindServ) (liftIO . sClose) loop 230remoteServer servAddr action = bracket (liftIO bindServ) (liftIO . sClose) loop
235 where 231 where