-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.go
113 lines (100 loc) · 2.74 KB
/
data.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright (c) 2019 Andrew Young. All Rights Reserved.
This file is part of UDP Tester.
UDP Tester is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UDP Tester is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UDP Tester. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"io"
"log"
"net"
)
// A ReceivedData struct is returned when data is received
type ReceivedData struct {
Data []byte
Address net.Addr
Error error
}
// A DataClient sends and receives data over UDP.
type DataClient struct {
remoteAddress *net.UDPAddr
localAddress string
connection net.PacketConn
received chan ReceivedData
}
// NewDataClient creates a new DataClient instance
func NewDataClient(address string, localAddress string) (*DataClient, error) {
remoteAddress, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return nil, err
}
conn, err := net.ListenPacket("udp", localAddress)
if err != nil {
return nil, err
}
client := &DataClient{
remoteAddress: remoteAddress,
localAddress: localAddress,
connection: conn,
received: make(chan ReceivedData),
}
go client.readLoop(conn)
return client, nil
}
func (client *DataClient) readLoop(conn net.PacketConn) {
defer func() {
e := recover()
if e != nil {
err, ok := e.(error)
if ok && err.Error() == "send on closed channel" {
// This happens sometimes when closing the client and is not actually an error.
return
}
log.Printf("Caught panic in readLoop(): %v", e)
}
}()
buf := make([]byte, 65536)
for {
n, addr, err := client.connection.ReadFrom(buf)
r := ReceivedData{
Data: make([]byte, n),
Address: addr,
Error: err,
}
copy(r.Data, buf)
client.received <- r
if err == io.EOF {
return
}
}
}
// Close shuts down the DataClient
func (client *DataClient) Close() error {
client.connection.Close()
close(client.received)
return nil
}
// Send sends the given data to the device
func (client *DataClient) Send(data []byte) error {
for len(data) > 0 {
bytesWritten, err := client.connection.WriteTo(data, client.remoteAddress)
if err != nil {
return err
}
data = data[bytesWritten:]
}
return nil
}
// Receive returns a channel that can be used to receive data from the device
func (client *DataClient) Receive() chan ReceivedData {
return client.received
}