Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app
- import zio._
- import zio.clock._
- import zio.console._
- import zio.random._
- import zio.nio.channels.{ AsynchronousServerSocketChannel, AsynchronousSocketChannel }
- import zio.nio._
- import zio.duration._
- object Main extends App {
- def run(args: List[String]): ZIO[Environment, Nothing, Int] =
- Server.start(9002)
- .foldM(e => putStrLn("Error: " + e.getMessage) *> ZIO.succeed(1), _ => ZIO.succeed(0))
- }
- object Server {
- def start(port: Int): RIO[Console with Clock with Random, Unit] =
- AsynchronousServerSocketChannel().use(
- server =>
- for {
- socketAddress <- SocketAddress.inetSocketAddress(port)
- _ <- server.bind(socketAddress)
- _ <- putStrLn("Listening on port: " + port)
- _ <- server.accept.reserve.flatMap(accept).forever
- } yield ()
- )
- private def accept(reservation: Reservation[Any, Exception, AsynchronousSocketChannel]) =
- reservation.acquire.flatMap { socket =>
- socket.read(24, 30.seconds)
- .flatMap(chunk => putStrLn("Read chunk: " + chunk.mkString))
- .whenM(socket.isOpen)
- .forever
- .ensuring(reservation.release(Exit.Success(0)) *> putStrLn("Connection closed."))
- .catchAll(e => putStrLn("Connection closed due to: " + e.getMessage))
- .fork
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement