-
Notifications
You must be signed in to change notification settings - Fork 76
/
datachannel.go
227 lines (191 loc) · 5.51 KB
/
datachannel.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
/*
Package webrtc/data contains the go wrapper for the Peer-to-Peer Data API
portion of WebRTC spec.
See: https://w3c.github.io/webrtc-pc/#idl-def-RTCDataChannel
*/
package webrtc
/*
#cgo CXXFLAGS: -std=c++0x
#cgo LDFLAGS: -L${SRCDIR}/lib
#cgo android pkg-config: webrtc-android-armeabi-v7a.pc
#cgo linux,arm pkg-config: webrtc-linux-arm.pc
#cgo linux,386 pkg-config: webrtc-linux-386.pc
#cgo linux,amd64 pkg-config: webrtc-linux-amd64.pc
#cgo darwin,amd64 pkg-config: webrtc-darwin-amd64.pc
#include <stdlib.h> // Needed for C.free
#include "datachannel.h"
*/
import "C"
import (
"unsafe"
)
type DataState int
const (
DataStateConnecting DataState = iota
DataStateOpen
DataStateClosing
DataStateClosed
)
func (s DataState) String() string {
return EnumToStringSafe(int(s), []string{
"Connecting",
"Open",
"Closing",
"Closed",
})
}
var DCMap = NewCGOMap()
/* DataChannel
OnError - is not implemented because the underlying Send
always returns true as specified for SCTP, there is no reasonable
exposure of other specific errors from the native code, and OnClose
already covers the bases.
*/
type DataChannel struct {
BufferedAmountLowThreshold int
BinaryType string
// Event Handlers
OnOpen func()
OnClose func()
OnMessage func([]byte) // byte slice.
OnBufferedAmountLow func()
cgoChannel C.CGO_Channel // Internal DataChannel functionality.
cgoChannelObserver unsafe.Pointer
index int // Index into the DCMap
}
// Create a Go Channel struct, and prepare internal CGO references / observers.
// The most reasonable place for this to be created is from PeerConnection,
// which is not available in the subpackage.
func NewDataChannel(o unsafe.Pointer) *DataChannel {
if o == nil {
return nil
}
c := new(DataChannel)
c.index = DCMap.Set(c)
c.BinaryType = "blob"
cgoChannel := C.CGO_Channel_RegisterObserver(o, C.int(c.index))
c.cgoChannel = (C.CGO_Channel)(cgoChannel)
c.cgoChannelObserver = o
return c
}
func deleteDataChannel(index int) {
DCMap.Delete(index)
return
}
// Send a message over a DataChannel in binary mode.
func (c *DataChannel) Send(data []byte) {
c.sendInternal(data, true)
}
// SendText sends a message over the DataChannel in text mode.
func (c *DataChannel) SendText(text string) {
if len(text) > 0 {
c.sendInternal([]byte(text), false)
}
}
func (c *DataChannel) sendInternal(data []byte, binary bool) {
if nil == data {
return
}
C.CGO_Channel_Send(c.cgoChannel, unsafe.Pointer(&data[0]), C.int(len(data)), C.bool(binary))
}
func (c *DataChannel) Close() error {
C.CGO_Channel_Close(c.cgoChannel)
return nil
}
func (c *DataChannel) Label() string {
s := C.CGO_Channel_Label(c.cgoChannel)
defer C.free(unsafe.Pointer(s))
return C.GoString(s)
}
func (c *DataChannel) Ordered() bool {
return bool(C.CGO_Channel_Ordered(c.cgoChannel))
}
func (c *DataChannel) Protocol() string {
p := C.CGO_Channel_Protocol(c.cgoChannel)
defer C.free(unsafe.Pointer(p))
return C.GoString(p)
}
func (c *DataChannel) MaxPacketLifeTime() uint {
return uint(C.CGO_Channel_MaxRetransmitTime(c.cgoChannel))
}
func (c *DataChannel) MaxRetransmits() uint {
return uint(C.CGO_Channel_MaxRetransmits(c.cgoChannel))
}
func (c *DataChannel) Negotiated() bool {
return bool(C.CGO_Channel_Negotiated(c.cgoChannel))
}
func (c *DataChannel) ID() int {
return int(C.CGO_Channel_ID(c.cgoChannel))
}
func (c *DataChannel) ReadyState() DataState {
return (DataState)(C.CGO_Channel_ReadyState(c.cgoChannel))
}
func (c *DataChannel) BufferedAmount() int {
return int(C.CGO_Channel_BufferedAmount(c.cgoChannel))
}
type DataChannelInit struct {
Ordered bool
MaxPacketLifeTime int
MaxRetransmits int
Protocol string
Negotiated bool
ID int
}
//
// === cgo hooks for user-provided Go callbacks, and enums ===
//
//export cgoChannelOnMessage
func cgoChannelOnMessage(goChannel int, cBytes unsafe.Pointer, size int) {
bytes := C.GoBytes(cBytes, C.int(size))
dc := DCMap.Get(goChannel).(*DataChannel)
if nil != dc.OnMessage {
dc.OnMessage(bytes)
}
}
//export cgoChannelOnStateChange
func cgoChannelOnStateChange(goChannel int) {
dc := DCMap.Get(goChannel).(*DataChannel)
switch dc.ReadyState() {
// Picks between different Go callbacks...
case DataStateConnecting:
case DataStateClosing:
// golang switches don't fallthrough
case DataStateOpen:
if nil != dc.OnOpen {
dc.OnOpen()
}
case DataStateClosed:
if nil != dc.OnClose {
dc.OnClose()
}
default:
panic("fired an un-implemented data.Channel StateChange.")
}
}
//export cgoChannelOnBufferedAmountChange
func cgoChannelOnBufferedAmountChange(goChannel int, amount int) {
dc := DCMap.Get(goChannel).(*DataChannel)
if nil != dc.OnBufferedAmountLow {
if amount <= dc.BufferedAmountLowThreshold {
dc.OnBufferedAmountLow()
}
}
}
var _cgoDataStateConnecting = int(C.CGO_DataStateConnecting)
var _cgoDataStateOpen = int(C.CGO_DataStateOpen)
var _cgoDataStateClosing = int(C.CGO_DataStateClosing)
var _cgoDataStateClosed = int(C.CGO_DataStateClosed)
// Testing helpers
func cgoFakeDataChannel() unsafe.Pointer {
return unsafe.Pointer(C.CGO_getFakeDataChannel())
}
func cgoFakeMessage(c *DataChannel, b []byte, size int) {
C.CGO_fakeMessage((C.CGO_Channel)(c.cgoChannel),
unsafe.Pointer(&b[0]), C.int(size))
}
func cgoFakeStateChange(c *DataChannel, s DataState) {
C.CGO_fakeStateChange((C.CGO_Channel)(c.cgoChannel), (C.int)(s))
}
func cgoFakeBufferAmount(c *DataChannel, amount int) {
C.CGO_fakeBufferAmount((C.CGO_Channel)(c.cgoChannel), (C.int)(amount))
}