-
Notifications
You must be signed in to change notification settings - Fork 19
/
server.go
90 lines (70 loc) · 1.75 KB
/
server.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
package stats
import (
"encoding/json"
"fmt"
"net"
"strconv"
)
// ServerConfig is used to initialize a new ServerStats object
type ServerConfig struct {
Domain string
Port int
Debug bool
CustomBufferSize int
}
// ServerStats is the object used to receive, store and send data for usage
type ServerStats struct {
addr string
debug bool
bufferSize int
}
// NewServer create a new server object for use
func NewServer(config *ServerConfig) (*ServerStats, error) {
bSize := config.CustomBufferSize
if bSize == 0 {
bSize = defaultBufferSize
}
return &ServerStats{
addr: config.Domain + ":" + strconv.Itoa(config.Port),
debug: config.Debug,
bufferSize: bSize,
}, nil
}
// Run starts receiving the profiling stats for storage and usage
func (s *ServerStats) Run() <-chan *Stats {
results := make(chan *Stats)
go func(results chan<- *Stats) {
var serverAddr *net.UDPAddr
var server *net.UDPConn
var err error
serverAddr, err = net.ResolveUDPAddr(udp, s.addr)
if err != nil {
panic(err)
}
server, err = net.ListenUDP(udp, serverAddr)
if err != nil {
panic(err)
}
defer server.Close()
server.SetReadBuffer(s.bufferSize)
var addr *net.UDPAddr
var bytesRead int
buff := make([]byte, s.bufferSize)
stats := new(Stats)
for {
bytesRead, addr, err = server.ReadFromUDP(buff)
if err != nil {
fmt.Printf("Error:%s bytes read %d\n", err, bytesRead)
continue
}
if s.debug {
fmt.Printf("Recieved: %s from %s Read %d bytes\n", string(buff[0:bytesRead]), addr, bytesRead)
}
if err := json.Unmarshal(buff[0:bytesRead], &stats); err != nil {
fmt.Println("Error Unmarshalling:", err)
}
results <- stats
}
}(results)
return results
}