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>
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package multiplex
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
"github.com/xtls/xray-core/common/signal"
|
|
"github.com/xtls/xray-core/transport"
|
|
)
|
|
|
|
type responseReader struct {
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
activity *signal.ActivityTimer
|
|
open func() (buf.Reader, error)
|
|
body buf.Reader
|
|
terminal error
|
|
}
|
|
|
|
func (r *responseReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|
if err := r.ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if r.open != nil {
|
|
r.body, r.terminal = r.open()
|
|
r.open = nil
|
|
}
|
|
for r.terminal == nil {
|
|
data, err := r.body.ReadMultiBuffer()
|
|
r.terminal = err
|
|
if err := r.ctx.Err(); err != nil {
|
|
buf.ReleaseMulti(data)
|
|
return nil, err
|
|
}
|
|
if !data.IsEmpty() {
|
|
r.activity.Update()
|
|
return data, nil
|
|
}
|
|
buf.ReleaseMulti(data)
|
|
}
|
|
return nil, r.terminal
|
|
}
|
|
|
|
func (r *responseReader) Interrupt() {
|
|
r.cancel()
|
|
r.activity.SetTimeout(0)
|
|
}
|
|
|
|
func OpenLink(ctx context.Context, cancel context.CancelFunc, writer buf.Writer, activity *signal.ActivityTimer, openResponse func() (buf.Reader, error)) (*transport.Link, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
cancel()
|
|
activity.SetTimeout(0)
|
|
return nil, err
|
|
}
|
|
return &transport.Link{Reader: &responseReader{ctx: ctx, cancel: cancel, activity: activity, open: openResponse}, Writer: writer}, nil
|
|
}
|