connection.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package ray
  2. import (
  3. "io"
  4. "net"
  5. "time"
  6. "v2ray.com/core/common/buf"
  7. "v2ray.com/core/common/signal"
  8. )
  9. type ConnectionOption func(*connection)
  10. func ConnLocalAddr(addr net.Addr) ConnectionOption {
  11. return func(c *connection) {
  12. c.localAddr = addr
  13. }
  14. }
  15. func ConnRemoteAddr(addr net.Addr) ConnectionOption {
  16. return func(c *connection) {
  17. c.remoteAddr = addr
  18. }
  19. }
  20. func ConnCloseSignal(s *signal.Notifier) ConnectionOption {
  21. return func(c *connection) {
  22. c.closeSignal = s
  23. }
  24. }
  25. type connection struct {
  26. input InputStream
  27. output OutputStream
  28. closed bool
  29. localAddr net.Addr
  30. remoteAddr net.Addr
  31. closeSignal *signal.Notifier
  32. reader *buf.BufferedReader
  33. }
  34. var zeroAddr net.Addr = &net.TCPAddr{IP: []byte{0, 0, 0, 0}}
  35. // NewConnection wraps a Ray into net.Conn.
  36. func NewConnection(input InputStream, output OutputStream, options ...ConnectionOption) net.Conn {
  37. c := &connection{
  38. input: input,
  39. output: output,
  40. localAddr: zeroAddr,
  41. remoteAddr: zeroAddr,
  42. reader: buf.NewBufferedReader(input),
  43. }
  44. for _, opt := range options {
  45. opt(c)
  46. }
  47. return c
  48. }
  49. // Read implements net.Conn.Read().
  50. func (c *connection) Read(b []byte) (int, error) {
  51. if c.closed {
  52. return 0, io.EOF
  53. }
  54. return c.reader.Read(b)
  55. }
  56. // ReadMultiBuffer implements buf.Reader.
  57. func (c *connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
  58. return c.reader.ReadMultiBuffer()
  59. }
  60. // Write implements net.Conn.Write().
  61. func (c *connection) Write(b []byte) (int, error) {
  62. if c.closed {
  63. return 0, io.ErrClosedPipe
  64. }
  65. l := len(b)
  66. mb := buf.NewMultiBufferCap(l/buf.Size + 1)
  67. mb.Write(b)
  68. return l, c.output.WriteMultiBuffer(mb)
  69. }
  70. func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
  71. if c.closed {
  72. return io.ErrClosedPipe
  73. }
  74. return c.output.WriteMultiBuffer(mb)
  75. }
  76. // Close implements net.Conn.Close().
  77. func (c *connection) Close() error {
  78. c.closed = true
  79. c.output.Close()
  80. c.input.CloseError()
  81. if c.closeSignal != nil {
  82. c.closeSignal.Signal()
  83. }
  84. return nil
  85. }
  86. // LocalAddr implements net.Conn.LocalAddr().
  87. func (c *connection) LocalAddr() net.Addr {
  88. return c.localAddr
  89. }
  90. // RemoteAddr implements net.Conn.RemoteAddr().
  91. func (c *connection) RemoteAddr() net.Addr {
  92. return c.remoteAddr
  93. }
  94. // SetDeadline implements net.Conn.SetDeadline().
  95. func (c *connection) SetDeadline(t time.Time) error {
  96. return nil
  97. }
  98. // SetReadDeadline implements net.Conn.SetReadDeadline().
  99. func (c *connection) SetReadDeadline(t time.Time) error {
  100. return nil
  101. }
  102. // SetWriteDeadline implement net.Conn.SetWriteDeadline().
  103. func (c *connection) SetWriteDeadline(t time.Time) error {
  104. return nil
  105. }