gtcp_func.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gtcp
  7. import (
  8. "crypto/rand"
  9. "crypto/tls"
  10. "net"
  11. "time"
  12. "github.com/gogf/gf/os/gfile"
  13. )
  14. const (
  15. gDEFAULT_CONN_TIMEOUT = 30 * time.Second // Default connection timeout.
  16. gDEFAULT_RETRY_INTERVAL = 100 * time.Millisecond // Default retry interval.
  17. gDEFAULT_READ_BUFFER_SIZE = 128 // (Byte) Buffer size for reading.
  18. )
  19. type Retry struct {
  20. Count int // Retry count.
  21. Interval time.Duration // Retry interval.
  22. }
  23. // NewNetConn creates and returns a net.Conn with given address like "127.0.0.1:80".
  24. // The optional parameter <timeout> specifies the timeout for dialing connection.
  25. func NewNetConn(addr string, timeout ...time.Duration) (net.Conn, error) {
  26. d := gDEFAULT_CONN_TIMEOUT
  27. if len(timeout) > 0 {
  28. d = timeout[0]
  29. }
  30. return net.DialTimeout("tcp", addr, d)
  31. }
  32. // NewNetConnTLS creates and returns a TLS net.Conn with given address like "127.0.0.1:80".
  33. // The optional parameter <timeout> specifies the timeout for dialing connection.
  34. func NewNetConnTLS(addr string, tlsConfig *tls.Config, timeout ...time.Duration) (net.Conn, error) {
  35. dialer := &net.Dialer{
  36. Timeout: gDEFAULT_CONN_TIMEOUT,
  37. }
  38. if len(timeout) > 0 {
  39. dialer.Timeout = timeout[0]
  40. }
  41. return tls.DialWithDialer(dialer, "tcp", addr, tlsConfig)
  42. }
  43. // NewNetConnKeyCrt creates and returns a TLS net.Conn with given TLS certificate and key files
  44. // and address like "127.0.0.1:80". The optional parameter <timeout> specifies the timeout for
  45. // dialing connection.
  46. func NewNetConnKeyCrt(addr, crtFile, keyFile string, timeout ...time.Duration) (net.Conn, error) {
  47. tlsConfig, err := LoadKeyCrt(crtFile, keyFile)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return NewNetConnTLS(addr, tlsConfig, timeout...)
  52. }
  53. // Send creates connection to <address>, writes <data> to the connection and then closes the connection.
  54. // The optional parameter <retry> specifies the retry policy when fails in writing data.
  55. func Send(address string, data []byte, retry ...Retry) error {
  56. conn, err := NewConn(address)
  57. if err != nil {
  58. return err
  59. }
  60. defer conn.Close()
  61. return conn.Send(data, retry...)
  62. }
  63. // SendRecv creates connection to <address>, writes <data> to the connection, receives response
  64. // and then closes the connection.
  65. //
  66. // The parameter <length> specifies the bytes count waiting to receive. It receives all buffer content
  67. // and returns if <length> is -1.
  68. //
  69. // The optional parameter <retry> specifies the retry policy when fails in writing data.
  70. func SendRecv(address string, data []byte, length int, retry ...Retry) ([]byte, error) {
  71. conn, err := NewConn(address)
  72. if err != nil {
  73. return nil, err
  74. }
  75. defer conn.Close()
  76. return conn.SendRecv(data, length, retry...)
  77. }
  78. // SendWithTimeout does Send logic with writing timeout limitation.
  79. func SendWithTimeout(address string, data []byte, timeout time.Duration, retry ...Retry) error {
  80. conn, err := NewConn(address)
  81. if err != nil {
  82. return err
  83. }
  84. defer conn.Close()
  85. return conn.SendWithTimeout(data, timeout, retry...)
  86. }
  87. // SendRecvWithTimeout does SendRecv logic with reading timeout limitation.
  88. func SendRecvWithTimeout(address string, data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error) {
  89. conn, err := NewConn(address)
  90. if err != nil {
  91. return nil, err
  92. }
  93. defer conn.Close()
  94. return conn.SendRecvWithTimeout(data, receive, timeout, retry...)
  95. }
  96. // isTimeout checks whether given <err> is a timeout error.
  97. func isTimeout(err error) bool {
  98. if err == nil {
  99. return false
  100. }
  101. if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
  102. return true
  103. }
  104. return false
  105. }
  106. // LoadKeyCrt creates and returns a TLS configuration object with given certificate and key files.
  107. func LoadKeyCrt(crtFile, keyFile string) (*tls.Config, error) {
  108. crtPath, err := gfile.Search(crtFile)
  109. if err != nil {
  110. return nil, err
  111. }
  112. keyPath, err := gfile.Search(keyFile)
  113. if err != nil {
  114. return nil, err
  115. }
  116. crt, err := tls.LoadX509KeyPair(crtPath, keyPath)
  117. if err != nil {
  118. return nil, err
  119. }
  120. tlsConfig := &tls.Config{}
  121. tlsConfig.Certificates = []tls.Certificate{crt}
  122. tlsConfig.Time = time.Now
  123. tlsConfig.Rand = rand.Reader
  124. return tlsConfig, nil
  125. }