connection.go 3.3 KB

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