From 7582957d6d9620490936466c378b318af5f95bd2 Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Sun, 24 Nov 2024 12:33:05 -0500 Subject: [PATCH] hopefully make jetstream code a little less silent-failure-prone --- src/jetstream.tcl | 1 + src/ws.tcl | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/jetstream.tcl b/src/jetstream.tcl index 3d116a5..5eefdd4 100644 --- a/src/jetstream.tcl +++ b/src/jetstream.tcl @@ -15,6 +15,7 @@ namespace eval ::jetstream { switch -- $mode { close { # cursed + puts "websocket $sock closed! reconnecting..." ::jetstream::on_ws_${postfix}_close } diff --git a/src/ws.tcl b/src/ws.tcl index 7b2a018..854f296 100644 --- a/src/ws.tcl +++ b/src/ws.tcl @@ -2,10 +2,9 @@ namespace eval ::ws { proc read_frame {sock} { fconfigure $sock -blocking 1 - if ![binary scan [read $sock 2] cc byte1 byte2] { - close $sock - return {} - } + set status [binary scan [read $sock 2] cc byte1 byte2] + if {!$status} { close $sock; return } + set byte1 [expr {$byte1 & 0xFF}] set byte2 [expr {$byte2 & 0xFF}] @@ -19,14 +18,17 @@ namespace eval ::ws { set p_len [expr {$byte2 & 0x7F}] if {$p_len == 126} { - binary scan [read $sock 2] S p_len + set status [binary scan [read $sock 2] S p_len] set p_len [expr {$p_len & 0xFFFF}] + if {!$status} { close $sock; return } } elseif {$p_len == 127} { - binary scan [read $sock 8] W p_len + set status [binary scan [read $sock 8] W p_len] + if {!$status} { close $sock; return } } if {$p_mask} { - binary scan [read $sock 4] c4 p_mask_key + set status [binary scan [read $sock 4] c4 p_mask_key] + if {!$status} { close $sock; return } set p_mask_bytes foreach byte $p_mask_key { lappend p_mask_bytes [expr {$byte & 0xFF}] @@ -34,6 +36,7 @@ namespace eval ::ws { } set data [read $sock $p_len] + if {[string length $data] != $p_len} { close $sock; return } fconfigure $sock -blocking 0 if {$p_mask} { @@ -53,7 +56,7 @@ namespace eval ::ws { set frame {} if {$opcode > 16 || $opcode < 0} { - return -code error "Opcode $opcode invalid. Must be in [0,16]." + return -code error "Opcode $opcode invalid. Must be in \[0,16\]." } append frame [binary format cc \ @@ -85,6 +88,7 @@ namespace eval ::ws { puts -nonewline $sock $frame } proc int-doping {sock} { + if {[chan names $sock] == $sock} return ping $sock after 10000 [list ::ws::int-doping $sock] } -- 2.45.2