From 8248764b1942a75aa9cc3fc8a427652d484faeef Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Thu, 16 Feb 2023 03:03:05 +0800 Subject: [PATCH] Fix --- go.mod | 1 + go.sum | 2 + websocket/Recoder.go | 92 ++++++++++++++++++++++++-------------------- 3 files changed, 54 insertions(+), 41 deletions(-) diff --git a/go.mod b/go.mod index b85c4ae..1278e3d 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( require ( github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect + github.com/dustin/go-humanize v1.0.1 github.com/go-ole/go-ole v1.2.5 // indirect github.com/stretchr/testify v1.8.1 // indirect golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect diff --git a/go.sum b/go.sum index 9bf3704..29981c8 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHG github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY= github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= diff --git a/websocket/Recoder.go b/websocket/Recoder.go index 34e2817..52c2a2c 100644 --- a/websocket/Recoder.go +++ b/websocket/Recoder.go @@ -4,10 +4,10 @@ import ( "bytes" "errors" "fmt" - "io" "strconv" "time" + "github.com/dustin/go-humanize" file "github.com/qydysky/part/file" funcCtrl "github.com/qydysky/part/funcCtrl" signal "github.com/qydysky/part/signal" @@ -75,7 +75,7 @@ func (t *Recorder) Stop() { t.onlyOnce.UnSet() } -func Play(filePath string, perReadSize int, maxReadSize int) (s *Server, close func()) { +func Play(filePath string) (s *Server, close func()) { sg := signal.Init() s = New_server() @@ -95,56 +95,66 @@ func Play(filePath string, perReadSize int, maxReadSize int) (s *Server, close f timer := time.NewTicker(time.Second) defer timer.Stop() - var ( - cu float64 - data []byte - err error - ) - - s.Interface().Pull_tag(map[string]func(any) (disable bool){ - `recv`: func(a any) (disable bool) { - if d, ok := a.(Uinterface); ok { - switch string(d.Data) { - case "%Cpause": - timer.Stop() - case "%Cplay": - timer.Reset(time.Second) - default: - cu, _ = strconv.ParseFloat(string(d.Data[2:]), 64) + if data, e := f.ReadAll(humanize.KByte, humanize.MByte); e != nil { + panic(e) + } else { + var ( + cu float64 + index int + ) + + sdata := bytes.Split(data, []byte{'\n'}) + + s.Interface().Pull_tag(map[string]func(any) (disable bool){ + `recv`: func(a any) (disable bool) { + if d, ok := a.(Uinterface); ok { + switch string(d.Data) { + case "pause": + timer.Stop() + case "play": + timer.Reset(time.Second) + default: + tmp, _ := strconv.ParseFloat(string(d.Data), 64) + if tmp < cu { + for index > 0 && index < len(sdata) { + tIndex := bytes.Index(sdata[index], []byte{','}) + if d, _ := strconv.ParseFloat(string(sdata[index][:tIndex]), 64); d > cu { + index -= 1 + continue + } else if d < cu { + break + } + } + } + cu = tmp + } } - } - return false - }, - }) - - for sg.Islive() { - <-timer.C - cu += 1 + return false + }, + }) for sg.Islive() { - if data == nil { - if data, err = f.ReadUntil('\n', perReadSize, maxReadSize); err != nil && !errors.Is(err, io.EOF) { - panic(err) - } - } - if len(data) != 0 { - tIndex := bytes.Index(data, []byte{','}) - if d, _ := strconv.ParseFloat(string(data[:tIndex]), 64); d > cu { + <-timer.C + cu += 1 + + for index > 0 && index < len(sdata) { + tIndex := bytes.Index(sdata[index], []byte{','}) + if d, _ := strconv.ParseFloat(string(sdata[index][:tIndex]), 64); d > cu+1 { break - } else if d-cu > 2 { + } else if d < cu { + index += 1 continue } - danmuIndex := tIndex + bytes.Index(data[tIndex+2:], []byte{','}) + 3 + index += 1 + + danmuIndex := tIndex + bytes.Index(sdata[index][tIndex+2:], []byte{','}) + 3 s.Interface().Push_tag(`send`, Uinterface{ Id: 0, //send to all - Data: data[danmuIndex:], + Data: sdata[index][danmuIndex:], }) - data = nil - } else { - break } - } + } } }() -- 2.39.2