network-fancy-0.2.4: Networking support with a cleaner API

Safe HaskellNone
LanguageHaskell98

Network.Fancy

Contents

Synopsis

Address

data Address Source

Constructors

IP HostName Int

Host name and port, either IPv4 or IPv6.

IPv4 HostName Int

Host name and port, only IPv4.

IPv6 HostName Int

Host name and port, only IPv6

Unix FilePath

Local unix socket, not supported on Windows.

Stream clients

withStream :: Address -> (Handle -> IO a) -> IO a Source

Open a stream (tcp) socket for the given block and close it afterwards.

connectStream :: Address -> IO Handle Source

Open a stream (tcp) socket.

Datagram clients

connectDgram :: Address -> IO Socket Source

Open a datagram (udp) socket.

withDgram :: Address -> (Socket -> IO a) -> IO a Source

Open a datagram (udp) socket for the given block and close it afterwards.

class StringLike string Source

Minimal complete definition

toBS, fromBS

recv :: StringLike string => Socket -> Int -> IO string Source

Receive one chunk with given maximum size

send :: StringLike string => Socket -> string -> IO () Source

Send the string as one chunk

closeSocket :: Socket -> IO () Source

Close the socket specified.

Servers

data ServerSpec Source

Constructors

ServerSpec 

Fields

address :: Address

Address for the server. Use hostname "" to bind to all interfaces.

reverseAddress :: Reverse

Should the address of connecting clients be suplied numerically or as a name to server function.

threading :: Threading

Handle requests Inline or Threaded.

closeConnection :: Bool

Close the client connection automatically after the ServerFun finishes.

recvSize :: Int

Buffer size for receiving datagrams.

serverSpec :: ServerSpec Source

Default server specification

data Threading Source

Constructors

Threaded

Run each request in a separate thread without blocking the server loop.

Inline

Run each request inline inside the request loop.

data Reverse Source

Constructors

ReverseNumeric

Use numeric addresses for peers.

ReverseName

Resolve reverse names if possible for peers.

streamServer :: ServerSpec -> (Handle -> Address -> IO ()) -> IO [ThreadId] Source

Run a stream (tcp) server. The function does not block, use sleepForever if that is desired.

dgramServer Source

Arguments

:: StringLike packet 
=> ServerSpec

Server specification

-> (packet -> Address -> IO [packet])

The server function is given a received packet and the Address of the peer. It returns a list of reply packets to send to that peer. Note that the list elements are invidual packets, not concatenated together.

-> IO [ThreadId]

ThreadIds of the server listener processes.

Run a datagram (udp) server. The function does not block, use sleepForever if that is desired.

sleepForever :: IO () Source

Sleep forever. Useful after a server.

Other

getCurrentHost :: IO HostName Source

Get the current hostname.