Skip to content

Commit

Permalink
getSocketType
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Nov 21, 2024
1 parent cca899e commit 24cf9d2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions FlyingSocks/Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public struct Socket: Sendable, Hashable {
}
}

public var socketType: SocketType {
get throws {
try SocketType(rawValue: getValue(for: .socketType))
}
}

public var flags: Flags {
get throws {
let flags = Socket.fcntl(file.rawValue, F_GETFL)
Expand Down Expand Up @@ -567,6 +573,11 @@ public extension SocketOption where Self == BoolSocketOption {
}

public extension SocketOption where Self == Int32SocketOption {

static var socketType: Self {
Int32SocketOption(name: SO_TYPE)
}

static var sendBufferSize: Self {
Int32SocketOption(name: SO_SNDBUF)
}
Expand Down
35 changes: 35 additions & 0 deletions FlyingSocks/Tests/SocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,41 @@ struct SocketTests {
#expect(SocketType.datagram.rawValue == Socket.datagram)
}

@Test
func getSocketType_stream() throws {
#expect(
try Socket(domain: AF_INET).socketType == .stream
)
#expect(
try Socket(domain: AF_INET6).socketType == .stream
)
#expect(
try Socket(domain: AF_UNIX).socketType == .stream
)
#expect(
try Socket(domain: AF_INET, type: .stream).socketType == .stream
)
#expect(
try Socket(domain: AF_INET, type: .stream).socketType == .stream
)
#expect(
try Socket(domain: AF_INET, type: .stream).socketType == .stream
)
}

@Test
func getSocketType_datagram() throws {
#expect(
try Socket(domain: AF_INET, type: .datagram).socketType == .datagram
)
#expect(
try Socket(domain: AF_INET, type: .datagram).socketType == .datagram
)
#expect(
try Socket(domain: AF_INET, type: .datagram).socketType == .datagram
)
}

@Test
func socketEvents() {
let events: Set<Socket.Event> = [.read, .write]
Expand Down

0 comments on commit 24cf9d2

Please sign in to comment.