Skip to content
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

Accept server sockets with non-blocking enabled #13

Merged
merged 3 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ final class EpollAsyncServerSocketChannel private (fd: Int)
handler: CompletionHandler[AsynchronousSocketChannel, _ >: A]
): Unit = {
if (readReady) {
val clientFd = syssocket.accept(fd, null, null)
val clientFd = syssocket.accept4(fd, null, null, EpollAsyncSocketChannel.SOCK_NONBLOCK)
if (clientFd == -1) {
if (errno.errno == posix.errno.EAGAIN || errno.errno == posix.errno.EWOULDBLOCK) {
readReady = false
Expand Down Expand Up @@ -183,5 +183,5 @@ object EpollAsyncServerSocketChannel {
@extern
@nowarn
private[net] object syssocket {
def accept(sockfd: CInt, addr: Ptr[Byte], addrlen: Ptr[Byte]): CInt = extern
def accept4(sockfd: CInt, addr: Ptr[Byte], addrlen: Ptr[Byte], flags: CInt): CInt = extern
}
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ final class EpollAsyncSocketChannel private (fd: Int) extends AsynchronousSocket
}

object EpollAsyncSocketChannel {
private final val SOCK_NONBLOCK = 2048
final val SOCK_NONBLOCK = 2048

def open(): EpollAsyncSocketChannel = {
val fd = posix
Expand Down
19 changes: 19 additions & 0 deletions tests/shared/src/test/scala/epollcat/tcp/TcpSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,23 @@ class TcpSuite extends EpollcatSuite {
.intercept[ClosedChannelException]
}

test("server socket read does not block") {
IOServerSocketChannel.open.use { server =>
IOSocketChannel.open.use { clientCh =>
for {
_ <- server.bind(new InetSocketAddress(0))
addr <- server.localAddress
_ <- clientCh.connect(addr)
_ <- clientCh.write(ByteBuffer.wrap("Hello!".getBytes))
_ <- server.accept.use { serverCh =>
for {
bb <- IO(ByteBuffer.allocate(8192))
_ <- serverCh.read(bb)
} yield ()
}
} yield ()
}
}
}

}