Skip to content

Commit

Permalink
Moved TransmissionTypes to their own package, remove Transport, updat…
Browse files Browse the repository at this point in the history
…ed BlueSocket
  • Loading branch information
Dr. Brandon Wiley committed Dec 7, 2021
1 parent 039ec8b commit 48c8aec
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 223 deletions.
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/OperatorFoundation/TransmissionTypes", from: "0.0.1"),
.package(url: "https://github.com/OperatorFoundation/Chord", from: "0.0.15"),
.package(url: "https://github.com/OperatorFoundation/Datable", from: "3.1.4"),
.package(name: "Socket", url: "https://github.com/OperatorFoundation/BlueSocket", from: "1.1.0"),
.package(name: "Socket", url: "https://github.com/Kitura/BlueSocket", from: "2.0.2"),
.package(url: "https://github.com/OperatorFoundation/Net", from: "0.0.1"),
.package(url: "https://github.com/OperatorFoundation/Transport", from: "2.3.11"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.2"),
.package(url: "https://github.com/OperatorFoundation/SwiftHexTools.git", from: "1.2.5")
],
Expand All @@ -29,7 +29,7 @@ let package = Package(
.target(
name: "TransmissionLinux",
dependencies: [
"Chord", "Socket", "Datable", "Transport", "Net", "SwiftHexTools",
"TransmissionTypes", "Chord", "Socket", "Datable", "Net", "SwiftHexTools",
.product(name: "Logging", package: "swift-log")
]
),
Expand Down
28 changes: 0 additions & 28 deletions Sources/TransmissionLinux/Connection.swift

This file was deleted.

14 changes: 0 additions & 14 deletions Sources/TransmissionLinux/Listener.swift

This file was deleted.

218 changes: 40 additions & 178 deletions Sources/TransmissionLinux/TransmissionConnection.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import Foundation
import Datable
import Transport
import Logging
import SwiftQueue
import SwiftHexTools

import Chord
import Socket
import Net
import TransmissionTypes

public class TransmissionConnection: Connection
{
let id: Int
var connection: NetworkConnection
var connection: Socket
var connectLock = DispatchGroup()
var readLock = DispatchGroup()
var writeLock = DispatchGroup()
Expand All @@ -29,7 +29,7 @@ public class TransmissionConnection: Connection
{
case .tcp:
guard let socket = try? Socket.create() else {return nil}
self.connection = .socket(socket)
self.connection = socket
self.id = Int(socket.socketfd)

do
Expand All @@ -46,69 +46,18 @@ public class TransmissionConnection: Connection
}
}

public required init?(transport: Transport.Connection, logger: Logger? = nil)
{
self.log = logger
maybeLog(message: "Initializing Transmission connection", logger: self.log)

self.id = Int.random(in: 0..<Int.max)

var mutableTransport = transport
self.connection = .transport(mutableTransport)
mutableTransport.stateUpdateHandler = self.handleState
mutableTransport.start(queue: .global())

let success = self.states.dequeue()
guard success else {return nil}
}

public init(socket: Socket)
{
self.connection = .socket(socket)
self.connection = socket
self.id = Int(socket.socketfd)
self.log = nil
}

func handleState(state: NWConnection.State)
{
connectLock.wait()

switch state
{
case .ready:
self.states.enqueue(element: true)
return
case .cancelled:
self.states.enqueue(element: false)
self.failConnect()
return
case .failed(_):
self.states.enqueue(element: false)
self.failConnect()
return
case .waiting(_):
self.states.enqueue(element: false)
self.failConnect()
return
default:
return
}
}

func failConnect()
{
maybeLog(message: "Failed to make a Transmission connection", logger: self.log)

switch self.connection
{
case .socket(let socket):
socket.close()
break
case .transport(var connection):
connection.stateUpdateHandler = nil
connection.cancel()
break
}
self.connection.close()
}

public func read(size: Int) -> Data?
Expand Down Expand Up @@ -184,45 +133,19 @@ public class TransmissionConnection: Connection

var data: Data?

switch self.connection
do
{
case .socket(let socket):
do
{
data = Data(repeating: 0, count: maxSize)
let bytesRead = try socket.read(into: &data!)
if (bytesRead < size)
{
data = Data(data![..<bytesRead])
}
}
catch
{
readLock.leave()
return nil
}

case .transport(let transport):
let transportLock = DispatchGroup()
transportLock.enter()
transport.receive(minimumIncompleteLength: 1, maximumLength: maxSize)
{
maybeData, maybeContext, isComplete, maybeError in

guard let transportData = maybeData else
{
data = nil
return
}

guard maybeError == nil else
{
data = nil
return
}

data = transportData
}
data = Data(repeating: 0, count: maxSize)
let bytesRead = try self.connection.read(into: &data!)
if (bytesRead < size)
{
data = Data(data![..<bytesRead])
}
}
catch
{
readLock.leave()
return nil
}

guard let bytes = data else
Expand Down Expand Up @@ -400,103 +323,42 @@ public class TransmissionConnection: Connection
{
var data: Data?

switch self.connection
do
{
case .socket(let socket):
do
{
data = Data(repeating: 0, count: size)
let bytesRead = try socket.read(into: &data!)
if (bytesRead < size)
{
data = Data(data![..<bytesRead])
}
if let realData = data {
print("networkRead size: \(size), bytesRead: \(bytesRead), data: \(realData.hex)")
} else {
print("networkRead size: \(size), bytesRead: \(bytesRead), data: nil")
}
}
catch
{
data = nil
break
}

case .transport(let transport):
let transportLock = DispatchGroup()
transportLock.enter()
transport.receive(minimumIncompleteLength: size, maximumLength: size)
{
maybeData, maybeContext, isComplete, maybeError in

guard let transportData = maybeData else
{
data = nil
return
}

guard maybeError == nil else
{
data = nil
return
}

data = transportData
}
data = Data(repeating: 0, count: size)
let bytesRead = try self.connection.read(into: &data!)
if (bytesRead < size)
{
data = Data(data![..<bytesRead])
}
if let realData = data {
print("networkRead size: \(size), bytesRead: \(bytesRead), data: \(realData.hex)")
} else {
print("networkRead size: \(size), bytesRead: \(bytesRead), data: nil")
}
}
catch
{
return nil
}

return data
}

func networkWrite(data: Data) -> Bool
{
var success = false
switch self.connection
do
{
case .socket(let socket):
do
{
try socket.write(from: data)
success = true
break
}
catch
{
success = false
break
}
case .transport(let transport):
let lock = DispatchGroup()
lock.enter()
transport.send(content: data, contentContext: .defaultMessage, isComplete: false, completion: NWConnection.SendCompletion.contentProcessed(
{
error in

guard error == nil else
{
success = false
lock.leave()
return
}

success = true
lock.leave()
return
}))
lock.wait()
try self.connection.write(from: data)
return true
}
catch
{
return false
}

return success
}
}

enum NetworkConnection
{
case socket(Socket)
case transport(Transport.Connection)
}

public func maybeLog(message: String, logger: Logger? = nil) {
if logger != nil {
logger!.debug("\(message)")
Expand Down
1 change: 1 addition & 0 deletions Sources/TransmissionLinux/TransmissionListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import Socket
import Chord
import Logging
import TransmissionTypes

public class TransmissionListener: Listener
{
Expand Down

0 comments on commit 48c8aec

Please sign in to comment.