mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-05 09:47:56 -05:00
refactor
This commit is contained in:
parent
ad083989aa
commit
c9f661f018
@ -211,40 +211,43 @@ func (m *Client) fetchOutput() {
|
|||||||
defer m.cancel()
|
defer m.cancel()
|
||||||
|
|
||||||
reader := NewReader(m.inboundRay.InboundOutput())
|
reader := NewReader(m.inboundRay.InboundOutput())
|
||||||
|
L:
|
||||||
for {
|
for {
|
||||||
meta, err := reader.ReadMetadata()
|
meta, err := reader.ReadMetadata()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Trace(newError("failed to read metadata").Base(err))
|
log.Trace(newError("failed to read metadata").Base(err))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if meta.SessionStatus == SessionStatusKeepAlive {
|
|
||||||
if meta.Option.Has(OptionData) {
|
var drainData bool
|
||||||
if err := drain(reader); err != nil {
|
switch meta.SessionStatus {
|
||||||
log.Trace(newError("failed to read data").Base(err))
|
case SessionStatusKeepAlive:
|
||||||
break
|
drainData = true
|
||||||
|
case SessionStatusEnd:
|
||||||
|
if s, found := m.sessionManager.Get(meta.SessionID); found {
|
||||||
|
s.CloseDownlink()
|
||||||
|
s.output.Close()
|
||||||
|
}
|
||||||
|
drainData = true
|
||||||
|
case SessionStatusNew:
|
||||||
|
drainData = true
|
||||||
|
case SessionStatusKeep:
|
||||||
|
if !meta.Option.Has(OptionData) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if s, found := m.sessionManager.Get(meta.SessionID); found {
|
||||||
|
if err := pipe(reader, s.output); err != nil {
|
||||||
|
log.Trace(newError("failed to pipe data").Base(err))
|
||||||
|
break L
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s, found := m.sessionManager.Get(meta.SessionID)
|
if drainData && meta.Option.Has(OptionData) {
|
||||||
if found && meta.SessionStatus == SessionStatusEnd {
|
if err := drain(reader); err != nil {
|
||||||
s.CloseDownlink()
|
log.Trace(newError("failed to drain data").Base(err))
|
||||||
s.output.Close()
|
break
|
||||||
}
|
}
|
||||||
if !meta.Option.Has(OptionData) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if found {
|
|
||||||
err = pipe(reader, s.output)
|
|
||||||
} else {
|
|
||||||
err = drain(reader)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Trace(newError("failed to read data").Base(err))
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -300,6 +303,7 @@ func handle(ctx context.Context, s *Session, output buf.Writer) {
|
|||||||
func (w *ServerWorker) run(ctx context.Context) {
|
func (w *ServerWorker) run(ctx context.Context) {
|
||||||
input := w.outboundRay.OutboundInput()
|
input := w.outboundRay.OutboundInput()
|
||||||
reader := NewReader(input)
|
reader := NewReader(input)
|
||||||
|
L:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@ -313,30 +317,25 @@ func (w *ServerWorker) run(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if meta.SessionStatus == SessionStatusKeepAlive {
|
var drainData bool
|
||||||
if meta.Option.Has(OptionData) {
|
switch meta.SessionStatus {
|
||||||
if err := drain(reader); err != nil {
|
case SessionStatusKeepAlive:
|
||||||
log.Trace(newError("failed to read data").Base(err))
|
drainData = true
|
||||||
break
|
case SessionStatusEnd:
|
||||||
}
|
if s, found := w.sessionManager.Get(meta.SessionID); found {
|
||||||
|
s.CloseUplink()
|
||||||
|
s.output.Close()
|
||||||
}
|
}
|
||||||
continue
|
drainData = true
|
||||||
}
|
case SessionStatusNew:
|
||||||
|
|
||||||
s, found := w.sessionManager.Get(meta.SessionID)
|
|
||||||
if found && meta.SessionStatus == SessionStatusEnd {
|
|
||||||
s.CloseUplink()
|
|
||||||
s.output.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
if meta.SessionStatus == SessionStatusNew {
|
|
||||||
log.Trace(newError("received request for ", meta.Target))
|
log.Trace(newError("received request for ", meta.Target))
|
||||||
inboundRay, err := w.dispatcher.Dispatch(ctx, meta.Target)
|
inboundRay, err := w.dispatcher.Dispatch(ctx, meta.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Trace(newError("failed to dispatch request.").Base(err))
|
log.Trace(newError("failed to dispatch request.").Base(err))
|
||||||
continue
|
drainData = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
s = &Session{
|
s := &Session{
|
||||||
input: inboundRay.InboundOutput(),
|
input: inboundRay.InboundOutput(),
|
||||||
output: inboundRay.InboundInput(),
|
output: inboundRay.InboundInput(),
|
||||||
parent: w.sessionManager,
|
parent: w.sessionManager,
|
||||||
@ -344,21 +343,30 @@ func (w *ServerWorker) run(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
w.sessionManager.Add(s)
|
w.sessionManager.Add(s)
|
||||||
go handle(ctx, s, w.outboundRay.OutboundOutput())
|
go handle(ctx, s, w.outboundRay.OutboundOutput())
|
||||||
|
if !meta.Option.Has(OptionData) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err := pipe(reader, s.output); err != nil {
|
||||||
|
log.Trace(newError("failed to read data").Base(err))
|
||||||
|
break L
|
||||||
|
}
|
||||||
|
case SessionStatusKeep:
|
||||||
|
if !meta.Option.Has(OptionData) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if s, found := w.sessionManager.Get(meta.SessionID); found {
|
||||||
|
if err := pipe(reader, s.output); err != nil {
|
||||||
|
log.Trace(newError("failed to read data").Base(err))
|
||||||
|
break L
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !meta.Option.Has(OptionData) {
|
if meta.Option.Has(OptionData) && drainData {
|
||||||
continue
|
if err := drain(reader); err != nil {
|
||||||
}
|
log.Trace(newError("failed to drain data").Base(err))
|
||||||
|
break
|
||||||
if s != nil {
|
}
|
||||||
err = pipe(reader, s.output)
|
|
||||||
} else {
|
|
||||||
err = drain(reader)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Trace(newError("failed to read data").Base(err))
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
package mux
|
|
||||||
|
|
||||||
type statusHandler func(meta *FrameMetadata) error
|
|
Loading…
Reference in New Issue
Block a user