Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix disconnect remote endpoints on close endpoint #417

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,34 @@ apiNewEndPoint state = handle (return . Left) $ atomically $ do
TransportError ResolveMulticastGroupUnsupported "Multicast not supported"

apiCloseEndPoint :: TVar TransportState -> EndPointAddress -> IO ()
apiCloseEndPoint state addr = atomically $ whenValidTransportState state $ \vst ->
forM_ (vst ^. localEndPointAt addr) $ \lep -> do
old <- swapTVar (localEndPointState lep) LocalEndPointClosed
case old of
LocalEndPointClosed -> return ()
LocalEndPointValid lepvst -> do
forM_ (Map.elems (lepvst ^. connections)) $ \lconn -> do
st <- swapTVar (localConnectionState lconn) LocalConnectionClosed
case st of
LocalConnectionClosed -> return ()
LocalConnectionFailed -> return ()
_ -> forM_ (vst ^. localEndPointAt (localConnectionRemoteAddress lconn)) $ \thep ->
whenValidLocalEndPointState thep $ \_ -> do
writeTChan (localEndPointChannel thep)
(ConnectionClosed (localConnectionId lconn))
writeTChan (localEndPointChannel lep) EndPointClosed
writeTVar (localEndPointState lep) LocalEndPointClosed
writeTVar state (TransportValid $ (localEndPoints ^: Map.delete addr) vst)
apiCloseEndPoint state addr = atomically $ whenValidTransportState state $ \vst -> do

forM_ (Map.toList $ _localEndPoints vst) $
\(theirAddr, lep) -> do

if theirAddr == addr
then do
old <- swapTVar (localEndPointState lep) LocalEndPointClosed
case old of
LocalEndPointClosed -> return ()
LocalEndPointValid lepvst -> do
forM_ (Map.elems (lepvst ^. connections)) $ \lconn -> do
st <- swapTVar (localConnectionState lconn) LocalConnectionClosed
case st of
LocalConnectionClosed -> return ()
LocalConnectionFailed -> return ()
_ -> do
forM_ (vst ^. localEndPointAt (localConnectionRemoteAddress lconn)) $ \thep ->
whenValidLocalEndPointState thep $ \_ -> do
writeTChan (localEndPointChannel thep)
(ConnectionClosed (localConnectionId lconn))
writeTChan (localEndPointChannel lep) EndPointClosed
writeTVar (localEndPointState lep) LocalEndPointClosed

else do
apiBreakConnection state addr theirAddr "remote endpoint disconnected"

writeTVar state (TransportValid $ (localEndPoints ^: Map.delete addr) vst)

-- | Tear down functions that should be called in case if conncetion fails.
apiBreakConnection :: TVar TransportState
Expand Down
Loading