ray.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package ray
  2. import (
  3. "github.com/v2ray/v2ray-core/common/alloc"
  4. )
  5. const (
  6. bufferSize = 16
  7. )
  8. func NewRay() Ray {
  9. return &directRay{
  10. Input: make(chan *alloc.Buffer, bufferSize),
  11. Output: make(chan *alloc.Buffer, bufferSize),
  12. }
  13. }
  14. // OutboundRay is a transport interface for outbound connections.
  15. type OutboundRay interface {
  16. // OutboundInput provides a stream for the input of the outbound connection.
  17. // The outbound connection shall write all the input until it is closed.
  18. OutboundInput() <-chan *alloc.Buffer
  19. // OutboundOutput provides a stream to retrieve the response from the
  20. // outbound connection. The outbound connection shall close the channel
  21. // after all responses are receivced and put into the channel.
  22. OutboundOutput() chan<- *alloc.Buffer
  23. }
  24. // InboundRay is a transport interface for inbound connections.
  25. type InboundRay interface {
  26. // InboundInput provides a stream to retrieve the request from client.
  27. // The inbound connection shall close the channel after the entire request
  28. // is received and put into the channel.
  29. InboundInput() chan<- *alloc.Buffer
  30. // InboudBound provides a stream of data for the inbound connection to write
  31. // as response. The inbound connection shall write all the data from the
  32. // channel until it is closed.
  33. InboundOutput() <-chan *alloc.Buffer
  34. }
  35. // Ray is an internal tranport channel bewteen inbound and outbound connection.
  36. type Ray interface {
  37. InboundRay
  38. OutboundRay
  39. }
  40. type directRay struct {
  41. Input chan *alloc.Buffer
  42. Output chan *alloc.Buffer
  43. }
  44. func (ray *directRay) OutboundInput() <-chan *alloc.Buffer {
  45. return ray.Input
  46. }
  47. func (ray *directRay) OutboundOutput() chan<- *alloc.Buffer {
  48. return ray.Output
  49. }
  50. func (ray *directRay) InboundInput() chan<- *alloc.Buffer {
  51. return ray.Input
  52. }
  53. func (ray *directRay) InboundOutput() <-chan *alloc.Buffer {
  54. return ray.Output
  55. }