fix: Implement proactive packet flushing based on Content-Length to resolve UI update lag
This commit is contained in:
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user