github.com/zhongdalu/gf@v1.0.0/g/net/gtcp/gtcp_func_pkg.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/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/zhongdalu/gf.
     6  
     7  package gtcp
     8  
     9  import "time"
    10  
    11  // 简单协议: (面向短链接)发送消息包
    12  func SendPkg(addr string, data []byte, option ...PkgOption) error {
    13  	conn, err := NewConn(addr)
    14  	if err != nil {
    15  		return err
    16  	}
    17  	defer conn.Close()
    18  	return conn.SendPkg(data, option...)
    19  }
    20  
    21  // 简单协议: (面向短链接)发送数据并等待接收返回数据
    22  func SendRecvPkg(addr string, data []byte, option ...PkgOption) ([]byte, error) {
    23  	conn, err := NewConn(addr)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	defer conn.Close()
    28  	return conn.SendRecvPkg(data, option...)
    29  }
    30  
    31  // 简单协议: (面向短链接)带超时时间的数据发送
    32  func SendPkgWithTimeout(addr string, data []byte, timeout time.Duration, option ...PkgOption) error {
    33  	conn, err := NewConn(addr)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	defer conn.Close()
    38  	return conn.SendPkgWithTimeout(data, timeout, option...)
    39  }
    40  
    41  // 简单协议: (面向短链接)发送数据并等待接收返回数据(带返回超时等待时间)
    42  func SendRecvPkgWithTimeout(addr string, data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error) {
    43  	conn, err := NewConn(addr)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	defer conn.Close()
    48  	return conn.SendRecvPkgWithTimeout(data, timeout, option...)
    49  }