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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
package protocol
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"io"
"reflect"
)
type Command string
const (
CmdMsg Command = "MSG"
CmdNop Command = "NOP"
CmdBye Command = "BYE"
CmdNil Command = "NIL"
)
type ConnectionPhase uint16
func (c ConnectionPhase) String() string {
s, ok := phaseStrings[c]
if ok {
return s
}
return fmt.Sprintf("<Unknown ConnectionPhase %v>", int(c))
}
const (
PhaseHandshake ConnectionPhase = 1
PhaseModeSelection ConnectionPhase = 2
)
var phaseStrings = map[ConnectionPhase]string{
PhaseHandshake: "Handshake",
PhaseModeSelection: "ModeSelection",
}
type protocolError struct {
phase ConnectionPhase
message string
}
func (p protocolError) Error() string {
return fmt.Sprintf("protocol error, phase=%v, server message=%v", p.phase, p.message)
}
func NewClient(tx *bufio.Writer, rx *bufio.Reader) Client {
return Client{
tx: tx,
rx: rx,
}
}
type Client struct {
tx *bufio.Writer
rx *bufio.Reader
}
func (c *Client) read(v interface{}) error {
return binary.Read(c.rx, binary.BigEndian, v)
}
// write given data in write format.
// Note: this method DOES NOT flush the write buffer!
func (c *Client) write(v interface{}) error {
if v == nil {
panic(errors.New("writing nil"))
}
vv := reflect.ValueOf(v)
if vv.Kind() == reflect.String {
_, err := c.tx.Write([]byte(vv.String()))
return err
}
if vv.Kind() >= reflect.Int && vv.Kind() <= reflect.Uint64 {
return binary.Write(c.tx, binary.BigEndian, v)
}
panic(fmt.Errorf("unsupported type to write: %v, kind: %v", vv.Type(), vv.Kind()))
}
func (c *Client) writeFlush(v interface{}) error {
err := c.write(v)
if err != nil {
return err
}
return c.flush()
}
func (c *Client) flush() error {
return c.tx.Flush()
}
const handshakeSequence = "PSMB"
func (c *Client) Handshake() error {
err := c.write(handshakeSequence)
if err != nil {
return err
}
const (
protocolVersion uint32 = 2
protocolOptions uint32 = 0
)
err = c.write(protocolVersion)
if err != nil {
return err
}
err = c.write(protocolOptions)
if err != nil {
return err
}
err = c.flush()
if err != nil {
return err
}
msg, err := c.rx.ReadString('\x00')
if err != nil {
return err
}
msg = msg[:len(msg)-1]
if msg != "OK" {
return protocolError{
phase: PhaseHandshake,
message: msg,
}
}
var serverOptions uint32
err = c.read(&serverOptions)
if err != nil {
return err
}
if serverOptions != 0 {
return fmt.Errorf("invalid server options: %v", serverOptions)
}
return nil
}
func (c *Client) Publish(msg io.Reader, n int64) error {
err := c.write(CmdMsg)
if err != nil {
return err
}
err = c.write(uint64(n))
if err != nil {
return err
}
_, err = io.CopyN(c.tx, msg, n)
if err != nil {
return err
}
err = c.flush()
if err != nil {
return err
}
return err
}
func (c *Client) PublishBytes(msg []byte) error {
err := c.write(CmdMsg)
if err != nil {
return err
}
err = c.write(uint64(len(msg)))
if err != nil {
return err
}
err = c.write(msg)
if err != nil {
return err
}
return c.flush()
}
func (c *Client) Nop() error {
return c.writeFlush(CmdNop)
}
func (c *Client) Bye() error {
return c.writeFlush(CmdBye)
}
func (c *Client) Nil() error {
return c.writeFlush(CmdNil)
}
|