-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
180 lines (160 loc) · 4.52 KB
/
message.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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
// message type constants
// make sure to update message.js in ydb-client when updating these values..
const (
messageUpdate = 0
messageSub = 1
messageConfirmation = 2
messageSubConf = 3
messageHostUnconfirmedByClient = 4
messageConfirmedByHost = 5
)
// a message is structured as [length of payload, payload], where payload is [messageType, typePayload]
type message interface {
ReadByte() (byte, error)
Read(p []byte) (int, error)
}
func readMessage(m message, session *session) (err error) {
messageType, err := binary.ReadUvarint(m)
if err != nil {
return err
}
switch messageType {
case messageSub:
debug("reading sub message")
err = readSubMessage(m, session)
case messageUpdate:
debug("reading update message")
err = readUpdateMessage(m, session)
case messageConfirmation:
debug("reading conf message")
err = readConfirmationMessage(m, session)
default:
debug(fmt.Sprintf("received unknown message type %d", messageType))
}
return err
}
func readSubMessage(m message, session *session) error {
subConfBuf := &bytes.Buffer{}
writeUvarint(subConfBuf, messageSubConf)
nSubs, _ := binary.ReadUvarint(m)
writeUvarint(subConfBuf, nSubs)
var i uint64
for i = 0; i < nSubs; i++ {
roomname, _ := readRoomname(m)
writeRoomname(subConfBuf, roomname)
clientOffset, _ := binary.ReadUvarint(m)
clientRsid, _ := binary.ReadUvarint(m)
room := getRoom(roomname)
room.mux.Lock()
roomRsid := uint64(room.roomsessionid)
roomOffset := uint64(room.offset)
room.mux.Unlock()
if roomRsid != clientRsid || roomOffset < clientOffset {
// in case of mismatch suggest the client to resync. TODO: Init Yjs sync here
clientOffset = 0
clientRsid = roomRsid
}
writeUvarint(subConfBuf, clientOffset)
writeUvarint(subConfBuf, clientRsid)
subscribeRoom(roomname, session, uint32(clientRsid), uint32(clientOffset))
}
session.send(subConfBuf.Bytes())
return nil
}
func readConfirmationMessage(m message, session *session) (err error) {
conf, err := binary.ReadUvarint(m)
session.serverConfirmation.clientConfirmed(conf)
return
}
type subDefinition struct {
roomname roomname
offset uint64
rsid uint64
}
func createMessageSubscribe(conf uint64, subs ...subDefinition) []byte {
buf := &bytes.Buffer{}
writeUvarint(buf, messageSub)
writeUvarint(buf, conf)
writeUvarint(buf, uint64(len(subs)))
for _, sub := range subs {
writeRoomname(buf, sub.roomname)
writeUvarint(buf, sub.offset)
writeUvarint(buf, sub.rsid)
}
return buf.Bytes()
}
//
func createMessageUpdate(roomname roomname, offsetOrConf uint64, data []byte) []byte {
buf := &bytes.Buffer{}
writeUvarint(buf, messageUpdate)
writeUvarint(buf, offsetOrConf)
writeRoomname(buf, roomname)
writePayload(buf, data)
return buf.Bytes()
}
func createMessageHostUnconfirmedByClient(clientConf uint64, offset uint64) []byte {
buf := &bytes.Buffer{}
writeUvarint(buf, messageHostUnconfirmedByClient)
writeUvarint(buf, clientConf)
writeUvarint(buf, offset)
return buf.Bytes()
}
func createMessageConfirmedByHost(roomname roomname, offset uint64) []byte {
buf := &bytes.Buffer{}
writeUvarint(buf, messageConfirmedByHost)
writeRoomname(buf, roomname)
writeUvarint(buf, offset)
return buf.Bytes()
}
func createMessageConfirmation(conf uint64) []byte {
buf := &bytes.Buffer{}
writeUvarint(buf, messageConfirmation)
writeUvarint(buf, conf)
return buf.Bytes()
}
func readUpdateMessage(m message, session *session) error {
confirmation, _ := binary.ReadUvarint(m)
roomname, _ := readRoomname(m)
bs, _ := readPayload(m)
// send the rest of message
updateRoom(roomname, session, confirmation, bs)
return nil
}
func readString(m message) (string, error) {
bs, err := readPayload(m)
return string(bs), err
}
func readRoomname(m message) (roomname, error) {
name, err := readString(m)
return roomname(name), err
}
func readPayload(m message) ([]byte, error) {
len, _ := binary.ReadUvarint(m)
bs := make([]byte, len)
m.Read(bs)
return bs, nil
}
func writeUvarint(buf io.Writer, n uint64) error {
bs := make([]byte, binary.MaxVarintLen64)
len := binary.PutUvarint(bs, n)
buf.Write(bs[:len])
return nil
}
func writeString(buf io.Writer, str string) error {
return writePayload(buf, []byte(str))
}
func writeRoomname(buf io.Writer, roomname roomname) error {
return writeString(buf, string(roomname))
}
func writePayload(buf io.Writer, payload []byte) error {
writeUvarint(buf, uint64(len(payload)))
buf.Write(payload)
return nil
}