gf/net/gtcp/gtcp_func_pkg.go

54 lines
2.0 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-04-28 23:55:23 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gtcp
import "time"
// SendPkg sends a package containing <data> to <address> and closes the connection.
// The optional parameter <option> specifies the package options for sending.
func SendPkg(address string, data []byte, option ...PkgOption) error {
conn, err := NewConn(address)
2019-04-28 23:55:23 +08:00
if err != nil {
return err
}
defer conn.Close()
2019-06-03 23:53:59 +08:00
return conn.SendPkg(data, option...)
2019-04-28 23:55:23 +08:00
}
// SendRecvPkg sends a package containing <data> to <address>, receives the response
// and closes the connection. The optional parameter <option> specifies the package options for sending.
func SendRecvPkg(address string, data []byte, option ...PkgOption) ([]byte, error) {
conn, err := NewConn(address)
2019-04-28 23:55:23 +08:00
if err != nil {
return nil, err
}
defer conn.Close()
2019-06-03 23:53:59 +08:00
return conn.SendRecvPkg(data, option...)
2019-04-28 23:55:23 +08:00
}
// SendPkgWithTimeout sends a package containing <data> to <address> with timeout limitation
// and closes the connection. The optional parameter <option> specifies the package options for sending.
func SendPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) error {
conn, err := NewConn(address)
2019-04-28 23:55:23 +08:00
if err != nil {
return err
}
defer conn.Close()
2019-06-03 23:53:59 +08:00
return conn.SendPkgWithTimeout(data, timeout, option...)
2019-04-28 23:55:23 +08:00
}
// SendRecvPkgWithTimeout sends a package containing <data> to <address>, receives the response with timeout limitation
// and closes the connection. The optional parameter <option> specifies the package options for sending.
func SendRecvPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error) {
conn, err := NewConn(address)
2019-04-28 23:55:23 +08:00
if err != nil {
return nil, err
}
defer conn.Close()
2019-06-03 23:53:59 +08:00
return conn.SendRecvPkgWithTimeout(data, timeout, option...)
2019-06-19 09:06:52 +08:00
}