Initial Commit
This commit is contained in:
5
Sources/MessageUtils/Utils/DataCompatibleP.swift
Normal file
5
Sources/MessageUtils/Utils/DataCompatibleP.swift
Normal file
@@ -0,0 +1,5 @@
|
||||
import Foundation
|
||||
|
||||
public protocol DataCompatibleP {
|
||||
var data : Data {get}
|
||||
}
|
||||
32
Sources/MessageUtils/Utils/DataToUtils.swift
Normal file
32
Sources/MessageUtils/Utils/DataToUtils.swift
Normal file
@@ -0,0 +1,32 @@
|
||||
import Foundation
|
||||
|
||||
public extension Data {
|
||||
|
||||
var uint8 : UInt8 {
|
||||
return self.withUnsafeBytes{ $0.bindMemory(to: UInt8.self) }[0]
|
||||
}
|
||||
|
||||
var uint16: UInt16 {
|
||||
return self.withUnsafeBytes{ $0.bindMemory(to: UInt16.self) }[0]
|
||||
}
|
||||
|
||||
var uint32: UInt32 {
|
||||
return self.withUnsafeBytes{ $0.bindMemory(to: UInt32.self) }[0]
|
||||
}
|
||||
|
||||
var uint64: UInt64 {
|
||||
return self.withUnsafeBytes{ $0.bindMemory(to: UInt64.self) }[0]
|
||||
}
|
||||
|
||||
var uint128: UInt128 {
|
||||
return self.withUnsafeBytes{ $0.bindMemory(to: UInt128.self) }[0]
|
||||
}
|
||||
|
||||
var uint: UInt {
|
||||
return self.withUnsafeBytes{ $0.bindMemory(to: UInt.self) }[0]
|
||||
}
|
||||
|
||||
var timestamp: Date {
|
||||
return self.withUnsafeBytes{ $0.bindMemory(to: Date.self) }[0]
|
||||
}
|
||||
}
|
||||
52
Sources/MessageUtils/Utils/MessageBytes.md
Normal file
52
Sources/MessageUtils/Utils/MessageBytes.md
Normal file
@@ -0,0 +1,52 @@
|
||||
# Message Structure
|
||||
```
|
||||
+-+ 64 - bits---+----------------+-------------+----------------+-----------------------+
|
||||
|0| Version 7 | Message Type 8 | Dev Type 8 | RESERVED 16 | Sign Type 32 |
|
||||
+-+-------------+----------------+-------------+----------------+-----------------------+
|
||||
| Timestamp 64 |
|
||||
+---------------------------------------------------------------------------------------+
|
||||
| DevID 128 bits |
|
||||
| |
|
||||
+---------------------------------------------------------------------------------------+
|
||||
| Location 192 bits |
|
||||
| |
|
||||
| |
|
||||
+---------------------------------------------------------------------------------------+
|
||||
\ /
|
||||
| Fields -----------------------------------------------------------------------------|
|
||||
/ \
|
||||
+---------------------------------------------------------------------------------------+
|
||||
| 0 Padding 64 - n |
|
||||
+---------------------------------------------------------------------------------------+
|
||||
| Signature up to-512 bits |
|
||||
| |
|
||||
| |
|
||||
| |
|
||||
| |
|
||||
| |
|
||||
| |
|
||||
| |
|
||||
+---------------------------------------------------------------------------------------+
|
||||
```
|
||||
|
||||
# Fields
|
||||
each field can be at most 8 * 2^32 bits of length:
|
||||
- 34359738368 bits
|
||||
- 4294967296 Bytes
|
||||
|
||||
## Key
|
||||
**MUST** be a `String`
|
||||
|
||||
## Value
|
||||
It's up to the Application to decide whether the value is
|
||||
a string or another type of datum
|
||||
|
||||
```
|
||||
+-- 64 - bits--------------------------+----------------------------------------+
|
||||
| Key-Length 32 | Value-Length 32 |
|
||||
+--------------------------------------+----------------------------------------+
|
||||
\ Key /
|
||||
|-----------------------------------------------------------------------------|
|
||||
/ Value \
|
||||
+-------------------------------------------------------------------------------+
|
||||
```
|
||||
43
Sources/MessageUtils/Utils/ToDataUtils.swift
Normal file
43
Sources/MessageUtils/Utils/ToDataUtils.swift
Normal file
@@ -0,0 +1,43 @@
|
||||
import Foundation
|
||||
|
||||
extension UInt8: DataCompatibleP {
|
||||
public var data: Data {
|
||||
var obj = self
|
||||
return Data(bytes: &obj, count: MemoryLayout<Self>.stride)
|
||||
}
|
||||
}
|
||||
|
||||
extension UInt16: DataCompatibleP {
|
||||
public var data: Data {
|
||||
var obj = self
|
||||
return Data(bytes: &obj, count: MemoryLayout<Self>.stride)
|
||||
}
|
||||
}
|
||||
|
||||
extension UInt32: DataCompatibleP {
|
||||
public var data: Data {
|
||||
var obj = self
|
||||
return Data(bytes: &obj, count: MemoryLayout<Self>.stride)
|
||||
}
|
||||
}
|
||||
|
||||
extension UInt64: DataCompatibleP {
|
||||
public var data: Data {
|
||||
var obj = self
|
||||
return Data(bytes: &obj, count: MemoryLayout<Self>.stride)
|
||||
}
|
||||
}
|
||||
|
||||
extension UInt128: DataCompatibleP {
|
||||
public var data: Data {
|
||||
var obj = self
|
||||
return Data(bytes: &obj, count: MemoryLayout<Self>.stride)
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: DataCompatibleP {
|
||||
public var data: Data {
|
||||
var obj = self
|
||||
return Data(bytes: &obj, count: MemoryLayout<Self>.stride)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user