Repository: apple/swift-nio
File: Sources/NIOPosix/SocketChannel.swift (Swift)

Code before:
            switch self.localAddress?.protocol {
            case .some(.inet):
                return try (self.socket.getOption(level: .ip,
                                                  name: .ip_recv_tos) != 0) as! Option.Value
            case .some(.inet6):
                return try (self.socket.getOption(level: .ipv6,
                                                  name: .ipv6_recv_tclass) != 0) as! Option.Value
            default:
                // Explicit congestion notification is only supported for IP
                throw ChannelError.operationUnsupported
            }
        case _ as ChannelOptions.Types.ReceivePacketInfo:
            switch self.localAddress?.protocol {
            case .some(.inet):
                return try (self.socket.getOption(level: .ip,
                                                  name: .ip_recv_pktinfo) != 0) as! Option.Value
            case .some(.inet6):
                return try (self.socket.getOption(level: .ipv6,
                                                  name: .ipv6_recv_pktinfo) != 0) as! Option.Value
            default:
                // Receiving packet info is only supported for IP
                throw ChannelError.operationUnsupported
            }
        case _ as ChannelOptions.Types.DatagramGenericSegmentationOffload:
            if System.supportsUDPSegmentationOffload {
                #if os(Linux)
                return try self.socket.getOption(level: .sol_udp, name: .udp_gso)
                #else
                // Shouldn't be reachable: supportsUDPSegmentationOffload returns false for all
                // non-Linux platforms.
                preconditionFailure("Setting UDP_SEGMENT is not supported on this platform")
                #endif
            } else {
                throw ChannelError.operationUnsupported
            }

        default:
            return try super.getOption0(option)
        }
    }

    func registrationFor(interested: SelectorEventSet, registrationID: SelectorRegistrationID) -> NIORegistration {
        return NIORegistration(channel: .datagramChannel(self),
                               interested: interested,
                               registrationID: registrationID)
    }

    override func connectSocket(to address: SocketAddress) throws -> Bool {
        // TODO: this could be a channel option to do other things instead here, e.g. fail the connect
        if !self.pendingWrites.isEmpty {
            self.pendingWrites.failAll(

Review comment (suggestion):
Instead of putting the compile time checks in here, can we put them into functions in `BSDSocketAPI` with different implementations in the various placse? I'd like us to avoid too much multiplication of `#if os` throughout this file, so if we can split these branches into functions that exist on all platforms but that are only implemented non-trivially on the platforms we care about, that would be very helpful.

Code after:
                                                  name: .ipv6_recv_tclass) != 0) as! Option.Value
            default:
                // Explicit congestion notification is only supported for IP
                throw ChannelError.operationUnsupported
            }
        case _ as ChannelOptions.Types.ReceivePacketInfo:
            switch self.localAddress?.protocol {
            case .some(.inet):
                return try (self.socket.getOption(level: .ip,
                                                  name: .ip_recv_pktinfo) != 0) as! Option.Value
            case .some(.inet6):
                return try (self.socket.getOption(level: .ipv6,
                                                  name: .ipv6_recv_pktinfo) != 0) as! Option.Value
            default:
                // Receiving packet info is only supported for IP
                throw ChannelError.operationUnsupported
            }
        case _ as ChannelOptions.Types.DatagramSegmentSize:
            guard System.supportsUDPSegmentationOffload else {
                throw ChannelError.operationUnsupported
            }
            return try self.socket.getUDPSegmentSize() as! Option.Value
        default:
            return try super.getOption0(option)
        }
    }

    func registrationFor(interested: SelectorEventSet, registrationID: SelectorRegistrationID) -> NIORegistration {
        return NIORegistration(channel: .datagramChannel(self),
                               interested: interested,
                               registrationID: registrationID)
    }

    override func connectSocket(to address: SocketAddress) throws -> Bool {
        // TODO: this could be a channel option to do other things instead here, e.g. fail the connect
        if !self.pendingWrites.isEmpty {
            self.pendingWrites.failAll(
                error: IOError(
                    errnoCode: EISCONN,
                    reason: "Socket was connected before flushing pending write."),
                close: false)
        }
        if try self.socket.connect(to: address) {
            self.pendingWrites.markConnected(to: address)
            return true
        } else {
            preconditionFailure("Connect of datagram socket did not complete synchronously.")
        }
    }

    override func finishConnectSocket() throws {