1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-07-20 03:33:48 +00:00

export gzip writer & inline UniPacket

This commit is contained in:
wdvxdr
2021-04-16 16:15:14 +08:00
parent 5b44879005
commit f2de387430
6 changed files with 68 additions and 45 deletions

View File

@ -32,30 +32,25 @@ func PutBuffer(w *Writer) {
}
}
type gzipWriter struct {
w *gzip.Writer
buf *bytes.Buffer
}
var gzipPool = sync.Pool{
New: func() interface{} {
buf := new(bytes.Buffer)
w := gzip.NewWriter(buf)
return &gzipWriter{
return &GzipWriter{
w: w,
buf: buf,
}
},
}
func acquireGzipWriter() *gzipWriter {
ret := gzipPool.Get().(*gzipWriter)
func AcquireGzipWriter() *GzipWriter {
ret := gzipPool.Get().(*GzipWriter)
ret.buf.Reset()
ret.w.Reset(ret.buf)
return ret
}
func releaseGzipWriter(w *gzipWriter) {
func ReleaseGzipWriter(w *GzipWriter) {
// See https://golang.org/issue/23199
const maxSize = 1 << 16
if w.buf.Cap() < maxSize {

View File

@ -117,10 +117,8 @@ func (r *Reader) Len() int {
}
func (tlv TlvMap) Exists(key uint16) bool {
if _, ok := tlv[key]; ok {
return true
}
return false
_, ok := tlv[key]
return ok
}
// --- Network reader ---
@ -144,13 +142,13 @@ func (r *NetworkReader) ReadByte() (byte, error) {
func (r *NetworkReader) ReadBytes(len int) ([]byte, error) {
buf := make([]byte, len)
_, err := io.ReadFull(r.conn, buf)
//for i := 0; i < len; i++ {
// b, err := r.ReadByte()
// if err != nil {
// for i := 0; i < len; i++ {
// b, err := r.ReadByte()
// if err != nil {
// return nil, err
// }
// buf[i] = b
//}
// }
// buf[i] = b
// }
return buf, err
}

View File

@ -12,6 +12,23 @@ import (
"github.com/Mrs4s/MiraiGo/utils"
)
type GzipWriter struct {
w *gzip.Writer
buf *bytes.Buffer
}
func (w *GzipWriter) Write(p []byte) (int, error) {
return w.w.Write(p)
}
func (w *GzipWriter) Close() error {
return w.w.Close()
}
func (w *GzipWriter) Bytes() []byte {
return w.buf.Bytes()
}
func ZlibUncompress(src []byte) []byte {
b := bytes.NewReader(src)
var out bytes.Buffer
@ -31,11 +48,11 @@ func ZlibCompress(data []byte) []byte {
}
func GZipCompress(data []byte) []byte {
gw := acquireGzipWriter()
_, _ = gw.w.Write(data)
_ = gw.w.Close()
gw := AcquireGzipWriter()
_, _ = gw.Write(data)
_ = gw.Close()
ret := append([]byte(nil), gw.buf.Bytes()...)
releaseGzipWriter(gw)
ReleaseGzipWriter(gw)
return ret
}

View File

@ -79,20 +79,24 @@ func (w *Writer) WriteIntLvPacket(offset int, f func(writer *Writer)) {
}
func (w *Writer) WriteUniPacket(commandName string, sessionId, extraData, body []byte) {
w.WriteIntLvPacket(4, func(w *Writer) {
w.WriteString(commandName)
w.WriteUInt32(8)
w.Write(sessionId)
w1 := NewWriter()
{ // WriteIntLvPacket
w1.WriteString(commandName)
w1.WriteUInt32(8)
w1.Write(sessionId)
if len(extraData) == 0 {
w.WriteUInt32(0x04)
w1.WriteUInt32(0x04)
} else {
w.WriteUInt32(uint32(len(extraData) + 4))
w.Write(extraData)
w1.WriteUInt32(uint32(len(extraData) + 4))
w1.Write(extraData)
}
})
w.WriteIntLvPacket(4, func(w *Writer) {
w.Write(body)
})
}
data := w1.Bytes()
w.WriteUInt32(uint32(len(data) + 4))
w.Write(data)
PutBuffer(w1)
w.WriteUInt32(uint32(len(body) + 4)) // WriteIntLvPacket
w.Write(body)
}
func (w *Writer) WriteBytesShort(data []byte) {