mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 11:07:40 +08:00
network: remove unnecessary sync.Pool
This commit is contained in:
parent
c515024783
commit
3555d23136
@ -105,7 +105,8 @@ func (s *Session) UploadBDH(input BdhInput) ([]byte, error) {
|
||||
ReqExtendinfo: input.Ext,
|
||||
})
|
||||
offset += rl
|
||||
_, err = io.Copy(conn, network.HeadBodyFrame(head, chunk))
|
||||
frame := network.HeadBodyFrame(head, chunk)
|
||||
_, err = frame.WriteTo(conn)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "write conn error")
|
||||
}
|
||||
@ -189,7 +190,7 @@ func (s *Session) UploadBDHMultiThread(input BdhMultiThreadInput, threadCount in
|
||||
return err
|
||||
}
|
||||
|
||||
buffer := make([]byte, blockSize)
|
||||
chunk := make([]byte, blockSize)
|
||||
for {
|
||||
nextId := atomic.AddUint32(&BlockId, 1)
|
||||
if nextId >= uint32(len(blocks)) {
|
||||
@ -203,10 +204,10 @@ func (s *Session) UploadBDHMultiThread(input BdhMultiThreadInput, threadCount in
|
||||
}
|
||||
cond.L.Unlock()
|
||||
}
|
||||
buffer = buffer[:blockSize]
|
||||
chunk = chunk[:blockSize]
|
||||
|
||||
cond.L.Lock() // lock protect reading
|
||||
n, err := input.Body.ReadAt(buffer, block.Offset)
|
||||
n, err := input.Body.ReadAt(chunk, block.Offset)
|
||||
cond.L.Unlock()
|
||||
|
||||
if err != nil {
|
||||
@ -214,12 +215,12 @@ func (s *Session) UploadBDHMultiThread(input BdhMultiThreadInput, threadCount in
|
||||
break
|
||||
}
|
||||
if err == io.ErrUnexpectedEOF {
|
||||
buffer = buffer[:n]
|
||||
chunk = chunk[:n]
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
ch := md5.Sum(buffer)
|
||||
ch := md5.Sum(chunk)
|
||||
head, _ := proto.Marshal(&pb.ReqDataHighwayHead{
|
||||
MsgBasehead: s.dataHighwayHead(4096, input.CommandID, 2052),
|
||||
MsgSeghead: &pb.SegHead{
|
||||
@ -232,7 +233,8 @@ func (s *Session) UploadBDHMultiThread(input BdhMultiThreadInput, threadCount in
|
||||
},
|
||||
ReqExtendinfo: input.Ext,
|
||||
})
|
||||
_, err = io.Copy(conn, network.HeadBodyFrame(head, buffer))
|
||||
frame := network.HeadBodyFrame(head, chunk)
|
||||
_, err = frame.WriteTo(conn)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "write conn error")
|
||||
}
|
||||
|
@ -86,7 +86,8 @@ func (s *Session) Upload(addr Addr, input Input) error {
|
||||
ReqExtendinfo: []byte{},
|
||||
})
|
||||
offset += rl
|
||||
_, err = io.Copy(conn, network.HeadBodyFrame(head, chunk))
|
||||
frame := network.HeadBodyFrame(head, chunk)
|
||||
_, err = frame.WriteTo(conn)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "write conn error")
|
||||
}
|
||||
@ -145,7 +146,8 @@ func (s *Session) UploadExciting(input ExcitingInput) ([]byte, error) {
|
||||
ReqExtendinfo: input.Ext,
|
||||
})
|
||||
offset += int64(rl)
|
||||
req, _ := http.NewRequest("POST", url, network.HeadBodyFrame(head, chunk))
|
||||
frame := network.HeadBodyFrame(head, chunk)
|
||||
req, _ := http.NewRequest("POST", url, &frame)
|
||||
req.Header.Set("Accept", "*/*")
|
||||
req.Header.Set("Connection", "Keep-Alive")
|
||||
req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
|
||||
@ -206,7 +208,8 @@ func (s *Session) sendHeartbreak(conn net.Conn) error {
|
||||
LocaleId: 2052,
|
||||
},
|
||||
})
|
||||
_, err := io.Copy(conn, network.HeadBodyFrame(head, nil))
|
||||
frame := network.HeadBodyFrame(head, nil)
|
||||
_, err := frame.WriteTo(conn)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -2,30 +2,11 @@ package network
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var etx = []byte{0x29}
|
||||
|
||||
type Buffers struct {
|
||||
net.Buffers
|
||||
}
|
||||
|
||||
var pool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
lenHead := make([]byte, 9)
|
||||
lenHead[0] = 0x28
|
||||
return &Buffers{net.Buffers{lenHead, nil, nil, etx}}
|
||||
},
|
||||
}
|
||||
|
||||
func (b *Buffers) WriteTo(w io.Writer) (n int64, err error) {
|
||||
defer pool.Put(b) // implement auto put to pool
|
||||
return b.Buffers.WriteTo(w)
|
||||
}
|
||||
|
||||
// HeadBodyFrame 包格式
|
||||
// * STX
|
||||
// * head length
|
||||
@ -34,17 +15,19 @@ func (b *Buffers) WriteTo(w io.Writer) (n int64, err error) {
|
||||
// * body data
|
||||
// * ETX
|
||||
// 节省内存, 可被go runtime优化为writev操作
|
||||
func HeadBodyFrame(head []byte, body []byte) *Buffers {
|
||||
b := pool.Get().(*Buffers)
|
||||
if len(b.Buffers) == 0 {
|
||||
lenHead := make([]byte, 9)
|
||||
lenHead[0] = 0x28
|
||||
b.Buffers = net.Buffers{lenHead, nil, nil, etx}
|
||||
}
|
||||
b.Buffers[2] = body
|
||||
b.Buffers[1] = head
|
||||
_ = b.Buffers[0][8]
|
||||
binary.BigEndian.PutUint32(b.Buffers[0][1:], uint32(len(head)))
|
||||
binary.BigEndian.PutUint32(b.Buffers[0][5:], uint32(len(body)))
|
||||
return b
|
||||
func HeadBodyFrame(head []byte, body []byte) net.Buffers {
|
||||
buffers := make(net.Buffers, 4)
|
||||
// buffer0 format:
|
||||
// * STX
|
||||
// * head length
|
||||
// * body length
|
||||
buffer0 := make([]byte, 9)
|
||||
buffer0[0] = 0x28
|
||||
binary.BigEndian.PutUint32(buffer0[1:], uint32(len(head)))
|
||||
binary.BigEndian.PutUint32(buffer0[5:], uint32(len(body)))
|
||||
buffers[0] = buffer0
|
||||
buffers[1] = head
|
||||
buffers[2] = body
|
||||
buffers[3] = etx
|
||||
return buffers
|
||||
}
|
||||
|
@ -7,8 +7,6 @@ const (
|
||||
RequestTypeSimple = 0x0B
|
||||
)
|
||||
|
||||
var emptyKey = make([]byte, 16)
|
||||
|
||||
type EncryptType uint32
|
||||
|
||||
const (
|
||||
|
@ -46,6 +46,7 @@ func (t *Transport) ReadResponse(head []byte) (*Response, error) {
|
||||
case EncryptTypeD2Key:
|
||||
body = binary.NewTeaCipher(t.Sig.D2Key).Decrypt(body)
|
||||
case EncryptTypeEmptyKey:
|
||||
emptyKey := make([]byte, 16)
|
||||
body = binary.NewTeaCipher(emptyKey).Decrypt(body)
|
||||
}
|
||||
err := t.readSSOFrame(resp, body)
|
||||
|
@ -89,6 +89,7 @@ func (t *Transport) PackPacket(req *Request) []byte {
|
||||
case EncryptTypeD2Key:
|
||||
body = binary.NewTeaCipher(t.Sig.D2Key).Encrypt(body)
|
||||
case EncryptTypeEmptyKey:
|
||||
emptyKey := make([]byte, 16)
|
||||
body = binary.NewTeaCipher(emptyKey).Encrypt(body)
|
||||
}
|
||||
w.Write(body)
|
||||
|
Loading…
x
Reference in New Issue
Block a user