-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
119 lines (96 loc) · 2.14 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package rfc868
import (
"fmt"
"net"
"sync"
"time"
"bytes"
"encoding/binary"
)
type TimeHandle struct {
data []byte
rfc868_epoch time.Time
mtx sync.RWMutex
}
func NewTimeHandle() (*TimeHandle, error) {
epoch, err := time.Parse("2006-01-02 15:04:05", "1900-01-01 00:00:00")
if err != nil {
return nil, err
}
return &TimeHandle{
make([]byte, 4),
epoch,
sync.RWMutex{},
}, nil
}
func (th *TimeHandle) update() {
th.mtx.Lock()
// In all this, we hope to avoid memory allocation. Allocation
// shouldn't happen since we are Write()ing 4 bytes -- the
// size of int32 -- to a bytes.Buffer that is initialized from
// a byte slice that is already exactly that size, and we call
// bytes.Buffer.Reset() before writing. I have checked that
// this holds, but a sanity check might be in order (with a
// panic() if not passed).
buf := bytes.NewBuffer(th.data)
buf.Reset()
// We must send back time encoded as a Big endian 32 bit (signed) int
err := binary.Write(buf, binary.BigEndian,
uint32(time.Since(th.rfc868_epoch)/time.Second))
if err != nil {
panic(err)
}
// fmt.Println(buf.Len(), cap(buf.Bytes()))
th.mtx.Unlock()
}
func update_service(th *TimeHandle) {
var s1 time.Duration = time.Second
for {
th.update()
time.Sleep(s1)
}
}
func (th *TimeHandle) send(udpconn *net.UDPConn, caddr *net.UDPAddr) error {
th.mtx.RLock()
_, err := udpconn.WriteToUDP(th.data, caddr)
if err != nil {
return err
}
th.mtx.RUnlock()
return nil
}
func ServeTime(addr string) error {
tx := make([]byte, 4)
timehandle, err := NewTimeHandle()
go update_service(timehandle)
if err != nil {
return err
}
udpaddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return err
}
udpconn, err := net.ListenUDP("udp", udpaddr)
if err != nil {
return err
}
//fps := fpsCounter()
for {
_, caddr, err := udpconn.ReadFromUDP(tx)
if err != nil {
fmt.Println("error: " + err.Error())
continue
}
err = timehandle.send(udpconn, caddr)
if err != nil {
fmt.Println("error: " + err.Error())
continue
}
//fmt.Println("rps", fps())
}
err = udpconn.Close()
if err != nil {
return err
}
return nil
}