@@ -8,6 +8,7 @@ function decode end
8
8
struct SubscriptionOptions{T, F}
9
9
msgtype:: Type{T}
10
10
handler:: F
11
+ channel:: String
11
12
end
12
13
13
14
struct Subscription{T <: SubscriptionOptions }
@@ -135,23 +136,31 @@ struct RecvBuf
135
136
lcm:: Ptr{Cvoid}
136
137
end
137
138
138
- function onresponse (rbuf:: RecvBuf , channelbytes:: Ptr{UInt8} , opts:: SubscriptionOptions{T} ) where T
139
- channel = unsafe_string (channelbytes)
139
+ function check_channel_name (channelptr:: Ptr{UInt8} , opts:: SubscriptionOptions )
140
+ i = 1
141
+ while (byte = unsafe_load (channelptr, i)) != 0x00
142
+ byte == codeunit (opts. channel, i) || error (" Mismatch between received channel name and subscription channel name" )
143
+ i += 1
144
+ end
145
+ end
146
+
147
+ function onresponse (rbuf:: RecvBuf , channelptr:: Ptr{UInt8} , opts:: SubscriptionOptions{T} ) where T
148
+ check_channel_name (channelptr, opts)
140
149
msgdata = unsafe_wrap (Vector{UInt8}, rbuf. data, rbuf. data_size)
141
150
if T === Nothing
142
- opts. handler (channel, msgdata)
151
+ opts. handler (opts . channel, msgdata)
143
152
else
144
153
msg = decode (msgdata, opts. msgtype)
145
- opts. handler (channel, msg)
154
+ opts. handler (opts . channel, msg)
146
155
end
147
156
return nothing
148
157
end
149
158
150
- function subscribe (lcm:: LCM , channel :: String , options:: T ) where T <: SubscriptionOptions
159
+ function subscribe (lcm:: LCM , options:: T ) where T <: SubscriptionOptions
151
160
csubscription = ccall ((:lcm_subscribe , liblcm), Ptr{Cvoid},
152
161
(Ptr{Cvoid}, Ptr{UInt8}, Ptr{Cvoid}, Ptr{Cvoid}),
153
162
lcm,
154
- channel,
163
+ options . channel,
155
164
@cfunction (onresponse, Cvoid, (Ref{RecvBuf}, Ptr{UInt8}, Ref{T})),
156
165
Ref (options))
157
166
sub = Subscription (options, csubscription)
@@ -160,7 +169,7 @@ function subscribe(lcm::LCM, channel::String, options::T) where T <: Subscriptio
160
169
end
161
170
162
171
function subscribe (lcm:: LCM , channel:: String , handler, msgtype= Nothing)
163
- subscribe (lcm, channel, SubscriptionOptions (msgtype, handler))
172
+ subscribe (lcm, SubscriptionOptions (msgtype, handler, channel ))
164
173
end
165
174
166
175
function unsubscribe (lcm:: LCM , subscription:: Subscription )
0 commit comments