Skip to content

Instantly share code, notes, and snippets.

@iamcrypticcoder
Created March 30, 2018 18:15
Show Gist options
  • Save iamcrypticcoder/7ded8690ecd7b222f74f56fcccdf65fe to your computer and use it in GitHub Desktop.
Save iamcrypticcoder/7ded8690ecd7b222f74f56fcccdf65fe to your computer and use it in GitHub Desktop.
class ThunderboltAdapter {
var connection: Connection
init(_ connection: Connection) {
self.connection = connection
}
func sendData(_ data: String) {
if connection is EthernetConnection {
let sequence = stride(from: 0, to: data.count, by: 10)
for i in sequence {
let start = data.index(data.startIndex, offsetBy: i)
let end = i + 10 >= data.count ?
data.index(data.endIndex, offsetBy: 0) :
data.index(data.startIndex, offsetBy: i+10)
connection.sendPacket(String(data[start..<end]))
}
return
}
if connection is MobileDataConnection {
let sequence = stride(from: 0, to: data.count, by: 5)
for i in sequence {
let start = data.index(data.startIndex, offsetBy: i)
let end = i + 5 >= data.count ?
data.index(data.endIndex, offsetBy: 0) :
data.index(data.startIndex, offsetBy: i+5)
connection.sendPacket(String(data[start..<end]))
}
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment