Skip to content
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
21 changes: 15 additions & 6 deletions FlyingSocks/Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,12 @@ public struct Socket: Sendable, Hashable {
private func read(into buffer: UnsafeMutablePointer<UInt8>, length: Int) throws -> Int {
let count = Socket.read(file.rawValue, buffer, length)
guard count > 0 else {
if errno == EWOULDBLOCK {
// count == 0 is EOF; errno is only valid after a -1 return.
if count == 0 {
throw SocketError.disconnected
} else if errno == EWOULDBLOCK {
throw SocketError.blocked
} else if errnoSignalsDisconnected() || count == 0 {
} else if errnoSignalsDisconnected() {
throw SocketError.disconnected
} else {
throw SocketError.makeFailed("Read")
Expand Down Expand Up @@ -292,9 +295,12 @@ public struct Socket: Sendable, Hashable {
Socket.recvfrom(file.rawValue, buffer, length, 0, $0, &size)
}
guard count > 0 else {
if errno == EWOULDBLOCK {
// count == 0 is EOF; errno is only valid after a -1 return.
if count == 0 {
throw SocketError.disconnected
} else if errno == EWOULDBLOCK {
throw SocketError.blocked
} else if errnoSignalsDisconnected() || count == 0 {
} else if errnoSignalsDisconnected() {
throw SocketError.disconnected
} else {
throw SocketError.makeFailed("RecvFrom")
Expand Down Expand Up @@ -357,9 +363,12 @@ public struct Socket: Sendable, Hashable {
}

guard count > 0 else {
if errno == EWOULDBLOCK || errno == EAGAIN {
// count == 0 is EOF; errno is only valid after a -1 return.
if count == 0 {
throw SocketError.disconnected
} else if errno == EWOULDBLOCK || errno == EAGAIN {
throw SocketError.blocked
} else if errnoSignalsDisconnected() || count == 0 {
} else if errnoSignalsDisconnected() {
throw SocketError.disconnected
} else {
throw SocketError.makeFailed("RecvMsg")
Expand Down
12 changes: 12 additions & 0 deletions FlyingSocks/Sources/SocketPool+ePoll.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@

#if canImport(CSystemLinux)
import CSystemLinux
#if canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Android)
import Android
#endif

public extension AsyncSocketPool where Self == SocketPool<ePoll> {
static func ePoll(triggering: ePoll.TriggerMode = .edge, maxEvents limit: Int = 20, logger: some Logging = .disabled) -> SocketPool<ePoll> {
Expand Down Expand Up @@ -147,6 +154,11 @@ public struct ePoll: EventQueue {
var events = Array(repeating: epoll_event(), count: eventsLimit)
let status = CSystemLinux.epoll_wait(file.rawValue, &events, Int32(eventsLimit), -1)
guard status > 0 else {
// EINTR (signal) is not a failure: report no events so the caller
// polls again rather than tearing down the server.
if status == -1 && errno == EINTR {
return []
}
throw SocketError.makeFailed("epoll wait")
}

Expand Down
Loading