-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
protocol.go
90 lines (79 loc) · 2.15 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
package centrifuge
import (
"fmt"
"github.com/centrifugal/protocol"
)
// Publication is a data sent to channel.
type Publication struct {
// Offset is an incremental position number inside history stream.
// Zero value means that channel does not maintain Publication stream.
Offset uint64
// Data published to channel.
Data []byte
// Info is optional information about client connection published
// this data to channel.
Info *ClientInfo
// Tags contain custom key-value pairs attached to Publication.
Tags map[string]string
}
// ClientInfo contains information about client connection.
type ClientInfo struct {
// Client is a client unique id.
Client string
// User is an ID of authenticated user. Zero value means anonymous user.
User string
// ConnInfo is additional information about connection.
ConnInfo []byte
// ChanInfo is additional information about connection in context of
// channel subscription.
ChanInfo []byte
}
// Error represents protocol-level error.
type Error struct {
Code uint32
Message string
Temporary bool
}
func errorFromProto(err *protocol.Error) *Error {
return &Error{Code: err.Code, Message: err.Message, Temporary: err.Temporary}
}
func (e Error) Error() string {
return fmt.Sprintf("%d: %s", e.Code, e.Message)
}
func newReplyDecoder(enc protocol.Type, data []byte) protocol.ReplyDecoder {
if enc == protocol.TypeJSON {
return protocol.NewJSONReplyDecoder(data)
}
return protocol.NewProtobufReplyDecoder(data)
}
func newCommandEncoder(enc protocol.Type) protocol.CommandEncoder {
if enc == protocol.TypeJSON {
return protocol.NewJSONCommandEncoder()
}
return protocol.NewProtobufCommandEncoder()
}
func infoFromProto(v *protocol.ClientInfo) ClientInfo {
info := ClientInfo{
Client: v.GetClient(),
User: v.GetUser(),
}
if len(v.ConnInfo) > 0 {
info.ConnInfo = v.ConnInfo
}
if len(v.ChanInfo) > 0 {
info.ChanInfo = v.ChanInfo
}
return info
}
func pubFromProto(pub *protocol.Publication) Publication {
p := Publication{
Offset: pub.GetOffset(),
Data: pub.Data,
Tags: pub.GetTags(),
}
if pub.GetInfo() != nil {
info := infoFromProto(pub.GetInfo())
p.Info = &info
}
return p
}