From 7f2e039034ff3ce548bec3ba057a19c7dd6a7757 Mon Sep 17 00:00:00 2001 From: qydysky Date: Tue, 8 Aug 2023 23:14:16 +0800 Subject: [PATCH] =?utf8?q?Improve=20=E4=BC=98=E5=8C=96=E5=86=85=E5=AD=98?= =?utf8?q?=E5=88=86=E9=85=8D=E5=8F=8A=E5=8D=8F=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- F/B_I.go | 165 +++++++++++++++++++++++++++--------------------- F/B_I_test.go | 6 ++ Reply/stream.go | 9 +-- 3 files changed, 102 insertions(+), 78 deletions(-) diff --git a/F/B_I.go b/F/B_I.go index ca3e283..690edcd 100644 --- a/F/B_I.go +++ b/F/B_I.go @@ -1,111 +1,134 @@ package F -import ( - "bytes" - "encoding/binary" - - p "github.com/qydysky/part" -) - /* 整数 字节转换区 64 8字节 32 4字节 16 2字节 */ -func Itob64(num int64) []byte { - var buffer bytes.Buffer - err := binary.Write(&buffer, binary.BigEndian, num) - if err != nil { - p.Logf().E(err) - } - return buffer.Bytes() +func Itob64(v int64) []byte { + //binary.BigEndian.PutUint64 + b := make([]byte, 8) + b[0] = byte(v >> 56) + b[1] = byte(v >> 48) + b[2] = byte(v >> 40) + b[3] = byte(v >> 32) + b[4] = byte(v >> 24) + b[5] = byte(v >> 16) + b[6] = byte(v >> 8) + b[7] = byte(v) + return b } -func Itob32(num int32) []byte { - var buffer bytes.Buffer - err := binary.Write(&buffer, binary.BigEndian, num) - if err != nil { - p.Logf().E(err) - } - return buffer.Bytes() +func Itob32(v int32) []byte { + //binary.BigEndian.PutUint32 + b := make([]byte, 4) + b[0] = byte(v >> 24) + b[1] = byte(v >> 16) + b[2] = byte(v >> 8) + b[3] = byte(v) + return b } -func Itob16(num int16) []byte { - var buffer bytes.Buffer - err := binary.Write(&buffer, binary.BigEndian, num) - if err != nil { - p.Logf().E(err) - } - return buffer.Bytes() -} - -func Btoi64(b []byte, offset int) int64 { - for len(b) < 8 { - b = append([]byte{0x00}, b...) - } - return btoi64(b[offset : offset+8]) +func Itob16(v int16) []byte { + //binary.BigEndian.PutUint16 + b := make([]byte, 2) + b[0] = byte(v >> 8) + b[1] = byte(v) + return b } func Btoi(b []byte, offset int, size int) int64 { if size > 8 { panic("最大8位") } - var buf = b[offset : offset+size] - for len(buf) < 8 { - buf = append([]byte{0x00}, buf...) + + bu := make([]byte, 8) + l := len(b) - offset + if l > size { + l = size + } + for i := 0; i < size && i < l; i++ { + bu[i+8-size] = b[offset+i] } - return btoi64(buf) + + //binary.BigEndian.Uint64 + return int64(uint64(bu[7]) | uint64(bu[6])<<8 | uint64(bu[5])<<16 | uint64(bu[4])<<24 | + uint64(bu[3])<<32 | uint64(bu[2])<<40 | uint64(bu[1])<<48 | uint64(bu[0])<<56) } func Btoui32(b []byte, offset int) uint32 { - return btoui32(b[offset : offset+4]) + s := 4 + bu := make([]byte, s) + l := len(b) - offset + if l > s { + l = s + } + for i := 0; i < s && i < l; i++ { + bu[i+s-l] = b[offset+i] + } + + //binary.BigEndian.Uint32 + return uint32(bu[3]) | uint32(bu[2])<<8 | uint32(bu[1])<<16 | uint32(bu[0])<<24 } func Btoi32(b []byte, offset int) int32 { - for len(b) < 4 { - b = append([]byte{0x00}, b...) + s := 4 + bu := make([]byte, s) + l := len(b) - offset + if l > s { + l = s } - return int32(btoui32(b[offset : offset+4])) + for i := 0; i < s && i < l; i++ { + bu[i+s-l] = b[offset+i] + } + + //binary.BigEndian.Uint32 + return int32((uint32(bu[3]) | uint32(bu[2])<<8 | uint32(bu[1])<<16 | uint32(bu[0])<<24)) } func Btoui16(b []byte, offset int) uint16 { - for len(b) < 2 { - b = append([]byte{0x00}, b...) + s := 2 + bu := make([]byte, s) + l := len(b) - offset + if l > s { + l = s } - return btoui16(b[offset : offset+2]) + for i := 0; i < s && i < l; i++ { + bu[i+s-l] = b[offset+i] + } + + //binary.BigEndian.Uint16 + return uint16(bu[1]) | uint16(bu[0])<<8 } func Btoi16(b []byte, offset int) int16 { - for len(b) < 2 { - b = append([]byte{0x00}, b...) + s := 2 + bu := make([]byte, s) + l := len(b) - offset + if l > s { + l = s } - return int16(btoui16(b[offset : offset+2])) -} - -func btoi64(b []byte) int64 { - var buffer int64 - err := binary.Read(bytes.NewReader(b), binary.BigEndian, &buffer) - if err != nil { - p.Logf().E(err) + for i := 0; i < s && i < l; i++ { + bu[i+s-l] = b[offset+i] } - return buffer -} -func btoui32(b []byte) uint32 { - var buffer uint32 - err := binary.Read(bytes.NewReader(b), binary.BigEndian, &buffer) - if err != nil { - p.Logf().E(err) - } - return buffer + //binary.BigEndian.Uint16 + return int16(uint16(bu[1]) | uint16(bu[0])<<8) } -func btoui16(b []byte) uint16 { - var buffer uint16 - err := binary.Read(bytes.NewReader(b), binary.BigEndian, &buffer) - if err != nil { - p.Logf().E(err) +func Btoi64(b []byte, offset int) int64 { + s := 8 + bu := make([]byte, s) + l := len(b) - offset + if l > s { + l = s + } + for i := 0; i < s && i < l; i++ { + bu[i+s-l] = b[offset+i] } - return buffer + + //binary.BigEndian.Uint64 + return int64(uint64(bu[7]) | uint64(bu[6])<<8 | uint64(bu[5])<<16 | uint64(bu[4])<<24 | + uint64(bu[3])<<32 | uint64(bu[2])<<40 | uint64(bu[1])<<48 | uint64(bu[0])<<56) } diff --git a/F/B_I_test.go b/F/B_I_test.go index 06a8311..9a65a56 100644 --- a/F/B_I_test.go +++ b/F/B_I_test.go @@ -11,3 +11,9 @@ func TestBtoi32(t *testing.T) { func TestItob32(t *testing.T) { t.Log(Itob64(1131984000)) } + +func Test1(t *testing.T) { + if Btoi([]byte{1, 2, 3, 4, 5}, 0, 4) != int64(Btoi32([]byte{1, 2, 3, 4, 5}, 0)) { + t.Fatal() + } +} diff --git a/Reply/stream.go b/Reply/stream.go index 9f27f76..00f72c9 100644 --- a/Reply/stream.go +++ b/Reply/stream.go @@ -1463,22 +1463,18 @@ func (t *M4SStream) PusherToHttp(w http.ResponseWriter, r *http.Request, startFu } } - contextC, cancel := context.WithCancel(r.Context()) - // cancelRec := t.Stream_msg.Pull_tag_async(map[string]func([]byte) bool{ `data`: func(b []byte) bool { select { - case <-contextC.Done(): + case <-r.Context().Done(): return true default: } if len(b) == 0 { - cancel() return true } if _, err := w.Write(b); err != nil { - cancel() return true } else if flushSupport { flusher.Flush() @@ -1486,11 +1482,10 @@ func (t *M4SStream) PusherToHttp(w http.ResponseWriter, r *http.Request, startFu return false }, `close`: func(_ []byte) bool { - cancel() return true }, }) - <-contextC.Done() + <-r.Context().Done() cancelRec() if e := stopFunc(t); e != nil { -- 2.39.2