-
Notifications
You must be signed in to change notification settings - Fork 8
/
OhmProtocolUnicast.cpp
267 lines (212 loc) · 6.06 KB
/
OhmProtocolUnicast.cpp
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
#include "OhmReceiver.h"
#include <OpenHome/Private/Ascii.h>
#include <OpenHome/Private/Arch.h>
#include <OpenHome/Private/Debug.h>
#include <OpenHome/Private/Env.h>
#ifdef _WIN32
# pragma warning(disable:4355) // use of 'this' in ctor lists safe in this case
#endif
using namespace OpenHome;
using namespace OpenHome::Av;
// OhmProtocolUnicast
OhmProtocolUnicast::OhmProtocolUnicast(Environment& aEnv, IOhmReceiver& aReceiver, IOhmMsgFactory& aFactory)
: iEnv(aEnv)
, iReceiver(&aReceiver)
, iFactory(&aFactory)
, iSocket(aEnv)
, iReadBuffer(iSocket)
, iTimerJoin(aEnv, MakeFunctor(*this, &OhmProtocolUnicast::SendJoin), "OhmProtocolUnicastJoin")
, iTimerListen(aEnv, MakeFunctor(*this, &OhmProtocolUnicast::SendListen), "OhmProtocolUnicastListen")
, iTimerLeave(aEnv, MakeFunctor(*this, &OhmProtocolUnicast::TimerLeaveExpired), "OhmProtocolUnicastLeave")
{
}
void OhmProtocolUnicast::HandleAudio(const OhmHeader& aHeader)
{
Broadcast(iFactory->CreateAudio(iReadBuffer, aHeader));
if (iLeaving) {
iTimerLeave.Cancel();
SendLeave();
iReadBuffer.ReadInterrupt();
}
}
void OhmProtocolUnicast::HandleTrack(const OhmHeader& aHeader)
{
Broadcast(iFactory->CreateTrack(iReadBuffer, aHeader));
}
void OhmProtocolUnicast::HandleMetatext(const OhmHeader& aHeader)
{
Broadcast(iFactory->CreateMetatext(iReadBuffer, aHeader));
}
void OhmProtocolUnicast::HandleSlave(const OhmHeader& aHeader)
{
OhmHeaderSlave headerSlave;
headerSlave.Internalise(iReadBuffer, aHeader);
iSlaveCount = headerSlave.SlaveCount();
ReaderBinary reader(iReadBuffer);
for (TUint i = 0; i < iSlaveCount; i++) {
TIpAddress address = reader.ReadUintLe(4); // utterly confused due to ohNet's ridiculous decision to pass IpAddresses around memory in BE form
TUint port = reader.ReadUintBe(2);
iSlaveList[i].SetAddress(address);
iSlaveList[i].SetPort(port);
}
}
void OhmProtocolUnicast::RequestResend(const Brx& aFrames)
{
TUint bytes = aFrames.Bytes();
if (bytes > 0)
{
Bws<OhmHeader::kHeaderBytes + 400> buffer;
WriterBuffer writer(buffer);
OhmHeaderResend headerResend(bytes / 4);
OhmHeader header(OhmHeader::kMsgTypeResend, headerResend.MsgBytes());
header.Externalise(writer);
headerResend.Externalise(writer);
writer.Write(aFrames);
iSocket.Send(buffer, iEndpoint);
}
}
void OhmProtocolUnicast::Broadcast(OhmMsg& aMsg)
{
if (iSlaveCount > 0)
{
WriterBuffer writer(iMessageBuffer);
writer.Flush();
aMsg.Externalise(writer);
for (TUint i = 0; i < iSlaveCount; i++) {
iSocket.Send(iMessageBuffer, iSlaveList[i]);
}
}
iReceiver->Add(aMsg);
}
void OhmProtocolUnicast::Play(TIpAddress aInterface, TUint aTtl, const Endpoint& aEndpoint)
{
iLeaving = false;
iSlaveCount = 0;
iEndpoint.Replace(aEndpoint);
iSocket.OpenUnicast(aInterface, aTtl);
try {
OhmHeader header;
SendJoin();
// Phase 1, periodically send join until Track and Metatext have been received
TBool joinComplete = false;
TBool receivedTrack = false;
TBool receivedMetatext = false;
while (!joinComplete) {
try {
header.Internalise(iReadBuffer);
switch(header.MsgType()) {
case OhmHeader::kMsgTypeJoin:
case OhmHeader::kMsgTypeListen:
case OhmHeader::kMsgTypeLeave:
break;
case OhmHeader::kMsgTypeAudio:
HandleAudio(header);
break;
case OhmHeader::kMsgTypeTrack:
HandleTrack(header);
receivedTrack = true;
joinComplete = receivedMetatext;
break;
case OhmHeader::kMsgTypeMetatext:
HandleMetatext(header);
receivedMetatext = true;
joinComplete = receivedTrack;
break;
case OhmHeader::kMsgTypeSlave:
HandleSlave(header);
break;
case OhmHeader::kMsgTypeResend:
iReceiver->ResendSeen();
break;
}
iReadBuffer.ReadFlush();
}
catch (OhmError&) {
}
}
iTimerJoin.Cancel();
// Phase 2, periodically send listen if required
iTimerListen.FireIn((kTimerListenTimeoutMs >> 2) - iEnv.Random(kTimerListenTimeoutMs >> 3)); // listen primary timeout
for (;;) {
try {
header.Internalise(iReadBuffer);
switch(header.MsgType()) {
case OhmHeader::kMsgTypeJoin:
case OhmHeader::kMsgTypeLeave:
break;
case OhmHeader::kMsgTypeListen:
iTimerListen.FireIn((kTimerListenTimeoutMs >> 1) - iEnv.Random(kTimerListenTimeoutMs >> 3)); // listen secondary timeout
break;
case OhmHeader::kMsgTypeAudio:
HandleAudio(header);
break;
case OhmHeader::kMsgTypeTrack:
HandleTrack(header);
break;
case OhmHeader::kMsgTypeMetatext:
HandleMetatext(header);
break;
case OhmHeader::kMsgTypeSlave:
HandleSlave(header);
break;
case OhmHeader::kMsgTypeResend:
iReceiver->ResendSeen();
break;
}
iReadBuffer.ReadFlush();
}
catch (OhmError&) {
}
}
}
catch (ReaderError&) {
}
iReadBuffer.ReadFlush();
iLeaving = false;
iTimerJoin.Cancel();
iTimerListen.Cancel();
iTimerLeave.Cancel();
iSocket.Close();
}
void OhmProtocolUnicast::Stop()
{
iLeaving = true;
iTimerLeave.FireIn(kTimerLeaveTimeoutMs);
}
void OhmProtocolUnicast::EmergencyStop()
{
SendLeave();
TimerLeaveExpired();
}
void OhmProtocolUnicast::SendJoin()
{
Send(OhmHeader::kMsgTypeJoin);
iTimerJoin.FireIn(kTimerJoinTimeoutMs);
}
void OhmProtocolUnicast::SendListen()
{
Send(OhmHeader::kMsgTypeListen);
iTimerListen.FireIn((kTimerListenTimeoutMs >> 2) - iEnv.Random(kTimerListenTimeoutMs >> 3)); // listen primary timeout
}
void OhmProtocolUnicast::SendLeave()
{
Send(OhmHeader::kMsgTypeLeave);
}
void OhmProtocolUnicast::Send(TUint aType)
{
Bws<OhmHeader::kHeaderBytes> buffer;
WriterBuffer writer(buffer);
OhmHeader msg(aType, 0);
msg.Externalise(writer);
try {
iSocket.Send(buffer, iEndpoint);
}
catch (NetworkError&)
{
}
}
void OhmProtocolUnicast::TimerLeaveExpired()
{
SendLeave();
iReadBuffer.ReadInterrupt();
}