connection.go 2.9 KB

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