github.com/gogf/gf/v2@v2.7.4/net/gtcp/gtcp_func_pkg.go (about)

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