Skip to content

Commit

Permalink
synchronize mem access, avoid duplicate events (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia authored Sep 6, 2022
1 parent 34c84ad commit 0fb098c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.5.1
=====

* Fix crashes caused by non-synchronized memory access upon getting `no ping` disconnect
* Fix issuing duplicate state change events

0.5.0
=====

Expand Down
34 changes: 20 additions & 14 deletions Sources/SwiftCentrifuge/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,11 @@ fileprivate extension CentrifugeClient {
self.pingTimer = DispatchSource.makeTimerSource()
self.pingTimer?.setEventHandler { [weak self] in
guard let strongSelf = self else { return }
guard strongSelf.state == .connected else { return }
strongSelf.processDisconnect(code: connectingCodeNoPing, reason: "no ping", reconnect: true)
strongSelf.syncQueue.async { [weak self] in
guard let strongSelf = self else { return }
guard strongSelf.state == .connected else { return }
strongSelf.processDisconnect(code: connectingCodeNoPing, reason: "no ping", reconnect: true)
}
}
self.pingTimer?.schedule(deadline: .now() + Double(self.pingInterval) + self.config.maxServerPingDelay)
self.pingTimer?.resume()
Expand Down Expand Up @@ -980,13 +983,14 @@ fileprivate extension CentrifugeClient {
return
}

let previousStatus = self.state
let previousState = self.state

if reconnect {
self.state = .connecting
} else {
self.state = .disconnected
}
let needEvent = previousState != self.state

self.client = nil

Expand All @@ -1010,23 +1014,25 @@ fileprivate extension CentrifugeClient {
}
subscriptionsLock.unlock()

if previousStatus == .connected {
if previousState == .connected {
for (channel, _) in self.serverSubs {
let event = CentrifugeServerSubscribingEvent(channel: channel)
self.delegate?.onSubscribing(self, event)
}
}

if (self.state == .disconnected) {
self.delegate?.onDisconnected(
self,
CentrifugeDisconnectedEvent(code: code, reason: reason)
)
} else {
self.delegate?.onConnecting(
self,
CentrifugeConnectingEvent(code: code, reason: reason)
)
if (needEvent) {
if (self.state == .disconnected) {
self.delegate?.onDisconnected(
self,
CentrifugeDisconnectedEvent(code: code, reason: reason)
)
} else {
self.delegate?.onConnecting(
self,
CentrifugeConnectingEvent(code: code, reason: reason)
)
}
}

self.conn?.disconnect()
Expand Down
2 changes: 1 addition & 1 deletion SwiftCentrifuge.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Pod::Spec.new do |s|
s.name = 'SwiftCentrifuge'
s.module_name = 'SwiftCentrifuge'
s.swift_version = '5.0'
s.version = '0.5.0'
s.version = '0.5.1'

s.homepage = 'https://github.com/centrifugal/centrifuge-swift'
s.summary = 'Centrifugo and Centrifuge client in Swift'
Expand Down

0 comments on commit 0fb098c

Please sign in to comment.