fix: Implement proactive packet flushing based on Content-Length to resolve UI update lag

This commit is contained in:
Jose Luis Montañes Ojados
2026-01-19 16:32:57 +01:00
parent 751bf380d7
commit 42d27c0647
2 changed files with 57 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -111,6 +112,7 @@ func (c *Capturer) processStream(r io.Reader) {
var buffer strings.Builder var buffer strings.Builder
inSIPMessage := false inSIPMessage := false
var msgNetInfo *NetInfo var msgNetInfo *NetInfo
contentLength := -1
for scanner.Scan() { for scanner.Scan() {
c.mu.Lock() c.mu.Lock()
@@ -149,14 +151,38 @@ func (c *Capturer) processStream(r io.Reader) {
} }
inSIPMessage = true inSIPMessage = true
contentLength = -1 // Reset
} }
if inSIPMessage { if inSIPMessage {
buffer.WriteString(line) buffer.WriteString(line)
buffer.WriteString("\r\n") buffer.WriteString("\r\n")
// Detect end of SIP message (double CRLF or content complete) // Check for Content-Length
// This is simplified - real implementation would track Content-Length lowerLine := strings.ToLower(line)
if strings.HasPrefix(lowerLine, "content-length:") || strings.HasPrefix(lowerLine, "l:") {
parts := strings.Split(line, ":")
if len(parts) >= 2 {
val := strings.TrimSpace(parts[1])
if val != "" {
if cl, err := strconv.Atoi(val); err == nil {
contentLength = cl
}
}
}
}
// Check for end of headers (empty line)
if line == "" {
// If Content-Length is 0 (or not found, treating as 0)
// Flush immediately
if contentLength <= 0 {
c.parseAndEmit(buffer.String(), msgNetInfo)
buffer.Reset()
inSIPMessage = false
contentLength = -1
}
}
} }
} }

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"os/exec" "os/exec"
"strconv"
"strings" "strings"
"sync" "sync"
@@ -133,6 +134,7 @@ func (c *LocalCapturer) processStream(r io.Reader) {
var buffer strings.Builder var buffer strings.Builder
inSIPMessage := false inSIPMessage := false
var msgNetInfo *NetInfo var msgNetInfo *NetInfo
contentLength := -1
for scanner.Scan() { for scanner.Scan() {
c.mu.Lock() c.mu.Lock()
@@ -192,11 +194,38 @@ func (c *LocalCapturer) processStream(r io.Reader) {
} }
inSIPMessage = true inSIPMessage = true
contentLength = -1 // Reset for new message
} }
if inSIPMessage { if inSIPMessage {
buffer.WriteString(line) buffer.WriteString(line)
buffer.WriteString("\r\n") buffer.WriteString("\r\n")
// Check for Content-Length
lowerLine := strings.ToLower(line)
if strings.HasPrefix(lowerLine, "content-length:") || strings.HasPrefix(lowerLine, "l:") {
parts := strings.Split(line, ":")
if len(parts) >= 2 {
val := strings.TrimSpace(parts[1])
if val != "" {
if cl, err := strconv.Atoi(val); err == nil {
contentLength = cl
}
}
}
}
// Check for end of headers (empty line)
if line == "" {
// If Content-Length is 0 (or not found, treating as 0 for safety in this context usually 0 for BYE/ACK)
// Flush immediately
if contentLength <= 0 {
c.parseAndEmit(buffer.String(), msgNetInfo)
buffer.Reset()
inSIPMessage = false
contentLength = -1
}
}
} }
} }