1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-20 08:16:55 -05:00

cleanup handler functions

This commit is contained in:
Darien Raymond 2017-04-14 10:03:10 +02:00
parent f57260c358
commit 0a2547b285
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -207,11 +207,46 @@ func pipe(reader *Reader, writer buf.Writer) error {
} }
} }
func (m *Client) handleStatueKeepAlive(meta *FrameMetadata, reader *Reader) error {
if meta.Option.Has(OptionData) {
return drain(reader)
}
return nil
}
func (m *Client) handleStatusNew(meta *FrameMetadata, reader *Reader) error {
if meta.Option.Has(OptionData) {
return drain(reader)
}
return nil
}
func (m *Client) handleStatusKeep(meta *FrameMetadata, reader *Reader) error {
if !meta.Option.Has(OptionData) {
return nil
}
if s, found := m.sessionManager.Get(meta.SessionID); found {
return pipe(reader, s.output)
}
return drain(reader)
}
func (m *Client) handleStatusEnd(meta *FrameMetadata, reader *Reader) error {
if s, found := m.sessionManager.Get(meta.SessionID); found {
s.CloseDownlink()
s.output.Close()
}
if meta.Option.Has(OptionData) {
return drain(reader)
}
return nil
}
func (m *Client) fetchOutput() { 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 {
@ -219,35 +254,23 @@ L:
break break
} }
var drainData bool
switch meta.SessionStatus { switch meta.SessionStatus {
case SessionStatusKeepAlive: case SessionStatusKeepAlive:
drainData = true err = m.handleStatueKeepAlive(meta, reader)
case SessionStatusEnd: case SessionStatusEnd:
if s, found := m.sessionManager.Get(meta.SessionID); found { err = m.handleStatusEnd(meta, reader)
s.CloseDownlink()
s.output.Close()
}
drainData = true
case SessionStatusNew: case SessionStatusNew:
drainData = true err = m.handleStatusNew(meta, reader)
case SessionStatusKeep: case SessionStatusKeep:
if !meta.Option.Has(OptionData) { err = m.handleStatusKeep(meta, reader)
break default:
} log.Trace(newError("unknown status: ", meta.SessionStatus).AtWarning())
if s, found := m.sessionManager.Get(meta.SessionID); found { return
if err := pipe(reader, s.output); err != nil {
log.Trace(newError("failed to pipe data").Base(err))
break L
}
}
} }
if drainData && meta.Option.Has(OptionData) { if err != nil {
if err := drain(reader); err != nil { log.Trace(newError("failed to process data").Base(err))
log.Trace(newError("failed to drain data").Base(err)) return
break
}
} }
} }
} }
@ -300,12 +323,63 @@ func handle(ctx context.Context, s *Session, output buf.Writer) {
s.CloseDownlink() s.CloseDownlink()
} }
func (w *ServerWorker) handleStatusKeepAlive(meta *FrameMetadata, reader *Reader) error {
if meta.Option.Has(OptionData) {
return drain(reader)
}
return nil
}
func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata, reader *Reader) error {
log.Trace(newError("received request for ", meta.Target))
inboundRay, err := w.dispatcher.Dispatch(ctx, meta.Target)
if err != nil {
if meta.Option.Has(OptionData) {
drain(reader)
}
return newError("failed to dispatch request.").Base(err)
}
s := &Session{
input: inboundRay.InboundOutput(),
output: inboundRay.InboundInput(),
parent: w.sessionManager,
ID: meta.SessionID,
}
w.sessionManager.Add(s)
go handle(ctx, s, w.outboundRay.OutboundOutput())
if meta.Option.Has(OptionData) {
return pipe(reader, s.output)
}
return nil
}
func (w *ServerWorker) handleStatusKeep(meta *FrameMetadata, reader *Reader) error {
if !meta.Option.Has(OptionData) {
return nil
}
if s, found := w.sessionManager.Get(meta.SessionID); found {
return pipe(reader, s.output)
}
return drain(reader)
}
func (w *ServerWorker) handleStatusEnd(meta *FrameMetadata, reader *Reader) error {
if s, found := w.sessionManager.Get(meta.SessionID); found {
s.CloseUplink()
s.output.Close()
}
if meta.Option.Has(OptionData) {
return drain(reader)
}
return nil
}
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)
defer w.sessionManager.Close() defer w.sessionManager.Close()
L:
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -319,56 +393,23 @@ L:
return return
} }
var drainData bool
switch meta.SessionStatus { switch meta.SessionStatus {
case SessionStatusKeepAlive: case SessionStatusKeepAlive:
drainData = true err = w.handleStatusKeepAlive(meta, reader)
case SessionStatusEnd: case SessionStatusEnd:
if s, found := w.sessionManager.Get(meta.SessionID); found { err = w.handleStatusEnd(meta, reader)
s.CloseUplink()
s.output.Close()
}
drainData = true
case SessionStatusNew: case SessionStatusNew:
log.Trace(newError("received request for ", meta.Target)) err = w.handleStatusNew(ctx, meta, reader)
inboundRay, err := w.dispatcher.Dispatch(ctx, meta.Target)
if err != nil {
log.Trace(newError("failed to dispatch request.").Base(err))
drainData = true
break
}
s := &Session{
input: inboundRay.InboundOutput(),
output: inboundRay.InboundInput(),
parent: w.sessionManager,
ID: meta.SessionID,
}
w.sessionManager.Add(s)
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: case SessionStatusKeep:
if !meta.Option.Has(OptionData) { err = w.handleStatusKeep(meta, reader)
break default:
} log.Trace(newError("unknown status: ", meta.SessionStatus).AtWarning())
if s, found := w.sessionManager.Get(meta.SessionID); found { return
if err := pipe(reader, s.output); err != nil {
log.Trace(newError("failed to read data").Base(err))
break L
}
}
} }
if meta.Option.Has(OptionData) && drainData { if err != nil {
if err := drain(reader); err != nil { log.Trace(newError("failed to process data").Base(err))
log.Trace(newError("failed to drain data").Base(err)) return
break
}
} }
} }
} }