outboundhandler.go 1007 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package mocks
  2. import (
  3. "bytes"
  4. "github.com/v2ray/v2ray-core"
  5. "github.com/v2ray/v2ray-core/common/alloc"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. )
  8. type OutboundConnectionHandler struct {
  9. Data2Send *bytes.Buffer
  10. Data2Return []byte
  11. Destination v2net.Destination
  12. }
  13. func (handler *OutboundConnectionHandler) Dispatch(packet v2net.Packet, ray core.OutboundRay) error {
  14. input := ray.OutboundInput()
  15. output := ray.OutboundOutput()
  16. handler.Destination = packet.Destination()
  17. if packet.Chunk() != nil {
  18. handler.Data2Send.Write(packet.Chunk().Value)
  19. }
  20. go func() {
  21. for {
  22. data, open := <-input
  23. if !open {
  24. break
  25. }
  26. handler.Data2Send.Write(data.Value)
  27. data.Release()
  28. }
  29. response := alloc.NewBuffer()
  30. response.Clear()
  31. response.Append(handler.Data2Return)
  32. output <- response
  33. close(output)
  34. }()
  35. return nil
  36. }
  37. func (handler *OutboundConnectionHandler) Create(point *core.Point, config interface{}) (core.OutboundConnectionHandler, error) {
  38. return handler, nil
  39. }