-
Notifications
You must be signed in to change notification settings - Fork 8
/
OhmSocket.cpp
172 lines (146 loc) · 3.16 KB
/
OhmSocket.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
#include "OhmSocket.h"
using namespace OpenHome;
using namespace OpenHome::Av;
// OhmSocket
// Sends on same socket in Unicast mode, but different socket in Multicast mode
OhmSocket::OhmSocket(Environment& aEnv)
: iEnv(aEnv)
, iRxSocket(0)
, iTxSocket(0)
, iReader(0)
{
}
void OhmSocket::OpenUnicast(TIpAddress aInterface, TUint aTtl)
{
ASSERT(!iRxSocket);
ASSERT(!iTxSocket);
ASSERT(!iReader);
iRxSocket = new SocketUdp(iEnv, 0, aInterface);
iRxSocket->SetTtl(aTtl);
iRxSocket->SetRecvBufBytes(kReceiveBufBytes);
iRxSocket->SetSendBufBytes(kSendBufBytes);
iReader = new UdpReader(*iRxSocket);
iThis.Replace(Endpoint(iRxSocket->Port(), aInterface));
}
void OhmSocket::OpenMulticast(TIpAddress aInterface, TUint aTtl, const Endpoint& aEndpoint)
{
ASSERT(!iRxSocket);
ASSERT(!iTxSocket);
ASSERT(!iReader);
iRxSocket = new SocketUdpMulticast(iEnv, aInterface, aEndpoint);
iRxSocket->SetRecvBufBytes(kReceiveBufBytes);
iTxSocket = new SocketUdp(iEnv, 0, aInterface);
iTxSocket->SetTtl(aTtl);
iTxSocket->SetSendBufBytes(kSendBufBytes);
iReader = new UdpReader(*iRxSocket);
iThis.Replace(aEndpoint);
}
void OhmSocket::Send(const Brx& aBuffer, const Endpoint& aEndpoint)
{
if (iTxSocket) {
iTxSocket->Send(aBuffer, aEndpoint);
}
else {
iRxSocket->Send(aBuffer, aEndpoint);
}
}
Endpoint OhmSocket::This() const
{
return (iThis);
}
Endpoint OhmSocket::Sender() const
{
ASSERT(iReader);
return (iReader->Sender());
}
void OhmSocket::Close()
{
ASSERT(iReader);
delete (iReader);
iReader = 0;
ASSERT (iRxSocket);
delete (iRxSocket);
iRxSocket = 0;
if (iTxSocket) {
delete (iTxSocket);
iTxSocket = 0;
}
iThis.Replace(Endpoint());
}
void OhmSocket::Read(Bwx& aBuffer)
{
ASSERT(iReader);
iReader->Read(aBuffer);
}
void OhmSocket::ReadFlush()
{
ASSERT(iReader);
iReader->ReadFlush();
}
void OhmSocket::ReadInterrupt()
{
ASSERT(iReader);
iReader->ReadInterrupt();
}
OhmSocket::~OhmSocket()
{
if (iRxSocket != 0) {
Close();
}
}
// OhzSocket
OhzSocket::OhzSocket(Environment& aEnv)
: iEnv(aEnv)
, iRxSocket(0)
, iTxSocket(0)
, iEndpoint(51972, Brn("239.255.255.250"))
{
}
const Endpoint& OhzSocket::This() const
{
return (iEndpoint);
}
void OhzSocket::Open(TIpAddress aInterface, TUint aTtl)
{
ASSERT(!iRxSocket);
iRxSocket = new SocketUdpMulticast(iEnv, aInterface, iEndpoint);
iTxSocket = new SocketUdp(iEnv, 0, aInterface);
iTxSocket->SetTtl(aTtl);
iReader = new UdpReader(*iRxSocket);
}
void OhzSocket::Send(const Brx& aBuffer)
{
ASSERT(iTxSocket);
iTxSocket->Send(aBuffer, iEndpoint);
}
void OhzSocket::Close()
{
ASSERT(iRxSocket);
delete (iRxSocket);
delete (iTxSocket);
delete (iReader);
iRxSocket = 0;
iTxSocket = 0;
iReader = 0;
}
void OhzSocket::Read(Bwx& aBuffer)
{
ASSERT(iRxSocket);
iReader->Read(aBuffer);
}
void OhzSocket::ReadFlush()
{
ASSERT(iRxSocket != 0);
iReader->ReadFlush();
}
void OhzSocket::ReadInterrupt()
{
ASSERT(iRxSocket != 0);
iReader->ReadInterrupt();
}
OhzSocket::~OhzSocket()
{
if (iRxSocket != 0) {
Close();
}
}