MCPcopy Create free account
hub / github.com/HumbleNet/HumbleNet / main_loop

Function main_loop

tests/test_peer.cpp:214–264  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

212static time_t last = 0;
213
214void main_loop()
215{
216 humblenet_p2p_wait(100);
217
218 // Check Peer
219 if (myPeer == 0) {
220 myPeer = humblenet_p2p_get_my_peer_id();
221 if (myPeer != 0) {
222 std::cout << "My Peer is " << myPeer << std::endl;
223 connected = true;
224 }
225 }
226
227 // Poll if connected
228 if (connected) {
229 std::unordered_set<PeerId> toRemove;
230
231 bool done = false;
232 do {
233 char buff[1024];
234 PeerId otherPeer = 0;
235 int ret = humblenet_p2p_recvfrom(buff, sizeof(buff), &otherPeer, CHANNEL);
236 if (ret < 0) {
237 if (otherPeer != 0) {
238 toRemove.insert(otherPeer);
239 } else {
240 done = true;
241 }
242 } else if (ret > 0) {
243 processMessage(otherPeer, buff, ret);
244 } else {
245 done = true;
246 }
247 } while (!done);
248
249 time_t now = time(NULL);
250 if (now > last) {
251 for (auto p: pendingPeers) {
252 if (!sendHello(p)) {
253 toRemove.insert(p);
254 }
255 }
256 last = now;
257 }
258
259 // disconnect lost peers
260 for (auto p: toRemove) {
261 disconnectPeer(p);
262 }
263 }
264}
265
266int main(int argc, char* argv[])
267{

Callers 1

mainFunction · 0.70

Calls 5

processMessageFunction · 0.85
timeFunction · 0.85
sendHelloFunction · 0.85
disconnectPeerFunction · 0.85
insertMethod · 0.45

Tested by

no test coverage detected