forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
various.go
67 lines (56 loc) · 1.31 KB
/
various.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
package gomavlib
import (
"fmt"
"math/rand"
"net"
"time"
)
var errorTerminated = fmt.Errorf("terminated")
// netTimedConn forces a net.Conn to use timeouts
type netTimedConn struct {
conn net.Conn
}
func (c *netTimedConn) Close() error {
return c.conn.Close()
}
func (c *netTimedConn) Read(buf []byte) (int, error) {
err := c.conn.SetReadDeadline(time.Now().Add(_NET_READ_TIMEOUT))
if err != nil {
return 0, err
}
return c.conn.Read(buf)
}
func (c *netTimedConn) Write(buf []byte) (int, error) {
err := c.conn.SetWriteDeadline(time.Now().Add(_NET_WRITE_TIMEOUT))
if err != nil {
return 0, err
}
return c.conn.Write(buf)
}
func randomByte() byte {
var buf [1]byte
rand.Read(buf[:])
return buf[0]
}
func uint24Decode(in []byte) uint32 {
return uint32(in[2])<<16 | uint32(in[1])<<8 | uint32(in[0])
}
func uint24Encode(buf []byte, in uint32) []byte {
buf[0] = byte(in)
buf[1] = byte(in >> 8)
buf[2] = byte(in >> 16)
return buf[:3]
}
func uint48Decode(in []byte) uint64 {
return uint64(in[5])<<40 | uint64(in[4])<<32 | uint64(in[3])<<24 |
uint64(in[2])<<16 | uint64(in[1])<<8 | uint64(in[0])
}
func uint48Encode(buf []byte, in uint64) []byte {
buf[0] = byte(in)
buf[1] = byte(in >> 8)
buf[2] = byte(in >> 16)
buf[3] = byte(in >> 24)
buf[4] = byte(in >> 32)
buf[5] = byte(in >> 40)
return buf[:6]
}