connection.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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"
  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.NewBufferedReader(buf.NewReader(reader))
  34. }
  35. }
  36. func ConnectionOutputMulti(reader buf.Reader) ConnectionOption {
  37. return func(c *connection) {
  38. c.reader = buf.NewBufferedReader(reader)
  39. }
  40. }
  41. func ConnectionOnClose(s *signal.Notifier) ConnectionOption {
  42. return func(c *connection) {
  43. c.onClose = s
  44. }
  45. }
  46. func NewConnection(opts ...ConnectionOption) net.Conn {
  47. c := &connection{
  48. done: signal.NewDone(),
  49. }
  50. for _, opt := range opts {
  51. opt(c)
  52. }
  53. return c
  54. }
  55. type connection struct {
  56. reader *buf.BufferedReader
  57. writer buf.Writer
  58. done *signal.Done
  59. onClose *signal.Notifier
  60. local Addr
  61. remote Addr
  62. }
  63. func (c *connection) Read(b []byte) (int, error) {
  64. return c.reader.Read(b)
  65. }
  66. // ReadMultiBuffer implements buf.Reader.
  67. func (c *connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
  68. return c.reader.ReadMultiBuffer()
  69. }
  70. // Write implements net.Conn.Write().
  71. func (c *connection) Write(b []byte) (int, error) {
  72. if c.done.Done() {
  73. return 0, io.ErrClosedPipe
  74. }
  75. l := len(b)
  76. mb := buf.NewMultiBufferCap(int32(l)/buf.Size + 1)
  77. common.Must2(mb.Write(b))
  78. return l, c.writer.WriteMultiBuffer(mb)
  79. }
  80. func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
  81. if c.done.Done() {
  82. return io.ErrClosedPipe
  83. }
  84. return c.writer.WriteMultiBuffer(mb)
  85. }
  86. // Close implements net.Conn.Close().
  87. func (c *connection) Close() error {
  88. common.Must(c.done.Close())
  89. common.Close(c.reader)
  90. common.Close(c.writer)
  91. if c.onClose != nil {
  92. c.onClose.Signal()
  93. }
  94. return nil
  95. }
  96. // LocalAddr implements net.Conn.LocalAddr().
  97. func (c *connection) LocalAddr() net.Addr {
  98. return c.local
  99. }
  100. // RemoteAddr implements net.Conn.RemoteAddr().
  101. func (c *connection) RemoteAddr() net.Addr {
  102. return c.remote
  103. }
  104. // SetDeadline implements net.Conn.SetDeadline().
  105. func (c *connection) SetDeadline(t time.Time) error {
  106. return nil
  107. }
  108. // SetReadDeadline implements net.Conn.SetReadDeadline().
  109. func (c *connection) SetReadDeadline(t time.Time) error {
  110. return nil
  111. }
  112. // SetWriteDeadline implements net.Conn.SetWriteDeadline().
  113. func (c *connection) SetWriteDeadline(t time.Time) error {
  114. return nil
  115. }