typed-protocols-examples
Safe HaskellNone
LanguageHaskell2010

Network.TypedProtocol.Channel

Synopsis

Documentation

data Channel (m :: Type -> Type) a Source #

One end of a duplex channel. It is a reliable, ordered channel of some medium. The medium does not imply message boundaries, it can be just bytes.

Constructors

Channel 

Fields

  • send :: a -> m ()

    Write output to the channel.

    It may raise exceptions (as appropriate for the monad and kind of channel).

  • recv :: m (Maybe a)

    Read some input from the channel, or Nothing to indicate EOF.

    Note that having received EOF it is still possible to send. The EOF condition is however monotonic.

    It may raise exceptions (as appropriate for the monad and kind of channel).

hoistChannel :: (forall x. m x -> n x) -> Channel m a -> Channel n a Source #

isoKleisliChannel :: forall a b m. Monad m => (a -> m b) -> (b -> m a) -> Channel m a -> Channel m b Source #

Given an isomorphism between a and b (in Kleisli category), transform a Channel m a into Channel m b.

fixedInputChannel :: MonadSTM m => [a] -> m (Channel m a) Source #

A Channel with a fixed input, and where all output is discarded.

The input is guaranteed to be supplied via read with the given chunk boundaries.

This is only useful for testing. In particular the fixed chunk boundaries can be used to test that framing and other codecs work with any possible chunking.

mvarsAsChannel :: forall (m :: Type -> Type) a. MonadSTM m => TMVar m a -> TMVar m a -> Channel m a Source #

Make a Channel from a pair of TMVars, one for reading and one for writing.

handlesAsChannel Source #

Arguments

:: Handle

Read handle

-> Handle

Write handle

-> Channel IO ByteString 

Make a Channel from a pair of IO Handles, one for reading and one for writing.

The Handles should be open in the appropriate read or write mode, and in binary mode. Writes are flushed after each write, so it is safe to use a buffering mode. On unix named pipes can be used, see prop_namedPipePipelined_IO

For bidirectional handles it is safe to pass the same handle for both.

createConnectedChannels :: (MonadLabelledSTM m, MonadTraceSTM m, Show a) => m (Channel m a, Channel m a) Source #

Create a pair of channels that are connected via one-place buffers.

This is primarily useful for testing protocols.

createConnectedBufferedChannels :: MonadSTM m => Natural -> m (Channel m a, Channel m a) Source #

Create a pair of channels that are connected via N-place buffers.

This variant blocks when send would exceed the maximum buffer size. Use this variant when you want the environment rather than the Peer to limit the pipelining.

This is primarily useful for testing protocols.

createConnectedBufferedChannelsUnbounded :: MonadSTM m => m (Channel m a, Channel m a) Source #

Create a pair of channels that are connected via two unbounded buffers.

This is primarily useful for testing protocols.

createPipelineTestChannels :: MonadSTM m => Natural -> m (Channel m a, Channel m a) Source #

Create a pair of channels that are connected via N-place buffers.

This variant fails when send would exceed the maximum buffer size. Use this variant when you want the Peer to limit the pipelining itself, and you want to check that it does not exceed the expected level of pipelining.

This is primarily useful for testing protocols.

channelEffect Source #

Arguments

:: Monad m 
=> (a -> m ())

Action before send

-> (Maybe a -> m ())

Action after recv

-> Channel m a 
-> Channel m a 

Transform a channel to add an extra action before every send and after every receive.

delayChannel :: forall (m :: Type -> Type) a. MonadDelay m => DiffTime -> Channel m a -> Channel m a Source #

Delay a channel on the receiver end.

This is intended for testing, as a crude approximation of network delays. More accurate models along these lines are of course possible.

loggingChannel :: forall (m :: Type -> Type) id a. (MonadSay m, Show id, Show a) => id -> Channel m a -> Channel m a Source #

Channel which logs sent and received messages.