gtcp_func_pkg.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 "time"
  8. // SendPkg sends a package containing <data> to <address> and closes the connection.
  9. // The optional parameter <option> specifies the package options for sending.
  10. func SendPkg(address string, data []byte, option ...PkgOption) error {
  11. conn, err := NewConn(address)
  12. if err != nil {
  13. return err
  14. }
  15. defer conn.Close()
  16. return conn.SendPkg(data, option...)
  17. }
  18. // SendRecvPkg sends a package containing <data> to <address>, receives the response
  19. // and closes the connection. The optional parameter <option> specifies the package options for sending.
  20. func SendRecvPkg(address string, data []byte, option ...PkgOption) ([]byte, error) {
  21. conn, err := NewConn(address)
  22. if err != nil {
  23. return nil, err
  24. }
  25. defer conn.Close()
  26. return conn.SendRecvPkg(data, option...)
  27. }
  28. // SendPkgWithTimeout sends a package containing <data> to <address> with timeout limitation
  29. // and closes the connection. The optional parameter <option> specifies the package options for sending.
  30. func SendPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) error {
  31. conn, err := NewConn(address)
  32. if err != nil {
  33. return err
  34. }
  35. defer conn.Close()
  36. return conn.SendPkgWithTimeout(data, timeout, option...)
  37. }
  38. // SendRecvPkgWithTimeout sends a package containing <data> to <address>, receives the response with timeout limitation
  39. // and closes the connection. The optional parameter <option> specifies the package options for sending.
  40. func SendRecvPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error) {
  41. conn, err := NewConn(address)
  42. if err != nil {
  43. return nil, err
  44. }
  45. defer conn.Close()
  46. return conn.SendRecvPkgWithTimeout(data, timeout, option...)
  47. }