2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017-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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-07-18 11:43:30 +08:00
|
|
|
|
|
|
|
package gudp
|
|
|
|
|
2018-08-06 21:19:48 +08:00
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"net"
|
2018-08-06 21:19:48 +08:00
|
|
|
)
|
2018-07-18 11:43:30 +08:00
|
|
|
|
2019-10-01 17:53:49 +08:00
|
|
|
// NewNetConn creates and returns a *net.UDPConn with given addresses.
|
|
|
|
func NewNetConn(remoteAddress string, localAddress ...string) (*net.UDPConn, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
var err error
|
|
|
|
var remoteAddr, localAddr *net.UDPAddr
|
2019-10-01 17:53:49 +08:00
|
|
|
remoteAddr, err = net.ResolveUDPAddr("udp", remoteAddress)
|
2019-06-19 09:06:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-01 17:53:49 +08:00
|
|
|
if len(localAddress) > 0 {
|
|
|
|
localAddr, err = net.ResolveUDPAddr("udp", localAddress[0])
|
2019-06-19 09:06:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn, err := net.DialUDP("udp", localAddr, remoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
2018-07-18 11:43:30 +08:00
|
|
|
}
|
|
|
|
|
2019-10-30 19:55:50 +08:00
|
|
|
// Send writes data to <address> using UDP connection and then closes the connection.
|
2019-10-01 17:53:49 +08:00
|
|
|
// Note that it is used for short connection usage.
|
2019-10-30 19:55:50 +08:00
|
|
|
func Send(address string, data []byte, retry ...Retry) error {
|
|
|
|
conn, err := NewConn(address)
|
2019-06-19 09:06:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
return conn.Send(data, retry...)
|
2018-07-18 11:43:30 +08:00
|
|
|
}
|
|
|
|
|
2019-10-30 19:55:50 +08:00
|
|
|
// SendRecv writes data to <address> using UDP connection, reads response and then closes the connection.
|
2019-10-01 17:53:49 +08:00
|
|
|
// Note that it is used for short connection usage.
|
2019-10-30 19:55:50 +08:00
|
|
|
func SendRecv(address string, data []byte, receive int, retry ...Retry) ([]byte, error) {
|
|
|
|
conn, err := NewConn(address)
|
2019-06-19 09:06:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
return conn.SendRecv(data, receive, retry...)
|
2018-08-06 21:19:48 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 17:53:49 +08:00
|
|
|
// isTimeout checks whether given <err> is a timeout error.
|
2018-08-06 21:19:48 +08:00
|
|
|
func isTimeout(err error) bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|