Skip to content

Commit 9e330ae

Browse files
committed
mark datagram connections as connected when first reply received
1 parent 053c52d commit 9e330ae

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

Sources/SwiftOCA/OCP.1/Ocp1Connection+Connect.swift

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,16 @@ extension Ocp1Connection {
136136
}
137137
}
138138

139-
/// functions with `_` prefix (with the exception of `_updateConnectionState()`) expect the
140-
/// caller to update the connection state
141-
142-
private func _updateConnectionState(_ connectionState: Ocp1ConnectionState) {
139+
func updateConnectionState(_ connectionState: Ocp1ConnectionState) {
143140
logger.trace("_updateConnectionState: \(_connectionState.value) => \(connectionState)")
144141
_connectionState.send(connectionState)
145142
}
146143

147144
private func _didConnectDevice() async throws {
148-
_updateConnectionState(.connected)
145+
if !isDatagram {
146+
// otherwise, set connected state when we receive first keepAlive PDU
147+
updateConnectionState(.connected)
148+
}
149149

150150
_startMonitor()
151151

@@ -168,13 +168,13 @@ extension Ocp1Connection {
168168
public func connect() async throws {
169169
logger.trace("connecting...")
170170

171-
_updateConnectionState(.connecting)
171+
updateConnectionState(.connecting)
172172

173173
do {
174174
try await _connectDeviceWithTimeout()
175175
} catch {
176176
logger.trace("connection failed: \(error)")
177-
_updateConnectionState(error.ocp1ConnectionState)
177+
updateConnectionState(error.ocp1ConnectionState)
178178
throw error
179179
}
180180

@@ -186,6 +186,14 @@ extension Ocp1Connection {
186186
throw connectionState.error ?? .notConnected
187187
}
188188
}
189+
190+
var isConnecting: Bool {
191+
_connectionState.value == .connecting || _connectionState.value == .reconnecting
192+
}
193+
194+
public var isConnected: Bool {
195+
_connectionState.value == .connected
196+
}
189197
}
190198

191199
// MARK: - disconnection handling
@@ -213,7 +221,7 @@ extension Ocp1Connection {
213221
public func disconnect() async throws {
214222
await removeSubscriptions()
215223

216-
_updateConnectionState(.notConnected)
224+
updateConnectionState(.notConnected)
217225

218226
let clearObjectCache = !options.flags.contains(.retainObjectCacheAfterDisconnect)
219227
try await _disconnectDevice(clearObjectCache: clearObjectCache)
@@ -257,7 +265,7 @@ extension Ocp1Connection {
257265
var lastError: Error?
258266
var backoff: Duration = options.reconnectPauseInterval
259267

260-
_updateConnectionState(.reconnecting)
268+
updateConnectionState(.reconnecting)
261269

262270
logger
263271
.trace(
@@ -283,11 +291,11 @@ extension Ocp1Connection {
283291

284292
if let lastError {
285293
logger.trace("reconnection abandoned: \(lastError)")
286-
_updateConnectionState(lastError.ocp1ConnectionState)
294+
updateConnectionState(lastError.ocp1ConnectionState)
287295
throw lastError
288-
} else if !isConnected {
296+
} else if !isDatagram && !isConnected {
289297
logger.trace("reconnection abandoned after too many tries")
290-
_updateConnectionState(.notConnected)
298+
updateConnectionState(.notConnected)
291299
throw Ocp1Error.notConnected
292300
}
293301
}
@@ -325,7 +333,7 @@ extension Ocp1Connection {
325333
"failed to send message: error \(error), new connection state \(connectionState); disconnecting"
326334
)
327335
if isConnected {
328-
_updateConnectionState(connectionState)
336+
updateConnectionState(connectionState)
329337
try await _disconnectDeviceAfterConnectionFailure()
330338
}
331339
}
@@ -336,15 +344,11 @@ extension Ocp1Connection {
336344

337345
logger.trace("expiring connection with policy \(_reconnectionPolicy), error \(error)")
338346

339-
_updateConnectionState(error.ocp1ConnectionState)
347+
updateConnectionState(error.ocp1ConnectionState)
340348

341349
if _reconnectionPolicy == .reconnectInMonitor {
342350
try await _disconnectDeviceAfterConnectionFailure()
343351
Task { try await reconnectDeviceWithBackoff() }
344352
}
345353
}
346-
347-
public var isConnected: Bool {
348-
_connectionState.value == .connected
349-
}
350354
}

Sources/SwiftOCA/OCP.1/Ocp1ConnectionMonitor.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ extension Ocp1Connection.Monitor {
8888
}
8989
}
9090

91+
@OcaConnection
92+
private func _markDatagramConnectionConnected(_ connection: Ocp1Connection) async {
93+
if connection.isDatagram, connection.isConnecting {
94+
connection.updateConnectionState(.connected)
95+
}
96+
}
97+
9198
private func receiveMessage(_ connection: Ocp1Connection) async throws {
9299
var messagePdus = [Data]()
93100
let messageType = try await receiveMessagePdu(connection, messages: &messagePdus)
@@ -96,6 +103,7 @@ extension Ocp1Connection.Monitor {
96103
}
97104

98105
updateLastMessageReceivedTime()
106+
await _markDatagramConnectionConnected(connection)
99107

100108
for message in messages {
101109
try await processMessage(connection, message)

0 commit comments

Comments
 (0)