-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
361 lines (316 loc) · 6.3 KB
/
client.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2016 The Venti Authors. All rights reserved.
// Use of this source code is governed by an ISC-style
// license that can be found in the LICENSE file.
package venti
import (
"bytes"
"fmt"
"io"
"net"
"strings"
"sync"
"github.com/hdonnay/venti/internal/msg"
"github.com/hdonnay/venti/internal/pack"
)
func Dial(addr string) (*Client, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
return NewClient(conn)
}
func NewClient(conn net.Conn) (*Client, error) {
c := &Client{
Conn: conn,
w: pack.Chunk(conn),
r: pack.Dechunk(conn),
done: make(chan struct{}),
ts: &tagset{},
}
//c.w.Chatty = true
//c.r.Chatty = true
var err error
c.Version, err = c.version()
if err != nil {
conn.Close()
return nil, err
}
go c.recv()
if err := c.hello(); err != nil {
conn.Close()
return nil, err
}
return c, nil
}
type Client struct {
net.Conn
w *pack.Chunker
r *pack.Dechunker
done chan struct{}
pool sync.Pool
err error
ts *tagset
Version string
}
func (c *Client) recv() {
for {
select {
case <-c.done:
return
default:
}
buf := c.newBuf()
n, err := io.Copy(buf, c.r)
if err != nil {
c.err = err
return
}
if n == 0 {
c.doneBuf(buf)
continue
}
tag := buf.Bytes()[1]
// Send is a channel send and clunk
c.ts.Send(tag, buf)
}
}
func (c *Client) version() (string, error) {
buf := &bytes.Buffer{}
fmt.Fprintf(buf, "venti-%s-%s\n", strings.Join(vers, ":"), verComment)
if _, err := io.Copy(c.Conn, buf); err != nil {
return "", err
}
srvV, err := c.r.Line()
if err != nil {
return "", err
}
cm := ""
vs := ParseVersion(srvV)
// walk backwards so we get the biggest number, in the future with jetpacks
// and multiple numbers.
Cmp:
for i := len(vs) - 1; i >= 0; i-- {
for _, v := range vers {
if v == vs[i] {
cm = v
break Cmp
}
}
}
if cm == "" {
defer c.Close()
return "", fmt.Errorf("no common version")
}
return cm, nil
}
func (c *Client) hello() error {
tag, r := c.ts.New()
t := &msg.Thello{
Tag: tag,
Version: c.Version,
UID: "anonymous",
}
w := c.w.New()
if _, err := io.Copy(w, t); err != nil {
return err
}
w.Close()
buf := <-r
defer c.doneBuf(buf)
if err := want(msg.KindRhello, buf); err != nil {
return err
}
h := &msg.Rhello{}
if _, err := io.Copy(h, buf); err != nil {
return err
}
// if negotiating stuff, do that here.
return nil
}
func (c *Client) goodbye() error {
// we never get a response, the tags doesn't matter
t := &msg.Tgoodbye{Tag: 0x42}
w := c.w.New()
defer w.Close()
if _, err := io.Copy(w, t); err != nil {
return err
}
return nil
}
func (c *Client) Read(t Type, s Score, ct int64) (io.ReadCloser, error) {
if c.err != nil {
return nil, c.err
}
if ct < 0 {
return nil, fmt.Errorf("bad count")
}
tag, res := c.ts.New()
tr := &msg.Tread{
Tag: tag,
Score: s,
Type: byte(t),
Count: uint32(ct),
}
w := c.w.New()
if _, err := io.Copy(w, tr); err != nil {
c.ts.Clunk(tag)
return nil, err
}
w.Close()
buf := <-res
// can't defer putting the buffer back automatically
if err := want(msg.KindRread, buf); err != nil {
return nil, err
}
buf.Next(1) // discard the tag
return poolCloser(c, buf), nil
}
func (c *Client) Write(t Type, r io.Reader) (Score, error) {
if c.err != nil {
return nil, c.err
}
tag, res := c.ts.New()
tw := &msg.Twrite{
Tag: tag,
Type: byte(t),
}
w := c.w.New()
if _, err := io.Copy(w, tw); err != nil {
c.ts.Clunk(tag)
return nil, err
}
if _, err := io.Copy(w, r); err != nil {
c.ts.Clunk(tag)
return nil, err
}
w.Close()
buf := <-res
defer c.doneBuf(buf)
if err := want(msg.KindRwrite, buf); err != nil {
return nil, err
}
rw := &msg.Rwrite{}
if err := rw.UnmarshalBinary(buf.Bytes()); err != nil {
return nil, err
}
return Score(rw.Score), nil
}
func (c *Client) Ping() error {
if c.err != nil {
return c.err
}
tag, r := c.ts.New()
t := &msg.Tping{Tag: tag}
w := c.w.New()
if _, err := io.Copy(w, t); err != nil {
c.ts.Clunk(tag)
return err
}
w.Close()
buf := <-r
defer c.doneBuf(buf)
if err := want(msg.KindRping, buf); err != nil {
return err
}
// Don't bother reading the packet, if we've gotten here, it's all good.
return nil
}
func (c *Client) Sync() error {
if c.err != nil {
return c.err
}
tag, r := c.ts.New()
t := &msg.Tsync{Tag: tag}
w := c.w.New()
if _, err := io.Copy(w, t); err != nil {
c.ts.Clunk(tag)
return err
}
w.Close()
buf := <-r
defer c.doneBuf(buf)
if err := want(msg.KindRsync, buf); err != nil {
return err
}
// Don't bother reading the packet, if we've gotten here, it's all good.
return nil
}
func (c *Client) Close() error {
c.goodbye()
close(c.done)
return c.Conn.Close()
}
func (c *Client) newBuf() *bytes.Buffer {
if b := c.pool.Get(); b != nil {
return b.(*bytes.Buffer)
}
return bytes.NewBuffer(make([]byte, 0, initBufSz))
}
func (c *Client) doneBuf(b *bytes.Buffer) {
b.Reset()
c.pool.Put(b)
}
type tagset struct {
sync.Mutex
next uint8
wait [256]chan *bytes.Buffer
}
func (t *tagset) New() (uint8, chan *bytes.Buffer) {
// There might be a problem if we attempt more than 256 requests in-flight
t.Lock()
defer t.Unlock()
// The Plan 9 libventi is a stickler for the client to start at 0x00, which
// seems like it shouldn't matter?
lp := t.next - 1
for ; t.next != lp; t.next++ {
if t.wait[t.next] == nil {
ch := make(chan *bytes.Buffer)
t.wait[t.next] = ch
return t.next, ch
}
}
panic("too many concurrent requests") // time to rewrite this if you're here
}
func (t *tagset) Tag(tg uint8) chan *bytes.Buffer {
t.Lock()
defer t.Unlock()
return t.wait[tg]
}
func (t *tagset) Clunk(tg uint8) {
t.Lock()
defer t.Unlock()
close(t.wait[tg])
t.wait[tg] = nil
}
func (t *tagset) Send(tg uint8, buf *bytes.Buffer) {
t.Lock()
defer t.Unlock()
t.wait[tg] <- buf
close(t.wait[tg])
t.wait[tg] = nil
}
func want(w byte, buf *bytes.Buffer) error {
h := buf.Next(1)[0]
switch h {
case msg.KindRerror:
r := &msg.Rerror{}
io.Copy(r, buf)
return r
case w:
break
default:
return fmt.Errorf("incoming message: wanted %x, got %x", w, h)
}
return nil
}
func poolCloser(c *Client, buf *bytes.Buffer) io.ReadCloser {
return &pc{*buf, c}
}
type pc struct {
bytes.Buffer
c *Client
}
func (pc *pc) Close() error {
pc.c.doneBuf(&pc.Buffer)
return nil
}