Skip to content

Latest commit

 

History

History
94 lines (66 loc) · 2.73 KB

README.md

File metadata and controls

94 lines (66 loc) · 2.73 KB

zio-tcp

Summary

The zio-tcp library implements for both client and server:

Supporting functionality:

  • Host name resolution

Related projects

Comparisons

zio-nio has the explicit goal of providing acccess to Java NIO functionality via a ZIO API.

ZIO has an implementation for a TCP server, that is currently incomplete and has life cycle management that is not suitable for a long lived server instance. There is no client implementation.

Documentation

Examples

Echo server

import io.github.searler.zio_tcp.TCP
import zio._

object ServerExample extends App {
  override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = {
    val program = for {
      server <- TCP
        .fromSocketServer(8888) // standard server listening on 8888 and wildcard bound
        .mapMParUnordered(4)( //Execute up to 4 requests concurrently
          TCP.handlerServer( // standard MEP
            _ =>   // ignore the identity of the connecting client
              Predef.identity)) // echo the request
        .runDrain
    } yield ()

    program.exitCode
  }
}

Client that makes a fixed request

import io.github.searler.zio_tcp.TCP
import zio._

object ClientExample extends App {
  override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = {

    val program = for {
      conn <- TCP.fromSocketClient(8888, "localhost"). // connect to server
        retry(Schedule.forever) // retry connection until successful
      receive <- TCP.requestChunk( // Send Chunk[Byte] and receive a Chunk[Byte]
        Chunk.fromArray("Request".getBytes()) // create request Chunk
      )(conn) // request via connection
      _ <- console.putStrLn(new String(receive.toArray))
    } yield ()

    program.exitCode
  }
}

See the test cases for further examples

Releases

still to be implemented

Planned enhancements

  • TLS

Background

This repository captures functionality from ZIO pull requests that are not (yet) accepted:

zio/zio#5176 zio/zio#5186

The code was identical, modulo module name so there is no naming conflict. The implementation has since been improved and additional functionality has been provided.