From bda62efef1ad38779f23b38b4e1436f06fb9c7c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Tue, 1 Aug 2023 23:01:30 +0200 Subject: Network protocol refactoring with explicit data flows --- src/ICE.chs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'src/ICE.chs') diff --git a/src/ICE.chs b/src/ICE.chs index 98584a2..d553a88 100644 --- a/src/ICE.chs +++ b/src/ICE.chs @@ -17,7 +17,6 @@ module ICE ( ) where import Control.Arrow -import Control.Concurrent.Chan import Control.Concurrent.MVar import Control.Monad import Control.Monad.Except @@ -31,6 +30,7 @@ import Data.Text (Text) import qualified Data.Text as T import qualified Data.Text.Encoding as T import qualified Data.Text.Read as T +import Data.Void import Foreign.C.String import Foreign.C.Types @@ -39,17 +39,16 @@ import Foreign.Marshal.Array import Foreign.Ptr import Foreign.StablePtr +import Flow import Storage #include "pjproject.h" data IceSession = IceSession { isStrans :: PjIceStrans - , isChan :: MVar (Either [ByteString] (MappedChan ByteString)) + , isChan :: MVar (Either [ByteString] (Flow Void ByteString)) } -data MappedChan a = forall b. MappedChan (a -> b) (Chan b) - instance Eq IceSession where (==) = (==) `on` isStrans @@ -188,13 +187,13 @@ foreign export ccall ice_call_cb :: StablePtr (IO ()) -> IO () ice_call_cb :: StablePtr (IO ()) -> IO () ice_call_cb = join . deRefStablePtr -iceSetChan :: IceSession -> (ByteString -> a) -> Chan a -> IO () -iceSetChan sess f chan = do +iceSetChan :: IceSession -> Flow Void ByteString -> IO () +iceSetChan sess chan = do modifyMVar_ (isChan sess) $ \orig -> do case orig of - Left buf -> writeList2Chan chan $ map f $ reverse buf + Left buf -> mapM_ (writeFlowIO chan) $ reverse buf Right _ -> return () - return $ Right $ MappedChan f chan + return $ Right chan foreign export ccall ice_rx_data :: StablePtr IceSession -> Ptr CChar -> Int -> IO () ice_rx_data :: StablePtr IceSession -> Ptr CChar -> Int -> IO () @@ -202,5 +201,5 @@ ice_rx_data sptr buf len = do sess <- deRefStablePtr sptr bs <- packCStringLen (buf, len) modifyMVar_ (isChan sess) $ \case - mc@(Right (MappedChan f chan)) -> writeChan chan (f bs) >> return mc + mc@(Right chan) -> writeFlowIO chan bs >> return mc Left bss -> return $ Left (bs:bss) -- cgit v1.2.3