1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

network,highway: move HeadBodyFrame to highway

This code only used by highway
This commit is contained in:
wdvxdr 2022-03-01 13:39:27 +08:00
parent 9884d9b0de
commit 15a746802b
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
3 changed files with 24 additions and 29 deletions

View File

@ -12,7 +12,6 @@ import (
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client/internal/network"
"github.com/Mrs4s/MiraiGo/client/pb" "github.com/Mrs4s/MiraiGo/client/pb"
"github.com/Mrs4s/MiraiGo/internal/proto" "github.com/Mrs4s/MiraiGo/internal/proto"
) )
@ -72,7 +71,7 @@ func (s *Session) UploadBDH(input BdhInput) ([]byte, error) {
} }
ch := md5.Sum(chunk) ch := md5.Sum(chunk)
head, _ := proto.Marshal(&pb.ReqDataHighwayHead{ head, _ := proto.Marshal(&pb.ReqDataHighwayHead{
MsgBasehead: s.dataHighwayHead(4096, input.CommandID, 2052), MsgBasehead: s.dataHighwayHead(_REQ_CMD_DATA, 4096, input.CommandID, 2052),
MsgSeghead: &pb.SegHead{ MsgSeghead: &pb.SegHead{
Filesize: input.Size, Filesize: input.Size,
Dataoffset: int64(offset), Dataoffset: int64(offset),
@ -84,7 +83,7 @@ func (s *Session) UploadBDH(input BdhInput) ([]byte, error) {
ReqExtendinfo: input.Ext, ReqExtendinfo: input.Ext,
}) })
offset += rl offset += rl
frame := network.HeadBodyFrame(head, chunk) frame := newFrame(head, chunk)
_, err = frame.WriteTo(conn) _, 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")
@ -178,7 +177,7 @@ func (s *Session) UploadBDHMultiThread(input BdhInput, threadCount int) ([]byte,
} }
ch := md5.Sum(chunk) ch := md5.Sum(chunk)
head, _ := proto.Marshal(&pb.ReqDataHighwayHead{ head, _ := proto.Marshal(&pb.ReqDataHighwayHead{
MsgBasehead: s.dataHighwayHead(4096, input.CommandID, 2052), MsgBasehead: s.dataHighwayHead(_REQ_CMD_DATA, 4096, input.CommandID, 2052),
MsgSeghead: &pb.SegHead{ MsgSeghead: &pb.SegHead{
Filesize: input.Size, Filesize: input.Size,
Dataoffset: off, Dataoffset: off,
@ -189,7 +188,7 @@ func (s *Session) UploadBDHMultiThread(input BdhInput, threadCount int) ([]byte,
}, },
ReqExtendinfo: input.Ext, ReqExtendinfo: input.Ext,
}) })
frame := network.HeadBodyFrame(head, chunk) frame := newFrame(head, chunk)
_, err = frame.WriteTo(conn) _, err = frame.WriteTo(conn)
if err != nil { if err != nil {
return errors.Wrap(err, "write conn error") return errors.Wrap(err, "write conn error")

View File

@ -1,4 +1,4 @@
package network package highway
import ( import (
"encoding/binary" "encoding/binary"
@ -7,15 +7,15 @@ import (
var etx = []byte{0x29} var etx = []byte{0x29}
// HeadBodyFrame 包格式 // newFrame 包格式
// * STX // * STX: 0x28(40)
// * head length // * head length
// * body length // * body length
// * head data // * head data
// * body data // * body data
// * ETX // * ETX: 0x29(41)
// 节省内存, 可被go runtime优化为writev操作 // 节省内存, 可被go runtime优化为writev操作
func HeadBodyFrame(head []byte, body []byte) net.Buffers { func newFrame(head []byte, body []byte) net.Buffers {
buffers := make(net.Buffers, 4) buffers := make(net.Buffers, 4)
// buffer0 format: // buffer0 format:
// * STX // * STX

View File

@ -12,12 +12,17 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client/internal/network"
"github.com/Mrs4s/MiraiGo/client/pb" "github.com/Mrs4s/MiraiGo/client/pb"
"github.com/Mrs4s/MiraiGo/internal/proto" "github.com/Mrs4s/MiraiGo/internal/proto"
"github.com/Mrs4s/MiraiGo/utils" "github.com/Mrs4s/MiraiGo/utils"
) )
// see com/tencent/mobileqq/highway/utils/BaseConstants.java#L120-L121
const (
_REQ_CMD_DATA = "PicUp.DataUp"
_REQ_CMD_HEART_BREAK = "PicUp.Echo"
)
type Session struct { type Session struct {
Uin string Uin string
AppID int32 AppID int32
@ -74,7 +79,7 @@ func (s *Session) Upload(addr Addr, input Input) error {
} }
ch := md5.Sum(chunk) ch := md5.Sum(chunk)
head, _ := proto.Marshal(&pb.ReqDataHighwayHead{ head, _ := proto.Marshal(&pb.ReqDataHighwayHead{
MsgBasehead: s.dataHighwayHead(4096, input.CommandID, 2052), MsgBasehead: s.dataHighwayHead(_REQ_CMD_DATA, 4096, input.CommandID, 2052),
MsgSeghead: &pb.SegHead{ MsgSeghead: &pb.SegHead{
Filesize: length, Filesize: length,
Dataoffset: int64(offset), Dataoffset: int64(offset),
@ -86,7 +91,7 @@ func (s *Session) Upload(addr Addr, input Input) error {
ReqExtendinfo: []byte{}, ReqExtendinfo: []byte{},
}) })
offset += rl offset += rl
frame := network.HeadBodyFrame(head, chunk) frame := newFrame(head, chunk)
_, err = frame.WriteTo(conn) _, err = frame.WriteTo(conn)
if err != nil { if err != nil {
return errors.Wrap(err, "write conn error") return errors.Wrap(err, "write conn error")
@ -134,7 +139,7 @@ func (s *Session) UploadExciting(input ExcitingInput) ([]byte, error) {
} }
ch := md5.Sum(chunk) ch := md5.Sum(chunk)
head, _ := proto.Marshal(&pb.ReqDataHighwayHead{ head, _ := proto.Marshal(&pb.ReqDataHighwayHead{
MsgBasehead: s.dataHighwayHead(0, input.CommandID, 0), MsgBasehead: s.dataHighwayHead(_REQ_CMD_DATA, 0, input.CommandID, 0),
MsgSeghead: &pb.SegHead{ MsgSeghead: &pb.SegHead{
Filesize: fileLength, Filesize: fileLength,
Dataoffset: offset, Dataoffset: offset,
@ -146,7 +151,7 @@ func (s *Session) UploadExciting(input ExcitingInput) ([]byte, error) {
ReqExtendinfo: input.Ext, ReqExtendinfo: input.Ext,
}) })
offset += int64(rl) offset += int64(rl)
frame := network.HeadBodyFrame(head, chunk) frame := newFrame(head, chunk)
req, _ := http.NewRequest("POST", url, &frame) 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")
@ -182,33 +187,24 @@ func (s *Session) nextSeq() int32 {
return atomic.AddInt32(&s.seq, 2) return atomic.AddInt32(&s.seq, 2)
} }
func (s *Session) dataHighwayHead(flag, cmd, locale int32) *pb.DataHighwayHead { func (s *Session) dataHighwayHead(cmd string, flag, cmdID, locale int32) *pb.DataHighwayHead {
return &pb.DataHighwayHead{ return &pb.DataHighwayHead{
Version: 1, Version: 1,
Uin: s.Uin, Uin: s.Uin,
Command: "PicUp.DataUp", Command: cmd,
Seq: s.nextSeq(), Seq: s.nextSeq(),
Appid: s.AppID, Appid: s.AppID,
Dataflag: flag, Dataflag: flag,
CommandId: cmd, CommandId: cmdID,
LocaleId: locale, LocaleId: locale,
} }
} }
func (s *Session) sendHeartbreak(conn net.Conn) error { func (s *Session) sendHeartbreak(conn net.Conn) error {
head, _ := proto.Marshal(&pb.ReqDataHighwayHead{ head, _ := proto.Marshal(&pb.ReqDataHighwayHead{
MsgBasehead: &pb.DataHighwayHead{ MsgBasehead: s.dataHighwayHead(_REQ_CMD_HEART_BREAK, 4096, 0, 2052),
Version: 1,
Uin: s.Uin,
Command: "PicUp.Echo",
Seq: s.nextSeq(),
Appid: s.AppID,
Dataflag: 4096,
CommandId: 0,
LocaleId: 2052,
},
}) })
frame := network.HeadBodyFrame(head, nil) frame := newFrame(head, nil)
_, err := frame.WriteTo(conn) _, err := frame.WriteTo(conn)
return err return err
} }