WebMessageQueue.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import ThreemaFramework
  22. protocol WebMessageQueueDelegate: class {
  23. func sendMessageToWeb(blacklisted: Bool, msgpack: Data, _ connectionInfo: Bool)
  24. func connectionStatus() -> WCConnectionState?
  25. }
  26. class WebMessageQueue: NSObject, NSCoding {
  27. weak var delegate: WebMessageQueueDelegate?
  28. var queue: [[String: Any]]
  29. var dispatchQueue: DispatchQueue
  30. public override init() {
  31. queue = [[String: Any]]()
  32. dispatchQueue = DispatchQueue(label: "ch.threema.webClientResponseQueue", attributes: [])
  33. }
  34. // MARK: NSCoding
  35. required public init?(coder aDecoder: NSCoder) {
  36. // super.init(coder:) is optional, see notes below
  37. self.delegate = aDecoder.decodeObject(forKey: "delegate") as? WebMessageQueueDelegate
  38. self.queue = aDecoder.decodeObject(forKey: "queue") as! [[String: Any]]
  39. self.dispatchQueue = DispatchQueue(label: "ch.threema.webClientResponseQueue", attributes: [])
  40. }
  41. public func encode(with aCoder: NSCoder) {
  42. // super.encodeWithCoder(aCoder) is optional, see notes below
  43. aCoder.encode(delegate, forKey: "delegate")
  44. aCoder.encode(queue, forKey: "queue")
  45. }
  46. }
  47. extension WebMessageQueue {
  48. // MARK: public functions
  49. func enqueue(data: Data?, blackListed: Bool, _ disconnectMessage: Bool = false) {
  50. dispatchQueue.async {
  51. self._enqueue(data: data, blackListed: blackListed, disconnectMessage: disconnectMessage)
  52. }
  53. }
  54. func enqueueWait(data: Data?, blackListed: Bool, _ disconnectMessage: Bool = false) {
  55. dispatchQueue.sync {
  56. self._enqueue(data: data, blackListed: blackListed, disconnectMessage: disconnectMessage)
  57. }
  58. }
  59. func processQueue() {
  60. for dict in queue {
  61. delegate?.sendMessageToWeb(blacklisted: dict["blacklisted"] as! Bool, msgpack: dict["data"] as! Data, false)
  62. }
  63. }
  64. func processSendFinished(finishedData:Data?) {
  65. dispatchQueue.async {
  66. var index = -1
  67. var i = 0
  68. for dict in self.queue {
  69. if let data = dict["data"] as? Data {
  70. if data == finishedData {
  71. index = i
  72. }
  73. }
  74. i = i + 1
  75. }
  76. if index != -1 {
  77. self.queue.remove(at: index)
  78. }
  79. }
  80. }
  81. private func _enqueue(data: Data?, blackListed: Bool, disconnectMessage: Bool) {
  82. if data == nil {
  83. return
  84. }
  85. let dict = ["blacklisted": blackListed, "data": data!] as [String : Any]
  86. if delegate?.connectionStatus() == .ready {
  87. queue.append(dict)
  88. delegate?.sendMessageToWeb(blacklisted: blackListed, msgpack: data!, false)
  89. }
  90. else if disconnectMessage == true {
  91. queue.append(dict)
  92. delegate?.sendMessageToWeb(blacklisted: blackListed, msgpack: data!, true)
  93. }
  94. else {
  95. queue.append(dict)
  96. }
  97. }
  98. @objc func flush() {
  99. dispatchQueue.async {
  100. self.queue.removeAll()
  101. }
  102. }
  103. }