diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-10-01 06:10:41 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-10-01 06:10:41 +0400 |
commit | 258f21eb490ee3588dd3a1c7316ff41f7f355be7 (patch) | |
tree | c4eae6e12cfa8ebb7553a5b75654ae6371684d57 /src | |
parent | 4a0653fc58869f4fc61230d3d11ef92626a8f52d (diff) |
Allow passing ipv6 addresses
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/KRPC.hs | 9 | ||||
-rw-r--r-- | src/Network/KRPC/Protocol.hs | 25 |
2 files changed, 13 insertions, 21 deletions
diff --git a/src/Network/KRPC.hs b/src/Network/KRPC.hs index e667853a..3c9f9bee 100644 --- a/src/Network/KRPC.hs +++ b/src/Network/KRPC.hs | |||
@@ -97,7 +97,8 @@ | |||
97 | module Network.KRPC | 97 | module Network.KRPC |
98 | ( -- * Method | 98 | ( -- * Method |
99 | Method(..) | 99 | Method(..) |
100 | , method, idM | 100 | , method |
101 | , idM | ||
101 | 102 | ||
102 | -- * Client | 103 | -- * Client |
103 | , RemoteAddr | 104 | , RemoteAddr |
@@ -349,11 +350,11 @@ infix 1 ==>@ | |||
349 | -- it will not create new thread for each connection. | 350 | -- it will not create new thread for each connection. |
350 | -- | 351 | -- |
351 | server :: (MonadBaseControl IO remote, MonadIO remote) | 352 | server :: (MonadBaseControl IO remote, MonadIO remote) |
352 | => PortNumber -- ^ Port used to accept incoming connections. | 353 | => KRemoteAddr -- ^ Port used to accept incoming connections. |
353 | -> [MethodHandler remote] -- ^ Method table. | 354 | -> [MethodHandler remote] -- ^ Method table. |
354 | -> remote () | 355 | -> remote () |
355 | server servport handlers = do | 356 | server servAddr handlers = do |
356 | remoteServer servport $ \addr q -> do | 357 | remoteServer servAddr $ \addr q -> do |
357 | case dispatch (queryMethod q) of | 358 | case dispatch (queryMethod q) of |
358 | Nothing -> return $ Left $ MethodUnknown (queryMethod q) | 359 | Nothing -> return $ Left $ MethodUnknown (queryMethod q) |
359 | Just m -> m addr q | 360 | Just m -> m addr q |
diff --git a/src/Network/KRPC/Protocol.hs b/src/Network/KRPC/Protocol.hs index ad1dabca..2d905f06 100644 --- a/src/Network/KRPC/Protocol.hs +++ b/src/Network/KRPC/Protocol.hs | |||
@@ -202,10 +202,7 @@ kresponse :: [(ValName, BValue)] -> KResponse | |||
202 | kresponse = KResponse . M.fromList | 202 | kresponse = KResponse . M.fromList |
203 | {-# INLINE kresponse #-} | 203 | {-# INLINE kresponse #-} |
204 | 204 | ||
205 | 205 | type KRemoteAddr = SockAddr | |
206 | |||
207 | type KRemoteAddr = (HostAddress, PortNumber) | ||
208 | |||
209 | type KRemote = Socket | 206 | type KRemote = Socket |
210 | 207 | ||
211 | withRemote :: (MonadBaseControl IO m, MonadIO m) => (KRemote -> m a) -> m a | 208 | withRemote :: (MonadBaseControl IO m, MonadIO m) => (KRemote -> m a) -> m a |
@@ -224,8 +221,7 @@ maxMsgSize = 64 * 1024 -- max udp size | |||
224 | 221 | ||
225 | -- TODO eliminate toStrict | 222 | -- TODO eliminate toStrict |
226 | sendMessage :: BEncode msg => msg -> KRemoteAddr -> KRemote -> IO () | 223 | sendMessage :: BEncode msg => msg -> KRemoteAddr -> KRemote -> IO () |
227 | sendMessage msg (host, port) sock = | 224 | sendMessage msg addr sock = sendAllTo sock (LB.toStrict (encoded msg)) addr |
228 | sendAllTo sock (LB.toStrict (encoded msg)) (SockAddrInet port host) | ||
229 | {-# INLINE sendMessage #-} | 225 | {-# INLINE sendMessage #-} |
230 | 226 | ||
231 | recvResponse :: KRemote -> IO (Either KError KResponse) | 227 | recvResponse :: KRemote -> IO (Either KError KResponse) |
@@ -239,26 +235,21 @@ recvResponse sock = do | |||
239 | 235 | ||
240 | -- | Run server using a given port. Method invocation should be done manually. | 236 | -- | Run server using a given port. Method invocation should be done manually. |
241 | remoteServer :: (MonadBaseControl IO remote, MonadIO remote) | 237 | remoteServer :: (MonadBaseControl IO remote, MonadIO remote) |
242 | => PortNumber -- ^ Port number to listen. | 238 | => KRemoteAddr -- ^ Port number to listen. |
243 | -> (KRemoteAddr -> KQuery -> remote (Either KError KResponse)) | 239 | -> (KRemoteAddr -> KQuery -> remote (Either KError KResponse)) |
244 | -- ^ Handler. | 240 | -- ^ Handler. |
245 | -> remote () | 241 | -> remote () |
246 | remoteServer servport action = bracket (liftIO bindServ) (liftIO . sClose) loop | 242 | remoteServer servAddr action = bracket (liftIO bindServ) (liftIO . sClose) loop |
247 | where | 243 | where |
248 | bindServ = do | 244 | bindServ = do |
249 | sock <- socket AF_INET Datagram defaultProtocol | 245 | sock <- socket AF_INET Datagram defaultProtocol |
250 | bindSocket sock (SockAddrInet servport iNADDR_ANY) | 246 | bindSocket sock servAddr |
251 | return sock | 247 | return sock |
252 | 248 | ||
253 | loop sock = forever $ do | 249 | loop sock = forever $ do |
254 | (bs, addr) <- liftIO $ recvFrom sock maxMsgSize | 250 | (bs, addr) <- liftIO $ recvFrom sock maxMsgSize |
255 | case addr of | 251 | reply <- handleMsg bs addr |
256 | SockAddrInet port host -> do | 252 | liftIO $ sendMessage reply addr sock |
257 | let kaddr = (host, port) | ||
258 | reply <- handleMsg bs kaddr | ||
259 | liftIO $ sendMessage reply kaddr sock | ||
260 | _ -> return () | ||
261 | |||
262 | where | 253 | where |
263 | handleMsg bs addr = case decoded bs of | 254 | handleMsg bs addr = case decoded bs of |
264 | Right query -> (either toBEncode toBEncode <$> action addr query) | 255 | Right query -> (either toBEncode toBEncode <$> action addr query) |