connection.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package network
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. )
  7. // ConnectionReadOptions 从连接读取数据的选项
  8. type ConnectionReadOptions func(conn net.Conn) error
  9. // WithReadDeadline 读超时选项
  10. func WithReadDeadline(duration time.Duration) ConnectionReadOptions {
  11. return func(conn net.Conn) error {
  12. err := conn.SetReadDeadline(time.Now().Add(duration))
  13. if err != nil {
  14. return err
  15. }
  16. return nil
  17. }
  18. }
  19. // ConnectionWriteOptions 从连接写入数据的选项
  20. type ConnectionWriteOptions func(conn net.Conn) error
  21. // WithWriteDeadline 写超时选项
  22. func WithWriteDeadline(duration time.Duration) ConnectionWriteOptions {
  23. return func(conn net.Conn) error {
  24. err := conn.SetWriteDeadline(time.Now().Add(duration))
  25. if err != nil {
  26. return err
  27. }
  28. return nil
  29. }
  30. }
  31. // ReadUDP 读取UDP包
  32. func ReadUDP(conn *net.UDPConn, bufferSize int, opts ...ConnectionReadOptions) ([]byte, *net.UDPAddr, error) {
  33. buffer := make([]byte, bufferSize)
  34. for _, opt := range opts {
  35. err := opt(conn)
  36. if err != nil {
  37. return nil, nil, err
  38. }
  39. }
  40. n, rAddr, err := conn.ReadFromUDP(buffer)
  41. if err != nil {
  42. return nil, nil, err
  43. }
  44. return buffer[:n], rAddr, nil
  45. }
  46. // WriteUDPWithRemoteAddr 指定远端(对端)地址写出UDP包
  47. func WriteUDPWithRemoteAddr(conn *net.UDPConn, rAddr *net.UDPAddr, data []byte, opts ...ConnectionWriteOptions) error {
  48. for _, opt := range opts {
  49. err := opt(conn)
  50. if err != nil {
  51. return err
  52. }
  53. }
  54. _, err := conn.WriteToUDP(data, rAddr)
  55. if err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. // WriteUDP 写出UDP包
  61. func WriteUDP(conn *net.UDPConn, data []byte, opts ...ConnectionWriteOptions) error {
  62. for _, opt := range opts {
  63. err := opt(conn)
  64. if err != nil {
  65. return err
  66. }
  67. }
  68. _, err := conn.Write(data)
  69. if err != nil {
  70. return err
  71. }
  72. return nil
  73. }
  74. // ReadTCP 读取TCP数据
  75. func ReadTCP(conn net.Conn, bufferSize int, readCallback func(data []byte) (bool, error), opts ...ConnectionReadOptions) error {
  76. for {
  77. buffer := make([]byte, bufferSize)
  78. for _, opt := range opts {
  79. err := opt(conn)
  80. if err != nil {
  81. return err
  82. }
  83. }
  84. n, err := conn.Read(buffer)
  85. if err != nil {
  86. return err
  87. }
  88. if n != 0 {
  89. readOver, err := readCallback(buffer[:n])
  90. if err != nil {
  91. return err
  92. }
  93. if readOver {
  94. return nil
  95. }
  96. }
  97. }
  98. }
  99. // WriteTCP 写TCP数据
  100. func WriteTCP(conn net.Conn, data []byte, opts ...ConnectionWriteOptions) error {
  101. writeBytesCount := 0
  102. for {
  103. for _, opt := range opts {
  104. err := opt(conn)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. n, err := conn.Write(data[writeBytesCount:])
  110. if err != nil {
  111. return err
  112. }
  113. writeBytesCount += n
  114. if writeBytesCount < len(data) {
  115. continue
  116. }
  117. return nil
  118. }
  119. }
  120. // CloseConnection 关闭连接
  121. func CloseConnection(conn net.Conn) {
  122. err := conn.Close()
  123. if err != nil {
  124. fmt.Println("Close Connection Error:", err.Error())
  125. return
  126. }
  127. conn = nil
  128. }