Skip to content

Commit e6bc96a

Browse files
committed
Invalid FileDescriptor
1 parent fb53851 commit e6bc96a

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

Sources/Socket/PollingSocketPool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ final actor PollingSocketPool: AsyncSocketPool {
130130
var buffer = sockets.map {
131131
Socket.pollfd(fd: $0.file.rawValue, events: Int16($0.events.pollEvents.rawValue), revents: 0)
132132
}
133-
if Socket.poll(&buffer, nfds_t(buffer.count), interval.milliseconds) > 0 {
133+
if Socket.poll(&buffer, UInt32(buffer.count), interval.milliseconds) > 0 {
134134
for (socket, pollfd) in zip(sockets, buffer) {
135135
processPoll(socket: socket, revents: .makeRevents(pollfd.revents, for: socket.events))
136136
}

Sources/Socket/Socket+Darwin.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#if canImport(Darwin)
3333
import Darwin
3434

35+
extension Socket.FileDescriptor {
36+
static let invalid = Socket.FileDescriptor(rawValue: -1)
37+
}
38+
3539
extension Socket {
3640

3741
typealias FileDescriptorType = Int32
@@ -157,7 +161,7 @@ extension Socket {
157161
return Darwin.unlink(addr)
158162
}
159163

160-
static func poll(_ fds: UnsafeMutablePointer<pollfd>!, _ nfds: nfds_t, _ tmo_p: Int32) -> Int32 {
164+
static func poll(_ fds: UnsafeMutablePointer<pollfd>!, _ nfds: UInt32, _ tmo_p: Int32) -> Int32 {
161165
Darwin.poll(fds, nfds, tmo_p)
162166
}
163167

Sources/Socket/Socket+Glibc.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#if canImport(Glibc)
3333
import Glibc
3434

35+
extension Socket.FileDescriptor {
36+
static let invalid = Socket.FileDescriptor(rawValue: -1)
37+
}
38+
3539
extension Socket {
3640

3741
typealias FileDescriptorType = Int32
@@ -153,8 +157,8 @@ extension Socket {
153157
return Glibc.unlink(addr)
154158
}
155159

156-
static func poll(_ fds: UnsafeMutablePointer<pollfd>!, _ nfds: nfds_t, _ tmo_p: Int32) -> Int32 {
157-
Glibc.poll(fds, nfds, tmo_p)
160+
static func poll(_ fds: UnsafeMutablePointer<pollfd>!, _ nfds: UInt32, _ tmo_p: Int32) -> Int32 {
161+
Glibc.poll(fds, UInt(nfds), tmo_p)
158162
}
159163

160164
static func pollfd(fd: FileDescriptorType, events: Int16, revents: Int16) -> Glibc.pollfd {

Sources/Socket/Socket.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
// SOFTWARE.
3030
//
3131

32+
#if canImport(WinSDK)
33+
import WinSDK.WinSock2
34+
#endif
3235
import Foundation
3336

3437
struct Socket: Sendable, Hashable {
@@ -44,11 +47,11 @@ struct Socket: Sendable, Hashable {
4447
}
4548

4649
init(domain: Int32, type: Int32) throws {
47-
let descriptor = Socket.socket(domain, type, 0)
48-
if descriptor == -1 {
50+
let descriptor = FileDescriptor(rawValue: Socket.socket(domain, type, 0))
51+
guard descriptor != .invalid else {
4952
throw SocketError.makeFailed("CreateSocket")
5053
}
51-
self.file = FileDescriptor(rawValue: descriptor)
54+
self.file = descriptor
5255
}
5356

5457
var flags: Flags {
@@ -153,19 +156,19 @@ struct Socket: Sendable, Hashable {
153156

154157
let newFile = withUnsafeMutablePointer(to: &addr) {
155158
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
156-
Socket.accept(file.rawValue, $0, &len)
159+
FileDescriptor(rawValue: Socket.accept(file.rawValue, $0, &len))
157160
}
158161
}
159162

160-
guard newFile >= 0 else {
163+
guard newFile != .invalid else {
161164
if errno == EWOULDBLOCK {
162165
throw SocketError.blocked
163166
} else {
164167
throw SocketError.makeFailed("Accept")
165168
}
166169
}
167170

168-
return (FileDescriptor(rawValue: newFile), addr)
171+
return (newFile, addr)
169172
}
170173

171174
func connect<A: SocketAddress>(to address: A) throws {

0 commit comments

Comments
 (0)