Swiftpack.co -  Package - Vinz1911/FastSocket
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Vinz1911/FastSocket
Fast Socket is a Proprietary TCP Bandwidth Measurement Protocol
.package(url: "https://github.com/Vinz1911/FastSocket.git", from: "3.0.1")


FastSocket
FastSocket

FastSocket is a proprietary bi-directional message based communication protocol on top of TCP. The idea behind this project was, to create a TCP communication like the WebSocket Protocol with less overhead and the ability to track bytes that are read or written on the socket very granularly, without waiting for the whole message to be transmitted. This allows it to use it as performance protocol for measuring the TCP throughput performance. Our server-sided implementation is written in golang and it's optimized for maximum speed and performance.

The server sided implementation of the FastSocket Protocol can be found here: FastSocketServer. The repository also contains a demo implementation of the server code with a simple speedtest backend.

Features:

  • send and receive text and data messages
  • bi-directional network transfer
  • async, non-blocking & very fast
  • track input + output bytes
  • TCP + TLS support
  • Swift Packages + XCFramework

License:

License

Swift Version:

Swift 5.1 Swift 5.1

Installation:

XCFramework

Download the latest Framework Version from the Release section. Copy the FastSocket.xcframework to your Frameworks and Libraries section.

Swift Packages

Full support for SwiftPackageManager. Just add the repo to your project in the project settings under Swift Packages.

Import:

// import the Framework
import FastSocketKit

// normal init with TCP (unsecure) transfer type
let socket = FastSocket(host: "example.com", port: 8080)

// use TLS (secure) instead of TCP (unsecure)
// NOTE: The backend must be setted up with support for TLS otherwise
// this will not work and end up in an TLS Error
let socket = FastSocket(host: "example.com", port: 8000)
socket.parameters = .tls

// ...

socket.connect()

Closures:

socket.on.ready = {
    // this is called after the connection
    // was successfully established and is ready
}
socket.on.message = { message in
    // this is called everytime
    // a message was received
}
socket.on.bytes = { bytes in
    // this is called everytime bytes are readed 
    // or written from/on the socket
}
socket.on.close = {
    // this is called after
    // the socket was closed
}
socket.on.error = { error in
    // this is called everytime
    // an error appeared
}

Cast Messages:

socket.on.message = { message in
    // it's only possible to cast messages
    // as Data or as String
    if case let message as Data = message {
        // cast message as data
        print("Data count: \(message.count)")
    }
    if case let message as String = message {
        // cast message as string
        print("Message: \(message)")
    }
}

Read Bytes Count:

socket.on.bytes = { bytes in
    // input bytes are the ones, which are
    // readed from the socket, this function
    // returns the byte count
    if case .input(let count) = bytes {
        print("Bytes count: \(count)")
    }
    // output bytes are the ones, which are
    // written on the socket, this function
    // returns the byte count
    if case .output(let count) = bytes {
        print("Bytes count: \(count)")
    }
}

Connect & Disconnect:

// try to connect to the host
// timeout after 3.0 seconds
socket.connect()

// closes the connection
socket.disconnect()

Send Messages:

// the send func is a generic func
// it allows to send `String` and `Data`
// generic T don't accept other data types
socket.send(message: T)

// the send function also has an optional completion block
// this is only possible if its not referenced by the protocol
// if it's referenced by the protocol, you need to implement 
// the completion block, because there are no default values in protocols
socket.send(message: T) {
    // do anything if data is successfully
    // processed by the network stack
}

// NOTE: it's possible to send multiple messages at once
// we discovered a problem if you doing this in a loop,
// this can cause a overflow problem in network.framework's send process
// and the entire process get's stucked. So we implemented some technics to prevent this
// you can now send in a loop but this will end up in lost data because we give the process
// the time he needs to fully process the data into the network stack. If you want to send multiple
// messages at once you should do this inside the completion block. This guarantees the following:
// - 1. The entire data will be transmit, no data will be skipped
// - 2. The process performs with the maximum performance that network.framework can provide
// - 3. The performance scales linear, so it's no problem to send 1M+ messages after each other
// EXAMPLE:

// counter
var count: Int = 0

// send the message
func send() {
    socket.send(message: "Hello World!") {
        guard count <= 1_000_000 else { return }
        count += 1
        // recursive function call of the send method
        send()
    }
}

Additional Parameters:

// FastSocket was build in top of Apple's Network.framework
// that allows us to use lot of TCP features like fast open or
// to select the network interface type

// import the Framework
import FastSocketKit

// init FastSocket object
let socket = FastSocket(host: "example.com", port: 8080)

// ...

// set the traffics service class
socket.parameters.serviceClass = .interactiveVoice

// select the interface type
// if it's not available, it will cancel with an error
socket.parameters.requiredInterfaceType = .cellular

// also the entire parameters object can be overwritten
socket.parameters = NWParamters()

Author:

👨🏼‍💻 Vinzenz Weist

This is my heart project, it's made with a lot of love and dedication ❤️

Supporter:

👨🏽‍💻 Juan Romero

GitHub

link
Stars: 3
Last commit: 5 weeks ago

Release Notes

Framework Release 3.0.1
42 weeks ago

Changelog

This project adheres to Semantic Versioning.

[Released]

[3.0.1] - 2020-06-23

Added

  • minor code improvements
  • updated test backend

Removed

  • unnecessary and old code

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco