mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-27 06:10:26 +08:00
optimize(all): drop lstnq & impl. orbyte
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"runtime"
|
||||
|
||||
"github.com/klauspost/compress/zstd"
|
||||
"github.com/sirupsen/logrus"
|
||||
@@ -17,6 +18,8 @@ import (
|
||||
"github.com/fumiama/WireGold/gold/head"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
base14 "github.com/fumiama/go-base16384"
|
||||
"github.com/fumiama/orbyte"
|
||||
"github.com/fumiama/orbyte/pbuf"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -24,15 +27,19 @@ var (
|
||||
ErrTTL = errors.New("ttl exceeded")
|
||||
)
|
||||
|
||||
// WriteAndPut 向 peer 发包并将包放回缓存池
|
||||
func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
||||
defer p.Put()
|
||||
teatype := l.randkeyidx()
|
||||
sndcnt := uint16(l.incgetsndcnt())
|
||||
func randseq(i uint16) uint32 {
|
||||
var buf [4]byte
|
||||
_, _ = crand.Read(buf[:2])
|
||||
binary.BigEndian.PutUint16(buf[2:4], sndcnt)
|
||||
seq := binary.BigEndian.Uint32(buf[:])
|
||||
binary.BigEndian.PutUint16(buf[2:4], i)
|
||||
return binary.BigEndian.Uint32(buf[:])
|
||||
}
|
||||
|
||||
// WritePacket 向 peer 发包
|
||||
func (l *Link) WritePacket(p *orbyte.Item[head.Packet], istransfer bool) (n int, err error) {
|
||||
pp := p.Pointer()
|
||||
teatype := l.randkeyidx()
|
||||
sndcnt := uint16(l.incgetsndcnt())
|
||||
seq := randseq(sndcnt)
|
||||
mtu := l.mtu
|
||||
if l.mturandomrange > 0 {
|
||||
mtu -= uint16(rand.Intn(int(l.mturandomrange)))
|
||||
@@ -41,48 +48,48 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
|
||||
logrus.Debugln("[send] mtu:", mtu, ", addt:", sndcnt&0x07ff, ", key index:", teatype)
|
||||
}
|
||||
if !istransfer {
|
||||
l.encrypt(p, sndcnt, teatype)
|
||||
l.encrypt(pp, sndcnt, teatype)
|
||||
}
|
||||
delta := (int(mtu) - head.PacketHeadLen) & 0x0000fff8
|
||||
if delta <= 0 {
|
||||
logrus.Warnln("[send] reset invalid data frag len", delta, "to 8")
|
||||
delta = 8
|
||||
}
|
||||
remlen := p.BodyLen()
|
||||
remlen := pp.BodyLen()
|
||||
if remlen <= delta {
|
||||
return l.write(p, teatype, sndcnt, uint32(remlen), 0, istransfer, false, seq)
|
||||
}
|
||||
if istransfer && p.Flags.DontFrag() && remlen > delta {
|
||||
if istransfer && pp.Flags.DontFrag() && remlen > delta {
|
||||
return 0, ErrDropBigDontFragPkt
|
||||
}
|
||||
ttl := p.TTL
|
||||
ttl := pp.TTL
|
||||
totl := uint32(remlen)
|
||||
pos := 0
|
||||
packet := p.Copy()
|
||||
packet := head.ParsePacket(pp.ShallowCopy())
|
||||
for remlen > delta {
|
||||
remlen -= delta
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[send] split frag [", pos, "~", pos+delta, "], remain:", remlen)
|
||||
}
|
||||
packet.CropBody(pos, pos+delta)
|
||||
packet.Pointer().CropBody(pos, pos+delta)
|
||||
cnt, err := l.write(packet, teatype, sndcnt, totl, uint16(pos>>3), istransfer, true, seq)
|
||||
n += cnt
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
packet.TTL = ttl
|
||||
packet.Pointer().TTL = ttl
|
||||
pos += delta
|
||||
}
|
||||
packet.Put()
|
||||
if remlen > 0 {
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[send] last frag [", pos, "~", pos+remlen, "]")
|
||||
}
|
||||
p.CropBody(pos, pos+remlen)
|
||||
pp.CropBody(pos, pos+remlen)
|
||||
cnt := 0
|
||||
cnt, err = l.write(p, teatype, sndcnt, totl, uint16(pos>>3), istransfer, false, seq)
|
||||
n += cnt
|
||||
}
|
||||
runtime.KeepAlive(p)
|
||||
return n, err
|
||||
}
|
||||
|
||||
@@ -94,24 +101,27 @@ func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) {
|
||||
data := p.Body()
|
||||
if l.usezstd {
|
||||
w := helper.SelectWriter()
|
||||
defer helper.PutWriter(w)
|
||||
enc, _ := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.SpeedFastest))
|
||||
_, _ = io.Copy(enc, bytes.NewReader(data))
|
||||
enc.Close()
|
||||
data = w.Bytes()
|
||||
data = w.TransBytes()
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[send] data len after zstd:", len(data))
|
||||
}
|
||||
}
|
||||
p.SetBody(l.Encode(teatype, sndcnt&0x07ff, data), true)
|
||||
p.SetBody(l.Encode(teatype, sndcnt&0x07ff, data).Trans().Bytes())
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[send] data len after xchacha20:", p.BodyLen(), "addt:", sndcnt)
|
||||
}
|
||||
}
|
||||
|
||||
// write 向 peer 发包
|
||||
func (l *Link) write(p *head.Packet, teatype uint8, additional uint16, datasz uint32, offset uint16, istransfer, hasmore bool, seq uint32) (int, error) {
|
||||
if p.DecreaseAndGetTTL() <= 0 {
|
||||
func (l *Link) write(
|
||||
p *orbyte.Item[head.Packet], teatype uint8, additional uint16,
|
||||
datasz uint32, offset uint16, istransfer,
|
||||
hasmore bool, seq uint32,
|
||||
) (int, error) {
|
||||
if p.Pointer().DecreaseAndGetTTL() <= 0 {
|
||||
return 0, ErrTTL
|
||||
}
|
||||
if l.doublepacket {
|
||||
@@ -121,26 +131,28 @@ func (l *Link) write(p *head.Packet, teatype uint8, additional uint16, datasz ui
|
||||
}
|
||||
|
||||
// write 向 peer 发一个包
|
||||
func (l *Link) writeonce(p *head.Packet, teatype uint8, additional uint16, datasz uint32, offset uint16, istransfer, hasmore bool, seq uint32) (int, error) {
|
||||
func (l *Link) writeonce(
|
||||
p *orbyte.Item[head.Packet], teatype uint8, additional uint16,
|
||||
datasz uint32, offset uint16,
|
||||
istransfer, hasmore bool, seq uint32,
|
||||
) (int, error) {
|
||||
peerep := l.endpoint
|
||||
if helper.IsNilInterface(peerep) {
|
||||
return 0, errors.New("nil endpoint of " + p.Dst.String())
|
||||
return 0, errors.New("nil endpoint of " + p.Pointer().Dst.String())
|
||||
}
|
||||
|
||||
var d []byte
|
||||
var cl func()
|
||||
var d pbuf.Bytes
|
||||
// TODO: now all packet allow frag, adapt to DF
|
||||
if istransfer {
|
||||
d, cl = p.Marshal(nil, 0, 0, 0, offset, false, hasmore)
|
||||
d = p.Pointer().MarshalWith(nil, 0, 0, 0, offset, false, hasmore)
|
||||
} else {
|
||||
d, cl = p.Marshal(l.me.me, teatype, additional, datasz, offset, false, hasmore)
|
||||
d = p.Pointer().MarshalWith(l.me.me, teatype, additional, datasz, offset, false, hasmore)
|
||||
}
|
||||
defer cl()
|
||||
|
||||
bound := 64
|
||||
endl := "..."
|
||||
if len(d) < bound {
|
||||
bound = len(d)
|
||||
if d.Len() < bound {
|
||||
bound = d.Len()
|
||||
endl = "."
|
||||
}
|
||||
conn := l.me.conn
|
||||
@@ -148,17 +160,15 @@ func (l *Link) writeonce(p *head.Packet, teatype uint8, additional uint16, datas
|
||||
return 0, io.ErrClosedPipe
|
||||
}
|
||||
if config.ShowDebugLog {
|
||||
|
||||
logrus.Debugln("[send] write", len(d), "bytes data from ep", conn.LocalAddr(), "to", peerep, "offset", fmt.Sprintf("%04x", offset), "crc", fmt.Sprintf("%016x", p.CRC64()))
|
||||
logrus.Debugln("[send] data bytes", hex.EncodeToString(d[:bound]), endl)
|
||||
logrus.Debugln("[send] write", d.Len(), "bytes data from ep", conn.LocalAddr(), "to", peerep, "offset", fmt.Sprintf("%04x", offset), "crc", fmt.Sprintf("%016x", p.Pointer().CRC64()))
|
||||
logrus.Debugln("[send] data bytes", hex.EncodeToString(d.Bytes()[:bound]), endl)
|
||||
}
|
||||
d = l.me.xorenc(d, seq)
|
||||
d = l.me.xorenc(d.Bytes(), seq)
|
||||
if l.me.base14 {
|
||||
d = base14.Encode(d)
|
||||
d = pbuf.ParseBytes(base14.Encode(d.Bytes())...)
|
||||
}
|
||||
if config.ShowDebugLog {
|
||||
logrus.Debugln("[send] data xored", hex.EncodeToString(d[:bound]), endl)
|
||||
logrus.Debugln("[send] data xored", hex.EncodeToString(d.Bytes()[:bound]), endl)
|
||||
}
|
||||
defer helper.PutBytes(d)
|
||||
return conn.WriteToPeer(d, peerep)
|
||||
return conn.WriteToPeer(d.Trans().Bytes(), peerep)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user