Skip to content

Commit

Permalink
Minor refactor to boolean type support. Code cleanup. Updated documen…
Browse files Browse the repository at this point in the history
…tation. Removing WatchOS specialization.
  • Loading branch information
pfvernon2 committed Aug 28, 2021
1 parent e29cb5a commit b3006a9
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 98 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ client.connect(serviceName: "MyMixer", timeout: 10.0)
let bundle = OSCBundle(timeTag: OSCTimeTag.immediate,
bundleElements: [
OSCMessage(addressPattern: "/mixer/*/mute[0-9]", arguments: [.boolean(true)]),
OSCMessage(addressPattern: "/mixer/*/solo[0-9]", arguments: [.boolean(true)]),
OSCMessage(addressPattern: "/mixer/*/mute[0-9]", arguments: [.true]),
OSCMessage(addressPattern: "/mixer/*/solo[0-9]", arguments: [.false]),
OSCMessage(addressPattern: "/mixer/*/fader[0-9]", arguments: [.float(0.0)]),
OSCMessage(addressPattern: "/mixer/*/eq", arguments: [.float(0.0), .float(0.0), .float(0.0)]),
OSCMessage(addressPattern: "/mixer/*/label", arguments: [.string("")]),
Expand All @@ -61,10 +61,11 @@ class MyMethod: OSCMethod {
self.requiredArguments = requiredArguments
}
func handleMessage(_ message: OSCMessage, for match: OSCPatternMatchType) {
func handleMessage(_ message: OSCMessage, for match: OSCPatternMatchType, at timeTag: OSCTimeTag?) {
print("Received message: \(message.addressPattern)",
"with match: \(match)",
"arguments: \(String(describing: message.arguments))")
"arguments: \(String(describing: message.arguments)),
"time tag: \(String(describing: timeTag?.date))"")
}
}
Expand All @@ -83,7 +84,7 @@ try server.listen(serviceName: "MyMixer")
```
let mcast = OSCMulticast()
mcast.delegate = self //for group state notifications - see OSCMulticastDelegate
try mcast.joinGroup(on: "224.0.0251", port: 12345)
try mcast.joinGroup(on: "224.0.0.251", port: 12345)
//Register methods if you care to process or monitor messages sent to the group
try mcast.register(methods: [MyMethod(addressPattern: "/mixer/main/mute1", requiredArguments: [.anyBoolean]),
Expand All @@ -93,7 +94,7 @@ try mcast.register(methods: [MyMethod(addressPattern: "/mixer/main/mute1", requi
MyMethod(addressPattern: "/mixer/main/label")
//Note that Messages sent to the group will also be delivered to any Methods you register on the Multicast instance.
let bundle = OSCBundle(timeTag: OSCTimeTag(immediate: true),
let bundle = OSCBundle(timeTag: OSCTimeTag.immediate,
bundleElements: [
OSCMessage(addressPattern: "/mixer/*/mute[0-9]", arguments: [.true]),
OSCMessage(addressPattern: "/mixer/*/solo[0-9]", arguments: [.false]),
Expand Down
5 changes: 1 addition & 4 deletions Sources/OSCine/Network/Browser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import Network

///Simple Bonjour browser with optional timeout.
///
///This is primarly intended for internal use but may be useful if you want to present
///This class is primarly intended for internal use but may be useful if you plan to present
///a list of available OSC servers rather than simply defaulting to the first available.
@available(watchOS 7.0, *)
public class OSCServiceBrowser {
private (set) var serviceType: String

Expand Down Expand Up @@ -78,10 +77,8 @@ public class OSCServiceBrowser {

//MARK: - NWBrowserResultSet

@available(watchOS 7.0, *)
typealias NWBrowserResultSet = Set<NWBrowser.Result>

@available(watchOS 7.0, *)
extension NWBrowserResultSet {
//Utility to return first instance of service matching requested service name
func firstMatch(serviceName: String) -> NWBrowser.Result? {
Expand Down
9 changes: 2 additions & 7 deletions Sources/OSCine/Network/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import Network

//MARK: - OSCClientDelegate

///Delegate for notifications of network state change of the client
@available(watchOS 7.0, *)
///Delegate for notifications of network state change
public protocol OSCClientDelegate: AnyObject {
func connectionStateChange(_ state: NWConnection.State)
}

//MARK: - OSCClient

@available(watchOS 7.0, *)
///Protocol defining OSC client behavior
public protocol OSCClient: AnyObject {
var delegate: OSCClientDelegate? { get set }
var serviceType: String { get set }
Expand All @@ -36,7 +35,6 @@ public protocol OSCClient: AnyObject {
//MARK: - OSCClientUDP

///UDP based OSC client
@available(watchOS 7.0, *)
public class OSCClientUDP: OSCClient, NetworkClient {
weak public var delegate: OSCClientDelegate? = nil
public var serviceType: String = kOSCServiceTypeUDP
Expand All @@ -58,7 +56,6 @@ public class OSCClientUDP: OSCClient, NetworkClient {
//MARK: - OSCClientTCP

///TCP based OSC client
@available(watchOS 7.0, *)
public class OSCClientTCP: OSCClient, NetworkClient {
weak public var delegate: OSCClientDelegate? = nil
public var serviceType: String = kOSCServiceTypeTCP
Expand Down Expand Up @@ -89,14 +86,12 @@ public class OSCClientTCP: OSCClient, NetworkClient {

//MARK: - OSCNetworkClient Protocol

@available(watchOS 7.0, *)
internal protocol NetworkClient: AnyObject {
var connection: NWConnection? { get set }
var parameters: NWParameters { get set }
var browser: OSCServiceBrowser? { get set }
}

@available(watchOS 7.0, *)
public extension OSCClient {
func connect(endpoint: NWEndpoint) {
guard let client = self as? NetworkClient else {
Expand Down
9 changes: 3 additions & 6 deletions Sources/OSCine/Network/Multicast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ import Network

//MARK: - OSCMulticastDelegate

///Delegate for notifications of network state change of the mulitcast connection
@available(watchOS 7.0, *)
///Delegate for notifications of network state change
public protocol OSCMulticastDelegate: AnyObject {
func groupStateChange(state: NWConnectionGroup.State)
}

//MARK: - OSCMulticast

///TCP based OSC server
@available(watchOS 7.0, *)
///Multicast based OSC client and server
public class OSCMulticast {
weak public var delegate: OSCMulticastDelegate? = nil

Expand All @@ -33,7 +31,7 @@ public class OSCMulticast {

internal var manager: OSCConnectionManager = OSCConnectionManager()

internal let sendQueue = DispatchQueue(label: "com.cyberdev.oscmulticastclientserver", qos: .utility)
internal let sendQueue = DispatchQueue(label: "com.cyberdev.oscmulticast", qos: .utility)
internal var sendSemaphore: DispatchSemaphore = DispatchSemaphore(value: 1)

public init() {}
Expand Down Expand Up @@ -125,7 +123,6 @@ public class OSCMulticast {

//MARK: - NWProtocolUDP

@available(watchOS 7.0, *)
extension NWProtocolUDP {
/// Max datagram payload size
///
Expand Down
19 changes: 8 additions & 11 deletions Sources/OSCine/Network/SLIP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ extension SLIPEscapeCodes {
//Data extension for conversion to/from SLIP encoded datagrams
extension Data {
///Returns copy of data with SLIP encoding applied
/// Assumes data is single datagram
/// Assumes data is single complete datagram
func SLIPEncoded() -> Data {
var result = Data(self)
result.SLIPEncode()
return result
}

///Applies SLIP encoding in place on current data
/// Assumes data is single datagram
/// Assumes data is single complete datagram
mutating func SLIPEncode() {
//walk data looking for characters requiring escape and replace with escape sequence
var nextOffset = startIndex
Expand All @@ -93,15 +93,15 @@ extension Data {
}

///Returns copy of data with SLIP encoding removed
/// Assumes data is single datagram
/// Assumes data is single complete datagram
func SLIPDecoded() throws -> Data {
var result = Data(self)
try result.SLIPDecode()
return result
}

///Remove SLIP encoding in place on current data
/// Assumes data is single datagram
/// Assumes data is single complete datagram
mutating func SLIPDecode() throws {
//check for END at end... before decode
if last == SLIPEscapeCodes.END.rawValue {
Expand Down Expand Up @@ -133,7 +133,6 @@ extension Data {
//MARK: - Protocol Framer

///Protocol Framer Implementation of SLIP Protocol
@available(watchOS 7.0, *)
class SLIPProtocol: NWProtocolFramerImplementation {
static let definition = NWProtocolFramer.Definition(implementation: SLIPProtocol.self)
static var label: String { "SLIP" }
Expand All @@ -154,8 +153,6 @@ class SLIPProtocol: NWProtocolFramerImplementation {
let parsed = framer.parseInput(minimumIncompleteLength: 1,
maximumLength: Int.max) { (buffer, isComplete) -> Int in
//Confirm we have a buffer and that it contains at least one datagram terminator
//This is kind of cheating but testing for partial SLIP escape sequences complicated things unnecessarily
// as we need the complete packet before data is delivered up the stack
guard let buffer = buffer, let datagramTerminator = buffer.firstIndex(of: SLIPEscapeCodes.END.rawValue) else {
//many/most datagrams will be short so ask for more data as quickly as available
return .zero
Expand All @@ -170,7 +167,7 @@ class SLIPProtocol: NWProtocolFramerImplementation {
}

//even if we fail parsing return data as consumed since we can't process it
//this will drop the message
//this will drop the message but failure will have been logged.
return datagramTerminator + 1
}

Expand Down Expand Up @@ -204,7 +201,7 @@ class SLIPProtocol: NWProtocolFramerImplementation {
//write up to, but not including, the character needing escape without copying data
try framer.writeOutputNoCopy(length: esc)

//send the associated escape sequence
//write the associated escape sequence
switch slipEsc {
case SLIPEscapeCodes.END:
framer.writeOutput(data: SLIPEscapeCodes.endEscape)
Expand All @@ -218,7 +215,8 @@ class SLIPProtocol: NWProtocolFramerImplementation {

//skip over encoded char on next iteration
// note: writeOutputNoCopy() advances the data position for us
// so only indicate the data we need to skip over here
// so only indicate the single escaped char we need to
// skip over here
return 1
} else {
//nothing to escpape - send this entire portion of the message
Expand All @@ -243,7 +241,6 @@ class SLIPProtocol: NWProtocolFramerImplementation {
}
}

@available(watchOS 7.0, *)
extension NWProtocolFramer.Message {
convenience init(message: OSCMessage) {
self.init(definition: SLIPProtocol.definition)
Expand Down
10 changes: 2 additions & 8 deletions Sources/OSCine/Network/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import Network

//MARK: - OSCServerDelegate

///Delegate for notifications of network state change of the listener
@available(watchOS 7.0, *)
///Delegate for notifications of network state change
public protocol OSCServerDelegate: AnyObject {
func listenerStateChange(state: NWListener.State)
}

//MARK: - OSCServer

@available(watchOS 7.0, *)
///Protocol defining OSC server behavior
public protocol OSCServer: AnyObject {
var delegate: OSCServerDelegate? { get set }
var serviceType: String { get set }
Expand All @@ -37,7 +36,6 @@ public protocol OSCServer: AnyObject {
//MARK: - OSCServerUDP

///UDP based OSC server
@available(watchOS 7.0, *)
public class OSCServerUDP: OSCServer, NetworkServer {
weak public var delegate: OSCServerDelegate? = nil
public var serviceType: String = kOSCServiceTypeUDP
Expand All @@ -59,7 +57,6 @@ public class OSCServerUDP: OSCServer, NetworkServer {
//MARK: - OSCServerTCP

///TCP based OSC server
@available(watchOS 7.0, *)
public class OSCServerTCP: OSCServer, NetworkServer {
weak public var delegate: OSCServerDelegate? = nil
public var serviceType: String = kOSCServiceTypeTCP
Expand Down Expand Up @@ -91,14 +88,12 @@ public class OSCServerTCP: OSCServer, NetworkServer {

//MARK: - OSCNetworkServer protocol

@available(watchOS 7.0, *)
internal protocol NetworkServer: AnyObject {
var listener: NWListener? { get set }
var parameters: NWParameters { get set }
var manager: OSCConnectionManager { get set }
}

@available(watchOS 7.0, *)
public extension OSCServer {
func listen(on port: NWEndpoint.Port = .any, serviceName: String? = nil) throws {
guard let server = self as? NetworkServer else {
Expand Down Expand Up @@ -187,7 +182,6 @@ public extension OSCServer {

//MARK: - OSCConnectionManager

@available(watchOS 7.0, *)
///Internal class to manage server connections for Apple Network Framework
internal class OSCConnectionManager {
var addressSpace = OSCAddressSpace()
Expand Down
Loading

0 comments on commit b3006a9

Please sign in to comment.