RouteNextHopsMulti RouteNextHopsMulti::fromFollyDynamic(const folly::dynamic& json) { RouteNextHopsMulti nh; for (const auto& pair : json.items()) { nh.update(ClientID(pair.first.asInt()), RouteNextHopEntry::fromFollyDynamic(pair.second)); } return nh; }
ClientID RouteNextHopsMulti::findLowestAdminDistance() { if (map_.size() == 0) { // We'll set it on the next add return ClientID(-1); } auto lowest = map_.begin(); auto it = map_.begin(); while (it != map_.end()) { if (it->second.getAdminDistance() < lowest->second.getAdminDistance()) { lowest = it; } else if (it->second.isSame(lowest->second) && it->first < lowest->first) { // If we have two clients with the same admin distance, pick the lowest // client id - this is an arbritary, but deterministic, choice lowest = it; } ++it; } return lowest->first; }
bool Connect (const CString &sHostspec, CHTTPClientSession &Session) { CString sHost; CString sPort; urlParseHostspec(sHostspec, &sHost, &sPort); EInetsErrors iError = Session.Connect(sHost, sPort); if (iError != inetsOK) { switch (iError) { case inetsDNSError: printf("Unable to resolve IP address for: %s\n", sHost.GetASCIIZPointer()); return false; case inetsCannotConnect: printf("Unable to connect to: %s\n", sHostspec.GetASCIIZPointer()); return false; default: printf("Unknown error connecting to: %s\n", sHostspec.GetASCIIZPointer()); return false; } } // Use a constant clientID (representing TransData) BYTE ClientIDBytes[4] = { 0, 1, 0, 1 }; CIntegerIP ClientID(4, ClientIDBytes); CString sClientID = ClientID.AsBase64(); // Compose a connect payload CJSONValue Payload(CJSONValue::typeObject); Payload.InsertHandoff(FIELD_CLIENT_ID, CJSONValue(sClientID)); Payload.InsertHandoff(FIELD_CLIENT_VERSION, CJSONValue(CLIENT_TYPE)); Payload.InsertHandoff(FIELD_PROTOCOL_VERSION, CJSONValue(TRANS_SERVICE_VERSION)); // Now issue the connect command CHTTPMessage Request; InitRequest(sHost, METHOD_POST, FUNC_CONNECT, &Request); Request.SetBody(new CJSONMessage(Payload)); // Send the request and wait for response CHTTPMessage Response; if (iError = Session.Send(Request, &Response)) { Session.Disconnect(); printf("Unable to send to: %s\n", sHostspec.GetASCIIZPointer()); return false; } // If we got a 301 redirect then we try the whole thing again if (Response.GetStatusCode() == 301) { Session.Disconnect(); // LATER: Set up the new host and recurse // LATER: Keep track of recursion count, in case of ping-pong redirects printf("REDIRECT\n"); return false; } // If we get an error, return else if (Response.GetStatusCode() != 200) { Session.Disconnect(); printf("[%d] %s\n", Response.GetStatusCode(), Response.GetStatusMsg().GetASCIIZPointer()); return false; } // Get the JSON response CString sError; CJSONValue ResponseData; if (!GetJSONResponse(Response, &ResponseData, &sError)) { Session.Disconnect(); printf("Unable to parse JSON reply: %s\n", sError.GetASCIIZPointer()); return false; } // Done printf("Connected to %s\n", sHostspec.GetASCIIZPointer()); return true; }
void xrServer::Process_event (NET_Packet& P, ClientID sender) { # ifdef SLOW_VERIFY_ENTITIES VERIFY (verify_entities()); # endif u32 timestamp; u16 type; u16 destination; u32 MODE = net_flags(TRUE,TRUE); // correct timestamp with server-unique-time (note: direct message correction) P.r_u32 (timestamp ); // read generic info P.r_u16 (type ); P.r_u16 (destination); CSE_Abstract* receiver = game->get_entity_from_eid (destination); if (receiver) { R_ASSERT(receiver->owner); receiver->OnEvent (P,type,timestamp,sender); }; switch (type) { case GE_GAME_EVENT: { u16 game_event_type; P.r_u16(game_event_type); game->AddDelayedEvent(P,game_event_type,timestamp,sender); }break; case GE_INFO_TRANSFER: case GE_WPN_STATE_CHANGE: case GE_ZONE_STATE_CHANGE: case GE_ACTOR_JUMPING: case GEG_PLAYER_PLAY_HEADSHOT_PARTICLE: case GEG_PLAYER_ATTACH_HOLDER: case GEG_PLAYER_DETACH_HOLDER: case GEG_PLAYER_ITEM2SLOT: case GEG_PLAYER_ITEM2BELT: case GEG_PLAYER_ITEM2RUCK: case GE_GRENADE_EXPLODE: { SendBroadcast (BroadcastCID,P,MODE); }break; case GEG_PLAYER_ACTIVATEARTEFACT: { Process_event_activate (P,sender,timestamp,destination,P.r_u16(), true); break; }; case GE_INV_ACTION: { xrClientData* CL = ID_to_client(sender); if (CL) CL->net_Ready = TRUE; if (SV_Client) SendTo(SV_Client->ID, P, net_flags(TRUE, TRUE)); }break; case GE_RESPAWN: { CSE_Abstract* E = receiver; if (E) { R_ASSERT (E->s_flags.is(M_SPAWN_OBJECT_PHANTOM)); svs_respawn R; R.timestamp = timestamp + E->RespawnTime*1000; R.phantom = destination; q_respawn.insert (R); } } break; case GE_TRADE_BUY: case GE_OWNERSHIP_TAKE: { Process_event_ownership (P,sender,timestamp,destination); VERIFY (verify_entities()); }break; case GE_OWNERSHIP_TAKE_MP_FORCED: { Process_event_ownership (P,sender,timestamp,destination,TRUE); VERIFY (verify_entities()); }break; case GE_TRADE_SELL: case GE_OWNERSHIP_REJECT: case GE_LAUNCH_ROCKET: { Process_event_reject (P,sender,timestamp,destination,P.r_u16()); VERIFY (verify_entities()); }break; case GE_DESTROY: { Process_event_destroy (P,sender,timestamp,destination, NULL); VERIFY (verify_entities()); } break; case GE_TRANSFER_AMMO: { u16 id_entity; P.r_u16 (id_entity); CSE_Abstract* e_parent = receiver; // кто забирает (для своих нужд) CSE_Abstract* e_entity = game->get_entity_from_eid (id_entity); // кто отдает if (!e_entity) break; if (0xffff != e_entity->ID_Parent) break; // this item already taken xrClientData* c_parent = e_parent->owner; xrClientData* c_from = ID_to_client (sender); R_ASSERT (c_from == c_parent); // assure client ownership of event // Signal to everyone (including sender) SendBroadcast (BroadcastCID,P,MODE); // Perfrom real destroy entity_Destroy (e_entity ); VERIFY (verify_entities()); } break; case GE_HIT: case GE_HIT_STATISTIC: { P.r_pos -=2; if (type == GE_HIT_STATISTIC) { P.B.count -= 4; P.w_u32(sender.value()); }; game->AddDelayedEvent(P,GAME_EVENT_ON_HIT, 0, ClientID() ); } break; case GE_ASSIGN_KILLER: { u16 id_src; P.r_u16 (id_src); CSE_Abstract *e_dest = receiver; // кто умер // this is possible when hit event is sent before destroy event if (!e_dest) break; CSE_ALifeCreatureAbstract *creature = smart_cast<CSE_ALifeCreatureAbstract*>(e_dest); if (creature) creature->set_killer_id( id_src ); // Msg ("[%d][%s] killed [%d][%s]",id_src,id_src==u16(-1) ? "UNKNOWN" : game->get_entity_from_eid(id_src)->name_replace(),id_dest,e_dest->name_replace()); break; } case GE_CHANGE_VISUAL: { CSE_Visual* visual = smart_cast<CSE_Visual*>(receiver); VERIFY(visual); string256 tmp; P.r_stringZ (tmp); visual->set_visual (tmp); }break; case GE_DIE: { // Parse message u16 id_dest = destination, id_src; P.r_u16 (id_src); xrClientData *l_pC = ID_to_client(sender); VERIFY (game && l_pC); #ifndef MASTER_GOLD if ((game->Type() != eGameIDSingle) && l_pC && l_pC->owner) { Msg ("* [%2d] killed by [%2d] - sended by [%s:%2d]", id_dest, id_src, game->get_option_s(*l_pC->name,"name","Player"), l_pC->owner->ID); } #endif // #ifndef MASTER_GOLD CSE_Abstract* e_dest = receiver; // кто умер // this is possible when hit event is sent before destroy event if (!e_dest) break; #ifndef MASTER_GOLD if (game->Type() != eGameIDSingle) Msg ("* [%2d] is [%s:%s]", id_dest, *e_dest->s_name, e_dest->name_replace()); #endif // #ifndef MASTER_GOLD CSE_Abstract* e_src = game->get_entity_from_eid (id_src ); // кто убил if (!e_src) { xrClientData* C = (xrClientData*) game->get_client(id_src); if (C) e_src = C->owner; }; VERIFY (e_src); if (!e_src) { Msg("! ERROR: SV: src killer not exist."); return; } // R_ASSERT2 (e_dest && e_src, "Killer or/and being killed are offline or not exist at all :("); #ifndef MASTER_GOLD if (game->Type() != eGameIDSingle) Msg ("* [%2d] is [%s:%s]", id_src, *e_src->s_name, e_src->name_replace()); #endif // #ifndef MASTER_GOLD game->on_death (e_dest,e_src); xrClientData* c_src = e_src->owner; // клиент, чей юнит убил if (c_src->owner->ID == id_src) { // Main unit P.w_begin (M_EVENT); P.w_u32 (timestamp); P.w_u16 (type); P.w_u16 (destination); P.w_u16 (id_src); P.w_clientID (c_src->ID); } SendBroadcast (BroadcastCID,P,MODE); ////////////////////////////////////////////////////////////////////////// // if (game->Type() == eGameIDSingle) { P.w_begin (M_EVENT); P.w_u32 (timestamp); P.w_u16 (GE_KILL_SOMEONE); P.w_u16 (id_src); P.w_u16 (destination); SendTo (c_src->ID, P, net_flags(TRUE, TRUE)); } ////////////////////////////////////////////////////////////////////////// VERIFY (verify_entities()); } break; case GE_ADDON_ATTACH: case GE_ADDON_DETACH: case GE_CHANGE_POS: { SendTo(SV_Client->ID, P, net_flags(TRUE, TRUE)); }break; case GE_INSTALL_UPGRADE: { shared_str upgrade_id; P.r_stringZ ( upgrade_id ); CSE_ALifeInventoryItem* iitem = smart_cast<CSE_ALifeInventoryItem*>( receiver ); if ( !iitem ) { break; } iitem->add_upgrade ( upgrade_id ); }break; case GEG_PLAYER_DISABLE_SPRINT: case GEG_PLAYER_WEAPON_HIDE_STATE: { SendTo (SV_Client->ID, P, net_flags(TRUE, TRUE)); # ifdef SLOW_VERIFY_ENTITIES VERIFY (verify_entities()); # endif }break; case GEG_PLAYER_ACTIVATE_SLOT: case GEG_PLAYER_ITEM_EAT: { SendTo(SV_Client->ID, P, net_flags(TRUE, TRUE)); # ifdef SLOW_VERIFY_ENTITIES VERIFY (verify_entities()); # endif }break; case GEG_PLAYER_ITEM_SELL: { game->OnPlayer_Sell_Item(sender, P); }break; case GE_TELEPORT_OBJECT: { game->teleport_object (P,destination); }break; case GE_ADD_RESTRICTION: { game->add_restriction (P,destination); }break; case GE_REMOVE_RESTRICTION: { game->remove_restriction(P,destination); }break; case GE_REMOVE_ALL_RESTRICTIONS: { game->remove_all_restrictions(P,destination); }break; case GE_MONEY: { CSE_Abstract *e_dest = receiver; CSE_ALifeTraderAbstract* pTa = smart_cast<CSE_ALifeTraderAbstract*>(e_dest); pTa->m_dwMoney = P.r_u32(); }break; case GE_FREEZE_OBJECT: break; default: R_ASSERT2 (0,"Game Event not implemented!!!"); break; } }
void CLevel::Demo_Update () { if (!IsDemoPlay() || m_aDemoData.empty() || !m_bDemoStarted) return; if (float(m_lDemoOfs)/lFileSize>0.95f) { g_dwDemoDeltaFrame = 1; dFrame = 1; } static u32 Pos = 0; if (m_bDemoPlayByFrame) Demo_Load_toFrame(m_sDemoFileName.c_str(), m_dwCurDemoFrame+dFrame, m_lDemoOfs); if (Pos >= m_aDemoData.size()) return; if (!m_bDemoPlayByFrame) { for (Pos; Pos < m_aDemoData.size(); Pos++) { u32 CurTime = timeServer_Async(); DemoDataStruct* P = &(m_aDemoData[Pos]); { switch (P->m_dwDataType) { case DATA_SERVER_PACKET: break; case DATA_CLIENT_PACKET: { if (P->m_dwTimeReceive <= CurTime) { Msg("tReceive [%d] - CurTime [%d]",P->m_dwTimeReceive, CurTime); IPureClient::OnMessage(P->Packet.B.data, P->Packet.B.count); } else { break; }; }break; case DATA_FRAME: if (P->m_dwTimeReceive <= CurTime) { Msg("tsReceive [%d] - CurTime [%d]",P->m_dwTimeReceive, CurTime); Server->OnMessage(P->Packet, ClientID()); } else { break; }; break; } } }; } else { while (!m_aDemoData.empty()) { DemoDataStruct* P; P = &(m_aDemoData.front()); if (P->m_dwFrame > m_dwCurDemoFrame) { break; }; switch (P->m_dwDataType) { case DATA_FRAME: { Device.dwTimeDelta = P->FrameTime.dwTimeDelta; Device.dwTimeGlobal = P->FrameTime.dwTimeGlobal; // CurFrameTime.dwTimeServer = Level().timeServer(); // CurFrameTime.dwTimeServer_Delta = Level().timeServer_Delta(); Device.fTimeDelta = P->FrameTime.fTimeDelta; Device.fTimeGlobal = P->FrameTime.fTimeGlobal; }break; case DATA_CLIENT_PACKET: { u16 m_type; P->Packet.r_begin (m_type); IPureClient::OnMessage(P->Packet.B.data, P->Packet.B.count); }break; case DATA_SERVER_PACKET: { u16 m_type; P->Packet.r_begin (m_type); Server->OnMessage(P->Packet, ClientID()); }break; } m_aDemoData.pop_front(); }; }; //------------------------------- if (HUD().GetUI()) { CUIGameDM* game_dm_ui = smart_cast<CUIGameDM*>( HUD().GetUI()->UIGame()); if (game_dm_ui) { if (Pos < m_aDemoData.size()) { string1024 tmp; if (m_bDemoPlayByFrame) { // sprintf(tmp, "Demo Playing. %d perc.", u32(float(m_dwCurDemoFrame)/m_dwLastDemoFrame*100.0f)); if (float(m_lDemoOfs)/lFileSize > 0.9) { int x=0; x=x; } sprintf(tmp, "Demo Playing. %d perc.", u32(float(m_lDemoOfs)/lFileSize*100.0f)); } else { sprintf(tmp, "Demo Playing. %d perc.", u32(float(Pos)/m_aDemoData.size()*100.0f)); } game_dm_ui->SetDemoPlayCaption(tmp); } else game_dm_ui->SetDemoPlayCaption(""); } } //--------------------------------- // m_dwCurDemoFrame++; if (Pos >= m_aDemoData.size() || m_lDemoOfs>lFileSize) { Msg("! ------------- Demo Ended ------------"); }; };