-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
155 lines (145 loc) · 3.62 KB
/
protocol.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package qualisys
import (
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"strconv"
"time"
)
//go:generate stringer -type ComponentType -trimprefix ComponentType
type ComponentType int
const (
ComponentType3D ComponentType = iota + 1
ComponentType3DNoLabels
ComponentTypeAnalog
ComponentTypeForce
ComponentType6D
ComponentType6DEuler
ComponentType2D
ComponentType2DLinearized
ComponentType3DResidual
ComponentType3DNoLabelsResidual
ComponentType6DResidual
ComponentType6DEulerResidual
ComponentTypeAnalogSingle
ComponentTypeImage
ComponentTypeForceSingle
ComponentTypeGazeVector
ComponentTypeTimecode
ComponentTypeSkeleton
ComponentTypeEyeTracker
)
type Protocol struct {
conn net.Conn
buffer []byte
ip string
basePort int
}
const DefaultLittleEndianPort = 22223
func NewProtocol(ip string, basePort int) *Protocol {
const startBufferSize int = 4096
rt := new(Protocol)
rt.buffer = make([]byte, startBufferSize)
rt.ip = ip
rt.basePort = basePort
return rt
}
func (rt *Protocol) Connect() error {
if rt.IsConnected() {
rt.Disconnect()
}
portLittleEndian := rt.basePort + 1
ipAndPort := rt.ip + ":" + strconv.Itoa(portLittleEndian)
raddr, err := net.ResolveTCPAddr("tcp", ipAndPort)
if err != nil {
return fmt.Errorf("connect: resolvetcpaddr: %w", err)
}
conn, err := net.DialTCP("tcp", nil, raddr)
if err != nil {
return fmt.Errorf("connect: dial: %w", err)
}
rt.conn = conn
p, err := rt.Receive()
if err != nil {
return fmt.Errorf("connect: receive: %w", err)
}
const qtmConnectedResponse string = "QTM RT Interface connected"
if p.CommandResponse != qtmConnectedResponse {
return fmt.Errorf("connect: unexpected response (%s)", p.CommandResponse)
}
const (
majorVer = 1
minorVer = 22
)
err = rt.SetVersion(majorVer, minorVer)
return err
}
func (rt *Protocol) IsConnected() bool {
return rt.conn != nil
}
func (rt *Protocol) Disconnect() {
if !rt.IsConnected() {
return
}
rt.conn.Close()
rt.conn = nil
}
func (rt *Protocol) Receive() (*Packet, error) {
for i := range rt.buffer {
rt.buffer[i] = 0
}
if err := rt.conn.SetReadDeadline(time.Now().Add(1 * time.Second)); err != nil {
return nil, fmt.Errorf("receive: setreaddeadline: %w", err)
}
const packetHeaderSize = 8
packetSize, err := rt.conn.Read(rt.buffer[0:packetHeaderSize])
if err != nil {
var netError net.Error
if errors.As(err, &netError) && netError.Timeout() {
return &Packet{Type: PacketTypeNoMoreData}, nil
}
return nil, fmt.Errorf("receive: read %w", err)
}
if packetSize < packetHeaderSize {
return nil, fmt.Errorf("receive: packet to small for header")
}
var p Packet
p.Size = int(binary.LittleEndian.Uint32(rt.buffer[0:4]))
p.Type = PacketType(binary.LittleEndian.Uint32(rt.buffer[4:8]))
if len(rt.buffer) < p.Size {
rt.buffer = append(rt.buffer, make([]byte, p.Size)...)
}
if packetSize >= p.Size {
return &p, nil
}
if err := rt.conn.SetReadDeadline(time.Now().Add(1 * time.Second)); err != nil {
return nil, fmt.Errorf("receive: setreaddeadline: %w", err)
}
pos := packetSize
for {
n, err := rt.conn.Read(rt.buffer[pos:p.Size])
if err != nil {
var netError net.Error
if errors.As(err, &netError) && netError.Timeout() {
return &Packet{Type: PacketTypeNoMoreData}, nil
}
if !errors.Is(err, io.EOF) {
return nil, fmt.Errorf("receive: read %w", err)
}
break
}
pos += n
if pos >= p.Size {
break
}
}
if err = p.UnmarshalBinary(rt.buffer[0:p.Size]); err != nil {
return nil, fmt.Errorf("receive: unmarshalbinary %w", err)
}
if p.Type == PacketTypeError {
return &p, fmt.Errorf("receive: error packet returned (%s)", p.ErrorResponse)
}
return &p, nil
}