Last active
January 17, 2025 03:33
-
-
Save toby5box/f4901d1b50532358caf7580d0a962908 to your computer and use it in GitHub Desktop.
Revisions
-
toby5box revised this gist
Jan 17, 2025 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,15 +38,15 @@ object MainMinimal extends IOApp { case (host, port) => Stream.exec(IO.println(s"Listening on $host, $port")) ++ Network[IO].server(address = host, port = port) .map { skt => Stream.exec(IO.println(s"+++ Connection on $host, $port")) ++ skt.reads .through(lines) .evalTap(s => IO.println(s"xxxxxx")) .evalTap(s => IO.println(s"$host # $port : $s")) .handleErrorWith(t => Stream.exec(IO.println(s"Read Error: $t"))) ++ Stream.exec(IO.println(s"--- Disconnection from $host, $port")) }.parJoinUnbounded } .parJoinUnbounded } -
toby5box created this gist
Jan 17, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ import fs2.io.net import fs2.* import cats.effect.* import cats.implicits.* import cats.* import com.comcast.ip4s.* import fs2.io.net.Network import java.nio.charset.StandardCharsets object MainMinimal extends IOApp { final val CR: Byte = 0x0d.toByte private def lines(in: Stream[IO,Byte]): Stream[IO, String] = { def read(s: Stream[IO,Byte], acc: List[Byte]): Pull[IO,String,Unit] = { s.pull.uncons1.flatMap( _.fold( Pull.done ) { case (`CR`,rest) => // emit collected bytes as a String Pull.output1(new String(acc.reverse.toArray, StandardCharsets.US_ASCII)) >> read(rest, Nil) case (b,rest) => read(rest, b :: acc) } ) } read(in, Nil).stream } private def listen: Stream[IO,String] = { val host1 = Host.fromString("127.0.0.1") Stream.exec(IO.println(s"Starting server")) ++ Stream.emits(List((host1,Port.fromInt(8193)), (host1,Port.fromInt(8194)))) .map { case (host, port) => Stream.exec(IO.println(s"Listening on $host, $port")) ++ Network[IO].server(address = host, port = port) .flatMap { skt => Stream.exec(IO.println(s"+++ Connection on $host, $port")) ++ skt.reads .through(lines) .evalTap(s => IO.println(s"xxxxxx")) .evalTap(s => IO.println(s"$host # $port : $s")) .handleErrorWith(t => Stream.exec(IO.println(s"Read Error: $t"))) ++ Stream.exec(IO.println(s"--- Disconnection from $host, $port")) } } .parJoinUnbounded } override def run(args: List[String]): IO[ExitCode] = listen.compile.drain.as(ExitCode.Success) }