Skip to content

Commit

Permalink
refactor: 使用lagrangego的binary库
Browse files Browse the repository at this point in the history
  • Loading branch information
Redmomn committed Jun 28, 2024
1 parent 0589bad commit 953254d
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 373 deletions.
9 changes: 5 additions & 4 deletions coolq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"sync"
"time"

"github.com/LagrangeDev/LagrangeGo/utils/binary"

"github.com/Mrs4s/go-cqhttp/internal/mime"
"golang.org/x/image/webp"

"github.com/LagrangeDev/LagrangeGo/client/entity"
event2 "github.com/LagrangeDev/LagrangeGo/client/event"
"github.com/Mrs4s/go-cqhttp/utils"
"github.com/Mrs4s/go-cqhttp/utils/binary"

"github.com/LagrangeDev/LagrangeGo/client"
"github.com/LagrangeDev/LagrangeGo/message"
Expand Down Expand Up @@ -622,8 +623,8 @@ func formatMemberName(mem *entity.GroupMember) string {

// encodeMessageID 临时先这样, 暂时用不上
func encodeMessageID(target int64, seq int32) string {
return hex.EncodeToString(binary.NewWriterF(func(w *binary.Writer) {
w.WriteUInt64(uint64(target))
w.WriteUInt32(uint32(seq))
return hex.EncodeToString(binary.NewWriterF(func(w *binary.Builder) {
w.WriteU64(uint64(target))
w.WriteU32(uint32(seq))
}))
}
11 changes: 6 additions & 5 deletions coolq/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"path"
"strings"

"github.com/LagrangeDev/LagrangeGo/utils/binary"

"github.com/LagrangeDev/LagrangeGo/client/entity"

event2 "github.com/LagrangeDev/LagrangeGo/client/event"

"github.com/LagrangeDev/LagrangeGo/message"
"github.com/Mrs4s/go-cqhttp/utils/binary"

"github.com/LagrangeDev/LagrangeGo/client"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -544,11 +545,11 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
// i.Url = u
// }
//}
data := binary.NewWriterF(func(w *binary.Writer) {
data := binary.NewWriterF(func(w *binary.Builder) {
w.Write(i.Md5)
w.WriteUInt32(uint32(i.Size))
w.WriteString(i.ImageId)
w.WriteString(i.Url)
w.WriteU32(i.Size)
w.WritePacketString(i.ImageId, "u32", true)
w.WritePacketString(i.Url, "u32", true)
})
cache.Image.Insert(i.Md5, data)

Expand Down
3 changes: 2 additions & 1 deletion db/leveldb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package leveldb
import (
"path"

"github.com/LagrangeDev/LagrangeGo/utils/binary"

"github.com/Mrs4s/go-cqhttp/utils"
"github.com/Mrs4s/go-cqhttp/utils/binary"

"github.com/pkg/errors"
"github.com/syndtr/goleveldb/leveldb"
Expand Down
31 changes: 16 additions & 15 deletions db/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package sqlite3

import (
"github.com/Mrs4s/go-cqhttp/utils/binary"
"hash/crc64"
"os"
"path"
"strconv"
"sync"
"time"

"github.com/LagrangeDev/LagrangeGo/utils/binary"

"github.com/Mrs4s/go-cqhttp/utils"

sql "github.com/FloatTech/sqlite"
Expand Down Expand Up @@ -231,11 +232,11 @@ func (s *database) InsertGroupMessage(msg *db.StoredGroupMessage) error {
}
h := crc64.New(crc64.MakeTable(crc64.ISO))
if msg.Attribute != nil {
h.Write(binary.NewWriterF(func(w *binary.Writer) {
w.WriteUInt32(uint32(msg.Attribute.MessageSeq))
w.WriteUInt32(uint32(msg.Attribute.InternalID))
w.WriteUInt64(uint64(msg.Attribute.SenderUin))
w.WriteUInt64(uint64(msg.Attribute.Timestamp))
h.Write(binary.NewWriterF(func(w *binary.Builder) {
w.WriteU32(uint32(msg.Attribute.MessageSeq))
w.WriteU32(uint32(msg.Attribute.InternalID))
w.WriteU64(uint64(msg.Attribute.SenderUin))
w.WriteU64(uint64(msg.Attribute.Timestamp))
}))
h.Write(utils.S2B(msg.Attribute.SenderName))
id := int64(h.Sum64())
Expand Down Expand Up @@ -264,8 +265,8 @@ func (s *database) InsertGroupMessage(msg *db.StoredGroupMessage) error {
}
if msg.QuotedInfo != nil {
h.Write(utils.S2B(msg.QuotedInfo.PrevID))
h.Write(binary.NewWriterF(func(w *binary.Writer) {
w.WriteUInt32(uint32(msg.QuotedInfo.PrevGlobalID))
h.Write(binary.NewWriterF(func(w *binary.Builder) {
w.WriteU32(uint32(msg.QuotedInfo.PrevGlobalID))
}))
content, err := yaml.Marshal(&msg.QuotedInfo)
if err != nil {
Expand Down Expand Up @@ -312,11 +313,11 @@ func (s *database) InsertPrivateMessage(msg *db.StoredPrivateMessage) error {
}
h := crc64.New(crc64.MakeTable(crc64.ISO))
if msg.Attribute != nil {
h.Write(binary.NewWriterF(func(w *binary.Writer) {
w.WriteUInt32(uint32(msg.Attribute.MessageSeq))
w.WriteUInt32(uint32(msg.Attribute.InternalID))
w.WriteUInt64(uint64(msg.Attribute.SenderUin))
w.WriteUInt64(uint64(msg.Attribute.Timestamp))
h.Write(binary.NewWriterF(func(w *binary.Builder) {
w.WriteU32(uint32(msg.Attribute.MessageSeq))
w.WriteU32(uint32(msg.Attribute.InternalID))
w.WriteU64(uint64(msg.Attribute.SenderUin))
w.WriteU64(uint64(msg.Attribute.Timestamp))
}))
h.Write(utils.S2B(msg.Attribute.SenderName))
id := int64(h.Sum64())
Expand Down Expand Up @@ -345,8 +346,8 @@ func (s *database) InsertPrivateMessage(msg *db.StoredPrivateMessage) error {
}
if msg.QuotedInfo != nil {
h.Write(utils.S2B(msg.QuotedInfo.PrevID))
h.Write(binary.NewWriterF(func(w *binary.Writer) {
w.WriteUInt32(uint32(msg.QuotedInfo.PrevGlobalID))
h.Write(binary.NewWriterF(func(w *binary.Builder) {
w.WriteU32(uint32(msg.QuotedInfo.PrevGlobalID))
}))
content, err := yaml.Marshal(&msg.QuotedInfo)
if err != nil {
Expand Down
13 changes: 10 additions & 3 deletions global/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ package global
import (
"bytes"

"github.com/Mrs4s/go-cqhttp/utils/binary"
"github.com/LagrangeDev/LagrangeGo/utils/binary"
"github.com/RomiChan/syncx"
)

var bufferTable syncx.Map[*bytes.Buffer, *binary.Builder]

// NewBuffer 从池中获取新 bytes.Buffer
func NewBuffer() *bytes.Buffer {
return (*bytes.Buffer)(binary.SelectWriter())
builder := binary.SelectBuilder(nil)
bufferTable.Store(builder.Buffer(), builder)
return builder.Buffer()
}

// PutBuffer 将 Buffer放入池中
func PutBuffer(buf *bytes.Buffer) {
binary.PutWriter((*binary.Writer)(buf))
if v, ok := bufferTable.LoadAndDelete(buf); ok {
binary.PutBuilder(v)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/FloatTech/sqlite v1.6.3
github.com/LagrangeDev/LagrangeGo v0.0.0-20240627014402-be848f169052
github.com/LagrangeDev/LagrangeGo v0.0.0-20240628105048-21b1ba6e0035
github.com/Microsoft/go-winio v0.6.2-0.20230724192519-b29bbd58a65a
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ github.com/LagrangeDev/LagrangeGo v0.0.0-20240620044204-f5d774dbcde9 h1:0aqt14kc
github.com/LagrangeDev/LagrangeGo v0.0.0-20240620044204-f5d774dbcde9/go.mod h1:bXBGpyljx81ux9ItumiMB67xJGGpe2Sb5CTUFFeTR+Y=
github.com/LagrangeDev/LagrangeGo v0.0.0-20240627014402-be848f169052 h1:z5BAKvET+LeWpBmarOVa/cN23j6sM4c98Xve7zIGXzc=
github.com/LagrangeDev/LagrangeGo v0.0.0-20240627014402-be848f169052/go.mod h1:bXBGpyljx81ux9ItumiMB67xJGGpe2Sb5CTUFFeTR+Y=
github.com/LagrangeDev/LagrangeGo v0.0.0-20240628102308-7e959294274a h1:2P9ItthtlI+hZ6Fj033dBurh6SpCdILa2MMFbH3kuLs=
github.com/LagrangeDev/LagrangeGo v0.0.0-20240628102308-7e959294274a/go.mod h1:bXBGpyljx81ux9ItumiMB67xJGGpe2Sb5CTUFFeTR+Y=
github.com/LagrangeDev/LagrangeGo v0.0.0-20240628105048-21b1ba6e0035 h1:CEFp6oupY5IIs+15X1cWnKt/4AN5IYXsqAHpwVUUSMI=
github.com/LagrangeDev/LagrangeGo v0.0.0-20240628105048-21b1ba6e0035/go.mod h1:bXBGpyljx81ux9ItumiMB67xJGGpe2Sb5CTUFFeTR+Y=
github.com/Microsoft/go-winio v0.6.2-0.20230724192519-b29bbd58a65a h1:aU1703IHxupjzipvhu16qYKLMR03e+8WuNR+JMsKfGU=
github.com/Microsoft/go-winio v0.6.2-0.20230724192519-b29bbd58a65a/go.mod h1:OZqLNXdYJHmx7aqq/T6wAdFEdoGm5nmIfC4kU7M8P8o=
github.com/RomiChan/protobuf v0.1.1-0.20230204044148-2ed269a2e54d h1:/Xuj3fIiMY2ls1TwvPKmaqQrtJsPY+c9s+0lOScVHd8=
Expand Down
8 changes: 3 additions & 5 deletions internal/msg/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
package msg

import (
"bytes"

"github.com/Mrs4s/go-cqhttp/utils/binary"
"github.com/LagrangeDev/LagrangeGo/utils/binary"

"strings"
"unicode/utf8"
Expand Down Expand Up @@ -137,8 +135,8 @@ func (e *Element) WriteCQCodeTo(sb *strings.Builder) {

// MarshalJSON see encoding/json.Marshaler
func (e *Element) MarshalJSON() ([]byte, error) {
return binary.NewWriterF(func(w *binary.Writer) {
buf := (*bytes.Buffer)(w)
return binary.NewWriterF(func(w *binary.Builder) {
buf := w.Buffer()
// fmt.Fprintf(buf, `{"type":"%s","data":{`, e.Type)
buf.WriteString(`{"type":"`)
buf.WriteString(e.Type)
Expand Down
93 changes: 0 additions & 93 deletions utils/binary/pool.go

This file was deleted.

Loading

0 comments on commit 953254d

Please sign in to comment.