/
gudp_func.go
54 lines (49 loc) · 1.52 KB
/
gudp_func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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.
package gudp
import (
"net"
)
// NewNetConn creates and returns a *net.UDPConn with given addresses.
func NewNetConn(remoteAddress string, localAddress ...string) (*net.UDPConn, error) {
var err error
var remoteAddr, localAddr *net.UDPAddr
remoteAddr, err = net.ResolveUDPAddr("udp", remoteAddress)
if err != nil {
return nil, err
}
if len(localAddress) > 0 {
localAddr, err = net.ResolveUDPAddr("udp", localAddress[0])
if err != nil {
return nil, err
}
}
conn, err := net.DialUDP("udp", localAddr, remoteAddr)
if err != nil {
return nil, err
}
return conn, nil
}
// Send writes data to <address> using UDP connection and then closes the connection.
// Note that it is used for short connection usage.
func Send(address string, data []byte, retry ...Retry) error {
conn, err := NewConn(address)
if err != nil {
return err
}
defer conn.Close()
return conn.Send(data, retry...)
}
// SendRecv writes data to <address> using UDP connection, reads response and then closes the connection.
// Note that it is used for short connection usage.
func SendRecv(address string, data []byte, receive int, retry ...Retry) ([]byte, error) {
conn, err := NewConn(address)
if err != nil {
return nil, err
}
defer conn.Close()
return conn.SendRecv(data, receive, retry...)
}