1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-23 03:50:32 +08:00

optimize(all): drop lstnq & impl. orbyte

This commit is contained in:
源文雨
2025-02-25 19:38:16 +09:00
parent 4b60801a0f
commit 9f36504635
22 changed files with 501 additions and 573 deletions

View File

@@ -1,14 +1,18 @@
package tcp
import (
"bytes"
"encoding/binary"
"errors"
"io"
"net"
"runtime"
"time"
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/helper"
"github.com/fumiama/orbyte"
"github.com/fumiama/orbyte/pbuf"
"github.com/sirupsen/logrus"
)
@@ -33,17 +37,19 @@ var (
type packet struct {
typ packetType
len uint16
dat []byte
dat pbuf.Bytes
io.ReaderFrom
io.WriterTo
}
func (p *packet) pack() (net.Buffers, func()) {
d, cl := helper.OpenWriterF(func(w *helper.Writer) {
d := helper.NewWriterF(func(w *helper.Writer) {
w.WriteByte(byte(p.typ))
w.WriteUInt16(p.len)
})
return net.Buffers{magicbuf, d, p.dat}, cl
return net.Buffers{magicbuf, d.Bytes(), p.dat.Bytes()}, func() {
runtime.KeepAlive(d)
}
}
func (p *packet) Read(_ []byte) (int, error) {
@@ -81,7 +87,7 @@ func (p *packet) ReadFrom(r io.Reader) (n int64, err error) {
if err != nil {
return
}
p.dat = w.Bytes()
p.dat = pbuf.BufferItemToBytes((*orbyte.Item[bytes.Buffer])(w).Trans())
return
}

View File

@@ -14,7 +14,7 @@ import (
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/p2p"
"github.com/fumiama/WireGold/helper"
"github.com/fumiama/orbyte/pbuf"
)
type EndPoint struct {
@@ -377,9 +377,8 @@ func (conn *Conn) ReadFromPeer(b []byte) (int, p2p.EndPoint, error) {
if p.pckt.typ == packetTypeNormal {
break
}
defer helper.PutBytes(p.pckt.dat)
}
n := copy(b, p.pckt.dat)
n := copy(b, p.pckt.dat.Bytes())
return n, p.addr, nil
}
@@ -453,7 +452,7 @@ RECONNECT:
cnt, err := io.Copy(tcpconn, &packet{
typ: packetTypeNormal,
len: uint16(len(b)),
dat: b,
dat: pbuf.ParseBytes(b...),
})
if err != nil {
if subc == nil {