V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
jedihy
V2EX  ›  分享创造

写了一个纯 Swift 的异步 TCP 框架并支持与 KEXT 通信

  •  2
     
  •   jedihy · 2017-08-01 17:10:12 +08:00 · 3642 次点击
    这是一个创建于 2431 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本来只想写一个跟 osx 的 KEXT 通信的 class,来重写之前写的 Proximac,读了一下 CocoaAsyncSocket 的源码就把这个框架写完了。

    这个框架是基于 DispatchSource 来监听描述符实现的,跟 CocoaAsyncSocket 的函数签名几乎一模一样,但是没有超时的功能,需要自己写 timer,以后可能会加入。

    框架有几个特点:

    1. IPv6 支持,listen 全部是在 v6 上的,可以同时监听 v4/v6 的连接。
    2. 自带 DNS 解析和 CocoaAsyncSocket 一样,多线程并发,并根据 DNS 结果发起 v4/v6 connect。
    3. 完全基于 delegate,与 CocoaAsyncSocket 也一样。
    4. 支持 Kernel Extension 通信,方便接收内核的通知与内核交互。

    框架目前只做了单元测试,暂无问题,但是作为一个 network 的库,是只能经过长时间的 debug 来完善的。 支持 CocoaPods 和 Carthage 两种方式安装。

    最后,github 地址: https://github.com/csujedihy/SwiftDSSocket

    第 1 条附言  ·  2017-08-01 23:08:09 +08:00

    SwiftDSSocket

    Features

    Full Delegate Support

    • every operation invokes a call to your delagate method.

    IPv6 Support

    • listens only on IPv6 protocol but accepts both IPv4 and IPv6 incoming connections.
    • conforms to Apple's new App Store restriction on IPv6 only environment with NAT64/DNS64.

    DNS Enabled

    • takes advantage of sythesized IPv6 address introduced in iOS 9 and OS X 10.11 for better IPv6 support.
    • uses GCD to do DNS concurrently and connect to the first reachable address.

    KEXT Bi-directional Interaction

    • takes a bundle ID to interact with your KEXT like TCP stream.

    Using SwiftDSSocket

    Including in your project

    CocoaPods

        platform :ios, '10.0'
    
        target 'MyApp' do
            use_frameworks!
            pod 'SwiftDSSocket'
        end
    

    Carthage

        github "csujedihy/SwiftDSSocket"
    

    Documentation

    http://cocoadocs.org/docsets/SwiftDSSocket/

    Example:

    The following example creates a default SwiftDSSocket instance and then immediately starts listening on port 9999 and echoes back everything sent to this server.

    Simply use telnet 127.0.0.1 9999 to test it.

    import Cocoa
    import SwiftDSSocket
    
    class ViewController: NSViewController {
      var client: SwiftDSSocket?
      var server: SwiftDSSocket?
      let ServerTag = 0
      
      override func viewDidLoad() {
        super.viewDidLoad()
        server = SwiftDSSocket(delegate: self, delegateQueue: .main, type: .tcp)
        try? server?.accept(onPort: 9999)
      }
    }
    
    
    extension ViewController: SwiftDSSocketDelegate {
      func socket(sock: SwiftDSSocket, didAcceptNewSocket newSocket: SwiftDSSocket) {
        newSocket.readData(tag: ServerTag)
      }
      
      func socket(sock: SwiftDSSocket, didRead data: Data, tag: Int) {
        sock.write(data: data, tag: ServerTag)
        sock.readData(tag: ServerTag)
      }
    }
    
    
    7 条回复    2017-08-11 18:25:26 +08:00
    kfll
        1
    kfll  
       2017-08-02 09:34:19 +08:00 via Android
    不明觉厉

    赞一个
    jedihy
        2
    jedihy  
    OP
       2017-08-08 00:38:08 +08:00 via iPhone
    @kfll 谢谢!
    lj61785636
        3
    lj61785636  
       2017-08-11 09:16:00 +08:00   ❤️ 1
    好像很厉害的样子,已 star
    xiubin
        4
    xiubin  
       2017-08-11 09:34:30 +08:00
    Proximac 是用来 FQ 的?
    pqee
        5
    pqee  
       2017-08-11 11:13:27 +08:00
    支持。

    需要使用 Darwin 呀,就是说只能在 MAC 上跑。。。
    jedihy
        6
    jedihy  
    OP
       2017-08-11 12:39:28 +08:00
    @xiubin 跟 Proxifier 差不多
    @pqee 暂时只在 osx /ios 上跑,linux 其实是可以兼容的,用 Glibc 的就能跑,但是代码我还得改一下,以后会支持,顺便也想看看 swift4 的 server api 会有什么变化。
    yedashuai
        7
    yedashuai  
       2017-08-11 18:25:26 +08:00   ❤️ 1
    好棒啊,默默的点了一个 star,并关注了楼主
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1029 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 22:29 · PVG 06:29 · LAX 15:29 · JFK 18:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.