When binding 2 event handlers on 2 different UDP sockets on the same UDP port, one configured for multicast, one configured for unicast, both event handlers are fired up when a multicast packet is received, which is not expected behaviour. When an unicast packet is received, only the unicast handler is fired up, which is expected behaviour.
What could be wrong in my code ?
package require udp
package require logger
set log [logger::init m5140]
proc logger::tree::m5140::stdoutcmd {level text} {
variable service
puts "\[[clock format [clock seconds] -format {%Y%m%d-%H%M%S}]\] \[$service\] \[$level\] \'$text\'"
}
${log}::notice "[info script] Started"
set group 224.1.5.14
set port 5140
Unicast
proc mcastEvent {chan} {
variable mcount
incr mcount
lassign [chan configure $chan -peer] host port
set data [read $chan]
if { $data ne {} } {
incr mcount
after idle ${::log}::notice [list [format {multicast (%08d) from %s |%s| } $mcount $host $data]]
}
}
proc ucastEvent {chan} {
variable ucount
incr ucount
set data [read $chan]
lassign [chan configure $chan -peer] host port
if { $data ne {} } {
incr ucount
after idle ${::log}::notice [list [format {unicast (%08d) from %s |%s| } $ucount $host $data]]
}
}
######################## MULTICAST ###################################
# Create a listening Multicast socket
set msock [udp_open $port reuse]
${log}::notice "[info script] mcast_port $msock"
chan configure $msock -buffering line -blocking 0 -encoding utf-8 \
-mcastadd $group -remote [list $group $port]
######################## UNICAST ###################################
# Create a listening Unicast socket
set usock [udp_open $port reuse]
${log}::notice "[info script] ucast_port $port"
chan configure $usock -buffering line -blocking 0 -encoding utf-8
# Start multicast event handler
fileevent $msock readable [list mcastEvent $msock]
# Start Unicast event handler
fileevent $usock readable [list ucastEvent $usock]