Skip to content

Extract shared MQTT client core - #738

Open
hjianbo wants to merge 1 commit into
masterfrom
refactor/issue-699-lifecycle-contract
Open

Extract shared MQTT client core#738
hjianbo wants to merge 1 commit into
masterfrom
refactor/issue-699-lifecycle-contract

Conversation

@hjianbo

@hjianbo hjianbo commented Jul 31, 2026

Copy link
Copy Markdown
Member

What changed

  • add an internal MQTTClientCore used by both MQTT 3.1.1 and MQTT 5
  • move the protocol-neutral runtime into that core:
    • transport delegate normalization and reader lifecycle
    • connection state and callback dispatch
    • expected/unexpected disconnect sequencing
    • auto-reconnect and keepalive
    • frame serialization and socket writes
    • delivery engine, packet identifiers, and delivery tokens
    • common in-memory/persisted-session cleanup bookkeeping
  • keep protocol-specific behavior in the public facades through a typed adapter boundary: CONNECT/CONNACK frames, MQTT 5 capabilities, topic aliases, session expiry, properties, and reason codes
  • run the same connection-state contract tests against both public clients
  • include the shared core and contract tests in the Xcode project as well as SwiftPM

Why

The MQTT 3.1.1 and MQTT 5 facades had independently evolved copies of the same connection lifecycle. That made fixes easy to apply to only one protocol version. This change establishes one implementation for the shared runtime while leaving protocol semantics explicit in each facade.

Fixes #699

Compatibility

  • no public API, Objective-C delegate, or protocol-conformance changes (verified with Swift API Digester)
  • MQTT 5 keeps its public $connState projected value
  • both clients retain public CocoaMQTTSocketDelegate conformance
  • state writes remain synchronously visible; callbacks remain asynchronous on the captured delegateQueue
  • repeated state assignments, manual ping(), reconnect callback ordering, custom socket event-loop normalization, and deinit behavior are preserved

Verification

  • swift test --filter ConnectionStateContractTests
  • swift test --filter 'SendingMessageLifecycleTests|CocoaMQTTDeliverTests|ConnectionStateContractTests' — 64 passed
  • focused lifecycle/reconnect/event-loop/TLS/thread-safety suite — 172 passed
  • focused Thread Sanitizer suite across both clients — 6 passed, no reports
  • swift build --target CocoaMQTT -Xswiftc -swift-version -Xswiftc 6
  • xcodebuild -project CocoaMQTT.xcodeproj -scheme "Mac Framework" -derivedDataPath /private/tmp/CocoaMQTT-699-derived CODE_SIGNING_ALLOWED=NO build
  • Tools/lint.sh
  • git diff --check
  • Swift API Digester comparison against master — no API differences

The 307-test local non-broker run reaches only the pre-existing TLS/WSS loopback fixture failures (14 assertion failures), reproduced identically on an untouched master worktree; all other tests pass. CI provides the configured loopback environment.

@hjianbo
hjianbo force-pushed the refactor/issue-699-lifecycle-contract branch from 018dbb7 to 98ada77 Compare July 31, 2026 12:13
@hjianbo hjianbo changed the title Unify MQTT client connection state contract Extract shared MQTT client core Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant