From 0ba700982be32836628e25e694c53c3732afd8ff Mon Sep 17 00:00:00 2001 From: John Date: Mon, 6 Aug 2018 21:19:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dgtcp.PoolConn=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E8=A6=86=E7=9B=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/gtcp/gtcp_conn.go | 8 ------- g/net/gtcp/gtcp_func.go | 9 ++++++++ g/net/gtcp/gtcp_pool.go | 50 ++++++++++++++++++++++++++++++++++++++--- g/net/gudp/gudp_conn.go | 9 -------- g/net/gudp/gudp_func.go | 13 ++++++++++- 5 files changed, 68 insertions(+), 21 deletions(-) diff --git a/g/net/gtcp/gtcp_conn.go b/g/net/gtcp/gtcp_conn.go index 92ea4e739..e6849995f 100644 --- a/g/net/gtcp/gtcp_conn.go +++ b/g/net/gtcp/gtcp_conn.go @@ -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") } \ No newline at end of file diff --git a/g/net/gtcp/gtcp_func.go b/g/net/gtcp/gtcp_func.go index 43bd89313..1f007ed59 100644 --- a/g/net/gtcp/gtcp_func.go +++ b/g/net/gtcp/gtcp_func.go @@ -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") +} \ No newline at end of file diff --git a/g/net/gtcp/gtcp_pool.go b/g/net/gtcp/gtcp_pool.go index 6f3adea09..53ff81da1 100644 --- a/g/net/gtcp/gtcp_pool.go +++ b/g/net/gtcp/gtcp_pool.go @@ -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 + } } \ No newline at end of file diff --git a/g/net/gudp/gudp_conn.go b/g/net/gudp/gudp_conn.go index c522f2a93..c6bd41b23 100644 --- a/g/net/gudp/gudp_conn.go +++ b/g/net/gudp/gudp_conn.go @@ -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") } \ No newline at end of file diff --git a/g/net/gudp/gudp_func.go b/g/net/gudp/gudp_func.go index 411e7024d..3c6b15412 100644 --- a/g/net/gudp/gudp_func.go +++ b/g/net/gudp/gudp_func.go @@ -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") } \ No newline at end of file