function to run TCP server without TLS (#354)

This commit is contained in:
Evgeny Poberezkin
2022-04-18 08:56:08 +01:00
committed by GitHub
parent d38303d5f1
commit f84439f79d

View File

@@ -4,6 +4,7 @@
module Simplex.Messaging.Transport.Server
( runTransportServer,
runTCPServer,
loadTLSServerParams,
loadFingerprint,
serverHandshake,
@@ -37,7 +38,7 @@ runTransportServer started port serverParams server = do
clients <- newTVarIO S.empty
E.bracket
(startTCPServer started port)
(closeServer clients)
(closeServer started clients)
$ \sock -> forever $ do
(connSock, _) <- accept sock
tid <- forkIO $ connectClient u connSock `E.catch` \(_ :: E.SomeException) -> pure ()
@@ -49,11 +50,23 @@ runTransportServer started port serverParams server = do
(connectTLS serverParams connSock >>= getServerConnection)
closeConnection
(unliftIO u . server)
closeServer :: TVar (Set ThreadId) -> Socket -> IO ()
closeServer clients sock = do
readTVarIO clients >>= mapM_ killThread
close sock
void . atomically $ tryPutTMVar started False
runTCPServer :: TMVar Bool -> ServiceName -> (Socket -> IO ()) -> IO ()
runTCPServer started port server = do
clients <- newTVarIO S.empty
E.bracket
(startTCPServer started port)
(closeServer started clients)
$ \sock -> forever $ do
(connSock, _) <- accept sock
tid <- forkIO $ server connSock `E.catch` \(_ :: E.SomeException) -> pure ()
atomically . modifyTVar' clients $ S.insert tid
closeServer :: TMVar Bool -> TVar (Set ThreadId) -> Socket -> IO ()
closeServer started clients sock = do
readTVarIO clients >>= mapM_ killThread
close sock
void . atomically $ tryPutTMVar started False
startTCPServer :: TMVar Bool -> ServiceName -> IO Socket
startTCPServer started port = withSocketsDo $ resolve >>= open >>= setStarted