New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ZStream.fromTcpSocketServer #3649
Comments
@iravid can you please explain what is the logic to emit socket connections |
Every time a connection is made to the listening port, it is accepted and represented using |
oki thanks i will try to implement |
Something like this @iravid ? I was wondering if i put this function into import blocking._
import java.net.ServerSocket
def fromTcpSocketServer[R](port: Int, host: Option[String] = None): ZStream[Blocking, Throwable, Managed[Throwable, Connection]] = {
val server = Task{
val serverSocket = new ServerSocket()
serverSocket.bind(
host.fold(new InetSocketAddress(port))(new InetSocketAddress(_, port))
)
serverSocket
}
for{
srv <- ZStream.fromEffect(server)
socket <- ZStream.unfoldM(srv)(s => effectBlockingIO(s.accept()).map(conn => Option(conn -> s)))
conn = new Connection(socket)
} yield ZManaged.make(ZIO.succeed(conn))(c => ZIO(c.close()).ignore)
}
private[zio] class Connection(socket: Socket) {
def read: ZStream[Blocking, IOException, Byte] = fromInputStreamEffect(ZIO(socket.getInputStream).refineToOrDie[IOException])
def write: ZSink[Any, Throwable, Byte, Unit] = ???
private[zio] def close(): Unit = socket.close()
} |
@regis-leray Yes, that's a good start, but we have to use NIO :-) |
* #3649 - Add ZStream.fromTcpSocketServer * add unit test
A stream that binds a listening TCP socket and emits a stream of connections wrapped in ZManaged. Something like:
This ticket also involves defining the
Connection
representation. At a minimum, it should sport the following methods:The text was updated successfully, but these errors were encountered: