-
Notifications
You must be signed in to change notification settings - Fork 76
/
datachannel.cc
189 lines (151 loc) · 4.75 KB
/
datachannel.cc
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
#include "datachannel.h"
#include "datachannel.hpp"
#include <stdbool.h>
using namespace webrtc;
// Create and register a new DataChannelObserver.
CGO_Channel CGO_Channel_RegisterObserver(void *o, int goChannel) {
auto obs = (CGoDataChannelObserver*)o;
obs->goChannel = goChannel;
return obs->dc.get();
}
void CGO_Channel_Send(CGO_Channel channel, void *data, int size, bool binary) {
auto dc = (webrtc::DataChannelInterface*)channel;
auto bytes = rtc::CopyOnWriteBuffer((uint8_t*)data, size);
auto buffer = DataBuffer(bytes, binary);
dc->Send(buffer);
}
void CGO_Channel_Close(CGO_Channel channel) {
auto dc = (webrtc::DataChannelInterface*)channel;
assert(NULL != dc);
return dc->Close();
}
const char *CGO_Channel_Label(CGO_Channel channel) {
auto dc = (webrtc::DataChannelInterface*)channel;
assert(NULL != dc);
return strdup(dc->label().c_str());
}
const bool CGO_Channel_Ordered(CGO_Channel channel) {
auto dc = (webrtc::DataChannelInterface*)channel;
assert(NULL != dc);
return dc->ordered();
}
int CGO_Channel_MaxRetransmitTime(CGO_Channel channel) {
auto dc = (webrtc::DataChannelInterface*)channel;
assert(NULL != dc);
return dc->maxRetransmitTime();
}
int CGO_Channel_MaxRetransmits(CGO_Channel channel) {
auto dc = (webrtc::DataChannelInterface*)channel;
assert(NULL != dc);
return dc->maxRetransmits();
}
const char *CGO_Channel_Protocol(CGO_Channel channel) {
auto dc = (webrtc::DataChannelInterface*)channel;
assert(NULL != dc);
return strdup(dc->protocol().c_str());
}
const bool CGO_Channel_Negotiated(CGO_Channel channel) {
auto dc = (webrtc::DataChannelInterface*)channel;
assert(NULL != dc);
return dc->negotiated();
}
int CGO_Channel_ID(CGO_Channel channel) {
auto dc = (webrtc::DataChannelInterface*)channel;
assert(NULL != dc);
return dc->id();
}
int CGO_Channel_ReadyState(CGO_Channel channel) {
auto dc = (webrtc::DataChannelInterface*)channel;
assert(NULL != dc);
return dc->state();
}
int CGO_Channel_BufferedAmount(CGO_Channel channel) {
auto dc = (webrtc::DataChannelInterface*)channel;
assert(NULL != dc);
return dc->buffered_amount();
}
//
// === Testing helpers ===
//
// Real DataChannels can only be created from a correctly configured
// PeerConnection, which is outside the scope of this subpackage.
// However, we can still create a fake DataChannelInterface for testing.
class FakeDataChannel : public DataChannelInterface {
public:
virtual void RegisterObserver(DataChannelObserver* observer) {
obs_ = observer;
};
virtual void UnregisterObserver() {};
virtual std::string label() const {
return "fake";
};
virtual bool reliable() const {
return false;
};
virtual int id() const {
return 12345;
};
virtual DataState state() const {
return state_;
};
virtual uint32_t messages_sent() const {
return 1234;
};
virtual uint64_t bytes_sent() const {
return 1234;
};
virtual uint32_t messages_received() const {
return 1234;
};
virtual uint64_t bytes_received() const {
return 1234;
};
virtual uint64_t buffered_amount() const {
return 1234;
};
// Sends data to self.
bool Send(const DataBuffer& buffer) {
obs_->OnMessage(buffer);
return false;
};
void Close() {
SetState(DataState::kClosed);
};
void SetState(DataChannelInterface::DataState state) {
state_ = state;
obs_->OnStateChange();
}
void SetBufferedAmount(int amount) {
obs_->OnBufferedAmountChange(amount);
}
protected:
DataChannelObserver* obs_;
DataState state_ = DataState::kClosed;
};
std::vector<rtc::scoped_refptr<CGoDataChannelObserver>> test_observers;
void* CGO_getFakeDataChannel() {
rtc::scoped_refptr<FakeDataChannel> test_dc = new rtc::RefCountedObject<FakeDataChannel>();
rtc::scoped_refptr<CGoDataChannelObserver> to = new rtc::RefCountedObject<CGoDataChannelObserver>(test_dc);
test_observers.push_back(to);
auto o = to.get();
test_dc->RegisterObserver(o);
return (void *)o;
}
void CGO_fakeMessage(CGO_Channel channel, void *data, int size) {
auto dc = (FakeDataChannel*)channel;
auto bytes = rtc::CopyOnWriteBuffer((char*)data, size);
auto buffer = DataBuffer(bytes, true);
dc->Send(buffer);
}
void CGO_fakeStateChange(CGO_Channel channel, int state) {
auto dc = (FakeDataChannel*)channel;
dc->SetState((DataChannelInterface::DataState)state);
}
void CGO_fakeBufferAmount(CGO_Channel channel, int amount) {
auto dc = (FakeDataChannel*)channel;
dc->SetBufferedAmount(amount);
}
const int CGO_DataStateConnecting = DataChannelInterface::DataState::kConnecting;
const int CGO_DataStateOpen = DataChannelInterface::DataState::kOpen;
const int CGO_DataStateClosing = DataChannelInterface::DataState::kClosing;
const int CGO_DataStateClosed = DataChannelInterface::DataState::kClosed;