Repository navigation
Expand file tree
/
Copy pathbundle_broadcast.cpp
More file actions
238 lines (193 loc) · 5.71 KB
/
Copy pathbundle_broadcast.cpp
File metadata and controls
238 lines (193 loc) · 5.71 KB
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
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2016 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bundle_broadcast.h"
#ifndef CODE_INLINE
#include "bundle_broadcast.inl"
#endif
#include "network/address.h"
#include "network/event_dispatcher.h"
#include "network/network_interface.h"
#include "network/event_poller.h"
namespace KBEngine {
namespace Network
{
//-------------------------------------------------------------------------------------
BundleBroadcast::BundleBroadcast(NetworkInterface & networkInterface,
uint16 bindPort, uint32 recvWindowSize):
Bundle(NULL, Network::PROTOCOL_UDP),
epListen_(),
networkInterface_(networkInterface),
recvWindowSize_(recvWindowSize),
good_(false),
itry_(5)
{
epListen_.socket(SOCK_DGRAM);
epBroadcast_.socket(SOCK_DGRAM);
if (!epListen_.good() || !epBroadcast_.good())
{
ERROR_MSG(fmt::format("BundleBroadcast::BundleBroadcast: Socket initialization failed! {}\n",
kbe_strerror()));
networkInterface_.dispatcher().breakProcessing();
}
else
{
int count = 0;
while(true)
{
if (epListen_.bind(htons(bindPort), htonl(INADDR_ANY)) != 0)
{
good_ = false;
KBEngine::sleep(10);
count++;
if(count > 30)
{
WARNING_MSG(fmt::format("BundleBroadcast::BundleBroadcast: Cannot bind to port {}, {}\n",
bindPort, kbe_strerror()));
break;
}
}
else
{
epListen_.addr(htons(bindPort), htonl(INADDR_ANY));
good_ = true;
// DEBUG_MSG(fmt::format("BundleBroadcast::BundleBroadcast: epListen {}\n", epListen_.c_str()));
break;
}
}
}
pCurrPacket()->data_resize(recvWindowSize_);
}
//-------------------------------------------------------------------------------------
BundleBroadcast::~BundleBroadcast()
{
close();
}
//-------------------------------------------------------------------------------------
void BundleBroadcast::close()
{
// DEBUG_MSG("BundleBroadcast::close()\n");
epListen_.close();
epBroadcast_.close();
}
//-------------------------------------------------------------------------------------
EventDispatcher & BundleBroadcast::dispatcher()
{
return networkInterface_.dispatcher();
}
//-------------------------------------------------------------------------------------
bool BundleBroadcast::broadcast(uint16 port)
{
if (!epBroadcast_.good())
return false;
if(port == 0)
port = KBE_MACHINE_BROADCAST_SEND_PORT;
epBroadcast_.addr(port, Network::BROADCAST);
if(epBroadcast_.setbroadcast(true) != 0)
{
ERROR_MSG(fmt::format("BundleBroadcast::broadcast: Cannot broadcast socket on port {}, {}\n",
port, kbe_strerror()));
networkInterface_.dispatcher().breakProcessing();
return false;
}
epBroadcast_.sendto(this, htons(port), Network::BROADCAST);
return true;
}
//-------------------------------------------------------------------------------------
bool BundleBroadcast::receive(MessageArgs* recvArgs, sockaddr_in* psin, int32 timeout, bool showerr)
{
if (!epListen_.good())
return false;
struct timeval tv;
fd_set fds;
int icount = 1;
tv.tv_sec = 0;
tv.tv_usec = timeout;
if(!pCurrPacket())
newPacket();
while (1)
{
FD_ZERO( &fds );
FD_SET((int)epListen_, &fds);
int selgot = select(epListen_+1, &fds, NULL, NULL, &tv);
if (selgot == 0)
{
if(icount > itry_)
{
if(showerr)
{
ERROR_MSG("BundleBroadcast::receive: failed! It can be caused by the firewall, the broadcastaddr, etc."
"Maybe broadcastaddr is not a LAN ADDR, or the Machine process is not running.\n");
}
return false;
}
else
{
//DEBUG_MSG(fmt::format("BundleBroadcast::receive: retries({}), bind_addr({}) ...\n",
// icount, epListen_.addr()));
}
icount++;
continue;
}
else if (selgot == -1)
{
if(showerr)
{
ERROR_MSG(fmt::format("BundleBroadcast::receive: select error. {}.\n",
kbe_strerror()));
}
return false;
}
else
{
sockaddr_in sin;
pCurrPacket()->resetPacket();
if(psin == NULL)
psin = &sin;
pCurrPacket()->data_resize(recvWindowSize_);
int len = epListen_.recvfrom(pCurrPacket()->data(), recvWindowSize_, *psin);
if (len == -1)
{
if(showerr)
{
ERROR_MSG(fmt::format("BundleBroadcast::receive: recvfrom error. {}.\n",
kbe_strerror()));
}
continue;
}
//DEBUG_MSG(fmt::format("BundleBroadcast::receive: from {}, datalen={}.\n",
// inet_ntoa((struct in_addr&)psin->sin_addr.s_addr), len));
pCurrPacket()->wpos(len);
if(recvArgs != NULL)
{
try
{
recvArgs->createFromStream(*pCurrPacket());
}
catch(MemoryStreamException &)
{
ERROR_MSG(fmt::format("BundleBroadcast::receive: data wrong. size={}, from {}.\n",
len, inet_ntoa((struct in_addr&)psin->sin_addr.s_addr)));
continue;
}
}
break;
}
}
return true;
}
//-------------------------------------------------------------------------------------
}
}