forked from muka/peerjs-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_data.go
354 lines (290 loc) · 8.62 KB
/
conn_data.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
package peer
import (
"bytes"
"errors"
"github.com/muka/peerjs-go/enums"
"github.com/muka/peerjs-go/models"
"github.com/muka/peerjs-go/util"
"github.com/pion/webrtc/v3"
)
const (
//DataChannelIDPrefix used as prefix for random ID
DataChannelIDPrefix = "dc_"
//MaxBufferedAmount max amount to buffer
MaxBufferedAmount = 8 * 1024 * 1024
)
// NewDataConnection create new DataConnection
func NewDataConnection(peerID string, peer *Peer, opts ConnectionOptions) (*DataConnection, error) {
d := &DataConnection{
BaseConnection: newBaseConnection(enums.ConnectionTypeData, peer, opts),
buffer: bytes.NewBuffer([]byte{}),
// encodingQueue: NewEncodingQueue(),
}
d.peerID = peerID
d.id = opts.ConnectionID
if d.id == "" {
d.id = DataChannelIDPrefix + util.RandomToken()
}
d.Label = opts.Label
if d.Label == "" {
d.Label = d.id
}
d.Serialization = opts.Serialization
if d.Serialization == "" {
d.Serialization = enums.SerializationTypeRaw
}
d.Reliable = opts.Reliable
// d.encodingQueue.On("done", d.onQueueDone)
// d.encodingQueue.On("error", d.onQueueErr)
d.negotiator = NewNegotiator(d, opts)
err := d.negotiator.StartConnection(opts)
return d, err
}
type chunkedData struct {
Data []byte
Count int
Total int
}
// DataConnection track a connection with a remote Peer
type DataConnection struct {
BaseConnection
buffer *bytes.Buffer
bufferSize int
buffering bool
// chunkedData map[int]chunkedData
// encodingQueue *EncodingQueue
}
// parse: (data: string) => any = JSON.parse;
// func (d *DataConnection) onQueueDone(data interface{}) {
// buf := data.([]byte)
// d.bufferedSend(buf)
// }
// func (d *DataConnection) onQueueErr(data interface{}) {
// err := data.(error)
// d.log.Errorf(`DC#%s: Error occured in encoding from blob to arraybuffer, close DC: %s`, d.GetID(), err)
// d.Close()
// }
//Initialize called by the Negotiator when the DataChannel is ready
func (d *DataConnection) Initialize(dc *webrtc.DataChannel) {
d.DataChannel = dc
d.configureDataChannel()
}
func (d *DataConnection) configureDataChannel() {
// TODO
// d.DataChannel.binaryType = "arraybuffer";
d.DataChannel.OnOpen(func() {
//TODO
d.log.Debugf(`DC#%s dc connection success`, d.GetID())
d.Open = true
d.Emit(enums.ConnectionEventTypeOpen, nil)
})
d.DataChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
d.log.Debugf(`DC#%s dc onmessage: %v`, d.GetID(), msg.Data)
d.handleDataMessage(msg)
})
d.DataChannel.OnClose(func() {
d.log.Debugf(`DC#%s dc closed for %s`, d.GetID(), d.peerID)
d.Close()
})
}
// Handles a DataChannel message.
func (d *DataConnection) handleDataMessage(msg webrtc.DataChannelMessage) {
// isBinarySerialization := d.Serialization == SerializationTypeBinary ||
// d.Serialization == SerializationTypeBinaryUTF8
if msg.IsString {
d.Emit(enums.ConnectionEventTypeData, string(msg.Data))
} else {
d.Emit(enums.ConnectionEventTypeData, msg.Data)
}
// if (isBinarySerialization) {
// if (datatype == Blob) {
// // Datatype should never be blob
// util.blobToArrayBuffer(data as Blob, (ab) => {
// const unpackedData = util.unpack(ab);
// d.emit(ConnectionEventType.Data, unpackedData);
// });
// return;
// } else if (datatype === ArrayBuffer) {
// deserializedData = util.unpack(data as ArrayBuffer);
// } else if (datatype === String) {
// // String fallback for binary data for browsers that don't support binary yet
// const ab = util.binaryStringToArrayBuffer(data as string);
// deserializedData = util.unpack(ab);
// }
// } else if (d.serialization === SerializationType.JSON) {
// deserializedData = d.parse(data as string);
// }
// // Check if we've chunked--if so, piece things back together.
// // We're guaranteed that this isn't 0.
// if deserializedData.__peerData {
// d.handleChunk(deserializedData)
// return
// }
// d.Emit(ConnectionEventTypeData, deserializedData)
}
// func (d *DataConnection) handleChunk(raw []byte) {
// // const id = data.__peerData;
// // const chunkInfo = d._chunkedData[id] || {
// // data: [],
// // count: 0,
// // total: data.total
// // };
// // chunkInfo.data[data.n] = data.data;
// // chunkInfo.count++;
// // d._chunkedData[id] = chunkInfo;
// // if (chunkInfo.total === chunkInfo.count) {
// // // Clean up before making the recursive call to `_handleDataMessage`.
// // delete d._chunkedData[id];
// // // We've received all the chunks--time to construct the complete data.
// // const data = new Blob(chunkInfo.data);
// // d._handleDataMessage({ data });
// // }
// }
/**
* Exposed functionality for users.
*/
//Close allows user to close connection
func (d *DataConnection) Close() error {
d.buffer = nil
d.bufferSize = 0
// d.chunkedData = map[int]chunkedData{}
if d.negotiator != nil {
d.negotiator.Cleanup()
d.negotiator = nil
}
if d.Provider != nil {
d.Provider.RemoveConnection(d)
d.Provider = nil
}
if d.DataChannel != nil {
d.DataChannel.OnOpen(func() {})
d.DataChannel.OnMessage(func(msg webrtc.DataChannelMessage) {})
d.DataChannel.OnClose(func() {})
d.DataChannel = nil
}
// if d.encodingQueue != nil {
// d.encodingQueue.Destroy()
// d.encodingQueue = nil
// }
if !d.Open {
return nil
}
d.Open = false
d.Emit(enums.ConnectionEventTypeClose, nil)
return nil
}
// Send allows user to send data.
func (d *DataConnection) Send(data []byte, chunked bool) error {
if !d.Open {
err := errors.New("Connection is not open. You should listen for the `open` event before sending messages")
d.Emit(
enums.ConnectionEventTypeError,
err,
)
return err
}
err := d.DataChannel.Send(data)
if err != nil {
d.log.Warnf("Send failed: %s", err)
return err
}
return nil
// if d.Serialization == SerializationTypeJSON {
// // JSON data must be marshalled before send!
// d.log.Debug("Send JSON")
// d.bufferedSend(raw)
// } else if d.Serialization == SerializationTypeBinary || d.Serialization == SerializationTypeBinaryUTF8 {
// panic(errors.New("binarypack encoding is not supported"))
// // NOTE we pack with MessagePack not with binarypack. Understant if this is good enough
// // blob, err := msgpack.Marshal(data)
// // if err != nil {
// // return fmt.Errorf("Failed to pack message: %s", err)
// // }
// // if !chunked && len(blob) > ChunkedMTU {
// // d.log.Debug("Chunk payload")
// // d.sendChunks(blob)
// // return nil
// // }
// // d.log.Debugf("Send encoded payload %v", raw)
// // d.bufferedSend(blob)
// } else {
// d.log.Debug("Send raw payload")
// d.bufferedSend(raw)
// }
}
// func (d *DataConnection) bufferedSend(msg []byte) {
// if d.buffering || !d.trySend(msg) {
// d.buffer.Write(msg)
// d.bufferSize = d.buffer.Len()
// }
// }
// // Returns true if the send succeeds.
// func (d *DataConnection) trySend(msg []byte) bool {
// if !d.Open {
// return false
// }
// if d.DataChannel.BufferedAmount() > MaxBufferedAmount {
// d.buffering = true
// <-time.After(time.Millisecond * 50)
// d.buffering = false
// d.tryBuffer()
// return false
// }
// err := d.DataChannel.Send(msg)
// if err != nil {
// d.log.Errorf(`DC#%s Error sending %s`, d.GetID(), err)
// d.buffering = true
// // d.Close()
// return false
// }
// return true
// }
// Try to send the first message in the buffer.
// func (d *DataConnection) tryBuffer() {
// if !d.Open {
// return
// }
// if d.buffer.Len() == 0 {
// return
// }
// // TODO here buffer is a slice not a continuous array
// // check or reimplement this part!
// msg := d.buffer.Bytes()
// if d.trySend(msg) {
// d.buffer.Reset()
// d.bufferSize = d.buffer.Len()
// d.tryBuffer()
// }
// }
// func (d *DataConnection) sendChunks(raw []byte) {
// panic("sendChunks: binarypack not implemented, please use SerializationTypeRaw")
// // // this method requires a [binarypack] encoding to work
// // chunks := util.Chunk(raw)
// // d.log.Debugf(`DC#%s Try to send %d chunks...`, d.GetID(), len(chunks))
// // for _, chunk := range chunks {
// // d.Send(chunk, true)
// // }
// }
// HandleMessage handles incoming messages
func (d *DataConnection) HandleMessage(message *models.Message) error {
payload := message.Payload
switch message.Type {
case enums.ServerMessageTypeAnswer:
d.negotiator.handleSDP(message.Type, *payload.SDP)
break
case enums.ServerMessageTypeCandidate:
err := d.negotiator.HandleCandidate(payload.Candidate)
if err != nil {
d.log.Errorf("Failed to handle candidate for peer=%s: %s", d.peerID, err)
}
break
default:
d.log.Warnf(
"Unrecognized message type: %s from peer: %s",
message.Type,
d.peerID,
)
break
}
return nil
}