修复gtcp.PoolConn方法覆盖问题

This commit is contained in:
John 2018-08-06 21:19:48 +08:00
parent 6959283c15
commit 0ba700982b
5 changed files with 68 additions and 21 deletions

View File

@ -245,12 +245,4 @@ func (c *Conn) LocalAddr() net.Addr {
func (c *Conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
// 判断是否是超时错误
func isTimeout(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "timeout")
}

View File

@ -9,6 +9,7 @@ package gtcp
import (
"net"
"time"
"strings"
)
const (
@ -78,3 +79,11 @@ func SendRecvWithTimeout(addr string, data []byte, receive int, timeout time.Dur
defer conn.Close()
return conn.SendRecvWithTimeout(data, receive, timeout, retry...)
}
// 判断是否是超时错误
func isTimeout(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "timeout")
}

View File

@ -7,6 +7,7 @@
package gtcp
import (
"time"
"gitee.com/johng/gf/g/container/gmap"
"gitee.com/johng/gf/g/container/gpool"
)
@ -60,7 +61,7 @@ func NewPoolConn(addr string, timeout...int) (*PoolConn, error) {
}
}
// 覆盖底层接口对象的Close方法
// (方法覆盖)覆盖底层接口对象的Close方法
func (c *PoolConn) Close() error {
if c.pool != nil && c.status == gCONN_STATUS_ACTIVE {
c.status = gCONN_STATUS_UNKNOWN
@ -71,7 +72,7 @@ func (c *PoolConn) Close() error {
return nil
}
// 发送数据
// (方法覆盖)发送数据
func (c *PoolConn) Send(data []byte, retry...Retry) error {
var err error
if err = c.Conn.Send(data, retry...); err != nil && c.status == gCONN_STATUS_UNKNOWN {
@ -90,7 +91,7 @@ func (c *PoolConn) Send(data []byte, retry...Retry) error {
return err
}
// 接收数据
// (方法覆盖)接收数据
func (c *PoolConn) Recv(length int, retry...Retry) ([]byte, error) {
data, err := c.Conn.Recv(length, retry...)
if err != nil {
@ -99,4 +100,47 @@ func (c *PoolConn) Recv(length int, retry...Retry) ([]byte, error) {
c.status = gCONN_STATUS_ACTIVE
}
return data, err
}
// (方法覆盖)按行读取数据,阻塞读取,直到完成一行读取位置(末尾以'\n'结尾,返回数据不包含换行符)
func (c *PoolConn) RecvLine(retry...Retry) ([]byte, error) {
data, err := c.Conn.RecvLine(retry...)
if err != nil {
c.status = gCONN_STATUS_ERROR
} else {
c.status = gCONN_STATUS_ACTIVE
}
return data, err
}
// (方法覆盖)带超时时间的数据获取
func (c *PoolConn) RecvWithTimeout(length int, timeout time.Duration, retry...Retry) ([]byte, error) {
c.SetRecvDeadline(time.Now().Add(timeout))
defer c.SetRecvDeadline(time.Time{})
return c.Recv(length, retry...)
}
// (方法覆盖)带超时时间的数据发送
func (c *PoolConn) SendWithTimeout(data []byte, timeout time.Duration, retry...Retry) error {
c.SetSendDeadline(time.Now().Add(timeout))
defer c.SetSendDeadline(time.Time{})
return c.Send(data, retry...)
}
// (方法覆盖)发送数据并等待接收返回数据
func (c *PoolConn) SendRecv(data []byte, receive int, retry...Retry) ([]byte, error) {
if err := c.Send(data, retry...); err == nil {
return c.Recv(receive, retry...)
} else {
return nil, err
}
}
// (方法覆盖)发送数据并等待接收返回数据(带返回超时等待时间)
func (c *PoolConn) SendRecvWithTimeout(data []byte, receive int, timeout time.Duration, retry...Retry) ([]byte, error) {
if err := c.Send(data, retry...); err == nil {
return c.RecvWithTimeout(receive, timeout, retry...)
} else {
return nil, err
}
}

View File

@ -10,7 +10,6 @@ import (
"net"
"time"
"io"
"strings"
)
// 封装的链接对象
@ -229,12 +228,4 @@ func (c *Conn) RemoteAddr() net.Addr {
func (c *Conn) Close() error {
return c.conn.Close()
}
// 判断是否是超时错误
func isTimeout(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "timeout")
}

View File

@ -6,7 +6,10 @@
package gudp
import "net"
import (
"net"
"strings"
)
// 创建标准库UDP链接操作对象
func NewNetConn(raddr string, laddr...string) (*net.UDPConn, error) {
@ -47,4 +50,12 @@ func SendRecv(addr string, data []byte, receive int, retry...Retry) ([]byte, err
}
defer conn.Close()
return conn.SendRecv(data, receive, retry...)
}
// 判断是否是超时错误
func isTimeout(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "timeout")
}