Skip to content

Commit

Permalink
Added UDP
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr. Brandon Wiley committed Sep 14, 2020
1 parent df8607a commit f816a0e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
20 changes: 16 additions & 4 deletions Sources/Transmission/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ public class Connection
var readLock = DispatchGroup()
var writeLock = DispatchGroup()

public convenience init?(host: String, port: Int)
public convenience init?(host: String, port: Int, type: ConnectionType = .tcp)
{
let nwhost = NWEndpoint.Host(host)

let port16 = UInt16(port)
let nwport = NWEndpoint.Port(integerLiteral: port16)

let nwconnection = NWConnection(host: nwhost, port: nwport, using: .tcp)

self.init(connection: nwconnection)
switch type
{
case .tcp:
let nwconnection = NWConnection(host: nwhost, port: nwport, using: .tcp)
self.init(connection: nwconnection)
case .udp:
let nwconnection = NWConnection(host: nwhost, port: nwport, using: .udp)
self.init(connection: nwconnection)
}
}

init?(connection: NWConnection)
Expand Down Expand Up @@ -116,3 +122,9 @@ public class Connection
return success
}
}

public enum ConnectionType
{
case udp
case tcp
}
13 changes: 11 additions & 2 deletions Sources/Transmission/Listener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ public class Listener
let queue: BlockingQueue<Connection> = BlockingQueue<Connection>()
let lock: DispatchGroup = DispatchGroup()

public init?(port: Int)
public init?(port: Int, type: ConnectionType = .tcp)
{
let port16 = UInt16(port)
let nwport = NWEndpoint.Port(integerLiteral: port16)

var params: NWParameters!
switch type
{
case .tcp:
params = NWParameters.tcp
case .udp:
params = NWParameters.udp
}

guard let listener = try? NWListener(using: .tcp, on: nwport) else {return nil}
guard let listener = try? NWListener(using: params, on: nwport) else {return nil}
self.listener = listener

self.listener.newConnectionHandler =
Expand Down
6 changes: 6 additions & 0 deletions Sources/TransmissionLinux/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ public class Connection
return true
}
}

public enum ConnectionType
{
case udp
case tcp
}

0 comments on commit f816a0e

Please sign in to comment.