MCPcopy Create free account
hub / github.com/deathkiller/jazz2-native / OnClientThread

Method OnClientThread

Sources/Jazz2/Multiplayer/NetworkManagerBase.cpp:1438–1517  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1436# endif
1437
1438 void NetworkManagerBase::OnClientThread(void* param)
1439 {
1440 Thread::SetCurrentName("Multiplayer client");
1441
1442 NetworkManagerBase* _this = static_cast<NetworkManagerBase*>(param);
1443 INetworkHandler* handler = _this->_handler;
1444
1445 ENetHost* host = nullptr;
1446 _this->_host = host;
1447
1448 // Try to connect to each specified endpoint
1449 ENetEvent ev{};
1450 for (std::int32_t i = 0; i < std::int32_t(_this->_desiredEndpoints.size()) && _this->_state != NetworkState::None; i++) {
1451 ENetAddress& addr = _this->_desiredEndpoints[i];
1452 LOGI("[MP] Connecting to {} ({}/{})", _this->AddressToString(addr, true), i + 1, _this->_desiredEndpoints.size());
1453
1454 if (host != nullptr) {
1455 enet_host_destroy(host);
1456 }
1457
1458 host = enet_host_create(nullptr, 1, std::size_t(NetworkChannel::Count), 0, 0);
1459 _this->_host = host;
1460 if (host == nullptr) {
1461 LOGE("[MP] Failed to create client");
1462 _this->OnPeerDisconnected({}, Reason::InvalidParameter);
1463 return;
1464 }
1465
1466 ENetPeer* peer = enet_host_connect(host, &addr, std::size_t(NetworkChannel::Count), _this->_clientData);
1467 if (peer == nullptr) {
1468 continue;
1469 }
1470
1471 std::int32_t n = 10;
1472 while (n > 0) {
1473 if (_this->_state == NetworkState::None) {
1474 n = 0;
1475 break;
1476 }
1477
1478 LOGD("enet_host_service() is trying to connect: {} ms", enet_time_get());
1479 if (enet_host_service(host, &ev, 1000) > 0 && ev.type == ENET_EVENT_TYPE_CONNECT) {
1480 break;
1481 }
1482
1483 n--;
1484 }
1485
1486 if (n != 0) {
1487 _this->_connectedPeers.push_back(Peer(ev.peer));
1488 break;
1489 }
1490 }
1491
1492 Reason reason;
1493 if (_this->_connectedPeers.empty()) {
1494 LOGE("[MP] Failed to connect to the server");
1495 _this->_state = NetworkState::None;

Callers

nothing calls this directly

Calls 14

enet_host_destroyFunction · 0.85
enet_host_createFunction · 0.85
enet_host_connectFunction · 0.85
enet_time_getFunction · 0.85
enet_host_serviceFunction · 0.85
PeerClass · 0.85
AddressToStringMethod · 0.80
DEATH_LIKELYFunction · 0.70
DEATH_UNLIKELYFunction · 0.70
sizeMethod · 0.45
OnPeerDisconnectedMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected