DispatchSource based socket framework written in pure Swift

Overview

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.

You might also like...
NWReachability - a pure Swift library for monitoring the network connection of iOS devices using Apple's Network framework.

NWReachability is a pure Swift library for monitoring the network connection of iOS devices using Apple's Network framework.

A native, lightweight and secure time-based (TOTP) & counter-based (HOTP) password client built for iOS
A native, lightweight and secure time-based (TOTP) & counter-based (HOTP) password client built for iOS

A native, lightweight and secure time-based (TOTP) & counter-based (HOTP) password client built for iOS Built by Tijme Gommers โ€“ Buy me a coffee via P

SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)
SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)

SwiftSoup is a pure Swift library, cross-platform (macOS, iOS, tvOS, watchOS and Linux!), for working with real-world HTML. It provides a very conveni

Super lightweight async HTTP server library in pure Swift runs in iOS / MacOS / Linux

Embassy Super lightweight async HTTP server in pure Swift. Please read: Embedded web server for iOS UI testing. See also: Our lightweight web framewor

Super lightweight web framework in Swift based on SWSGI

Ambassador Super lightweight web framework in Swift based on SWSGI Features Super lightweight Easy to use, designed for UI automatic testing API mocki

OAuth2 framework for macOS and iOS, written in Swift.
OAuth2 framework for macOS and iOS, written in Swift.

OAuth2 OAuth2 frameworks for macOS, iOS and tvOS written in Swift 5.0. โคต๏ธ Installation ๐Ÿ›  Usage ๐Ÿ–ฅ Sample macOS app (with data loader examples) ๐Ÿ“– Tec

iOS Network monitor/interceptor framework written in Swift
iOS Network monitor/interceptor framework written in Swift

NetShears NetShears is a Network interceptor framework written in Swift. NetShears adds a Request interceptor mechanisms to be able to modify the HTTP

Lightweight, flexible HTTP server framework written in Swift

Hummingbird Lightweight, flexible server framework written in Swift. Hummingbird consists of three main components, the core HTTP server, a minimal we

A peer to peer framework for OS X, iOS and watchOS 2 that presents a similar interface to the MultipeerConnectivity framework

This repository is a peer to peer framework for OS X, iOS and watchOS 2 that presents a similar interface to the MultipeerConnectivity framework (which is iOS only) that lets you connect any 2 devices from any platform. This framework works with peer to peer networks like bluetooth and ad hoc wifi networks when available it also falls back onto using a wifi router when necessary. It is built on top of CFNetwork and NSNetService. It uses the newest Swift 2's features like error handling and protocol extensions.

Comments
  • SwiftDSSocket deinit crash

    SwiftDSSocket deinit crash

    Sometimes it crashed on deinit.

    Example code:

    self.serverSocket = SwiftDSSocket(delegate: self, delegateQueue: DispatchQueue.global(qos: .default), type: .tcp)
    try self.serverSocket.accept(onPort: 12345)
    
    // ...
    // do something with the server socket
    // ...
    
    self.serverSocket.disconnect()
    self.serverSocket = nil
    

    Sometimes it crashes when call self.serverSocket = nil.

    opened by advoryanskiy 9
Owner
Yi Huang
Yi Huang
Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux.

BlueSocket Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux. Prerequisites Swift Swift Open Source swift-5.1

Kitura 1.3k Dec 26, 2022
A simple GCD based HTTP client and server, written in 'pure' Swift

SwiftyHTTP Note: I'm probably not going to update this any further - If you need a Swift networking toolset for the server side, consider: Macro.swift

Always Right Institute 116 Aug 6, 2022
๐Ÿ”Œ Non-blocking TCP socket layer, with event-driven server and client.

Original authors Honza Dvorsky - http://honzadvorsky.com, @czechboy0 Matthias Kreileder - @matthiaskr1 At the request of the original authors, we ask

Vapor Community 574 Dec 7, 2022
Socket.io iOS and OSX Client compatible with v1.0 and later

SocketIO-Kit โš ๏ธ This project is no longer maintained. Please use the official framework Socket.IO-Client-Swift. SocketIO-Kit is a Socket.io iOS client

Ricardo Pereira 140 Mar 9, 2022
Asynchronous socket networking library for Mac and iOS

CocoaAsyncSocket CocoaAsyncSocket provides easy-to-use and powerful asynchronous socket libraries for macOS, iOS, and tvOS. The classes are described

Robbie Hanson 12.3k Jan 8, 2023
MVVM project to show AQI of cities via web socket

Air Quality Monitoring Application Swift project with JSON parsing using codable About the application The app consists of live realtime data from the

Pran Kishore 0 Nov 27, 2021
This app is using web-socket for realtime updation of aqi for dedicated inidan cities.

CityWiseAQI This app is using web-socket for realtime updation of aqi for dedicated inidan cities. We're following MVVM architecture in this app. This

Vipul Kumar 2 Feb 7, 2022
A resource based, protocol oriented networking library designed for pure-SwiftUI applications.

Monarch ?? - WIP A resource based, protocol oriented networking library designed for pure-SwiftUI applications. Features: Async/Await Resource Based P

Emilio Pelaez Romero 4 Oct 17, 2022
Swift backend / server framework (Pure Swift, Supports Linux)

NetworkObjects NetworkObjects is a #PureSwift backend. This framework compiles for OS X, iOS and Linux and serves as the foundation for building power

Alsey Coleman Miller 258 Oct 6, 2022
The Oakland Post iOS app, written in pure Swift

Now available on the App Store! Check it out! The Oakland Post App The mobile companion for The Oakland Post's website, written entirely in Swift! Scr

Andrew Clissold 280 Dec 13, 2022