mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-05 16:46:58 +08:00
初步与rtmp插件调通
This commit is contained in:
@@ -1,7 +1,55 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
"net"
|
||||
)
|
||||
|
||||
type Buffer []byte
|
||||
|
||||
func (b *Buffer) ReadN(n int) Buffer {
|
||||
l := b.Len()
|
||||
r := (*b)[:n]
|
||||
*b = (*b)[n:l]
|
||||
return r
|
||||
}
|
||||
func (b *Buffer) ReadFloat64() float64 {
|
||||
return math.Float64frombits(b.ReadUint64())
|
||||
}
|
||||
func (b *Buffer) ReadUint64() uint64 {
|
||||
return binary.BigEndian.Uint64(b.ReadN(8))
|
||||
}
|
||||
func (b *Buffer) ReadUint32() uint32 {
|
||||
return binary.BigEndian.Uint32(b.ReadN(4))
|
||||
}
|
||||
func (b *Buffer) ReadUint24() uint32 {
|
||||
return ReadBE[uint32](b.ReadN(3))
|
||||
}
|
||||
func (b *Buffer) ReadUint16() uint16 {
|
||||
return binary.BigEndian.Uint16(b.ReadN(2))
|
||||
}
|
||||
func (b *Buffer) ReadByte() byte {
|
||||
return b.ReadN(1)[0]
|
||||
}
|
||||
func (b *Buffer) WriteFloat64(v float64) {
|
||||
PutBE(b.Malloc(8), math.Float64bits(v))
|
||||
}
|
||||
func (b *Buffer) WriteUint32(v uint32) {
|
||||
binary.BigEndian.PutUint32(b.Malloc(4), v)
|
||||
}
|
||||
func (b *Buffer) WriteUint24(v uint32) {
|
||||
PutBE(b.Malloc(3), v)
|
||||
}
|
||||
func (b *Buffer) WriteUint16(v uint16) {
|
||||
binary.BigEndian.PutUint16(b.Malloc(2), v)
|
||||
}
|
||||
func (b *Buffer) WriteUint8(v byte) {
|
||||
b.Malloc(1)[0] = v
|
||||
}
|
||||
func (b *Buffer) WriteString(a string) {
|
||||
*b = append(*b, a...)
|
||||
}
|
||||
func (b *Buffer) Write(a []byte) (n int, err error) {
|
||||
*b = append(*b, a...)
|
||||
return len(a), nil
|
||||
@@ -18,13 +66,59 @@ func (b Buffer) SubBuf(start int, length int) Buffer {
|
||||
|
||||
func (b *Buffer) Malloc(count int) Buffer {
|
||||
l := b.Len()
|
||||
if l+count > b.Cap() {
|
||||
n := make(Buffer, l+count)
|
||||
newL := l + count
|
||||
if newL > b.Cap() {
|
||||
n := make(Buffer, newL)
|
||||
copy(n, *b)
|
||||
*b = n
|
||||
} else {
|
||||
*b = b.SubBuf(0, newL)
|
||||
}
|
||||
return b.SubBuf(l, count)
|
||||
}
|
||||
func (b *Buffer) Reset() {
|
||||
*b = b.SubBuf(0, 0)
|
||||
}
|
||||
func (b *Buffer) Glow(n int) {
|
||||
l := b.Len()
|
||||
b.Malloc(n)
|
||||
*b = b.SubBuf(0, l)
|
||||
}
|
||||
|
||||
// SizeOfBuffers 计算Buffers的内容长度
|
||||
func SizeOfBuffers(buf net.Buffers) (size int) {
|
||||
for _, b := range buf {
|
||||
size += len(b)
|
||||
}
|
||||
return
|
||||
}
|
||||
func CutBuffers(buf net.Buffers, size int) {
|
||||
|
||||
}
|
||||
// SplitBuffers 按照一定大小分割 Buffers
|
||||
func SplitBuffers(buf net.Buffers, size int) (result []net.Buffers) {
|
||||
for total := SizeOfBuffers(buf); total > 0; {
|
||||
if total <= size {
|
||||
return append(result, buf)
|
||||
} else {
|
||||
var before net.Buffers
|
||||
sizeOfBefore := 0
|
||||
for _, b := range buf {
|
||||
need := size - sizeOfBefore
|
||||
if lenOfB := len(b); lenOfB > need {
|
||||
before = append(before, b[:need])
|
||||
result = append(result, before)
|
||||
total -= need
|
||||
buf[0] = b[need:]
|
||||
break
|
||||
} else {
|
||||
sizeOfBefore += lenOfB
|
||||
before = append(before, b)
|
||||
total -= lenOfB
|
||||
buf = buf[1:]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user