Update API
This commit is contained in:
parent
f599cb790b
commit
32b4c8c81a
@ -27,8 +27,8 @@ struct DeviceResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Shorthand property for an invalid message.
|
/// Shorthand property for an invalid message.
|
||||||
static var invalidMessageData: DeviceResponse {
|
static var invalidMessageSize: DeviceResponse {
|
||||||
.init(event: .invalidMessageData)
|
.init(event: .invalidMessageSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shorthand property for missing body data.
|
/// Shorthand property for missing body data.
|
||||||
@ -83,8 +83,8 @@ struct DeviceResponse {
|
|||||||
/// Get the reponse encoded in bytes.
|
/// Get the reponse encoded in bytes.
|
||||||
var encoded: Data {
|
var encoded: Data {
|
||||||
guard let message = response else {
|
guard let message = response else {
|
||||||
return Data([event.rawValue])
|
return event.encoded
|
||||||
}
|
}
|
||||||
return Data([event.rawValue]) + message.encoded
|
return event.encoded + message.encoded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,8 @@ enum MessageResult: UInt8 {
|
|||||||
/// A socket event on the device was unexpected (not binary data)
|
/// A socket event on the device was unexpected (not binary data)
|
||||||
case unexpectedSocketEvent = 2
|
case unexpectedSocketEvent = 2
|
||||||
|
|
||||||
/// The size of the payload (i.e. message) was invalid, or the data could not be read
|
/// The size of the payload (i.e. message) was invalid
|
||||||
case invalidMessageData = 3
|
case invalidMessageSize = 3
|
||||||
|
|
||||||
/// The transmitted message could not be authenticated using the key
|
/// The transmitted message could not be authenticated using the key
|
||||||
case messageAuthenticationFailed = 4
|
case messageAuthenticationFailed = 4
|
||||||
@ -44,6 +44,10 @@ enum MessageResult: UInt8 {
|
|||||||
|
|
||||||
/// The device is connected
|
/// The device is connected
|
||||||
case deviceConnected = 15
|
case deviceConnected = 15
|
||||||
|
|
||||||
|
case invalidUrlParameter = 20
|
||||||
|
|
||||||
|
case invalidResponseAuthentication = 21
|
||||||
}
|
}
|
||||||
|
|
||||||
extension MessageResult: CustomStringConvertible {
|
extension MessageResult: CustomStringConvertible {
|
||||||
@ -54,7 +58,7 @@ extension MessageResult: CustomStringConvertible {
|
|||||||
return "The device received unexpected text"
|
return "The device received unexpected text"
|
||||||
case .unexpectedSocketEvent:
|
case .unexpectedSocketEvent:
|
||||||
return "Unexpected socket event for the device"
|
return "Unexpected socket event for the device"
|
||||||
case .invalidMessageData:
|
case .invalidMessageSize:
|
||||||
return "Invalid message data"
|
return "Invalid message data"
|
||||||
case .messageAuthenticationFailed:
|
case .messageAuthenticationFailed:
|
||||||
return "Message authentication failed"
|
return "Message authentication failed"
|
||||||
@ -76,6 +80,17 @@ extension MessageResult: CustomStringConvertible {
|
|||||||
return "Another operation is in progress"
|
return "Another operation is in progress"
|
||||||
case .deviceConnected:
|
case .deviceConnected:
|
||||||
return "The device is connected"
|
return "The device is connected"
|
||||||
|
case .invalidUrlParameter:
|
||||||
|
return "The url parameter could not be found"
|
||||||
|
case .invalidResponseAuthentication:
|
||||||
|
return "The response could not be authenticated"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension MessageResult {
|
||||||
|
|
||||||
|
var encoded: Data {
|
||||||
|
Data([rawValue])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -11,8 +11,6 @@ struct ServerMessage {
|
|||||||
|
|
||||||
static let authTokenSize = SHA256.byteCount
|
static let authTokenSize = SHA256.byteCount
|
||||||
|
|
||||||
static let length = authTokenSize + Message.length
|
|
||||||
|
|
||||||
let authToken: Data
|
let authToken: Data
|
||||||
|
|
||||||
let message: Message
|
let message: Message
|
||||||
@ -22,30 +20,7 @@ struct ServerMessage {
|
|||||||
self.message = message
|
self.message = message
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
Decode a message from a byte buffer.
|
|
||||||
The buffer must contain at least `ServerMessage.length` bytes, or it will return `nil`.
|
|
||||||
- Parameter buffer: The buffer containing the bytes.
|
|
||||||
*/
|
|
||||||
init?(decodeFrom buffer: ByteBuffer) {
|
|
||||||
guard let data = buffer.getBytes(at: 0, length: ServerMessage.length) else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
self.authToken = Data(data.prefix(ServerMessage.authTokenSize))
|
|
||||||
self.message = Message(decodeFrom: Data(data.dropFirst(ServerMessage.authTokenSize)))
|
|
||||||
}
|
|
||||||
|
|
||||||
var encoded: Data {
|
var encoded: Data {
|
||||||
authToken + message.encoded
|
authToken + message.encoded
|
||||||
}
|
}
|
||||||
|
|
||||||
static func token(from buffer: ByteBuffer) -> Data? {
|
|
||||||
guard buffer.readableBytes == authTokenSize else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
guard let bytes = buffer.getBytes(at: 0, length: authTokenSize) else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return Data(bytes)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user