gf/net/gudp/gudp_conn.go

251 lines
6.8 KiB
Go
Raw Normal View History

// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
2018-07-18 11:43:30 +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.
2018-07-18 11:43:30 +08:00
package gudp
import (
2019-06-19 09:06:52 +08:00
"io"
"net"
"time"
2019-06-22 22:05:39 +08:00
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/errors/gerror"
2018-07-18 11:43:30 +08:00
)
2019-10-01 17:53:49 +08:00
// Conn handles the UDP connection.
2018-07-18 11:43:30 +08:00
type Conn struct {
2019-10-01 17:53:49 +08:00
*net.UDPConn // Underlying UDP connection.
remoteAddr *net.UDPAddr // Remote address.
recvDeadline time.Time // Timeout point for reading data.
sendDeadline time.Time // Timeout point for writing data.
recvBufferWait time.Duration // Interval duration for reading buffer.
2018-07-18 11:43:30 +08:00
}
const (
2019-10-01 17:53:49 +08:00
gDEFAULT_RETRY_INTERVAL = 100 * time.Millisecond // Retry interval.
gDEFAULT_READ_BUFFER_SIZE = 64 // (KB)Buffer size.
gRECV_ALL_WAIT_TIMEOUT = time.Millisecond // Default interval for reading buffer.
2018-07-18 11:43:30 +08:00
)
type Retry struct {
2019-10-01 17:53:49 +08:00
Count int // Max retry count.
Interval time.Duration // Retry interval.
2018-07-18 11:43:30 +08:00
}
2019-10-01 17:53:49 +08:00
// NewConn creates UDP connection to <remoteAddress>.
// The optional parameter <localAddress> specifies the local address for connection.
func NewConn(remoteAddress string, localAddress ...string) (*Conn, error) {
if conn, err := NewNetConn(remoteAddress, localAddress...); err == nil {
2019-06-19 09:06:52 +08:00
return NewConnByNetConn(conn), nil
} else {
return nil, err
}
2018-07-18 11:43:30 +08:00
}
2019-10-01 17:53:49 +08:00
// NewConnByNetConn creates a UDP connection object with given *net.UDPConn object.
2018-07-18 11:43:30 +08:00
func NewConnByNetConn(udp *net.UDPConn) *Conn {
2019-06-19 09:06:52 +08:00
return &Conn{
2019-06-21 22:23:07 +08:00
UDPConn: udp,
2019-06-19 09:06:52 +08:00
recvDeadline: time.Time{},
sendDeadline: time.Time{},
recvBufferWait: gRECV_ALL_WAIT_TIMEOUT,
}
2018-07-18 11:43:30 +08:00
}
2019-10-01 17:53:49 +08:00
// Send writes data to remote address.
2019-06-19 09:06:52 +08:00
func (c *Conn) Send(data []byte, retry ...Retry) (err error) {
for {
2019-10-01 17:53:49 +08:00
if c.remoteAddr != nil {
_, err = c.WriteToUDP(data, c.remoteAddr)
2019-06-19 09:06:52 +08:00
} else {
2019-06-21 22:23:07 +08:00
_, err = c.Write(data)
2019-06-19 09:06:52 +08:00
}
if err != nil {
2019-10-01 17:53:49 +08:00
// Connection closed.
2019-06-19 09:06:52 +08:00
if err == io.EOF {
return err
}
2019-10-01 17:53:49 +08:00
// Still failed even after retrying.
2019-06-19 09:06:52 +08:00
if len(retry) == 0 || retry[0].Count == 0 {
return err
}
if len(retry) > 0 {
retry[0].Count--
if retry[0].Interval == 0 {
retry[0].Interval = gDEFAULT_RETRY_INTERVAL
}
2019-10-01 17:53:49 +08:00
time.Sleep(retry[0].Interval)
2019-06-19 09:06:52 +08:00
}
} else {
return nil
}
}
2018-07-18 11:43:30 +08:00
}
2019-10-01 17:53:49 +08:00
// Recv receives data from remote address.
2019-06-03 23:53:59 +08:00
//
2019-10-01 17:53:49 +08:00
// Note that,
// 1. There's package border in UDP protocol, so we can receive a complete package if it specifies length < 0.
// 2. If length = 0, it means it receives the data from current buffer and returns immediately.
// 3. If length > 0, it means it blocks reading data from connection until length size was received.
2019-06-19 09:06:52 +08:00
func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) {
2019-10-01 17:53:49 +08:00
var err error // Reading error.
var size int // Reading size.
var index int // Received size.
var buffer []byte // Buffer object.
var bufferWait bool // Whether buffer reading timeout set.
var remoteAddr *net.UDPAddr // Current remote address for reading.
2019-06-19 09:06:52 +08:00
if length > 0 {
buffer = make([]byte, length)
} else {
buffer = make([]byte, gDEFAULT_READ_BUFFER_SIZE)
}
for {
if length < 0 && index > 0 {
bufferWait = true
2019-06-21 22:23:07 +08:00
if err = c.SetReadDeadline(time.Now().Add(c.recvBufferWait)); err != nil {
2019-06-19 09:06:52 +08:00
return nil, err
}
}
2019-10-01 17:53:49 +08:00
size, remoteAddr, err = c.ReadFromUDP(buffer[index:])
2019-06-19 09:06:52 +08:00
if err == nil {
2019-10-01 17:53:49 +08:00
c.remoteAddr = remoteAddr
2019-06-19 09:06:52 +08:00
}
if size > 0 {
index += size
if length > 0 {
2019-10-01 17:53:49 +08:00
// It reads til <length> size if <length> is specified.
2019-06-19 09:06:52 +08:00
if index == length {
break
}
} else {
if index >= gDEFAULT_READ_BUFFER_SIZE {
2019-10-01 17:53:49 +08:00
// If it exceeds the buffer size, it then automatically increases its buffer size.
2019-06-19 09:06:52 +08:00
buffer = append(buffer, make([]byte, gDEFAULT_READ_BUFFER_SIZE)...)
} else {
2019-10-01 17:53:49 +08:00
// It returns immediately if received size is lesser than buffer size.
2019-06-19 09:06:52 +08:00
if !bufferWait {
break
}
}
}
}
if err != nil {
2019-10-01 17:53:49 +08:00
// Connection closed.
2019-06-19 09:06:52 +08:00
if err == io.EOF {
break
}
2019-10-01 17:53:49 +08:00
// Re-set the timeout when reading data.
2019-06-19 09:06:52 +08:00
if bufferWait && isTimeout(err) {
2019-06-21 22:23:07 +08:00
if err = c.SetReadDeadline(c.recvDeadline); err != nil {
2019-06-19 09:06:52 +08:00
return nil, err
}
err = nil
break
}
if len(retry) > 0 {
2019-10-01 17:53:49 +08:00
// It fails even it retried.
2019-06-19 09:06:52 +08:00
if retry[0].Count == 0 {
break
}
retry[0].Count--
if retry[0].Interval == 0 {
retry[0].Interval = gDEFAULT_RETRY_INTERVAL
}
time.Sleep(time.Duration(retry[0].Interval) * time.Millisecond)
continue
}
break
}
2019-10-01 17:53:49 +08:00
// Just read once from buffer.
2019-06-19 09:06:52 +08:00
if length == 0 {
break
}
}
return buffer[:index], err
2018-07-18 11:43:30 +08:00
}
2019-10-01 17:53:49 +08:00
// SendRecv writes data to connection and blocks reading response.
2019-06-19 09:06:52 +08:00
func (c *Conn) 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
}
}
2019-10-01 17:53:49 +08:00
// RecvWithTimeout reads data from remote address with timeout.
2019-06-21 22:23:07 +08:00
func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error) {
2019-06-19 09:06:52 +08:00
if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil {
return nil, err
}
2019-06-21 22:23:07 +08:00
defer func() {
2019-06-29 23:35:32 +08:00
err = gerror.Wrap(c.SetRecvDeadline(time.Time{}), "SetRecvDeadline error")
2019-06-21 22:23:07 +08:00
}()
data, err = c.Recv(length, retry...)
return
}
2019-10-01 17:53:49 +08:00
// SendWithTimeout writes data to connection with timeout.
2019-06-21 22:23:07 +08:00
func (c *Conn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) (err error) {
2019-06-04 18:26:32 +08:00
if err := c.SetSendDeadline(time.Now().Add(timeout)); err != nil {
return err
}
2019-06-21 22:23:07 +08:00
defer func() {
2019-06-29 23:35:32 +08:00
err = gerror.Wrap(c.SetSendDeadline(time.Time{}), "SetSendDeadline error")
2019-06-21 22:23:07 +08:00
}()
err = c.Send(data, retry...)
return
}
2019-10-01 17:53:49 +08:00
// SendRecvWithTimeout writes data to connection and reads response with timeout.
2019-06-19 09:06:52 +08:00
func (c *Conn) 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
}
}
func (c *Conn) SetDeadline(t time.Time) error {
2019-06-21 22:23:07 +08:00
err := c.UDPConn.SetDeadline(t)
2019-06-19 09:06:52 +08:00
if err == nil {
c.recvDeadline = t
c.sendDeadline = t
}
return err
}
func (c *Conn) SetRecvDeadline(t time.Time) error {
2019-06-21 22:23:07 +08:00
err := c.SetReadDeadline(t)
2019-06-19 09:06:52 +08:00
if err == nil {
c.recvDeadline = t
}
return err
}
func (c *Conn) SetSendDeadline(t time.Time) error {
2019-06-21 22:23:07 +08:00
err := c.SetWriteDeadline(t)
2019-06-19 09:06:52 +08:00
if err == nil {
c.sendDeadline = t
}
return err
}
2019-10-01 17:53:49 +08:00
// SetRecvBufferWait sets the buffer waiting timeout when reading all data from connection.
// The waiting duration cannot be too long which might delay receiving data from remote address.
func (c *Conn) SetRecvBufferWait(d time.Duration) {
2019-06-19 09:06:52 +08:00
c.recvBufferWait = d
}
2019-10-01 17:53:49 +08:00
// RemoteAddr returns the remote address of current UDP connection.
// Note that it cannot use c.conn.RemoteAddr() as it's nil.
func (c *Conn) RemoteAddr() net.Addr {
2019-06-19 09:06:52 +08:00
//return c.conn.RemoteAddr()
2019-10-01 17:53:49 +08:00
return c.remoteAddr
}