Skip to content

Commit

Permalink
Merge pull request #24 from Vinz1911/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Vinz1911 authored Apr 13, 2023
2 parents bb68236 + 517c110 commit b1caae8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# FusionKit

`FusionKit` is a library which implements the `FNConnection-Protocol`. The `FNConnection-Protocol` is proprietary networking protocol which uses a small and lightweight header with a performance as fast as raw tcp performance. Built directly on top of Apples `Network.framework` with support for plain tcp and tls encrypted connections. The implementation for the host is [Network](https://github.com/Vinz1911/network) written in golang with awesome concurrency support to ensure maximum performance.
`FusionKit` is a library which implements the `Fusion Framing Protocol (FFP)`. The `Fusion Framing Protocol (FFP)` is proprietary networking protocol which uses a small and lightweight header with a performance as fast as raw tcp performance. Built directly on top of Apples `Network.framework` with support for plain tcp and tls encrypted connections. The implementation for the host is [Fusion](https://github.com/Vinz1911/fusion) written in golang with awesome concurrency support to ensure maximum performance.

## License:
[![License](https://img.shields.io/badge/license-GPLv3-blue.svg?longCache=true&style=flat)](https://github.com/Vinz1911/FusionKit/blob/main/LICENSE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// FNConnectionResult.swift
// FNConnectionState.swift
// FusionKit
//
// Created by Vinzenz Weist on 09.06.21.
Expand All @@ -15,7 +15,7 @@ public struct FNConnectionBytes {
}

/// network connection result type
public enum FNConnectionResult {
public enum FNConnectionState {
case ready
case cancelled
case failed(Error?)
Expand Down
2 changes: 1 addition & 1 deletion Sources/FusionKit/Network/FNConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import Network

public final class FNConnection: FNConnectionProtocol {
public var stateUpdateHandler: (FNConnectionResult) -> Void = { _ in }
public var stateUpdateHandler: (FNConnectionState) -> Void = { _ in }
private var frame = FNConnectionFrame()
private let queue: DispatchQueue
private var connection: NWConnection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// NetworkFrameProtocol.swift
// FNConnectionFrameProtocol.swift
// FusionKit
//
// Created by Vinzenz Weist on 07.06.21.
Expand Down
2 changes: 1 addition & 1 deletion Sources/FusionKit/Protocols/FNConnectionProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Network

public protocol FNConnectionProtocol {
/// result type
var stateUpdateHandler: (FNConnectionResult) -> Void { get set }
var stateUpdateHandler: (FNConnectionState) -> Void { get set }

/// create a new connection with 'FusionKit'
/// - Parameters:
Expand Down
56 changes: 46 additions & 10 deletions Tests/FusionKitTests/FusionKitTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//
// FusionKitTests.swift.swift
// FusionKit
//
// Created by Vinzenz Weist on 07.06.21.
// Copyright © 2021 Vinzenz Weist. All rights reserved.
//

import XCTest
@testable import FusionKit

Expand All @@ -9,7 +17,7 @@ class FusionKitTests: XCTestCase {
private var connection = FNConnection(host: "127.0.0.1", port: 7878)
private var buffer = "50000"
private let timeout = 10.0
private var cases: TestCase? = nil
private let uuid = UUID().uuidString
private var exp: XCTestExpectation?

/// set up
Expand All @@ -20,40 +28,68 @@ class FusionKitTests: XCTestCase {

/// start test sending single text message
func testTextMessage() {
cases = .string; start()
start(test: .string)
}

/// start test sending single binary message
func testBinaryMessage() {
cases = .data; start()
start(test: .data)
}

/// start test sending single ping message
func testPingMessage() {
cases = .ping; start()
start(test: .ping)
}

/// start test creating and parsing string based message
func testParsingStringMessage() {
let message = uuid
framer(message: message)
}

/// start test creating and parsing data based message
func testParsingDataMessage() {
guard let message = uuid.data(using: .utf8) else { return }
framer(message: message)
}
}

// MARK: - Private API Extension -

private extension FusionKitTests {
/// create a connection and start
private func start() {
stateUpdateHandler(connection: connection)
/// - Parameter test: test case
private func start(test: TestCase) {
stateUpdateHandler(connection: connection, test: test)
connection.start()
wait(for: [exp!], timeout: timeout)
}

/// message framer
private func framer<T: FNConnectionMessage>(message: T) {
let framer = FNConnectionFrame()
let message = framer.create(message: message)
if let error = message.error { XCTFail("failed with error: \(error)") }
guard let data = message.data else { XCTFail("failed to get message data"); return }

framer.parse(data: data) { message, error in
if case let message as String = message { XCTAssertEqual(message, uuid); self.exp?.fulfill() }
if case let message as Data = message { XCTAssertEqual(message, uuid.data(using: .utf8)); self.exp?.fulfill() }
if let error = error { XCTFail("failed with error: \(error)") }
}
wait(for: [exp!], timeout: timeout)
}

/// state update handler for connection
/// - Parameter connection: instance of 'NetworkConnection'
private func stateUpdateHandler(connection: FNConnection) {
private func stateUpdateHandler(connection: FNConnection, test: TestCase) {
connection.stateUpdateHandler = { [weak self] state in
guard let self = self else { return }
switch state {
case .ready:
if self.cases == .string { connection.send(message: self.buffer) }
if self.cases == .data { connection.send(message: Data(count: Int(self.buffer)!)) }
if self.cases == .ping { connection.send(message: UInt16(self.buffer)!) }
if test == .string { connection.send(message: self.buffer) }
if test == .data { connection.send(message: Data(count: Int(self.buffer)!)) }
if test == .ping { connection.send(message: UInt16(self.buffer)!) }

case .message(let message):
if case let message as UInt16 = message {
Expand Down

0 comments on commit b1caae8

Please sign in to comment.