summaryrefslogtreecommitdiff
path: root/dht/Connection
diff options
context:
space:
mode:
Diffstat (limited to 'dht/Connection')
-rw-r--r--dht/Connection/Tcp.hs16
1 files changed, 10 insertions, 6 deletions
diff --git a/dht/Connection/Tcp.hs b/dht/Connection/Tcp.hs
index 2572eba6..06290a1c 100644
--- a/dht/Connection/Tcp.hs
+++ b/dht/Connection/Tcp.hs
@@ -37,6 +37,7 @@ import Data.Map (Map)
37import Data.Monoid ( (<>) ) 37import Data.Monoid ( (<>) )
38import Control.Concurrent.ThreadUtil 38import Control.Concurrent.ThreadUtil
39 39
40import Control.Arrow
40import Control.Concurrent.STM 41import Control.Concurrent.STM
41-- import Control.Concurrent.STM.TMVar 42-- import Control.Concurrent.STM.TMVar
42-- import Control.Concurrent.STM.TChan 43-- import Control.Concurrent.STM.TChan
@@ -76,6 +77,7 @@ import Network.StreamServer
76import Network.SocketLike hiding (sClose) 77import Network.SocketLike hiding (sClose)
77import qualified Connection as G 78import qualified Connection as G
78 ;import Connection (Manager (..), PeerAddress (..), Policy (..)) 79 ;import Connection (Manager (..), PeerAddress (..), Policy (..))
80import Network.Address (localhost4)
79import DPut 81import DPut
80import DebugTag 82import DebugTag
81 83
@@ -96,7 +98,7 @@ data ConnectionParameters conkey u =
96 -- ^ The miliseconds of idle after 'RequiresPing' is signaled 98 -- ^ The miliseconds of idle after 'RequiresPing' is signaled
97 -- that are necessary for the connection to be considered 99 -- that are necessary for the connection to be considered
98 -- lost and signalling 'EOF'. 100 -- lost and signalling 'EOF'.
99 , makeConnKey :: RestrictedSocket -> IO (conkey,u) 101 , makeConnKey :: (RestrictedSocket,(Local SockAddr, Remote SockAddr)) -> IO (conkey,u)
100 -- ^ This action creates a lookup key for a new connection. If 'duplex' 102 -- ^ This action creates a lookup key for a new connection. If 'duplex'
101 -- is 'True' and the result is already assocatied with an established 103 -- is 'True' and the result is already assocatied with an established
102 -- connection, then an 'EOF' will be forced before the the new 104 -- connection, then an 'EOF' will be forced before the the new
@@ -122,7 +124,7 @@ data ConnectionParameters conkey u =
122-- * 'duplex' = True 124-- * 'duplex' = True
123-- 125--
124connectionDefaults 126connectionDefaults
125 :: (RestrictedSocket -> IO (conkey,u)) -> ConnectionParameters conkey u 127 :: ((RestrictedSocket,(Local SockAddr,Remote SockAddr)) -> IO (conkey,u)) -> ConnectionParameters conkey u
126connectionDefaults f = ConnectionParameters 128connectionDefaults f = ConnectionParameters
127 { pingInterval = 28000 129 { pingInterval = 28000
128 , timeout = 2000 130 , timeout = 2000
@@ -337,14 +339,15 @@ server allocate sessionConduits = do
337 handle (\e -> do -- let t = ioeGetErrorType e 339 handle (\e -> do -- let t = ioeGetErrorType e
338 when (isDoesNotExistError e) $ return () -- warn "GOTCHA" 340 when (isDoesNotExistError e) $ return () -- warn "GOTCHA"
339 -- warn $ "connect-error: " <> bshow e 341 -- warn $ "connect-error: " <> bshow e
340 (conkey,u) <- makeConnKey params (restrictSocket sock) -- XXX: ? 342 (conkey,u) <- makeConnKey params (restrictSocket sock,(Local localhost4, Remote addr)) -- XXX: ?
341 Socket.close sock 343 Socket.close sock
342 atomically 344 atomically
343 $ writeTChan (serverEvent server) 345 $ writeTChan (serverEvent server)
344 $ ((conkey,u),ConnectFailure addr)) 346 $ ((conkey,u),ConnectFailure addr))
345 $ do 347 $ do
346 connect sock addr 348 connect sock addr
347 (conkey,u) <- makeConnKey params (restrictSocket sock) 349 laddr <- Socket.getSocketName sock
350 (conkey,u) <- makeConnKey params (restrictSocket sock, (Local laddr, Remote addr))
348 h <- socketToHandle sock ReadWriteMode 351 h <- socketToHandle sock ReadWriteMode
349 newConnection server sessionConduits params conkey u h Out 352 newConnection server sessionConduits params conkey u h Out
350 return () 353 return ()
@@ -375,13 +378,14 @@ server allocate sessionConduits = do
375 sock <- socket (socketFamily addr) Stream proto 378 sock <- socket (socketFamily addr) Stream proto
376 handle (\(SomeException e) -> do 379 handle (\(SomeException e) -> do
377 -- Any thing else goes wrong and we broadcast ConnectFailure. 380 -- Any thing else goes wrong and we broadcast ConnectFailure.
378 do (conkey,u) <- makeConnKey params (restrictSocket sock) 381 do (conkey,u) <- makeConnKey params (restrictSocket sock,(Local localhost4, Remote addr))
379 Socket.close sock 382 Socket.close sock
380 atomically $ writeTChan (serverEvent server) ((conkey,u),ConnectFailure addr) 383 atomically $ writeTChan (serverEvent server) ((conkey,u),ConnectFailure addr)
381 `onException` return () 384 `onException` return ()
382 atomically $ readTVar retryVar) $ do 385 atomically $ readTVar retryVar) $ do
383 connect sock addr 386 connect sock addr
384 (conkey,u) <- makeConnKey params (restrictSocket sock) 387 laddr <- Socket.getSocketName sock
388 (conkey,u) <- makeConnKey params (restrictSocket sock, (Local laddr, Remote addr))
385 h <- socketToHandle sock ReadWriteMode 389 h <- socketToHandle sock ReadWriteMode
386 threads <- newConnection server sessionConduits params conkey u h Out 390 threads <- newConnection server sessionConduits params conkey u h Out
387 atomically $ do threadsWait threads 391 atomically $ do threadsWait threads