Z-IO-0.1.5.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

Contents

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

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
Show UVStream Source # 
Instance details

Defined in Z.IO.UV.UVStream

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

defaultTCPServerConfig :: TCPServerConfig Source #

A default hello world server on localhost: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 server

Fork new worker thread 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

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

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