diff options
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/Lossless.hs | 21 | ||||
-rw-r--r-- | src/Network/Tox/Session.hs | 11 |
2 files changed, 25 insertions, 7 deletions
diff --git a/src/Network/Lossless.hs b/src/Network/Lossless.hs index ff0b8cc1..4d5521fd 100644 --- a/src/Network/Lossless.hs +++ b/src/Network/Lossless.hs | |||
@@ -14,6 +14,7 @@ import Control.Monad | |||
14 | import Control.Monad.STM | 14 | import Control.Monad.STM |
15 | import Data.Function | 15 | import Data.Function |
16 | import Data.Word | 16 | import Data.Word |
17 | import System.IO.Error | ||
17 | 18 | ||
18 | import Data.PacketBuffer as PB | 19 | import Data.PacketBuffer as PB |
19 | import DPut | 20 | import DPut |
@@ -32,12 +33,19 @@ data SequenceInfo = SequenceInfo | |||
32 | } | 33 | } |
33 | deriving (Eq,Ord,Show) | 34 | deriving (Eq,Ord,Show) |
34 | 35 | ||
36 | data OutgoingInfo y = OutgoingInfo | ||
37 | { oIsLossy :: Bool -- ^ True if the packet is treated as lossy. | ||
38 | , oEncoded :: y -- ^ The packet. | ||
39 | , oHandleException :: Maybe (IOError -> IO ()) -- ^ Optionally handle send failure. | ||
40 | } | ||
41 | |||
35 | -- | Obtain a reliable transport form an unreliable one. | 42 | -- | Obtain a reliable transport form an unreliable one. |
36 | lossless :: Show addr => | 43 | lossless :: Show addr => |
37 | (x -> addr -> IO (PacketInboundEvent (x',addr'))) -- ^ Used to classify newly arrived packets. | 44 | (x -> addr -> IO (PacketInboundEvent (x',addr'))) -- ^ Used to classify newly arrived packets. |
38 | -> (SequenceInfo -> x' -> addr' -> IO (Bool,y)) -- ^ Used to encode and classify outbound packets. | 45 | -> (SequenceInfo -> x' -> addr' -> IO (OutgoingInfo y)) -- ^ Used to encode and classify outbound packets. |
39 | -> addr -- ^ The remote address for this session. | 46 | -> addr -- ^ The remote address for this session. |
40 | -> TransportA String addr x y -- ^ An unreliable lossy transport. | 47 | -> TransportA String addr x y -- ^ An unreliable lossy transport. |
48 | |||
41 | -> IO ( Transport String addr' x' -- ^ A reliable lossless transport. | 49 | -> IO ( Transport String addr' x' -- ^ A reliable lossless transport. |
42 | , [Word32] -> IO () -- ^ Use this to request lost packets be re-sent. | 50 | , [Word32] -> IO () -- ^ Use this to request lost packets be re-sent. |
43 | , IO ([Word32],Word32) -- ^ Use this to discover missing packets to request. | 51 | , IO ([Word32],Word32) -- ^ Use this to discover missing packets to request. |
@@ -86,7 +94,7 @@ lossless isLossless encode saddr udp = do | |||
86 | seqno <- PB.nextToSendSequenceNumber pb | 94 | seqno <- PB.nextToSendSequenceNumber pb |
87 | ack <- PB.expectingSequenceNumber pb | 95 | ack <- PB.expectingSequenceNumber pb |
88 | return $ SequenceInfo seqno ack | 96 | return $ SequenceInfo seqno ack |
89 | (islossy,x) <- encode seqno x' a' | 97 | OutgoingInfo islossy x oops <- encode seqno x' a' |
90 | (isfull,nn) <- | 98 | (isfull,nn) <- |
91 | if islossy | 99 | if islossy |
92 | then do | 100 | then do |
@@ -100,7 +108,8 @@ lossless isLossless encode saddr udp = do | |||
100 | atomically $ do | 108 | atomically $ do |
101 | (isfull,_) <- PB.grokOutboundPacket pb (PacketSent (sequenceNumber seqno) x) | 109 | (isfull,_) <- PB.grokOutboundPacket pb (PacketSent (sequenceNumber seqno) x) |
102 | when isfull retry | 110 | when isfull retry |
103 | sendMessage udp saddr x | 111 | let sendit = sendMessage udp saddr x |
112 | maybe sendit (catchIOError sendit) oops | ||
104 | , closeTransport = do | 113 | , closeTransport = do |
105 | atomically $ writeTChan oob Nothing -- quit rloop thread | 114 | atomically $ writeTChan oob Nothing -- quit rloop thread |
106 | closeTransport udp | 115 | closeTransport udp |
diff --git a/src/Network/Tox/Session.hs b/src/Network/Tox/Session.hs index 525338b2..c672087f 100644 --- a/src/Network/Tox/Session.hs +++ b/src/Network/Tox/Session.hs | |||
@@ -11,6 +11,7 @@ module Network.Tox.Session | |||
11 | 11 | ||
12 | import Control.Concurrent.STM | 12 | import Control.Concurrent.STM |
13 | import Control.Monad | 13 | import Control.Monad |
14 | import Control.Exception | ||
14 | import Data.Functor.Identity | 15 | import Data.Functor.Identity |
15 | import Data.Word | 16 | import Data.Word |
16 | import Network.Socket (SockAddr) | 17 | import Network.Socket (SockAddr) |
@@ -132,7 +133,15 @@ plainHandshakeH sp saddr skey handshake = do | |||
132 | <- lossless (\cp a -> return $ fmap (,a) $ checkLossless $ runIdentity $ pktData cp) | 133 | <- lossless (\cp a -> return $ fmap (,a) $ checkLossless $ runIdentity $ pktData cp) |
133 | (\seqno p _ -> do | 134 | (\seqno p _ -> do |
134 | y <- encryptPacket sk $ bookKeeping seqno p | 135 | y <- encryptPacket sk $ bookKeeping seqno p |
135 | return (lossyness (msgID p) == Lossy, y)) | 136 | return OutgoingInfo |
137 | { oIsLossy = lossyness (msgID p) == Lossy | ||
138 | , oEncoded = y | ||
139 | , oHandleException = Just $ \e -> do | ||
140 | dput XUnexpected $ unlines | ||
141 | [ "<-- " ++ show e | ||
142 | , "<-- while sending " ++ show (seqno,p) ] | ||
143 | throwIO e | ||
144 | }) | ||
136 | () | 145 | () |
137 | t | 146 | t |
138 | let _ = t :: TransportA String () (CryptoPacket Identity) (CryptoPacket Encrypted) | 147 | let _ = t :: TransportA String () (CryptoPacket Identity) (CryptoPacket Encrypted) |