ray.go 1.7 KB

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