Skip to content

Commit be95270

Browse files
authored
Merge branch 'zevv:master' into is-errored
2 parents 66ca2aa + a704fa9 commit be95270

3 files changed

Lines changed: 94 additions & 45 deletions

File tree

.github/workflows/main.yml

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
branches: [ master ]
66
pull_request:
77
branches: [ master ]
8+
workflow_dispatch:
89

910
jobs:
1011
build:
@@ -13,28 +14,69 @@ jobs:
1314
matrix:
1415
os:
1516
- ubuntu-latest
16-
- windows-latest
17-
- macOS-latest
17+
# - windows-latest
18+
# - macOS-latest
1819
version:
19-
- stable
20+
# - stable
21+
# - 1.6.18
22+
- binary:stable
23+
- binary:1.6.18
2024

2125
steps:
22-
- uses: actions/checkout@v1
23-
- uses: jiro4989/setup-nim-action@master
24-
with:
25-
nim-version: ${{ matrix.version }}
26+
- uses: actions/checkout@v4
2627

27-
- name: Print Nim version
28-
run: nim -v
2928

30-
- name: Print Nimble version
31-
run: nimble -v
29+
#
30+
# Nim
31+
#
32+
- name: Cache Nim binaries
33+
uses: actions/cache@v4
34+
with:
35+
path: |
36+
nimdir
37+
~/.nimble
38+
key: "nim-${{ matrix.version }}-${{ matrix.os }}"
3239

33-
- name: Nimble Refresh
34-
run: nimble -y refresh
40+
- name: Check for cached Nim binaries
41+
id: nim-binaries-cache
42+
run: |
43+
if [ -f "nimdir/bin/nim" ]; then
44+
echo "found=true" >> $GITHUB_OUTPUT
45+
else
46+
echo "found=false" >> $GITHUB_OUTPUT
47+
fi
3548
36-
- name: Nimble Install dependencies
37-
run: nimble -y install --depsOnly
49+
- name: Install Nim (if not cached)
50+
if: steps.nim-binaries-cache.outputs.found == 'false'
51+
uses: iffy/install-nim@v5
52+
with:
53+
version: ${{ matrix.version }}
54+
env:
55+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3856

39-
- name: Build binaries
40-
run: nimble build -d:release
57+
- name: Add cached Nim to PATH
58+
if: steps.nim-binaries-cache.outputs.found == 'true'
59+
run: |
60+
echo "${{ github.workspace }}/nimdir/bin" >> $GITHUB_PATH
61+
echo "$HOME/.nimble/bin" >> $GITHUB_PATH
62+
63+
64+
#
65+
# Nim
66+
#
67+
- name: Print Nim version
68+
run: |
69+
nim -v
70+
71+
- name: Print Nimble version
72+
run: |
73+
nimble -v
74+
75+
- name: Nimble Refresh
76+
run: nimble -y refresh
77+
78+
- name: Nimble Install dependencies
79+
run: nimble -y install --depsOnly
80+
81+
- name: Build binaries
82+
run: nimble build -d:release

nmqtt.nim

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -835,18 +835,19 @@ proc onPublish(ctx: MqttCtx, pkt: Pkt) {.async.} =
835835
verbose("Retained ", mqttbroker.retained)
836836

837837
when not defined(broker):
838+
var callbacks: seq[PubCallback]
838839
for top, cb in ctx.pubCallbacks:
839840
if top == topic or top == "#":
840-
cb.cb(topic, message)
841+
callbacks.add(cb)
841842
if top.endsWith("/#"):
842843
# the multi-level wildcard can represent zero levels.
843844
if topic == top[0 .. ^3]:
844-
cb.cb(topic, message)
845+
callbacks.add(cb)
845846
continue
846847
var topicw = top
847848
topicw.removeSuffix("#")
848849
if topic.contains(topicw):
849-
cb.cb(topic, message)
850+
callbacks.add(cb)
850851
if top.contains("+"):
851852
var topelem = split(top, '/')
852853
if len(topelem) == count(topic, '/') + 1:
@@ -855,7 +856,9 @@ proc onPublish(ctx: MqttCtx, pkt: Pkt) {.async.} =
855856
if topelem[i] != "+" and e != topelem[i]: break
856857
i = i+1
857858
if i == len(topelem):
858-
cb.cb(topic, message)
859+
callbacks.add(cb)
860+
for cb in callbacks:
861+
cb.cb(topic, message)
859862

860863
if qos == 1:
861864
ctx.workQueue[msgId] = Work(wk: PubWork, msgId: msgId, state: WorkNew, qos: 1, typ: PubAck)
@@ -1062,36 +1065,40 @@ proc connectBroker(ctx: MqttCtx) {.async.} =
10621065

10631066
if ctx.verbosity >= 1:
10641067
ctx.dbg "Connecting to " & ctx.host & ":" & $ctx.port
1065-
try:
1066-
ctx.s = await asyncnet.dial(ctx.host, ctx.port)
1067-
if ctx.sslOn:
1068-
when defined(ssl):
1069-
ctx.ssl = newContext(protSSLv23, CVerifyNone, ctx.sslCert, ctx.sslKey)
1070-
wrapConnectedSocket(ctx.ssl, ctx.s, handshakeAsClient)
1071-
else:
1072-
ctx.wrn "Requested SSL session but ssl is not enabled"
1073-
await ctx.close("SSL not enabled")
1074-
ctx.state = Error
1075-
let ok = await ctx.sendConnect()
1076-
if ok:
1077-
asyncCheck ctx.runRx()
1078-
asyncCheck ctx.runPing()
1079-
except OSError as e:
1080-
if ctx.verbosity >= 1 or not ctx.beenConnected:
1081-
ctx.dbg "Error connecting to " & ctx.host
1082-
if ctx.verbosity >= 2:
1083-
echo e.msg
1084-
ctx.state = Error
1068+
1069+
ctx.state = Error # set to Connecting by sendConnect
1070+
1071+
ctx.s = await asyncnet.dial(ctx.host, ctx.port)
1072+
if ctx.sslOn:
1073+
when defined(ssl):
1074+
ctx.ssl = newContext(protSSLv23, CVerifyNone, ctx.sslCert, ctx.sslKey)
1075+
wrapConnectedSocket(ctx.ssl, ctx.s, handshakeAsClient)
1076+
else:
1077+
ctx.wrn "Requested SSL session but ssl is not enabled"
1078+
await ctx.close("SSL not enabled")
1079+
1080+
let ok = await ctx.sendConnect()
1081+
if ok:
1082+
asyncCheck ctx.runRx()
1083+
asyncCheck ctx.runPing()
1084+
10851085

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

10901089
while true:
10911090
if ctx.state == Disabled:
10921091
break
10931092
elif ctx.state in [Disconnected, Error]:
1094-
await ctx.connectBroker()
1093+
try:
1094+
await ctx.connectBroker()
1095+
except OSError as e:
1096+
if ctx.verbosity >= 1 or not ctx.beenConnected:
1097+
ctx.dbg "Error connecting to " & ctx.host
1098+
if ctx.verbosity >= 2:
1099+
echo e.msg
1100+
ctx.state = Error
1101+
10951102
# If the client has been disconnect, it is necessary to tell the broker,
10961103
# that we still want to be Subscribed. PubCallbacks still holds the
10971104
# callbacks, but we need to re-Subscribe to the broker.
@@ -1111,7 +1118,7 @@ proc runConnect(ctx: MqttCtx) {.async.} =
11111118

11121119
proc newMqttCtx*(clientId: string): MqttCtx =
11131120
## Initiate a new MQTT client
1114-
MqttCtx(clientId: clientId)
1121+
MqttCtx(clientId: clientId, state: Disconnected)
11151122

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

nmqtt.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Package
2-
version = "1.0.5"
2+
version = "1.0.7"
33
author = "zevv & ThomasTJdev"
44
description = "Native MQTT library and binaries for publishing, subscribing and broker"
55
license = "MIT"

0 commit comments

Comments
 (0)