Skip to content

Commit

Permalink
#25 Hang reading isConnected (#26)
Browse files Browse the repository at this point in the history
#25 Hang reading isConnected
  • Loading branch information
dsmurfin authored Nov 13, 2022
1 parent a4a04bf commit a13ab83
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions Sources/OTPKit/Components/Consumer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ final public class OTPConsumer: Component {

/// Whether the consumer is able to send/receive data (thread-safe).
public var isConnected: Bool {
get { Self.socketDelegateQueue.sync { _isConnected } }
get { Self.queue.sync(flags: .barrier) { _isConnected } }
}

/// The private socket consumer send/receive status.
Expand Down Expand Up @@ -338,7 +338,7 @@ final public class OTPConsumer: Component {
try unicastSocket.startListening()
try multicast4Socket?.startListening(multicastGroups: [IPv4.advertisementMessageHostname])
try multicast6Socket?.startListening(multicastGroups: [IPv6.advertisementMessageHostname])
Self.socketDelegateQueue.sync {
Self.queue.sync(flags: .barrier) {
self._isConnected = true
}

Expand Down Expand Up @@ -1607,9 +1607,11 @@ extension OTPConsumer: ComponentSocketDelegate {

*/
func socketDidClose(_ socket: ComponentSocket, withError error: Error?) {
if self._isConnected != false {
self._isConnected = false
delegateQueue.async { self.consumerDelegate?.disconnected(self, with: error) }
Self.queue.sync(flags: .barrier) {
if self._isConnected != false {
self._isConnected = false
delegateQueue.async { self.consumerDelegate?.disconnected(self, with: error) }
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions Sources/OTPKit/Components/Producer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ final public class OTPProducer: Component {

/// Whether the producer is able to send/receive data (thread-safe).
public var isConnected: Bool {
get { Self.socketDelegateQueue.sync { _isConnected } }
get { Self.queue.sync(flags: .barrier) { _isConnected } }
}

/// The private socket producer send/receive status.
Expand Down Expand Up @@ -367,7 +367,7 @@ final public class OTPProducer: Component {
try unicastSocket.startListening()
try multicast4Socket?.startListening(multicastGroups: [IPv4.advertisementMessageHostname])
try multicast6Socket?.startListening(multicastGroups: [IPv6.advertisementMessageHostname])
Self.socketDelegateQueue.sync {
Self.queue.sync(flags: .barrier) {
self._isConnected = true
}

Expand Down Expand Up @@ -1883,9 +1883,11 @@ extension OTPProducer: ComponentSocketDelegate {

*/
func socketDidClose(_ socket: ComponentSocket, withError error: Error?) {
if self._isConnected != false {
self._isConnected = false
delegateQueue.async { self.producerDelegate?.disconnected(self, with: error) }
Self.queue.sync(flags: .barrier) {
if self._isConnected != false {
self._isConnected = false
delegateQueue.async { self.producerDelegate?.disconnected(self, with: error) }
}
}
}

Expand Down

0 comments on commit a13ab83

Please sign in to comment.