~aleteoryx/tclfeed-bsky

e7e6818c81278aafe4035e9dac60958e6e72bbc1 — Aleteoryx 25 days ago f9c699e
maybe workaround db locking?
1 files changed, 51 insertions(+), 47 deletions(-)

M src/ws.tcl
M src/ws.tcl => src/ws.tcl +51 -47
@@ 220,59 220,63 @@ namespace eval ::ws::c {
      [set callback($sock)] $sock close [dict get $frame data]
    }

    if {[catch {
      switch -- [dict get $frame opcode] {
        0 {
          if {[set mode($sock)] ni {1 2}} {
            ${log}::error "Got a continuation frame with no starting frame on $sock! Closing!"
    set madeit 0
    for {set i 1} {$i <= 3 && !$madeit} {incr i} {
      if {[catch {
        switch -- [dict get $frame opcode] {
          0 {
            if {[set mode($sock)] ni {1 2}} {
              ${log}::error "Got a continuation frame with no starting frame on $sock! Closing!"
              close $sock
              [set callback($sock)] $sock close [set frag($sock)]
            }
            append frags($sock) [dict get $frame data]
            if {[dict get $frame fin]} {
              set frags($sock) {}
            }
          }
          1 {
            if {[dict get $frame fin]} {
              set mode($sock) {}
              set frag($sock) {}
              [set callback($sock)] $sock text [dict get $frame data]
            } else {
              set mode($sock) text
              set frag($sock) [dict get $frame data]
            }
          }
          2 {
            if {[dict get $frame fin]} {
              set mode($sock) {}
              set frag($sock) {}
              [set callback($sock)] $sock binary [dict get $frame data]
            } else {
              set mode($sock) binary
              set frag($sock) [dict get $frame data]
            }
          }

          8 {
            close $sock
            [set callback($sock)] $sock close [set frag($sock)]
            [set callback($sock)] $sock close [dict get $frame data]
          }
          append frags($sock) [dict get $frame data]
          if {[dict get $frame fin]} {
            set frags($sock) {}
          9 {
            ::ws::send_frame 0 10 [dict get $frame data] [dict get $frame len] 1
          }
        }
        1 {
          if {[dict get $frame fin]} {
            set mode($sock) {}
            set frag($sock) {}
            [set callback($sock)] $sock text [dict get $frame data]
          } else {
            set mode($sock) text
            set frag($sock) [dict get $frame data]
          10 {
            # TODO: kill connection on missed ping? but jetstream doesn't send any it looks like
          }
        }
        2 {
          if {[dict get $frame fin]} {
            set mode($sock) {}
            set frag($sock) {}
            [set callback($sock)] $sock binary [dict get $frame data]
          } else {
            set mode($sock) binary
            set frag($sock) [dict get $frame data]
          default {
            ${log}::error "Unknown opcode [dict get $frame opcode] on $sock! Closing!"
            close $sock
            [set callback($sock)] $sock close [dict get $frame data]
          }
        }

        8 {
          close $sock
          [set callback($sock)] $sock close [dict get $frame data]
        }
        9 {
          ::ws::send_frame 0 10 [dict get $frame data] [dict get $frame len] 1
        }
        10 {
          # TODO: kill connection on missed ping? but jetstream doesn't send any it looks like
        }
        default {
          ${log}::error "Unknown opcode [dict get $frame opcode] on $sock! Closing!"
          close $sock
          [set callback($sock)] $sock close [dict get $frame data]
        }
        set madeit 1
      } result options]} {
        ${log}::error "error in websocket callback for ${sock} (attempt $i): $result"
        ${log}::error "options: $options"
      }
    } result options]} {
      ${log}::error "error in websocket callback for ${sock}: $result"
      ${log}::error "options: $options"
    }
    }; # end for
  }
}