Z-IO-0.6.1.0: Simple and high performance IO toolkit for Haskell
Copyright(c) Dong Han 2018
LicenseBSD
Maintainerwinterland1989@gmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Z.IO.Network.TCP

Description

This module provides an API for creating TCP servers and clients.

Synopsis

TCP Client

data TCPClientConfig Source #

A TCP client configuration

Constructors

TCPClientConfig 

Fields

Instances

Instances details
Eq TCPClientConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

Ord TCPClientConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

Show TCPClientConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

Generic TCPClientConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

Associated Types

type Rep TCPClientConfig :: Type -> Type #

JSON TCPClientConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

Print TCPClientConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

type Rep TCPClientConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

type Rep TCPClientConfig = D1 ('MetaData "TCPClientConfig" "Z.IO.Network.TCP" "Z-IO-0.6.1.0-gzKEsYtajW4dyRTMbokGF" 'False) (C1 ('MetaCons "TCPClientConfig" 'PrefixI 'True) ((S1 ('MetaSel ('Just "tcpClientAddr") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe SocketAddr)) :*: S1 ('MetaSel ('Just "tcpRemoteAddr") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 SocketAddr)) :*: (S1 ('MetaSel ('Just "tcpClientNoDelay") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool) :*: S1 ('MetaSel ('Just "tcpClientKeepAlive") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 CUInt))))

data UVStream Source #

A haskell data type wrap an uv_stream_t inside

UVStream DO NOT provide thread safety! Use UVStream concurrently in multiple threads will lead to undefined behavior.

Instances

Instances details
Show UVStream Source # 
Instance details

Defined in Z.IO.UV.UVStream

Print UVStream Source # 
Instance details

Defined in Z.IO.UV.UVStream

Methods

toUTF8BuilderP :: Int -> UVStream -> Builder () #

Output UVStream Source # 
Instance details

Defined in Z.IO.UV.UVStream

Methods

writeOutput :: UVStream -> Ptr Word8 -> Int -> IO () Source #

Input UVStream Source # 
Instance details

Defined in Z.IO.UV.UVStream

Methods

readInput :: UVStream -> Ptr Word8 -> Int -> IO Int Source #

defaultTCPClientConfig :: TCPClientConfig Source #

Default config, connect to localhost:8888.

initTCPClient :: HasCallStack => TCPClientConfig -> Resource UVStream Source #

init a TCP client Resource, which open a new connect when used.

getTCPSockName :: HasCallStack => UVStream -> IO SocketAddr Source #

Get the current address to which the handle is bound.

TCP Server

data TCPServerConfig Source #

A TCP server configuration

Constructors

TCPServerConfig 

Fields

Instances

Instances details
Eq TCPServerConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

Ord TCPServerConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

Show TCPServerConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

Generic TCPServerConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

Associated Types

type Rep TCPServerConfig :: Type -> Type #

JSON TCPServerConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

Print TCPServerConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

type Rep TCPServerConfig Source # 
Instance details

Defined in Z.IO.Network.TCP

type Rep TCPServerConfig = D1 ('MetaData "TCPServerConfig" "Z.IO.Network.TCP" "Z-IO-0.6.1.0-gzKEsYtajW4dyRTMbokGF" 'False) (C1 ('MetaCons "TCPServerConfig" 'PrefixI 'True) ((S1 ('MetaSel ('Just "tcpListenAddr") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 SocketAddr) :*: S1 ('MetaSel ('Just "tcpListenBacklog") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int)) :*: (S1 ('MetaSel ('Just "tcpServerWorkerNoDelay") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool) :*: S1 ('MetaSel ('Just "tcpServerWorkerKeepAlive") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 CUInt))))

defaultTCPServerConfig :: TCPServerConfig Source #

A default hello world server on 0.0.0.0:8888

Test it with main = startTCPServer defaultTCPServerConfig helloWorldWorker or main = startTCPServer defaultTCPServerConfig echoWorker, now try nc -v 127.0.0.1 8888

startTCPServer Source #

Arguments

:: HasCallStack 
=> TCPServerConfig 
-> (UVStream -> IO ())

worker which will get an accepted TCP stream and run in a seperated haskell thread, will be closed upon exception or worker finishes.

-> IO () 

Start a TCP server

Fork new worker threads upon a new connection.

getTCPPeerName :: HasCallStack => UVStream -> IO SocketAddr Source #

Get the address of the peer connected to the handle.

For test

helloWorld :: UVStream -> IO () Source #

Write "hello world" to a UVStream.

echo :: UVStream -> IO () Source #

Echo whatever received bytes.

Internal helper

startServerLoop Source #

Arguments

:: HasCallStack 
=> Int

backLog

-> (UVManager -> Resource UVStream)

uv_tream_t initializer

-> (Ptr UVHandle -> IO ())

bind function

-> (FD -> (UVStream -> IO ()) -> IO ())

thread spawner

-> (UVStream -> IO ())

worker

-> IO () 

Start a server loop with different kind of uv_streams, such as tcp or pipe.

setTCPNoDelay :: HasCallStack => UVStream -> Bool -> IO () Source #

Enable or disable TCP_NODELAY, which enable or disable Nagle’s algorithm.

setTCPKeepAlive :: HasCallStack => UVStream -> CUInt -> IO () Source #

Enable / disable TCP keep-alive. delay is the initial delay in seconds, ignored when enable is zero.

After delay has been reached, 10 successive probes, each spaced 1 second from the previous one, will still happen. If the connection is still lost at the end of this procedure, then the connection is closed, pending io thread will throw TimeExpired exception.