mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 11:07:40 +08:00
client: use UploadBDH for uploading forward message
This commit is contained in:
parent
ed15fbca26
commit
f630205782
@ -1,21 +0,0 @@
|
|||||||
package highway
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/Mrs4s/MiraiGo/binary"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Addr struct {
|
|
||||||
IP uint32
|
|
||||||
Port int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a Addr) AsNetIP() net.IP {
|
|
||||||
return net.IPv4(byte(a.IP>>24), byte(a.IP>>16), byte(a.IP>>8), byte(a.IP))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a Addr) String() string {
|
|
||||||
return fmt.Sprintf("%v:%v", binary.UInt32ToIPV4Address(a.IP), a.Port)
|
|
||||||
}
|
|
@ -74,13 +74,9 @@ func uploadBDH(s *Session, addr Addr, trans *Transaction) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const chunkSize = 256 * 1024
|
const chunkSize = 256 * 1024
|
||||||
var rspExt, chunk []byte
|
var rspExt []byte
|
||||||
offset := 0
|
offset := 0
|
||||||
if trans.Size > chunkSize {
|
chunk := make([]byte, chunkSize)
|
||||||
chunk = make([]byte, chunkSize)
|
|
||||||
} else {
|
|
||||||
chunk = make([]byte, trans.Size)
|
|
||||||
}
|
|
||||||
for {
|
for {
|
||||||
chunk = chunk[:cap(chunk)]
|
chunk = chunk[:cap(chunk)]
|
||||||
rl, err := io.ReadFull(trans.Body, chunk)
|
rl, err := io.ReadFull(trans.Body, chunk)
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
@ -22,6 +21,19 @@ const (
|
|||||||
_REQ_CMD_HEART_BREAK = "PicUp.Echo"
|
_REQ_CMD_HEART_BREAK = "PicUp.Echo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Addr struct {
|
||||||
|
IP uint32
|
||||||
|
Port int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Addr) AsNetIP() net.IP {
|
||||||
|
return net.IPv4(byte(a.IP>>24), byte(a.IP>>16), byte(a.IP>>8), byte(a.IP))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Addr) String() string {
|
||||||
|
return fmt.Sprintf("%v:%v", binary.UInt32ToIPV4Address(a.IP), a.Port)
|
||||||
|
}
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
Uin string
|
Uin string
|
||||||
AppID int32
|
AppID int32
|
||||||
@ -46,56 +58,6 @@ func (s *Session) AppendAddr(ip, port uint32) {
|
|||||||
s.SsoAddr = append(s.SsoAddr, addr)
|
s.SsoAddr = append(s.SsoAddr, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) Upload(addr Addr, trans Transaction) error {
|
|
||||||
conn, err := net.DialTimeout("tcp", addr.String(), time.Second*3)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "connect error")
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
const chunkSize = 8192 * 8
|
|
||||||
chunk := make([]byte, chunkSize)
|
|
||||||
offset := 0
|
|
||||||
reader := binary.NewNetworkReader(conn)
|
|
||||||
for {
|
|
||||||
chunk = chunk[:chunkSize]
|
|
||||||
rl, err := io.ReadFull(trans.Body, chunk)
|
|
||||||
if errors.Is(err, io.EOF) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if errors.Is(err, io.ErrUnexpectedEOF) {
|
|
||||||
chunk = chunk[:rl]
|
|
||||||
}
|
|
||||||
ch := md5.Sum(chunk)
|
|
||||||
head, _ := proto.Marshal(&pb.ReqDataHighwayHead{
|
|
||||||
MsgBasehead: s.dataHighwayHead(_REQ_CMD_DATA, 4096, trans.CommandID, 2052),
|
|
||||||
MsgSeghead: &pb.SegHead{
|
|
||||||
Filesize: trans.Size,
|
|
||||||
Dataoffset: int64(offset),
|
|
||||||
Datalength: int32(rl),
|
|
||||||
Serviceticket: trans.Ticket,
|
|
||||||
Md5: ch[:],
|
|
||||||
FileMd5: trans.Sum,
|
|
||||||
},
|
|
||||||
ReqExtendinfo: []byte{},
|
|
||||||
})
|
|
||||||
offset += rl
|
|
||||||
frame := newFrame(head, chunk)
|
|
||||||
_, err = frame.WriteTo(conn)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "write conn error")
|
|
||||||
}
|
|
||||||
rspHead, err := readResponse(reader)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "highway upload error")
|
|
||||||
}
|
|
||||||
if rspHead.ErrorCode != 0 {
|
|
||||||
return errors.New("upload failed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Session) UploadExciting(trans Transaction) ([]byte, error) {
|
func (s *Session) UploadExciting(trans Transaction) ([]byte, error) {
|
||||||
return s.retry(uploadExciting, &trans)
|
return s.retry(uploadExciting, &trans)
|
||||||
}
|
}
|
||||||
|
@ -313,11 +313,9 @@ func (builder *ForwardMessageBuilder) Main(m *message.ForwardMessage) *message.F
|
|||||||
Sum: bodyHash[:],
|
Sum: bodyHash[:],
|
||||||
Size: int64(len(body)),
|
Size: int64(len(body)),
|
||||||
}
|
}
|
||||||
for i, ip := range rsp.Uint32UpIp {
|
_, err = c.highwaySession.UploadBDH(input)
|
||||||
addr := highway.Addr{IP: uint32(ip), Port: int(rsp.Uint32UpPort[i])}
|
|
||||||
err := c.highwaySession.Upload(addr, input)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
return &message.ForwardElement{
|
return &message.ForwardElement{
|
||||||
FileName: filename,
|
FileName: filename,
|
||||||
@ -325,5 +323,3 @@ func (builder *ForwardMessageBuilder) Main(m *message.ForwardMessage) *message.F
|
|||||||
ResId: rsp.MsgResid,
|
ResId: rsp.MsgResid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user