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

     1  // Copyright 2018 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 (
    10  	"time"
    11  
    12  	"github.com/zhongdalu/gf/g/errors/gerror"
    13  )
    14  
    15  // 简单协议: (方法覆盖)发送数据
    16  func (c *PoolConn) SendPkg(data []byte, option ...PkgOption) (err error) {
    17  	if err = c.Conn.SendPkg(data, option...); err != nil && c.status == gCONN_STATUS_UNKNOWN {
    18  		if v, e := c.pool.NewFunc(); e == nil {
    19  			c.Conn = v.(*PoolConn).Conn
    20  			err = c.Conn.SendPkg(data, option...)
    21  		} else {
    22  			err = e
    23  		}
    24  	}
    25  	if err != nil {
    26  		c.status = gCONN_STATUS_ERROR
    27  	} else {
    28  		c.status = gCONN_STATUS_ACTIVE
    29  	}
    30  	return err
    31  }
    32  
    33  // 简单协议: (方法覆盖)接收数据
    34  func (c *PoolConn) RecvPkg(option ...PkgOption) ([]byte, error) {
    35  	data, err := c.Conn.RecvPkg(option...)
    36  	if err != nil {
    37  		c.status = gCONN_STATUS_ERROR
    38  	} else {
    39  		c.status = gCONN_STATUS_ACTIVE
    40  	}
    41  	return data, err
    42  }
    43  
    44  // 简单协议: (方法覆盖)带超时时间的数据获取
    45  func (c *PoolConn) RecvPkgWithTimeout(timeout time.Duration, option ...PkgOption) (data []byte, err error) {
    46  	if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil {
    47  		return nil, err
    48  	}
    49  	defer func() {
    50  		err = gerror.Wrap(c.SetRecvDeadline(time.Time{}), "SetRecvDeadline error")
    51  	}()
    52  	data, err = c.RecvPkg(option...)
    53  	return
    54  }
    55  
    56  // 简单协议: (方法覆盖)带超时时间的数据发送
    57  func (c *PoolConn) SendPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) (err error) {
    58  	if err := c.SetSendDeadline(time.Now().Add(timeout)); err != nil {
    59  		return err
    60  	}
    61  	defer func() {
    62  		err = gerror.Wrap(c.SetSendDeadline(time.Time{}), "SetSendDeadline error")
    63  	}()
    64  	err = c.SendPkg(data, option...)
    65  	return
    66  }
    67  
    68  // 简单协议: (方法覆盖)发送数据并等待接收返回数据
    69  func (c *PoolConn) SendRecvPkg(data []byte, option ...PkgOption) ([]byte, error) {
    70  	if err := c.SendPkg(data, option...); err == nil {
    71  		return c.RecvPkg(option...)
    72  	} else {
    73  		return nil, err
    74  	}
    75  }
    76  
    77  // 简单协议: (方法覆盖)发送数据并等待接收返回数据(带返回超时等待时间)
    78  func (c *PoolConn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error) {
    79  	if err := c.SendPkg(data, option...); err == nil {
    80  		return c.RecvPkgWithTimeout(timeout, option...)
    81  	} else {
    82  		return nil, err
    83  	}
    84  }