diff options
Diffstat (limited to 'src/Network/KRPC/Protocol.hs')
-rw-r--r-- | src/Network/KRPC/Protocol.hs | 20 |
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 | ||
103 | instance Exception KError | ||
104 | |||
105 | type ErrorCode = Int | 105 | type ErrorCode = Int |
106 | 106 | ||
107 | errorCode :: KError -> ErrorCode | 107 | errorCode :: KError -> ErrorCode |
@@ -194,29 +194,26 @@ kresponse :: BDict -> KResponse | |||
194 | kresponse = KResponse | 194 | kresponse = KResponse |
195 | {-# INLINE kresponse #-} | 195 | {-# INLINE kresponse #-} |
196 | 196 | ||
197 | type KRemoteAddr = SockAddr | ||
198 | type KRemote = Socket | ||
199 | |||
200 | sockAddrFamily :: SockAddr -> Family | 197 | sockAddrFamily :: SockAddr -> Family |
201 | sockAddrFamily (SockAddrInet _ _ ) = AF_INET | 198 | sockAddrFamily (SockAddrInet _ _ ) = AF_INET |
202 | sockAddrFamily (SockAddrInet6 _ _ _ _) = AF_INET6 | 199 | sockAddrFamily (SockAddrInet6 _ _ _ _) = AF_INET6 |
203 | sockAddrFamily (SockAddrUnix _ ) = AF_UNIX | 200 | sockAddrFamily (SockAddrUnix _ ) = AF_UNIX |
204 | 201 | ||
205 | withRemote :: (MonadBaseControl IO m, MonadIO m) => (KRemote -> m a) -> m a | 202 | withRemote :: (MonadBaseControl IO m, MonadIO m) => (Socket -> m a) -> m a |
206 | withRemote = bracket (liftIO (socket AF_INET6 Datagram defaultProtocol)) | 203 | withRemote = 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 | ||
210 | maxMsgSize :: Int | 207 | maxMsgSize :: Int |
211 | --maxMsgSize = 512 -- release: size of payload of one udp packet | 208 | --maxMsgSize = 512 -- release: size of payload of one udp packet |
212 | maxMsgSize = 64 * 1024 -- bench: max UDP MTU | 209 | maxMsgSize = 64 * 1024 -- bench: max UDP MTU |
213 | {-# INLINE maxMsgSize #-} | 210 | {-# INLINE maxMsgSize #-} |
214 | 211 | ||
215 | sendMessage :: BEncode msg => msg -> KRemoteAddr -> KRemote -> IO () | 212 | sendMessage :: BEncode msg => msg -> SockAddr -> Socket -> IO () |
216 | sendMessage msg addr sock = sendManyTo sock (LB.toChunks (encode msg)) addr | 213 | sendMessage msg addr sock = sendManyTo sock (LB.toChunks (encode msg)) addr |
217 | {-# INLINE sendMessage #-} | 214 | {-# INLINE sendMessage #-} |
218 | 215 | ||
219 | recvResponse :: KRemote -> IO (Either KError KResponse) | 216 | recvResponse :: Socket -> IO (Either KError KResponse) |
220 | recvResponse sock = do | 217 | recvResponse 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. |
229 | remoteServer :: (MonadBaseControl IO remote, MonadIO remote) | 226 | remoteServer :: (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 () |
234 | remoteServer servAddr action = bracket (liftIO bindServ) (liftIO . sClose) loop | 230 | remoteServer servAddr action = bracket (liftIO bindServ) (liftIO . sClose) loop |
235 | where | 231 | where |