Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate exceptions from connect() #41

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions nmqtt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1062,36 +1062,40 @@ proc connectBroker(ctx: MqttCtx) {.async.} =

if ctx.verbosity >= 1:
ctx.dbg "Connecting to " & ctx.host & ":" & $ctx.port
try:
ctx.s = await asyncnet.dial(ctx.host, ctx.port)
if ctx.sslOn:
when defined(ssl):
ctx.ssl = newContext(protSSLv23, CVerifyNone, ctx.sslCert, ctx.sslKey)
wrapConnectedSocket(ctx.ssl, ctx.s, handshakeAsClient)
else:
ctx.wrn "Requested SSL session but ssl is not enabled"
await ctx.close("SSL not enabled")
ctx.state = Error
let ok = await ctx.sendConnect()
if ok:
asyncCheck ctx.runRx()
asyncCheck ctx.runPing()
except OSError as e:
if ctx.verbosity >= 1 or not ctx.beenConnected:
ctx.dbg "Error connecting to " & ctx.host
if ctx.verbosity >= 2:
echo e.msg
ctx.state = Error

ctx.state = Error # set to Connecting by sendConnect

ctx.s = await asyncnet.dial(ctx.host, ctx.port)
if ctx.sslOn:
when defined(ssl):
ctx.ssl = newContext(protSSLv23, CVerifyNone, ctx.sslCert, ctx.sslKey)
wrapConnectedSocket(ctx.ssl, ctx.s, handshakeAsClient)
else:
ctx.wrn "Requested SSL session but ssl is not enabled"
await ctx.close("SSL not enabled")

let ok = await ctx.sendConnect()
if ok:
asyncCheck ctx.runRx()
asyncCheck ctx.runPing()


proc runConnect(ctx: MqttCtx) {.async.} =
## Auto-connect and reconnect to broker
await ctx.connectBroker()

while true:
if ctx.state == Disabled:
break
elif ctx.state in [Disconnected, Error]:
await ctx.connectBroker()
try:
await ctx.connectBroker()
except OSError as e:
if ctx.verbosity >= 1 or not ctx.beenConnected:
ctx.dbg "Error connecting to " & ctx.host
if ctx.verbosity >= 2:
echo e.msg
ctx.state = Error

# If the client has been disconnect, it is necessary to tell the broker,
# that we still want to be Subscribed. PubCallbacks still holds the
# callbacks, but we need to re-Subscribe to the broker.
Expand All @@ -1111,7 +1115,7 @@ proc runConnect(ctx: MqttCtx) {.async.} =

proc newMqttCtx*(clientId: string): MqttCtx =
## Initiate a new MQTT client
MqttCtx(clientId: clientId)
MqttCtx(clientId: clientId, state: Disconnected)

proc set_ping_interval*(ctx: MqttCtx, txInterval: int = 60) =
## Set the clients ping interval in seconds. Default is 60 seconds.
Expand Down