-
Notifications
You must be signed in to change notification settings - Fork 54
/
init.go
113 lines (92 loc) · 2.24 KB
/
init.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
package protocol
import (
"fmt"
"github.com/SAP/go-hdb/driver/internal/protocol/encoding"
)
type endianess int8
const (
bigEndian endianess = 0
littleEndian endianess = 1
)
const (
initRequestFillerSize = 4
)
var initRequestFiller uint32 = 0xffffffff
type version struct {
major int8
minor int16
}
func (v version) String() string {
return fmt.Sprintf("%d.%d", v.major, v.minor)
}
type initRequest struct {
product version
protocol version
numOptions int8
endianess endianess
}
func (r *initRequest) String() string {
switch r.numOptions {
default:
return fmt.Sprintf("productVersion %s protocolVersion %s", r.product, r.protocol)
case 1:
return fmt.Sprintf("productVersion %s protocolVersion %s endianess %s", r.product, r.protocol, r.endianess)
}
}
func (r *initRequest) decode(dec *encoding.Decoder) error {
dec.Skip(initRequestFillerSize) // filler
r.product.major = dec.Int8()
r.product.minor = dec.Int16()
r.protocol.major = dec.Int8()
r.protocol.minor = dec.Int16()
dec.Skip(1) // reserved filler
r.numOptions = dec.Int8()
switch r.numOptions {
default:
panic(fmt.Sprintf("invalid number of options %d", r.numOptions))
case 0:
dec.Skip(2)
case 1:
cnt := dec.Int8()
if cnt != 1 {
panic(fmt.Sprintf("invalid number of options %d - 1 expected", cnt))
}
r.endianess = endianess(dec.Int8())
}
return dec.Error()
}
func (r *initRequest) encode(enc *encoding.Encoder) error {
enc.Uint32(initRequestFiller)
enc.Int8(r.product.major)
enc.Int16(r.product.minor)
enc.Int8(r.protocol.major)
enc.Int16(r.protocol.minor)
switch r.numOptions {
default:
panic(fmt.Sprintf("invalid number of options %d", r.numOptions))
case 0:
enc.Zeroes(4)
case 1:
// reserved
enc.Zeroes(1)
enc.Int8(r.numOptions)
enc.Int8(int8(littleEndian))
enc.Int8(int8(r.endianess))
}
return nil
}
type initReply struct {
product version
protocol version
}
func (r *initReply) String() string {
return fmt.Sprintf("productVersion %s protocolVersion %s", r.product, r.protocol)
}
func (r *initReply) decode(dec *encoding.Decoder) error {
r.product.major = dec.Int8()
r.product.minor = dec.Int16()
r.protocol.major = dec.Int8()
r.protocol.minor = dec.Int16()
dec.Skip(2) // commitInitReplySize
return dec.Error()
}