git.iko.soy/distributed-process
on master help sign in
tweaked docs and Send benchmark
commitfc72a012bf8b454d366748cc8fae2fc7cd7331e4 authorAdam Foltzer <acfoltzer@gmail.com> dateThu 23 Feb 2012, 15:40 (15 years ago) parent595ed74
2 files changed+13−18
benchmarks/SendTransport.hs+3−3 network-transport/src/Network/Transport/TCP.hs+10−15
benchmarks/SendTransport.hs+3−3
@@ -92,12 +92,12 @@ main = do
9292
9393-- | The effect of `ping sourceEndPing targetEndPong bs` is to send the
9494-- `ByteString` `bs` using `sourceEndPing`, and to then receive
95−-- a single byte back from `targetEndPong`.
96−ping :: SourceEnd -> TargetEnd -> ByteString -> IO Bool
95+-- that string back. Returns the length of the received message.
96+ping :: SourceEnd -> TargetEnd -> ByteString -> IO Int
9797ping sourceEndPing targetEndPong bs = do
9898 send sourceEndPing [bs]
9999 cs <- receive targetEndPong
100− return $! BS.head bs == BS.head (head cs)
100+ return . fromIntegral $! sum (map BS.length cs)
101101
102102-- | This function takes a `TargetEnd` for the pings, and a `SourceEnd` for
103103-- pongs. Whenever a ping is received from the `TargetEnd`, a pong is sent
network-transport/src/Network/Transport/TCP.hs+10−15
@@ -49,7 +49,7 @@ decode = Ser.decode
4949import Data.ByteString.Lazy.Char8 (ByteString)
5050import qualified Data.ByteString.Lazy.Char8 as BS
5151import qualified Network.Socket.ByteString.Lazy as NBS
52−sendNBS sock bs = NBS.sendAll sock $! head bs
52+sendNBS sock = NBS.sendAll sock . BS.concat
5353encode = Ser.encodeLazy
5454decode = Ser.decodeLazy
5555#endif
@@ -133,7 +133,7 @@ mkSourceEnd host service chanId = withSocketsDo $ do
133133 return SourceEnd
134134 { send = {-# SCC "send" #-} \bss -> do
135135 let size = fromIntegral (sum . map BS.length $ bss) :: Int32
136− sizeStr | size < 255 = encode (fromIntegral size :: Word8)
136+ sizeStr | size < 255 = BS.singleton (toEnum $ fromEnum size)
137137 | otherwise = BS.cons (toEnum 255) (encode size)
138138 sendNBS sock (sizeStr:bss)
139139 , closeSourceEnd = {-# SCC "closeSourceEnd" #-} sClose sock
@@ -179,7 +179,7 @@ procConnections chans sock = forever $ do
179179
180180-- | This function first extracts a header of type Word8, which determines
181181-- the size of the ByteString that follows. If this size is 0, this indicates
182−-- that the ByteString is large, so the next value is an Int64, which
182+-- that the ByteString is large, so the next value is an Int32, which
183183-- determines the size of the ByteString that follows. The ByteString is then
184184-- extracted from the socket, and then written to the Chan only when
185185-- complete. If either of the first header size is null this indicates the
@@ -221,18 +221,13 @@ procMessages chans chanId chan sock = do
221221 writeChan chan [bs]
222222 procMessages chans chanId chan sock
223223
224−-- | The result of `recvExact sock n` is a `[ByteString]` whose concatenation
225−-- is of length `n`, received from `sock`. No more bytes than necessary are
226−-- read from the socket.
227−-- NB: This uses Network.Socket.ByteString.recv, which may *discard*
228−-- superfluous input depending on the socket type. Also note that
229−-- if `recv` returns an empty `ByteString` then this means that the socket
230−-- was closed: in this case, we return an empty list.
231−-- NB: It may be appropriate to change the return type to
232−-- IO (Either ByteString ByteString), where `return Left bs` indicates
233−-- a partial retrieval since the socket was closed, and `return Right bs`
234−-- indicates success. This hasn't been implemented, since a Transport `receive`
235−-- represents an atomic receipt of a message.
224+-- | The normal result of `recvExact sock n` is `Right ByteString`
225+-- whose string of length `n`, received from `sock`. If fewer than `n`
226+-- bytes are read from `sock` before it closes, the result is `Left
227+-- ByteString` whose string is those bytes that were received. No more
228+-- bytes than necessary are read from the socket. NB: This uses
229+-- Network.Socket.ByteString.recv, which may *discard* superfluous
230+-- input depending on the socket type.
236231#ifndef LAZY
237232recvExact :: Socket -> Int32 -> IO (Either ByteString ByteString)
238233recvExact sock l = do