<commit_before>//
//  NetServiceStreams.swift
//  NetService
//
//  Created by <NAME> on 10/12/16.
//  Copyright © 2016 <NAME>.  All rights reserved.
//
import CoreFoundation
import Foundation
import Dispatch

#if os(Linux)
    private let utf8 = CFStringEncoding(kCFStringEncodingUTF8)
    private func CFStreamPropertyKey(rawValue: CFString) -> CFString {
        return rawValue
    }
#else
    private let utf8 = CFStringBuiltInEncodings.UTF8.rawValue
#endif

extension CFSocketNativeHandle {
    func get(_ buffer: UnsafeMutableRawPointer, maxLength len: Int) -> Int {
        return Int(read(Int32(self), buffer, ssize_t(len)))
    }

    func put(_ buffer: UnsafeRawPointer, maxLength len: Int) -> Int {
        return Int(write(Int32(self), buffer, ssize_t(len)))
    }

    func closeReadingEnd() {
        shutdown(Int32(self), Int32(SHUT_RD))
    }

    func closeWritingEnd() {
        shutdown(Int32(self), Int32(SHUT_WR))
    }

    func has(events: Int16 = Int16(POLLIN)) -> Bool {
        var fd = pollfd(fd: Int32(self), events: events, revents: 0)
        return poll(&fd, 1, 0) > 0
    }

    func closeAll() {
        close(Int32(self))
    }
}

public class DNSSDNetServiceInputStream: InputStream {
    var sock: CFSocketNativeHandle
    var dispatchSource: DispatchSourceRead?
    var status = Status.open

    /// Initialise from a `CFSocketNativeHandle`
    ///
    /// - Parameter socket: Already connected socket to initialise from
    public init(_ socket: CFSocketNativeHandle) {
        sock = socket
        super.init(data: Data())
    }

    deinit {
        sock.closeAll()
    }

    public override func read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength len: Int) -> Int {
        return sock.get(buffer, maxLength: len)
    }

    public override func getBuffer(_ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>, length len: UnsafeMutablePointer<Int>) -> Bool {
        return false
    }

    public override var hasBytesAvailable: Bool {
        return sock.has(events: Int16(POLLIN))
    }

    public override func open() {
    }

    public override func close() {
        sock.closeReadingEnd()
        status = Status.closed
    }

    public override var streamStatus: Status {
        return status
    }

    /// Schedule the input stream in the given run loop
    ///
    /// - Parameters:
    ///   - runLoop: needs to be `RunLoop.main`
    ///   - mode: runloop mode to use (ignored)
    public override func schedule(in runLoop: RunLoop, forMode mode: RunLoopMode = .defaultRunLoopMode) {
        guard runLoop == RunLoop.main else {
            print("Alternate run loops are not supported")
            return
        }
        guard dispatchSource == nil else { return }
        let source = DispatchSource.makeReadSource(fileDescriptor: Int32(sock))
        dispatchSource = source
        source.resume()
    }
}


public class DNSSDNetServiceOutputStream: OutputStream {
    var sock: CFSocketNativeHandle
    var status = Status.open
    var properties = Dictionary<PropertyKey, PropertyValue>()

    #if os(Linux)
        public typealias PropertyValue = AnyObject

        public required init(toMemory: ()) {
            sock = -1
            super.init(toMemory: ())
        }
    #else
        public typealias PropertyValue = Any
    #endif

    public init(_ socket: CFSocketNativeHandle) {
        sock = socket<commit_msg>Add event handler<commit_after>//
//  NetServiceStreams.swift
//  NetService
//
//  Created by <NAME> on 10/12/16.
//  Copyright © 2016 <NAME>.  All rights reserved.
//
import CoreFoundation
import Foundation
import Dispatch

#if os(Linux)
    private let utf8 = CFStringEncoding(kCFStringEncodingUTF8)
    private func CFStreamPropertyKey(rawValue: CFString) -> CFString {
        return rawValue
    }
#else
    private let utf8 = CFStringBuiltInEncodings.UTF8.rawValue
#endif

extension CFSocketNativeHandle {
    func get(_ buffer: UnsafeMutableRawPointer, maxLength len: Int) -> Int {
        return Int(read(Int32(self), buffer, ssize_t(len)))
    }

    func put(_ buffer: UnsafeRawPointer, maxLength len: Int) -> Int {
        return Int(write(Int32(self), buffer, ssize_t(len)))
    }

    func closeReadingEnd() {
        shutdown(Int32(self), Int32(SHUT_RD))
    }

    func closeWritingEnd() {
        shutdown(Int32(self), Int32(SHUT_WR))
    }

    func has(events: Int16 = Int16(POLLIN)) -> Bool {
        var fd = pollfd(fd: Int32(self), events: events, revents: 0)
        return poll(&fd, 1, 0) > 0
    }

    func closeAll() {
        close(Int32(self))
    }
}

public class DNSSDNetServiceInputStream: InputStream {
    var sock: CFSocketNativeHandle
    var dispatchSource: DispatchSourceRead?
    var status = Status.open

    /// Initialise from a `CFSocketNativeHandle`
    ///
    /// - Parameter socket: Already connected socket to initialise from
    public init(_ socket: CFSocketNativeHandle) {
        sock = socket
        super.init(data: Data())
    }

    deinit {
        sock.closeAll()
    }

    public override func read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength len: Int) -> Int {
        return sock.get(buffer, maxLength: len)
    }

    public override func getBuffer(_ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>, length len: UnsafeMutablePointer<Int>) -> Bool {
        return false
    }

    public override var hasBytesAvailable: Bool {
        return sock.has(events: Int16(POLLIN))
    }

    public override func open() {
    }

    public override func close() {
        sock.closeReadingEnd()
        status = Status.closed
    }

    public override var streamStatus: Status {
        return status
    }

    /// Schedule the input stream in the given run loop
    ///
    /// - Parameters:
    ///   - runLoop: needs to be `RunLoop.main`
    ///   - mode: runloop mode to use (ignored)
    public override func schedule(in runLoop: RunLoop, forMode mode: RunLoopMode = .defaultRunLoopMode) {
        guard runLoop == RunLoop.main else {
            print("Alternate run loops are not supported")
            return
        }
        guard dispatchSource == nil else { return }
        let source = DispatchSource.makeReadSource(fileDescriptor: Int32(sock))
        dispatchSource = source
        source.setEventHandler { [weak self] in
            guard let source = self else {
                print("nil self, bailing out")
                return
            }
            print("Dispatch read source event triggered")
        }
        source.resume()
    }
}


public class DNSSDNetServiceOutputStream: OutputStream {
    var sock: CFSocketNativeHandle
    var status = Status.open
    var properties = Dictionary<PropertyKey, PropertyValue>()

    #if os(Linux)
        public typealias PropertyValue = AnyObject

        public required init(toMemory: ()) {
            sock = -1
            super.init(toMemory: ())
        }
    #else
        public typealias PropertyValue = Any
    #endif

    public init(_ socket: CFSocketNativeHandle) {
        sock = socket
{'fraction_non_alphanumeric': 0.09709425939050319, 'fraction_numerical': 0.012756909992912827, 'mean_word_length': 2.528, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '101347', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1996, 'n_words': 568}