optimize(link); use []byte in wait

This commit is contained in:
源文雨
2025-02-25 22:28:23 +09:00
parent c5f3864837
commit a2c442557a
2 changed files with 34 additions and 24 deletions

View File

@@ -114,7 +114,7 @@ func (m *Me) waitordispatch(index int, addr p2p.EndPoint, buf pbuf.Bytes, hasntf
atomic.StoreUint64(&m.recvtotlcnt, 0) atomic.StoreUint64(&m.recvtotlcnt, 0)
atomic.StoreInt64(&m.recvlooptime, now) atomic.StoreInt64(&m.recvlooptime, now)
} }
packet := m.wait(buf.Trans()) packet := m.wait(buf.Trans().Bytes())
if packet == nil { if packet == nil {
if index < 0 { if index < 0 {
if config.ShowDebugLog { if config.ShowDebugLog {

View File

@@ -1,16 +1,18 @@
package link package link
import ( import (
"bytes"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"hash/crc64" "hash/crc64"
"io"
"strconv" "strconv"
"github.com/fumiama/WireGold/config" "github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head" "github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/helper"
base14 "github.com/fumiama/go-base16384" base14 "github.com/fumiama/go-base16384"
"github.com/fumiama/orbyte" "github.com/fumiama/orbyte"
"github.com/fumiama/orbyte/pbuf"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -19,47 +21,55 @@ func (l *Link) Read() *orbyte.Item[head.Packet] {
return <-l.pipe return <-l.pipe
} }
func (m *Me) wait(data pbuf.Bytes) *orbyte.Item[head.Packet] { func (m *Me) wait(data []byte) *orbyte.Item[head.Packet] {
if data.Len() < head.PacketHeadLen { // not a valid packet if len(data) < head.PacketHeadLen { // not a valid packet
if config.ShowDebugLog { if config.ShowDebugLog {
logrus.Debugln("[recv] invalid data len", data.Len()) logrus.Debugln("[recv] invalid data len", len(data))
} }
return nil return nil
} }
bound := 64 bound := 64
endl := "..." endl := "..."
if data.Len() < bound { if len(data) < bound {
bound = data.Len() bound = len(data)
endl = "." endl = "."
} }
if config.ShowDebugLog { if config.ShowDebugLog {
logrus.Debugln("[recv] data bytes, len", data.Len(), "val", hex.EncodeToString(data.Bytes()[:bound]), endl) logrus.Debugln("[recv] data bytes, len", len(data), "val", hex.EncodeToString(data[:bound]), endl)
} }
if m.base14 { if m.base14 {
data = pbuf.ParseBytes(base14.Decode(data.Bytes())...) w := helper.SelectWriter()
if data.Len() < bound { _, err := io.Copy(w, base14.NewDecoder(bytes.NewReader(data)))
bound = data.Len() if err != nil { // not a valid packet
if config.ShowDebugLog {
logrus.Debugln("[recv] decode base14 err:", err)
}
return nil
}
data = w.TransBytes()
if len(data) < bound {
bound = len(data)
endl = "." endl = "."
} }
if config.ShowDebugLog { if config.ShowDebugLog {
logrus.Debugln("[recv] data b14ed, len", data.Len(), "val", hex.EncodeToString(data.Bytes()[:bound]), endl) logrus.Debugln("[recv] data b14ed, len", len(data), "val", hex.EncodeToString(data[:bound]), endl)
} }
if data.Len() < head.PacketHeadLen { // not a valid packet if len(data) < head.PacketHeadLen { // not a valid packet
if config.ShowDebugLog { if config.ShowDebugLog {
logrus.Debugln("[recv] invalid data len", data.Len()) logrus.Debugln("[recv] invalid data len", len(data))
} }
return nil return nil
} }
} }
seq, dat := m.xordec(data.Trans().Bytes()) seq, data := m.xordec(data) // inplace decoding
if len(dat) < bound { if len(data) < bound {
bound = len(dat) bound = len(data)
endl = "." endl = "."
} }
if config.ShowDebugLog { if config.ShowDebugLog {
logrus.Debugln("[recv] data xored, len", len(dat), "val", hex.EncodeToString(dat[:bound]), endl) logrus.Debugln("[recv] data xored, len", len(data), "val", hex.EncodeToString(data[:bound]), endl)
} }
header, err := head.ParsePacketHeader(dat) header, err := head.ParsePacketHeader(data)
if err != nil { // not a valid packet if err != nil { // not a valid packet
if config.ShowDebugLog { if config.ShowDebugLog {
logrus.Debugln("[recv] invalid packet header:", err) logrus.Debugln("[recv] invalid packet header:", err)
@@ -87,24 +97,24 @@ func (m *Me) wait(data pbuf.Bytes) *orbyte.Item[head.Packet] {
if config.ShowDebugLog { if config.ShowDebugLog {
logrus.Debugln( logrus.Debugln(
"[recv]", strconv.FormatUint(crc, 16), "[recv]", strconv.FormatUint(crc, 16),
len(dat), "bytes data with flag", header.Pointer().Flags, len(data), "bytes data with flag", header.Pointer().Flags,
"offset", header.Pointer().Flags.Offset(), "offset", header.Pointer().Flags.Offset(),
) )
} }
if header.Pointer().Flags.IsSingle() || header.Pointer().Flags.NoFrag() { if header.Pointer().Flags.IsSingle() || header.Pointer().Flags.NoFrag() {
ok := header.Pointer().ParseData(dat) ok := header.Pointer().ParseData(data)
if !ok { if !ok {
logrus.Errorln("[recv]", strconv.FormatUint(crc, 16), "unexpected !ok") logrus.Errorln("[recv]", strconv.FormatUint(crc, 16), "unexpected !ok")
return nil return nil
} }
if config.ShowDebugLog { if config.ShowDebugLog {
logrus.Debugln("[recv]", strconv.FormatUint(crc, 16), len(dat), "bytes full data waited") logrus.Debugln("[recv]", strconv.FormatUint(crc, 16), len(data), "bytes full data waited")
} }
return header return header
} }
crchash := crc64.New(crc64.MakeTable(crc64.ISO)) crchash := crc64.New(crc64.MakeTable(crc64.ISO))
_, _ = crchash.Write(head.Hash(data.Bytes())) _, _ = crchash.Write(head.Hash(data))
var buf [4]byte var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], seq) binary.LittleEndian.PutUint32(buf[:], seq)
_, _ = crchash.Write(buf[:]) _, _ = crchash.Write(buf[:])
@@ -116,7 +126,7 @@ func (m *Me) wait(data pbuf.Bytes) *orbyte.Item[head.Packet] {
if config.ShowDebugLog { if config.ShowDebugLog {
logrus.Debugln("[recv]", strconv.FormatUint(crc, 16), "get frag part of", strconv.FormatUint(hsh, 16), "isnew:", !got) logrus.Debugln("[recv]", strconv.FormatUint(crc, 16), "get frag part of", strconv.FormatUint(hsh, 16), "isnew:", !got)
} }
ok := h.Pointer().ParseData(dat) ok := h.Pointer().ParseData(data)
if !ok { if !ok {
if config.ShowDebugLog { if config.ShowDebugLog {
logrus.Debugln("[recv]", strconv.FormatUint(crc, 16), "wait other frag parts of", strconv.FormatUint(hsh, 16), "isnew:", !got) logrus.Debugln("[recv]", strconv.FormatUint(crc, 16), "wait other frag parts of", strconv.FormatUint(hsh, 16), "isnew:", !got)