pipes-network-0.4.0.2: Use network sockets together with the pipes library.

Safe HaskellNone

Control.Proxy.TCP.Sync

Contents

Description

This module exports Proxys that allow implementing synchronous RPC-like communication with a remote end by using a simple protocol on their downstream interface.

As opposed to the similar proxies found in Control.Proxy.TCP.Safe.Sync, these don't use the exception handling facilities provided by ExceptionP.

You may prefer the more general and efficient proxies from Control.Proxy.TCP.

Synopsis

Socket proxies

socketSyncServerSource

Arguments

:: Proxy p 
=> Socket

Connected socket.

-> Request ByteString 
-> Server p (Request ByteString) Response IO () 

Server able to send and receive bytes through a Socket.

If downstream requests Send bytes, then such bytes are sent to the remote end and then this proxy responds Sent downstream.

If downstream requests Receive num, then at most num bytes are received from the remote end. This proxy then responds downstream such received bytes as Received bytes. Less than the specified maximum number of bytes might be received at once.

This proxy returns if EOF is received or the remote end closes its side of the connection while we are trying to read from it.

socketSyncProxySource

Arguments

:: Proxy p 
=> Socket

Connected socket.

-> Request a' 
-> p a' ByteString (Request a') Response IO () 

Proxy able to send and receive bytes through a Socket.

If downstream requests Send a', then such a' request is forwarded upstream, which in return responds a ByteString that this proxy sends to the remote end. After sending to the remote end, this proxy responds Sent downstream.

If downstream requests Receive num, then at most num bytes are received from the remote end. This proxy then responds downstream such received bytes as Received bytes. Less than the specified maximum number of bytes might be received at once.

This proxy returns if EOF is received or the remote end closes its side of the connection while we are trying to read from it.

Timeouts

These proxies behave like the similarly named ones above, except support for timing out the interaction with the remote end is added.

socketSyncServerTimeoutSource

Arguments

:: Proxy p 
=> Int

Timeout in microseconds (1/10^6 seconds).

-> Socket

Connected socket.

-> Request ByteString 
-> Server (EitherP Timeout p) (Request ByteString) Response IO () 

Like socketSyncServer, except it throws a Timeout exception in the EitherP proxy transformer if interacting with the remote end takes more time than specified.

socketSyncProxyTimeoutSource

Arguments

:: Proxy p 
=> Int

Timeout in microseconds (1/10^6 seconds).

-> Socket

Connected socket.

-> Request a' 
-> EitherP Timeout p a' ByteString (Request a') Response IO () 

Like socketSyncProxy, except it throws a Timeout exception in the EitherP proxy transformer if interacting with the remote end takes more time than specified.

RPC support

syncDelimitSource

Arguments

:: (Monad m, Proxy p) 
=> Int

Maximum number of bytes to receive at once.

-> ByteString

Delimiting bytes.

-> b' 
-> p (Request b') Response b' ByteString m r 

When used together with one of the socketSync* proxies upstream, this proxy sends a single ByteString to the remote end and then repeatedly receives bytes from the remote end until the given delimiter is found. Finally, a single ByteString up to the given delimiter (inclusive) is sent downstream and then the whole process is repeated.

This proxy works cooperatively with any socketSync* proxy immediately upstream, so read their documentation to understand the purpose of the b' value received from downstream.

For example, if you'd like to convert a Socket into an synchronous line-oriented RPC client implemented as a Server in which RPC calls are received via the downstream interface and RPC responses are sent downstream, then you could use this proxy as:

 socketSyncServer ... >-> syncDelimit 4096 "\r\n"

Otherwise, if you'd like to convert a Socket into an synchronous line-oriented RPC client implemented as a Proxy in which RPC calls are received via the upstream interface and RPC responses are sent downstream, then you could use this proxy as:

 socketSyncProxy ... >-> syncDelimit 4096 "\r\n"

Protocol types

data Request t Source

A request made to one of the socketSync* proxies.

Constructors

Send t 
Receive Int 

Instances

Eq t => Eq (Request t) 
Read t => Read (Request t) 
Show t => Show (Request t) 

data Response Source

A response received from one of the socketSync* proxies.

Constructors

Sent 
Received ByteString