network-mux
Safe HaskellNone
LanguageHaskell2010

Network.Mux.Channel

Description

An extension of Channel, with additional Channel implementations.

Synopsis

Channel

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

A channel which can send and receive values.

It is more general than what `network-mux` requires, see ByteChannel instead. However this is useful for testing purposes when one is either using mux or connecting two ends directly.

Constructors

Channel 

Fields

  • send :: a -> m ()

    Write bytes to the channel.

    It maybe raise exceptions.

  • 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).

Channel API

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.

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

channelEffect Source #

Arguments

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

Action before send

-> (Maybe a -> m ())

Action after recv

-> Channel m a 
-> Channel m a 

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.

create a Channel

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

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

connected Channels

createConnectedChannels :: MonadSTM m => 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.

ByteChannel

type ByteChannel (m :: Type -> Type) = Channel m ByteString Source #

Channel using ByteString.

create a ByteChannel

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.

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

withFifosAsChannel Source #

Arguments

:: FilePath

FIFO for reading

-> FilePath

FIFO for writing

-> (ByteChannel IO -> IO a) 
-> IO a 

Open a pair of Unix FIFOs, and expose that as a Channel.

The peer process needs to open the same files but the other way around, for writing and reading.

This is primarily for the purpose of demonstrations that use communication between multiple local processes. It is Unix specific.

socketAsChannel :: Socket -> ByteChannel IO Source #

Make a Channel from a Socket. The socket must be a stream socket

connected ByteChannels

createBufferConnectedChannels :: MonadSTM m => m (ByteChannel m, ByteChannel m) Source #

Create a pair of Channels that are connected internally.

This is intended for inter-thread communication, such as between a multiplexing thread and a thread running a peer.

It uses lazy ByteStrings but it ensures that data written to the channel is fully evaluated first. This ensures that any work to serialise the data takes place on the writer side and not the reader side.

createPipeConnectedChannels :: IO (ByteChannel IO, ByteChannel IO) Source #

Create a local pipe, with both ends in this process, and expose that as a pair of Channels, one for each end.

This is primarily for testing purposes since it does not allow actual IPC.

createSocketConnectedChannels Source #

Arguments

:: Family

Usually AF_UNIX or AF_INET

-> IO (ByteChannel IO, ByteChannel IO)