diff options
author | James Crayne <jim.crayne@gmail.com> | 2018-05-28 19:54:59 +0000 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2018-05-28 19:54:59 +0000 |
commit | 8fe574a8f1f2f71b68c89aeacccd05d084a4003d (patch) | |
tree | dc665a9b6cc2928f63630f8401af3573338ec7a9 /src/Network | |
parent | 714f05030737b1df78a67ba694ab33fd605d55ed (diff) |
ncLastNMsgs is now CyclicBuffer type
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/Tox/Crypto/Handlers.hs | 23 |
1 files changed, 5 insertions, 18 deletions
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index ee8af399..92cb19b8 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs | |||
@@ -192,17 +192,12 @@ data NetCryptoSession = NCrypto | |||
192 | CryptoMessage | 192 | CryptoMessage |
193 | (CryptoPacket Encrypted) | 193 | (CryptoPacket Encrypted) |
194 | CryptoData | 194 | CryptoData |
195 | , ncLastNMsgs :: PacketQueue (Bool{-Handled?-},(ViewSnapshot,InOrOut CryptoMessage)) | 195 | , ncLastNMsgs :: CyclicBuffer (Bool{-Handled?-},(ViewSnapshot,InOrOut CryptoMessage)) |
196 | -- ^ cyclic buffer, holds the last N non-handshake crypto messages | 196 | -- ^ cyclic buffer, holds the last N non-handshake crypto messages |
197 | -- even if there is no attached user interface. | 197 | -- even if there is no attached user interface. |
198 | , ncListeners :: TVar (IntMap.IntMap (ListenerType,TMChan CryptoMessage)) | 198 | , ncListeners :: TVar (IntMap.IntMap (ListenerType,TMChan CryptoMessage)) |
199 | -- ^ user interfaces may "listen" by inserting themselves into this map | 199 | -- ^ user interfaces may "listen" by inserting themselves into this map |
200 | -- with a unique id and a new TChan, and then reading from the TChan | 200 | -- with a unique id and a new TChan, and then reading from the TChan |
201 | , ncMsgNumVar :: TVar Word32 | ||
202 | -- ^ The number of non-handshake crypto messages written to ncLastNMsgs total | ||
203 | , ncDropCntVar :: TVar Word32 | ||
204 | -- ^ The number of crypto messages that were overwritten in the ncLastNMsgs | ||
205 | -- before anybody got to see them. | ||
206 | } | 201 | } |
207 | 202 | ||
208 | data NetCryptoSessions = NCSessions | 203 | data NetCryptoSessions = NCSessions |
@@ -450,7 +445,7 @@ freshCryptoSession sessions | |||
450 | writeTVar ncMyPacketNonce0 n24plus1 | 445 | writeTVar ncMyPacketNonce0 n24plus1 |
451 | return (return (f n24, n24, ncOutgoingIdMap0)) | 446 | return (return (f n24, n24, ncOutgoingIdMap0)) |
452 | pktoq <- atomically $ PQ.newOutGoing pktq ncToWire toWireIO 0 (outboundQueueCapacity sessions) 0 | 447 | pktoq <- atomically $ PQ.newOutGoing pktq ncToWire toWireIO 0 (outboundQueueCapacity sessions) 0 |
453 | msgQ <- atomically (PQ.newOverwrite 10 0 :: STM (PacketQueue (Bool,(ViewSnapshot,InOrOut CryptoMessage)))) | 448 | lastNQ <- atomically (CB.new 10 0 :: STM (CyclicBuffer (Bool,(ViewSnapshot,InOrOut CryptoMessage)))) |
454 | listeners <- atomically $ newTVar IntMap.empty | 449 | listeners <- atomically $ newTVar IntMap.empty |
455 | msgNum <- atomically $ newTVar 0 | 450 | msgNum <- atomically $ newTVar 0 |
456 | dropNum <- atomically $ newTVar 0 | 451 | dropNum <- atomically $ newTVar 0 |
@@ -478,10 +473,8 @@ freshCryptoSession sessions | |||
478 | , ncDequeueThread = Nothing -- error "you want the NetCrypto-Dequeue thread id, but is it started?" | 473 | , ncDequeueThread = Nothing -- error "you want the NetCrypto-Dequeue thread id, but is it started?" |
479 | , ncPingMachine = Nothing -- error "you want the NetCrypto-PingMachine, but is it started?" | 474 | , ncPingMachine = Nothing -- error "you want the NetCrypto-PingMachine, but is it started?" |
480 | , ncOutgoingQueue = pktoq | 475 | , ncOutgoingQueue = pktoq |
481 | , ncLastNMsgs = msgQ | 476 | , ncLastNMsgs = lastNQ |
482 | , ncListeners = listeners | 477 | , ncListeners = listeners |
483 | , ncMsgNumVar = msgNum | ||
484 | , ncDropCntVar = dropNum | ||
485 | } | 478 | } |
486 | -- launch dequeue thread | 479 | -- launch dequeue thread |
487 | threadid <- forkIO $ do | 480 | threadid <- forkIO $ do |
@@ -886,17 +879,11 @@ hookHelper handledFlg typ session cm = do | |||
886 | addMsgToLastN :: Bool -> MessageType -> NetCryptoSession -> InOrOut CryptoMessage -> IO () | 879 | addMsgToLastN :: Bool -> MessageType -> NetCryptoSession -> InOrOut CryptoMessage -> IO () |
887 | addMsgToLastN handledFlg typ session cm = do | 880 | addMsgToLastN handledFlg typ session cm = do |
888 | let lastNQ = ncLastNMsgs session | 881 | let lastNQ = ncLastNMsgs session |
889 | msgNumVar = ncMsgNumVar session | ||
890 | dropCntVar = ncDropCntVar session | ||
891 | atomically $ do | 882 | atomically $ do |
892 | num <- readTVar msgNumVar | ||
893 | view <- readTVar (ncView session) | 883 | view <- readTVar (ncView session) |
894 | snapshot <- viewSnapshot view | 884 | snapshot <- viewSnapshot view |
895 | (wraps,offset) <- PQ.enqueue lastNQ num (handledFlg,(snapshot,cm)) | 885 | num <- CB.getNextSequenceNum lastNQ |
896 | capacity <- PQ.getCapacity lastNQ | 886 | CB.enqueue lastNQ num (handledFlg,(snapshot,cm)) |
897 | let dropped = wraps * capacity + offset | ||
898 | modifyTVar' msgNumVar (+1) | ||
899 | writeTVar dropCntVar dropped | ||
900 | 887 | ||
901 | 888 | ||
902 | -- | use to add a single hook to a specific session. | 889 | -- | use to add a single hook to a specific session. |