Skip to content

DispatchSource based socket framework written in pure Swift

License

Notifications You must be signed in to change notification settings

csujedihy/SwiftDSSocket

Repository files navigation

Travis-CI macOS iOS Swift Version CocoaPods Carthage Compatible SwiftPM Compatible

SwiftDSSocket

Overview

SwiftDSSocket is a purely swift based asynchronous socket framework built atop DispatchSource. Function signatures are pretty much similar to those in CocoaAsyncSocket because I implemented this framework by learning the source code of CocoaAsyncSocket. The initial idea to build this framework is driven by the need of network library to communicate with KEXT (NKE) to re-write my Proximac project but none of frameworks I found in github supports that. Thus, I decided to implemented my own framework to do so.

Note: This framework is still under active development. It only passes my unit tests and might have various bugs.

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

Swift Package Manager

To include SwiftDSSocket into a Swift Package Manager package, add it to the dependencies attribute defined in your Package.swift file. For example:

    dependencies: [
        .Package(url: "https://github.com/csujedihy/SwiftDSSocket", "x.x.x")
    ]

CocoaPods

To include SwiftDSSocket in a project using CocoaPods, you just add SwiftDSSocket to your Podfile, for example:

    target 'MyApp' do
        use_frameworks!
        pod 'SwiftDSSocket'
    end

Carthage

To include SwiftDSSocket in a project using Carthage, add a line to your Cartfile with the GitHub organization and project names and version. For example:

    github "csujedihy/SwiftDSSocket"

Documentation

https://csujedihy.github.io/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.

You can simply use telnet 127.0.0.1 9999 to connect to this server and send whatever you want.

import Cocoa
import SwiftDSSocket

class ViewController: NSViewController {
  var server: SwiftDSSocket?
  var newClient: 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) {
    newClient = newSocket
    newSocket.readData(tag: ServerTag)
  }
  
  func socket(sock: SwiftDSSocket, didRead data: Data, tag: Int) {
    sock.write(data: data, tag: ServerTag)
    sock.readData(tag: ServerTag)
  }
}

Tips: Check out Demo folder to see more examples for different environments.

About

DispatchSource based socket framework written in pure Swift

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published