connection.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package net
  2. import (
  3. "io"
  4. "net"
  5. "time"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/buf"
  8. "v2ray.com/core/common/signal/done"
  9. )
  10. type ConnectionOption func(*connection)
  11. func ConnectionLocalAddr(a net.Addr) ConnectionOption {
  12. return func(c *connection) {
  13. c.local = a
  14. }
  15. }
  16. func ConnectionRemoteAddr(a net.Addr) ConnectionOption {
  17. return func(c *connection) {
  18. c.remote = a
  19. }
  20. }
  21. func ConnectionInput(writer io.Writer) ConnectionOption {
  22. return func(c *connection) {
  23. c.writer = buf.NewWriter(writer)
  24. }
  25. }
  26. func ConnectionInputMulti(writer buf.Writer) ConnectionOption {
  27. return func(c *connection) {
  28. c.writer = writer
  29. }
  30. }
  31. func ConnectionOutput(reader io.Reader) ConnectionOption {
  32. return func(c *connection) {
  33. c.reader = &buf.BufferedReader{Reader: buf.NewReader(reader)}
  34. }
  35. }
  36. func ConnectionOutputMulti(reader buf.Reader) ConnectionOption {
  37. return func(c *connection) {
  38. c.reader = &buf.BufferedReader{Reader: reader}
  39. }
  40. }
  41. func ConnectionOnClose(n io.Closer) ConnectionOption {
  42. return func(c *connection) {
  43. c.onClose = n
  44. }
  45. }
  46. func NewConnection(opts ...ConnectionOption) net.Conn {
  47. c := &connection{
  48. done: done.New(),
  49. local: &net.TCPAddr{
  50. IP: []byte{0, 0, 0, 0},
  51. Port: 0,
  52. },
  53. remote: &net.TCPAddr{
  54. IP: []byte{0, 0, 0, 0},
  55. Port: 0,
  56. },
  57. }
  58. for _, opt := range opts {
  59. opt(c)
  60. }
  61. return c
  62. }
  63. type connection struct {
  64. reader *buf.BufferedReader
  65. writer buf.Writer
  66. done *done.Instance
  67. onClose io.Closer
  68. local Addr
  69. remote Addr
  70. }
  71. func (c *connection) Read(b []byte) (int, error) {
  72. return c.reader.Read(b)
  73. }
  74. // ReadMultiBuffer implements buf.Reader.
  75. func (c *connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
  76. return c.reader.ReadMultiBuffer()
  77. }
  78. // Write implements net.Conn.Write().
  79. func (c *connection) Write(b []byte) (int, error) {
  80. if c.done.Done() {
  81. return 0, io.ErrClosedPipe
  82. }
  83. l := len(b)
  84. mb := buf.NewMultiBufferCap(int32(l)/buf.Size + 1)
  85. common.Must2(mb.Write(b))
  86. return l, c.writer.WriteMultiBuffer(mb)
  87. }
  88. func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
  89. if c.done.Done() {
  90. return io.ErrClosedPipe
  91. }
  92. return c.writer.WriteMultiBuffer(mb)
  93. }
  94. // Close implements net.Conn.Close().
  95. func (c *connection) Close() error {
  96. common.Must(c.done.Close())
  97. common.Close(c.reader)
  98. common.Close(c.writer)
  99. if c.onClose != nil {
  100. return c.onClose.Close()
  101. }
  102. return nil
  103. }
  104. // LocalAddr implements net.Conn.LocalAddr().
  105. func (c *connection) LocalAddr() net.Addr {
  106. return c.local
  107. }
  108. // RemoteAddr implements net.Conn.RemoteAddr().
  109. func (c *connection) RemoteAddr() net.Addr {
  110. return c.remote
  111. }
  112. // SetDeadline implements net.Conn.SetDeadline().
  113. func (c *connection) SetDeadline(t time.Time) error {
  114. return nil
  115. }
  116. // SetReadDeadline implements net.Conn.SetReadDeadline().
  117. func (c *connection) SetReadDeadline(t time.Time) error {
  118. return nil
  119. }
  120. // SetWriteDeadline implements net.Conn.SetWriteDeadline().
  121. func (c *connection) SetWriteDeadline(t time.Time) error {
  122. return nil
  123. }