Skip to content

Commit

Permalink
net: fix compression
Browse files Browse the repository at this point in the history
log: add module and package name to log
  • Loading branch information
oq-x committed Sep 20, 2024
1 parent 1710644 commit 85507e3
Show file tree
Hide file tree
Showing 25 changed files with 206 additions and 205 deletions.
13 changes: 11 additions & 2 deletions commands/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/zeppelinmc/zeppelin/protocol/text"
"github.com/zeppelinmc/zeppelin/server"
"github.com/zeppelinmc/zeppelin/server/command"
"github.com/zeppelinmc/zeppelin/server/session"
"github.com/zeppelinmc/zeppelin/util"
)

Expand All @@ -14,7 +15,15 @@ var debug = command.Command{
Aliases: []string{"f3"},
Namespace: "zeppelin",
Callback: func(ccc command.CommandCallContext) {
player := ccc.Executor.Player()
s, ok := ccc.Executor.(session.Session)
if !ok {
ccc.Executor.SystemMessage(text.TextComponent{
Text: "This command should be used by a player.",
Color: "red",
})
return
}
player := s.Player()
if player == nil {
ccc.Executor.SystemMessage(text.TextComponent{
Text: "This command should be used by a player.",
Expand Down Expand Up @@ -57,7 +66,7 @@ var debug = command.Command{
name, props,
util.NormalizeYaw(yaw), pitch,
sky+block, sky, block,
ccc.Executor.ClientName(),
s.ClientName(),
))
},
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ require (
github.com/aquilax/go-perlin v1.1.0
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/sys v0.25.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
34 changes: 17 additions & 17 deletions protocol/nbt/qnbt/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func (d *Decoder) decodeListSlice(ptr unsafe.Pointer, t *ptrType) error {
tag, err := d.rd.readBytesString(1)
tag, err := d.readByte2()
if err != nil {
return err
}
Expand All @@ -19,7 +19,7 @@ func (d *Decoder) decodeListSlice(ptr unsafe.Pointer, t *ptrType) error {
return err
}

if tag == end_t_b {
if tag == end_t {
return nil
}

Expand Down Expand Up @@ -50,7 +50,7 @@ func (a *array_t) index(i int) unsafe.Pointer {
}

func (d *Decoder) decodeListArray(a *array_t) error {
tag, err := d.rd.readBytesString(1)
tag, err := d.readByte2()
if err != nil {
return err
}
Expand All @@ -60,7 +60,7 @@ func (d *Decoder) decodeListArray(a *array_t) error {
return err
}

if tag == end_t_b {
if tag == end_t {
return nil
}

Expand All @@ -71,7 +71,7 @@ func (d *Decoder) decodeListArray(a *array_t) error {
return d.readListArray(tag, l, a)
}

func (d *Decoder) readListArray(tag string, l int32, a *array_t) error {
func (d *Decoder) readListArray(tag byte, l int32, a *array_t) error {
kind := kindOf(a.elem)

for i := 0; i < int(l); i++ {
Expand All @@ -82,23 +82,23 @@ func (d *Decoder) readListArray(tag string, l int32, a *array_t) error {
//}

switch tag {
case byte_t_b:
case byte_t:
if err := d.readBytePtr(iptr); err != nil {
return err
}
case short_t_b:
case short_t:
if err := d.readShortPtr(iptr); err != nil {
return err
}
case int_t_b, float_t_b:
case int_t, float_t:
if err := d.readIntPtr(iptr); err != nil {
return err
}
case long_t_b, double_t_b:
case long_t, double_t:
if err := d.readLongPtr(iptr); err != nil {
return err
}
case byte_array_t_b:
case byte_array_t:
switch kind {
case array_k:
if err := d.readByteArray(iptr); err != nil {
Expand All @@ -116,11 +116,11 @@ func (d *Decoder) readListArray(tag string, l int32, a *array_t) error {
}
setSliceArray(iptr, ptr, int(l))
}
case string_t_b:
case string_t:
if err := d.readStringPtr(iptr); err != nil {
return err
}
case long_array_t_b:
case long_array_t:
switch kind {
case array_k:
if err := d.readLongArray(iptr); err != nil {
Expand All @@ -138,7 +138,7 @@ func (d *Decoder) readListArray(tag string, l int32, a *array_t) error {
}
setSliceArray(iptr, ptr, int(l))
}
case int_array_t_b:
case int_array_t:
switch kind {
case array_k:
if err := d.readIntArray(iptr); err != nil {
Expand All @@ -156,7 +156,7 @@ func (d *Decoder) readListArray(tag string, l int32, a *array_t) error {
}
setSliceArray(iptr, ptr, int(l))
}
case compound_t_b:
case compound_t:
switch kind {
case struct_k:
s := newStruct((*structType)(unsafe.Pointer(a.elem)), iptr)
Expand All @@ -171,22 +171,22 @@ func (d *Decoder) readListArray(tag string, l int32, a *array_t) error {
name: strconv.Itoa(i),
expectedKind: "struct/map[string]string",
gotKind: fmt.Sprintf("map[%s]%s", kind_name(kK), kind_name(kE)),
tag: tagName(tag),
//tag: tagName(tag),
}
}

m := *(*map[string]string)(iptr)

if m == nil {
m = make(map[string]string)
*(*uintptr)(iptr) = uintptr(unsafe.Pointer(&m))
*(*map[string]string)(iptr) = m
}

if err := d.decodeMapString(m); err != nil {
return err
}
}
case list_t_b:
case list_t:
switch kind {
case array_k:
arr := (*arrayType)(unsafe.Pointer(a.elem))
Expand Down
19 changes: 18 additions & 1 deletion protocol/nbt/qnbt/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (d *Decoder) readByteArray(ptr unsafe.Pointer) error {
return d.decodeByteArray(int(l), ptr)
}

func (d *Decoder) decodeByteArray(len int, ptr unsafe.Pointer) error {
/*func (d *Decoder) decodeByteArray(len int, ptr unsafe.Pointer) error {
data, err := d.rd.readBytes(len)
if err != nil {
return err
Expand All @@ -271,3 +271,20 @@ func (d *Decoder) decodeByteArray(len int, ptr unsafe.Pointer) error {
return nil
}
*/

func (d *Decoder) decodeByteArray(l int, ptr unsafe.Pointer) error {
for l > 0 {
lr := min(len(d.rd.buf), l)
data, err := d.rd.readBytes(lr)
if err != nil {
return err
}

copy(unsafe.Slice((*byte)(ptr), l), data)
l -= lr
ptr = unsafe.Add(ptr, lr)
}

return nil
}
9 changes: 4 additions & 5 deletions protocol/nbt/qnbt/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (d *Decoder) decodeStructCompound(s *struct_t) error {
return err
}

ptr := mallocgc(uintptr(l), nil, false)
ptr := mallocgc(uintptr(l), nil, true)
if err := d.decodeByteArray(int(l), ptr); err != nil {
return err
}
Expand All @@ -151,7 +151,7 @@ func (d *Decoder) decodeStructCompound(s *struct_t) error {
return err
}

ptr := mallocgc(uintptr(l)*8, nil, false)
ptr := mallocgc(uintptr(l)*8, nil, true)
if err := d.decodeLongArray(int(l), ptr); err != nil {
return err
}
Expand All @@ -169,7 +169,7 @@ func (d *Decoder) decodeStructCompound(s *struct_t) error {
return err
}

ptr := mallocgc(uintptr(l)*4, nil, false)
ptr := mallocgc(uintptr(l)*4, nil, true)
if err := d.decodeIntArray(int(l), ptr); err != nil {
return err
}
Expand Down Expand Up @@ -240,9 +240,8 @@ func (d *Decoder) decodeStructCompound(s *struct_t) error {
}
}
}
//fmt.Println(tag, name)
}

return nil
}

func match(name, tag_b string, t *unsafe2.Type) error {
Expand Down
23 changes: 8 additions & 15 deletions protocol/net/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sync/atomic"
"unicode/utf16"

"github.com/4kills/go-zlib"
"github.com/zeppelinmc/zeppelin/protocol/net/cfb8"
"github.com/zeppelinmc/zeppelin/protocol/net/io/compress"
"github.com/zeppelinmc/zeppelin/protocol/net/io/encoding"
Expand Down Expand Up @@ -154,27 +153,21 @@ func (conn *Conn) WritePacket(pk packet.Encodeable) error {
packetBuf.Bytes()[i+3] |= 0x80
}

z := compress.WZlib.Get().(*zlib.Writer)
defer compress.WZlib.Put(z)

c := pkcppool.Get().(*bytes.Buffer)
c.Reset()
defer pkcppool.Put(c)

z.Reset(c)
z.Write(packetBuf.Bytes()[6:])
z.Flush()
compressedPacket, err := compress.CompressZlib(packetBuf.Bytes()[6:], &MaxCompressedPacketSize)
if err != nil {
return err
}

compressedLength := c.Len() + 3
packetBuf.Truncate(6)
packetBuf.Write(compressedPacket)

if i := encoding.PutVarInt(packetBuf.Bytes()[:3], int32(compressedLength)); i != 2 {
if i := encoding.PutVarInt(packetBuf.Bytes()[:3], int32(packetBuf.Len())-3); i != 2 {
packetBuf.Bytes()[i] |= 0x80
}

if _, err := conn.Write(packetBuf.Bytes()[:6]); err != nil {
if _, err := packetBuf.WriteTo(conn); err != nil {
return err
}
_, err := conn.Write(c.Bytes())

return err
}
Expand Down
6 changes: 0 additions & 6 deletions protocol/net/io/buffers/buffers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,3 @@ func Size() int {

return buf.Cap()
}

func Reset() {
buf := Buffers.Get().(*bytes.Buffer)
buf.Reset()
Buffers.Put(buf)
}
2 changes: 1 addition & 1 deletion protocol/net/io/compress/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/4kills/go-zlib"
)

var Decompressors = sync.Pool{
var decompressors = sync.Pool{
New: func() any {
dc, _ := libdeflate.NewDecompressor()
return dc
Expand Down
4 changes: 2 additions & 2 deletions protocol/net/io/compress/zlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
// Decompress zlib. If decompressedLength is provided, the data returned will only be safe to use until the next operation
// It is recommmended to provide the decompressed length to avoid allocation. If you don't know it, provide a number likely bigger than the decompressed length.
func DecompressZlib(compressed []byte, decompressedLength *int) ([]byte, error) {
dc := Decompressors.Get().(libdeflate.Decompressor)
defer Decompressors.Put(dc)
dc := decompressors.Get().(libdeflate.Decompressor)
defer decompressors.Put(dc)

if decompressedLength != nil {
dst := bufs.Get().(*bytes.Buffer)
Expand Down
2 changes: 1 addition & 1 deletion server/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (a Arguments) Fallback(i int, fb string) string {

type CommandCallContext struct {
Command Command
Executor session.Session
Executor session.DummySession
Server any

Arguments Arguments
Expand Down
2 changes: 1 addition & 1 deletion server/command/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (mgr *Manager) Register(cmds ...Command) {
mgr.graph = nil
}

func (mgr *Manager) Call(command string, caller session.Session) {
func (mgr *Manager) Call(command string, caller session.DummySession) {
arguments := strings.Split(command, " ")
if len(arguments) == 0 {
caller.SystemMessage(
Expand Down
Loading

0 comments on commit 85507e3

Please sign in to comment.