mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-25 11:01:44 +00:00
https://github.com/XTLS/Xray-core/pull/6748#issuecomment-5752780799 https://github.com/XTLS/Xray-core/issues/7240#issuecomment-6439951754 https://github.com/XTLS/Xray-core/pull/4231#issuecomment-6679992732 https://github.com/XTLS/Xray-core/pull/7355#issuecomment-6739539943 https://github.com/XTLS/Xray-core/pull/7359#issuecomment-6742147165 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-6784892866 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-6812285253 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-6842747639 https://github.com/XTLS/Xray-core/pull/4231#issuecomment-7012427059 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-7108550396 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-7129493942 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-7130454962 --------- Co-authored-by: Codex <codex@users.noreply.github.com> Co-authored-by: RPRX <63339210+RPRX@users.noreply.github.com>
63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package multiplex
|
|
|
|
import (
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/buf"
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/common/serial"
|
|
)
|
|
|
|
// encodeBatch moves up to four complete wire frames from raw caller custody.
|
|
// An offer without buffers contains metadata only.
|
|
func (s *Session) encodeBatch(mb *buf.MultiBuffer) (frames buf.MultiBuffer, err error) {
|
|
stream := s.target.Network != net.Network_UDP
|
|
if stream && (*mb).IsEmpty() {
|
|
*mb = buf.ReleaseMulti(*mb)
|
|
}
|
|
for n := 0; n < 4 && (s.first || len(*mb) != 0); n++ {
|
|
frame := buf.NewWithSize(1024)
|
|
frames = append(frames, frame)
|
|
meta := FrameMetadata{
|
|
SessionID: s.ID, Target: s.target, GlobalID: s.globalID,
|
|
Inbound: s.inbound, SessionStatus: SessionStatusKeep,
|
|
}
|
|
if s.first {
|
|
s.first = false
|
|
meta.SessionStatus = SessionStatusNew
|
|
}
|
|
if len(*mb) == 0 {
|
|
return frames, meta.WriteTo(frame)
|
|
}
|
|
var chunk buf.MultiBuffer
|
|
if stream {
|
|
*mb, chunk = buf.SplitSize(*mb, 8*1024)
|
|
} else {
|
|
mb2, b := buf.SplitFirst(*mb)
|
|
*mb = mb2
|
|
chunk = buf.MultiBuffer{b}
|
|
}
|
|
if len(chunk) == 1 {
|
|
frame.UDP = chunk[0].UDP
|
|
}
|
|
frames = append(frames, chunk...)
|
|
meta.Option.Set(OptionData)
|
|
if err := meta.WriteTo(frame); err != nil {
|
|
return frames, err
|
|
}
|
|
if _, err := serial.WriteUint16(frame, uint16(chunk.Len())); err != nil {
|
|
return frames, err
|
|
}
|
|
}
|
|
return frames, nil
|
|
}
|
|
|
|
func endFrame(id uint16, failed bool) buf.MultiBuffer {
|
|
meta := FrameMetadata{SessionID: id, SessionStatus: SessionStatusEnd}
|
|
if failed {
|
|
meta.Option.Set(OptionError)
|
|
}
|
|
frame := buf.NewWithSize(1024)
|
|
common.Must(meta.WriteTo(frame))
|
|
return buf.MultiBuffer{frame}
|
|
}
|