Skip to content

Commit

Permalink
Merge pull request #2624 from mpilquist/topic/fix-unix-sockets-detection
Browse files Browse the repository at this point in the history
Fix UnixSockets classpath detection logic
  • Loading branch information
mpilquist committed Sep 20, 2021
2 parents 8ba9dc5 + cd24024 commit ac66d9b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
31 changes: 17 additions & 14 deletions io/jvm/src/main/scala/fs2/io/net/unixsocket/JdkUnixSockets.scala
Expand Up @@ -29,19 +29,22 @@ object JdkUnixSockets {

def supported: Boolean = StandardProtocolFamily.values.size > 2

implicit def forAsync[F[_]](implicit F: Async[F]): UnixSockets[F] =
new UnixSockets.AsyncUnixSockets[F] {
protected def openChannel(address: UnixSocketAddress) = F.delay {
val ch = SocketChannel.open(StandardProtocolFamily.UNIX)
ch.connect(UnixDomainSocketAddress.of(address.path))
ch
}
implicit def forAsync[F[_]: Async]: UnixSockets[F] =
new JdkUnixSocketsImpl[F]
}

private[unixsocket] class JdkUnixSocketsImpl[F[_]](implicit F: Async[F])
extends UnixSockets.AsyncUnixSockets[F] {
protected def openChannel(address: UnixSocketAddress) = F.delay {
val ch = SocketChannel.open(StandardProtocolFamily.UNIX)
ch.connect(UnixDomainSocketAddress.of(address.path))
ch
}

protected def openServerChannel(address: UnixSocketAddress) = F.blocking {
val serverChannel = ServerSocketChannel.open(StandardProtocolFamily.UNIX)
serverChannel.configureBlocking(false)
serverChannel.bind(UnixDomainSocketAddress.of(address.path))
(F.blocking(serverChannel.accept()), F.blocking(serverChannel.close()))
}
}
protected def openServerChannel(address: UnixSocketAddress) = F.blocking {
val serverChannel = ServerSocketChannel.open(StandardProtocolFamily.UNIX)
serverChannel.configureBlocking(false)
serverChannel.bind(UnixDomainSocketAddress.of(address.path))
(F.blocking(serverChannel.accept()), F.blocking(serverChannel.close()))
}
}
27 changes: 15 additions & 12 deletions io/jvm/src/main/scala/fs2/io/net/unixsocket/JnrUnixSockets.scala
Expand Up @@ -38,17 +38,20 @@ object JnrUnixSockets {
case _: ClassNotFoundException => false
}

implicit def forAsync[F[_]](implicit F: Async[F]): UnixSockets[F] =
new UnixSockets.AsyncUnixSockets[F] {
protected def openChannel(address: UnixSocketAddress) =
F.delay(UnixSocketChannel.open(new JnrUnixSocketAddress(address.path)))
implicit def forAsync[F[_]: Async]: UnixSockets[F] =
new JnrUnixSocketsImpl[F]
}

protected def openServerChannel(address: UnixSocketAddress) = F.blocking {
val serverChannel = UnixServerSocketChannel.open()
serverChannel.configureBlocking(false)
val sock = serverChannel.socket()
sock.bind(new JnrUnixSocketAddress(address.path))
(F.blocking(serverChannel.accept()), F.blocking(serverChannel.close()))
}
}
private[unixsocket] class JnrUnixSocketsImpl[F[_]](implicit F: Async[F])
extends UnixSockets.AsyncUnixSockets[F] {
protected def openChannel(address: UnixSocketAddress) =
F.delay(UnixSocketChannel.open(new JnrUnixSocketAddress(address.path)))

protected def openServerChannel(address: UnixSocketAddress) = F.blocking {
val serverChannel = UnixServerSocketChannel.open()
serverChannel.configureBlocking(false)
val sock = serverChannel.socket()
sock.bind(new JnrUnixSocketAddress(address.path))
(F.blocking(serverChannel.accept()), F.blocking(serverChannel.close()))
}
}

0 comments on commit ac66d9b

Please sign in to comment.