connection.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 ConnectionOutputMultiUDP(reader buf.Reader) ConnectionOption {
  42. return func(c *connection) {
  43. c.reader = &buf.BufferedReader{
  44. Reader: reader,
  45. Spliter: buf.SplitFirstBytes,
  46. }
  47. }
  48. }
  49. func ConnectionOnClose(n io.Closer) ConnectionOption {
  50. return func(c *connection) {
  51. c.onClose = n
  52. }
  53. }
  54. func NewConnection(opts ...ConnectionOption) net.Conn {
  55. c := &connection{
  56. done: done.New(),
  57. local: &net.TCPAddr{
  58. IP: []byte{0, 0, 0, 0},
  59. Port: 0,
  60. },
  61. remote: &net.TCPAddr{
  62. IP: []byte{0, 0, 0, 0},
  63. Port: 0,
  64. },
  65. }
  66. for _, opt := range opts {
  67. opt(c)
  68. }
  69. return c
  70. }
  71. type connection struct {
  72. reader *buf.BufferedReader
  73. writer buf.Writer
  74. done *done.Instance
  75. onClose io.Closer
  76. local Addr
  77. remote Addr
  78. }
  79. func (c *connection) Read(b []byte) (int, error) {
  80. return c.reader.Read(b)
  81. }
  82. // ReadMultiBuffer implements buf.Reader.
  83. func (c *connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
  84. return c.reader.ReadMultiBuffer()
  85. }
  86. // Write implements net.Conn.Write().
  87. func (c *connection) Write(b []byte) (int, error) {
  88. if c.done.Done() {
  89. return 0, io.ErrClosedPipe
  90. }
  91. l := len(b)
  92. mb := make(buf.MultiBuffer, 0, l/buf.Size+1)
  93. mb = buf.MergeBytes(mb, b)
  94. return l, c.writer.WriteMultiBuffer(mb)
  95. }
  96. func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
  97. if c.done.Done() {
  98. return io.ErrClosedPipe
  99. }
  100. return c.writer.WriteMultiBuffer(mb)
  101. }
  102. // Close implements net.Conn.Close().
  103. func (c *connection) Close() error {
  104. common.Must(c.done.Close())
  105. common.Interrupt(c.reader)
  106. common.Close(c.writer)
  107. if c.onClose != nil {
  108. return c.onClose.Close()
  109. }
  110. return nil
  111. }
  112. // LocalAddr implements net.Conn.LocalAddr().
  113. func (c *connection) LocalAddr() net.Addr {
  114. return c.local
  115. }
  116. // RemoteAddr implements net.Conn.RemoteAddr().
  117. func (c *connection) RemoteAddr() net.Addr {
  118. return c.remote
  119. }
  120. // SetDeadline implements net.Conn.SetDeadline().
  121. func (c *connection) SetDeadline(t time.Time) error {
  122. return nil
  123. }
  124. // SetReadDeadline implements net.Conn.SetReadDeadline().
  125. func (c *connection) SetReadDeadline(t time.Time) error {
  126. return nil
  127. }
  128. // SetWriteDeadline implements net.Conn.SetWriteDeadline().
  129. func (c *connection) SetWriteDeadline(t time.Time) error {
  130. return nil
  131. }