Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Aug 9, 2024
1 parent fe00bed commit b6c0a56
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 41 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ require (
github.com/gin-contrib/sessions v1.0.1
github.com/gin-gonic/gin v1.10.0
github.com/go-sql-driver/mysql v1.8.1
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1
github.com/goccy/go-json v0.10.3
github.com/god-jason/bucket v0.0.4
github.com/rs/xid v1.5.0
github.com/segmentio/ksuid v1.0.4
github.com/shamaton/msgpack/v2 v2.2.0
github.com/shirou/gopsutil/v4 v4.24.7
github.com/spf13/viper v1.19.0
go.bug.st/serial v1.6.2
gopkg.in/yaml.v3 v3.0.1
xorm.io/builder v0.3.13
xorm.io/xorm v1.3.9
)
Expand All @@ -34,7 +38,6 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gorilla/context v1.1.2 // indirect
Expand Down Expand Up @@ -79,7 +82,6 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/libc v1.54.2 // indirect
modernc.org/sqlite v1.30.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1 h1:FWNFq4fM1wPfcK40yHE5UO3RUdSNPaBC+j3PokzA6OQ=
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/god-jason/bucket v0.0.4 h1:SQ6mnFjtuRjffjOtCf9CICQdMSmLFt7pkER3MG0Bg1g=
Expand Down Expand Up @@ -140,6 +142,8 @@ github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6g
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c=
github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
github.com/shamaton/msgpack/v2 v2.2.0 h1:IP1m01pHwCrMa6ZccP9B3bqxEMKMSmMVAVKk54g3L/Y=
github.com/shamaton/msgpack/v2 v2.2.0/go.mod h1:6khjYnkx73f7VQU7wjcFS9DFjs+59naVWJv1TB7qdOI=
github.com/shirou/gopsutil/v4 v4.24.7 h1:V9UGTK4gQ8HvcnPKf6Zt3XHyQq/peaekfxpJ2HSocJk=
github.com/shirou/gopsutil/v4 v4.24.7/go.mod h1:0uW/073rP7FYLOkvxolUQM5rMOLTNmRXnFKafpb71rw=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
Expand Down
171 changes: 132 additions & 39 deletions rpc/pack.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,132 @@
package rpc


const (
DISCONNECT uint8 = iota
CONNECT
CONNECT_ACK
HEARTBEAT
REQUEST
REQUEST_END
RESPONSE
RESPONSE_END
STREAM
STREAM_END
PUBLISH
PUBLISH_END
PUBLISH_ACK
SUBSCRIBE
SUBSCRIBE_ACK
UNSUBSCRIBE
)

const (
BINARY uint8 = iota
JSON
XML
YAML
CSV
MSGPACK
PROTOBUF
)


type Pack struct {
Type uint8
Encoding uint8
Id uint16
Length uint16
}
package rpc

import (
"encoding/binary"
"encoding/xml"
"errors"
"github.com/gocarina/gocsv"
"github.com/goccy/go-json"
"github.com/shamaton/msgpack/v2"
"gopkg.in/yaml.v3"
)

var ErrEncoding = errors.New("编码不支持")
var ErrNotEnough = errors.New("长度不足")

type Encoder func(any) ([]byte, error)
type Decoder func([]byte, any) error

const MAGIC = "rgc"

const (
DISCONNECT uint8 = iota
CONNECT
CONNECT_ACK
HEARTBEAT
REQUEST
REQUEST_END
RESPONSE
RESPONSE_END
STREAM
STREAM_END
PUBLISH
PUBLISH_END
PUBLISH_ACK
SUBSCRIBE
SUBSCRIBE_ACK
UNSUBSCRIBE
)

const (
BINARY uint8 = iota
JSON
XML
YAML
CSV
MSGPACK
PROTOBUF
)

var encoders = map[uint8]Encoder{
JSON: json.Marshal,
XML: xml.Marshal,
YAML: yaml.Marshal,
CSV: gocsv.MarshalBytes,
MSGPACK: msgpack.Marshal,
}

var decoders = map[uint8]Decoder{
JSON: json.Unmarshal,
XML: xml.Unmarshal,
YAML: yaml.Unmarshal,
CSV: gocsv.UnmarshalBytes,
MSGPACK: msgpack.Unmarshal,
}

func RegisterEncoding(typ uint8, encoder Encoder, decoder Decoder) {
encoders[typ] = encoder
decoders[typ] = decoder
}

type Pack struct {
Type uint8
Encoding uint8
Id uint16
Length uint16
Data []byte
Content any
}

func (p *Pack) Encode() (buf []byte, err error) {
if p.Content != nil && p.Encoding > 0 {
if encoder, ok := encoders[p.Encoding]; ok {
p.Data, err = encoder(p.Content)
} else {
err = ErrEncoding
}

if err != nil {
return
}
}

//构建包
p.Length = uint16(len(p.Data))
buf = make([]byte, 8+len(p.Data))
copy(buf, MAGIC)
buf[3] = p.Type<<4 + p.Encoding&0xF0
binary.BigEndian.PutUint16(buf[4:], p.Id)
binary.BigEndian.PutUint16(buf[6:], p.Length)
if p.Data != nil {
copy(buf[8:], p.Data) //内存复制了
}
return
}

func (p *Pack) Decode(buf []byte) (err error) {
p.Id = binary.BigEndian.Uint16(buf[4:])
p.Length = binary.BigEndian.Uint16(buf[6:])
p.Type = buf[3] >> 4
p.Encoding = buf[3] & 0xF0

if p.Length > 0 {
if len(buf) < 8+int(p.Length) {
return ErrNotEnough
}
p.Data = buf[8 : 8+p.Length]

if p.Encoding > 0 {
if decoder, ok := decoders[p.Encoding]; ok {
err = decoder(p.Data, p.Content)
} else {
err = ErrEncoding
}
} else {
p.Content = p.Data
}
if err != nil {
return
}
}
return
}

0 comments on commit b6c0a56

Please sign in to comment.