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