diff options
author | Joe Crayne <joe@jerkface.net> | 2019-12-01 02:22:59 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 23:22:52 -0500 |
commit | 7c88315f6e01e203c537fa3193a83acd3a7afa2c (patch) | |
tree | 73d0164ee26f3d015c542d533e104695444633ac /dht/Connection/Tcp.hs | |
parent | 1a0819cd502f578d25faa7b2add75a20f5d5d0d7 (diff) |
Avoid calling getSocketName after socketToHandle.
Diffstat (limited to 'dht/Connection/Tcp.hs')
-rw-r--r-- | dht/Connection/Tcp.hs | 16 |
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) | |||
37 | import Data.Monoid ( (<>) ) | 37 | import Data.Monoid ( (<>) ) |
38 | import Control.Concurrent.ThreadUtil | 38 | import Control.Concurrent.ThreadUtil |
39 | 39 | ||
40 | import Control.Arrow | ||
40 | import Control.Concurrent.STM | 41 | import 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 | |||
76 | import Network.SocketLike hiding (sClose) | 77 | import Network.SocketLike hiding (sClose) |
77 | import qualified Connection as G | 78 | import qualified Connection as G |
78 | ;import Connection (Manager (..), PeerAddress (..), Policy (..)) | 79 | ;import Connection (Manager (..), PeerAddress (..), Policy (..)) |
80 | import Network.Address (localhost4) | ||
79 | import DPut | 81 | import DPut |
80 | import DebugTag | 82 | import 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 | -- |
124 | connectionDefaults | 126 | connectionDefaults |
125 | :: (RestrictedSocket -> IO (conkey,u)) -> ConnectionParameters conkey u | 127 | :: ((RestrictedSocket,(Local SockAddr,Remote SockAddr)) -> IO (conkey,u)) -> ConnectionParameters conkey u |
126 | connectionDefaults f = ConnectionParameters | 128 | connectionDefaults 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 |