servePut and clientPut implementation

Made the data-length header required even for v0. This simplifies the
implementation, and doesn't preclude extra verification being done for
v0.

The connectionWaitVar is an ugly hack. In servePut, nothing waits
on the waitvar, and I could not find a good way to make anything wait on
it.
This commit is contained in:
Joey Hess 2024-07-22 10:20:18 -04:00
parent eb4fb388bd
commit 4826a3745d
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
10 changed files with 222 additions and 185 deletions

View file

@ -80,11 +80,12 @@ mkRunState mk = do
data P2PHandle
= P2PHandle Handle
| P2PHandleTMVar (TMVar (Either L.ByteString Message)) (TMVar ())
| P2PHandleTMVar (TMVar (Either L.ByteString Message)) (Maybe (TMVar ()))
signalFullyConsumedByteString :: P2PHandle -> IO ()
signalFullyConsumedByteString (P2PHandle _) = return ()
signalFullyConsumedByteString (P2PHandleTMVar _ waitv) =
signalFullyConsumedByteString (P2PHandleTMVar _ Nothing) = return ()
signalFullyConsumedByteString (P2PHandleTMVar _ (Just waitv)) =
atomically $ putTMVar waitv ()
data P2PConnection = P2PConnection
@ -216,7 +217,7 @@ runNet runst conn runner f = case f of
ifM (atomically (tryPutTMVar mv (Right m)))
( return $ Right ()
, return $ Left $ toException $
P2PTMVarException "TMVar left full"
P2PTMVarException ("TMVar left full " ++ show m)
)
case v of
Left e -> return $ Left $ ProtoFailureException e
@ -256,7 +257,7 @@ runNet runst conn runner f = case f of
liftIO $ atomically $ putTMVar mv (Left b)
-- Wait for the whole bytestring to
-- be processed.
liftIO $ atomically $ takeTMVar waitv
liftIO $ maybe noop (atomically . takeTMVar) waitv
runner next
ReceiveBytes len p next ->
case connIhdl conn of