Skip to content

Commit

Permalink
SocketType
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Nov 21, 2024
1 parent 150911e commit 46f8c50
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
25 changes: 18 additions & 7 deletions FlyingSocks/Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public enum SocketType: Sendable {
case datagram
}

extension SocketType {
package extension SocketType {
var rawValue: Int32 {
switch self {
case .stream:
Expand All @@ -50,6 +50,17 @@ extension SocketType {
Socket.datagram
}
}

init(rawValue: Int32) throws {
switch rawValue {
case Socket.stream:
self = .stream
case Socket.datagram:
self = .datagram
default:
throw SocketError.makeFailed("Invalid SocketType")
}
}
}

public struct Socket: Sendable, Hashable {
Expand All @@ -74,20 +85,20 @@ public struct Socket: Sendable, Hashable {

@available(*, deprecated, message: "type is now SocketType")
public init(domain: Int32, type: Int32) throws {
let descriptor = FileDescriptor(rawValue: Socket.socket(domain, type, 0))
try self.init(domain: domain, type: SocketType(rawValue: type))
}

public init(domain: Int32, type: SocketType) throws {
let descriptor = FileDescriptor(rawValue: Socket.socket(domain, type.rawValue, 0))
guard descriptor != .invalid else {
throw SocketError.makeFailed("CreateSocket")
}
self.file = descriptor
if type == SocketType.datagram.rawValue {
if type == .datagram {
try setPktInfo(domain: domain)
}
}

public init(domain: Int32, type: SocketType) throws {
try self.init(domain: domain, type: type.rawValue)
}

public var flags: Flags {
get throws {
let flags = Socket.fcntl(file.rawValue, F_GETFL)
Expand Down
15 changes: 15 additions & 0 deletions FlyingSocks/Tests/SocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ import Testing

struct SocketTests {

@Test
func socketType_init() throws {
#expect(try SocketType(rawValue: Socket.stream) == .stream)
#expect(try SocketType(rawValue: Socket.datagram) == .datagram)
#expect(throws: (any Error).self) {
try SocketType(rawValue: -1)
}
}

@Test
func socketType_rawValue() {
#expect(SocketType.stream.rawValue == Socket.stream)
#expect(SocketType.datagram.rawValue == Socket.datagram)
}

@Test
func socketEvents() {
let events: Set<Socket.Event> = [.read, .write]
Expand Down
11 changes: 11 additions & 0 deletions FlyingSocks/XCTests/SocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ import XCTest

final class SocketTests: XCTestCase {

func testSocketType_init() {
XCTAssertEqual(try SocketType(rawValue: Socket.stream), .stream)
XCTAssertEqual(try SocketType(rawValue: Socket.datagram), .datagram)
XCTAssertThrowsError(try SocketType(rawValue: -1))
}

func funcSocketType_rawValue() {
XCTAssertEqual(SocketType.stream.rawValue, Socket.stream)
XCTAssertEqual(SocketType.datagram.rawValue, Socket.datagram)
}

func testSocketEvents() {
let events: Set<Socket.Event> = [.read, .write]

Expand Down

0 comments on commit 46f8c50

Please sign in to comment.