Skip to content
Merged
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
17 changes: 13 additions & 4 deletions FlyingSocks/Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public struct Socket: Sendable, Hashable {
public func setValue<O: SocketOption>(_ value: O.Value, for option: O) throws {
var value = option.makeSocketValue(from: value)
let result = withUnsafeBytes(of: &value) {
Socket.setsockopt(file.rawValue, SOL_SOCKET, option.name, $0.baseAddress!, socklen_t($0.count))
Socket.setsockopt(file.rawValue, option.level, option.name, $0.baseAddress!, socklen_t($0.count))
}
guard result >= 0 else {
throw SocketError.makeFailed("SetOption")
Expand All @@ -157,7 +157,7 @@ public struct Socket: Sendable, Hashable {
public func getValue<O: SocketOption>(for option: O) throws -> O.Value {
let valuePtr = UnsafeMutablePointer<O.SocketValue>.allocate(capacity: 1)
var length = socklen_t(MemoryLayout<O.SocketValue>.size)
guard Socket.getsockopt(file.rawValue, SOL_SOCKET, option.name, valuePtr, &length) >= 0 else {
guard Socket.getsockopt(file.rawValue, option.level, option.name, valuePtr, &length) >= 0 else {
throw SocketError.makeFailed("GetOption")
}
return option.makeValue(from: valuePtr.pointee)
Expand Down Expand Up @@ -522,15 +522,22 @@ public protocol SocketOption {
associatedtype Value
associatedtype SocketValue

var level: Int32 { get }
var name: Int32 { get }
func makeValue(from socketValue: SocketValue) -> Value
func makeSocketValue(from value: Value) -> SocketValue
}

public extension SocketOption {
var level: Int32 { SOL_SOCKET }
}

public struct BoolSocketOption: SocketOption {
public var level: Int32
public var name: Int32

public init(name: Int32) {
public init(level: Int32 = SOL_SOCKET, name: Int32) {
self.level = level
self.name = name
}

Expand All @@ -544,9 +551,11 @@ public struct BoolSocketOption: SocketOption {
}

public struct Int32SocketOption: SocketOption {
public var level: Int32
public var name: Int32

public init(name: Int32) {
public init(level: Int32 = SOL_SOCKET, name: Int32) {
self.level = level
self.name = name
}

Expand Down