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...
Pure makes Pure DI easy in Swift.

Pure Pure makes Pure DI easy in Swift. This repository also introduces a way to do Pure DI in a Swift application. Table of Contents Background Pure D

iOS Slide Menu View based on Google+, iQON, Feedly, Ameba iOS app.  It is written in pure swift.
iOS Slide Menu View based on Google+, iQON, Feedly, Ameba iOS app. It is written in pure swift.

SlideMenuControllerSwift iOS Slide View based on iQON, Feedly, Google+, Ameba iPhone app. Installation CocoaPods pod 'SlideMenuControllerSwift' Carth

A simple GCD based HTTP client and server, written in 'pure' Swift
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

πŸ”Œ  Non-blocking TCP socket layer, with event-driven server and client.
πŸ”Œ 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

πŸ”Œ  Non-blocking TCP socket layer, with event-driven server and client.
πŸ”Œ 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

Socket.io iOS and OSX Client compatible with v1.0 and later
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

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

Socket.IO-client for iOS/OS X.

Socket.IO-Client-Swift Socket.IO-client for iOS/OS X.

πŸ”Œ  Non-blocking TCP socket layer, with event-driven server and client.
πŸ”Œ 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

JustLog brings logging on iOS to the next level. It supports console, file and remote Logstash logging via TCP socket with no effort. Support for logz.io available.
JustLog brings logging on iOS to the next level. It supports console, file and remote Logstash logging via TCP socket with no effort. Support for logz.io available.

JustLog JustLog takes logging on iOS to the next level. It supports console, file and remote Logstash logging via TCP socket with no effort. Support f

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

An iOS App which shows live AQI (Air quality index) data for different cities using Web-socket
An iOS App which shows live AQI (Air quality index) data for different cities using Web-socket

AQI - Assignment This is an iOS App which shows live AQI (Air quality index) dat

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

A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift
A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift

A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift. It features a great API and a battle-tested core. Supporting both MongoDB in server and embedded environments.

A type-safe, protocol-based, pure Swift database offering effortless persistence of any object
A type-safe, protocol-based, pure Swift database offering effortless persistence of any object

There are many libraries out there that aims to help developers easily create and use SQLite databases. Unfortunately developers still have to get bogged down in simple tasks such as writing table definitions and SQL queries. SwiftyDB automatically handles everything you don't want to spend your time doing.

:musical_keyboard: A simple iOS synthesiser powered by Pure Data. Based on the Korg Monotron Delay.

Monotone Delay A simple iOS Synthesizer based on Pure Data. You can see a short demo of the app on youtube. If you want to buy the app you can find it

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

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

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

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-4.0

Kitura 1.3k Jan 3, 2023
πŸ”Œ 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
WebSocket(RFC-6455) library written using Swift

DNWebSocket Object-Oriented, Swift-style WebSocket Library (RFC 6455) for Swift-compatible Platforms. Tests Installation Requirements Usage Tests Conf

Gleb Radchenko 36 Jan 29, 2022
Websockets in swift for iOS and OSX

Starscream is a conforming WebSocket (RFC 6455) library in Swift. Features Conforms to all of the base Autobahn test suite. Nonblocking. Everything ha

Dalton 7.5k Jan 4, 2023
DispatchSource based socket framework written in pure Swift

SwiftDSSocket Overview SwiftDSSocket is a purely swift based asynchronous socket framework built atop DispatchSource. Function signatures are pretty m

Yi Huang 65 Nov 15, 2022
Socket.IO - Socket.IO client for iOS/OS X.

Socket.IO - Socket.IO client for iOS/OS X.

Socket.IO 4.9k Jan 9, 2023
A collection of socket utilities in pure Swift

SwifterSockets A collection of socket utilities in pure Swift SwifterSockets is part of the Swiftfire webserver project. The Swiftfire website The ref

Rien 58 Sep 9, 2022
Swift async/await based socket

Socket Swift async/await based socket library Introduction This library exposes an idiomatic Swift API for interacting with POSIX sockets via an async

null 89 Dec 29, 2022
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-4.0

Kitura 1.3k Jan 3, 2023
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