-
Notifications
You must be signed in to change notification settings - Fork 277
/
wspb.go
69 lines (54 loc) · 1.46 KB
/
wspb.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
// Package wspb provides convenience functions for reading and writing protobuf
// messages via a websocket.
package wspb
import (
"bytes"
"context"
"fmt"
"sync"
"google.golang.org/protobuf/proto"
"nhooyr.io/websocket"
)
var pool sync.Pool
func getBuffer() *bytes.Buffer {
b := pool.Get()
if b == nil {
return &bytes.Buffer{}
}
return b.(*bytes.Buffer)
}
func putBuffer(b *bytes.Buffer) {
b.Reset()
pool.Put(b)
}
// Read reads from websocket c into protobuf message m.
func Read(ctx context.Context, c *websocket.Conn, m proto.Message) error {
t, r, err := c.Reader(ctx)
if err != nil {
return err
}
if t != websocket.MessageBinary {
return fmt.Errorf("expected binary message for protobuf but got %q", t.String())
}
b := getBuffer()
defer putBuffer(b)
if _, err := b.ReadFrom(r); err != nil {
return fmt.Errorf("error reading from reader: %w", err)
}
if err := proto.Unmarshal(b.Bytes(), m); err != nil {
c.Close(websocket.StatusInvalidFramePayloadData, "failed to unmarshal protobuf")
return fmt.Errorf("failed to unmarshal protobuf: %w", err)
}
return nil
}
// Write writes protobuf message m to websocket c.
func Write(ctx context.Context, c *websocket.Conn, m proto.Message) error {
const op = "wspb.Write"
bytes, err := proto.Marshal(m)
if err != nil {
return fmt.Errorf("failed to marshal protobuf: %w", err)
}
return c.Write(ctx, websocket.MessageBinary, bytes)
}