clientiface.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <sstream>
  17. #include "clientiface.h"
  18. #include "network/connection.h"
  19. #include "network/serveropcodes.h"
  20. #include "remoteplayer.h"
  21. #include "settings.h"
  22. #include "mapblock.h"
  23. #include "serverenvironment.h"
  24. #include "map.h"
  25. #include "emerge.h"
  26. #include "content_sao.h" // TODO this is used for cleanup of only
  27. #include "log.h"
  28. #include "util/srp.h"
  29. #include "face_position_cache.h"
  30. const char *ClientInterface::statenames[] = {
  31. "Invalid",
  32. "Disconnecting",
  33. "Denied",
  34. "Created",
  35. "AwaitingInit2",
  36. "HelloSent",
  37. "InitDone",
  38. "DefinitionsSent",
  39. "Active",
  40. "SudoMode",
  41. };
  42. std::string ClientInterface::state2Name(ClientState state)
  43. {
  44. return statenames[state];
  45. }
  46. void RemoteClient::ResendBlockIfOnWire(v3s16 p)
  47. {
  48. // if this block is on wire, mark it for sending again as soon as possible
  49. if (m_blocks_sending.find(p) != m_blocks_sending.end()) {
  50. SetBlockNotSent(p);
  51. }
  52. }
  53. void RemoteClient::GetNextBlocks (
  54. ServerEnvironment *env,
  55. EmergeManager * emerge,
  56. float dtime,
  57. std::vector<PrioritySortedBlockTransfer> &dest)
  58. {
  59. // Increment timers
  60. m_nothing_to_send_pause_timer -= dtime;
  61. m_nearest_unsent_reset_timer += dtime;
  62. if(m_nothing_to_send_pause_timer >= 0)
  63. return;
  64. RemotePlayer *player = env->getPlayer(peer_id);
  65. // This can happen sometimes; clients and players are not in perfect sync.
  66. if (!player)
  67. return;
  68. PlayerSAO *sao = player->getPlayerSAO();
  69. if (!sao)
  70. return;
  71. // Won't send anything if already sending
  72. if(m_blocks_sending.size() >= g_settings->getU16
  73. ("max_simultaneous_block_sends_per_client"))
  74. {
  75. //infostream<<"Not sending any blocks, Queue full."<<std::endl;
  76. return;
  77. }
  78. v3f playerpos = sao->getBasePosition();
  79. const v3f &playerspeed = player->getSpeed();
  80. v3f playerspeeddir(0,0,0);
  81. if(playerspeed.getLength() > 1.0*BS)
  82. playerspeeddir = playerspeed / playerspeed.getLength();
  83. // Predict to next block
  84. v3f playerpos_predicted = playerpos + playerspeeddir*MAP_BLOCKSIZE*BS;
  85. v3s16 center_nodepos = floatToInt(playerpos_predicted, BS);
  86. v3s16 center = getNodeBlockPos(center_nodepos);
  87. // Camera position and direction
  88. v3f camera_pos = sao->getEyePosition();
  89. v3f camera_dir = v3f(0,0,1);
  90. camera_dir.rotateYZBy(sao->getPitch());
  91. camera_dir.rotateXZBy(sao->getYaw());
  92. /*infostream<<"camera_dir=("<<camera_dir.X<<","<<camera_dir.Y<<","
  93. <<camera_dir.Z<<")"<<std::endl;*/
  94. /*
  95. Get the starting value of the block finder radius.
  96. */
  97. if(m_last_center != center)
  98. {
  99. m_nearest_unsent_d = 0;
  100. m_last_center = center;
  101. }
  102. /*infostream<<"m_nearest_unsent_reset_timer="
  103. <<m_nearest_unsent_reset_timer<<std::endl;*/
  104. // Reset periodically to workaround for some bugs or stuff
  105. if(m_nearest_unsent_reset_timer > 20.0)
  106. {
  107. m_nearest_unsent_reset_timer = 0;
  108. m_nearest_unsent_d = 0;
  109. //infostream<<"Resetting m_nearest_unsent_d for "
  110. // <<server->getPlayerName(peer_id)<<std::endl;
  111. }
  112. //s16 last_nearest_unsent_d = m_nearest_unsent_d;
  113. s16 d_start = m_nearest_unsent_d;
  114. //infostream<<"d_start="<<d_start<<std::endl;
  115. u16 max_simul_sends_setting = g_settings->getU16
  116. ("max_simultaneous_block_sends_per_client");
  117. u16 max_simul_sends_usually = max_simul_sends_setting;
  118. /*
  119. Check the time from last addNode/removeNode.
  120. Decrease send rate if player is building stuff.
  121. */
  122. m_time_from_building += dtime;
  123. if(m_time_from_building < g_settings->getFloat(
  124. "full_block_send_enable_min_time_from_building"))
  125. {
  126. max_simul_sends_usually
  127. = LIMITED_MAX_SIMULTANEOUS_BLOCK_SENDS;
  128. }
  129. /*
  130. Number of blocks sending + number of blocks selected for sending
  131. */
  132. u32 num_blocks_selected = m_blocks_sending.size();
  133. /*
  134. next time d will be continued from the d from which the nearest
  135. unsent block was found this time.
  136. This is because not necessarily any of the blocks found this
  137. time are actually sent.
  138. */
  139. s32 new_nearest_unsent_d = -1;
  140. // get view range and camera fov from the client
  141. s16 wanted_range = sao->getWantedRange();
  142. float camera_fov = sao->getFov();
  143. // if FOV, wanted_range are not available (old client), fall back to old default
  144. if (wanted_range <= 0) wanted_range = 1000;
  145. if (camera_fov <= 0) camera_fov = (72.0*M_PI/180) * 4./3.;
  146. const s16 full_d_max = MYMIN(g_settings->getS16("max_block_send_distance"), wanted_range);
  147. const s16 d_opt = MYMIN(g_settings->getS16("block_send_optimize_distance"), wanted_range);
  148. const s16 d_blocks_in_sight = full_d_max * BS * MAP_BLOCKSIZE;
  149. //infostream << "Fov from client " << camera_fov << " full_d_max " << full_d_max << std::endl;
  150. s16 d_max = full_d_max;
  151. s16 d_max_gen = MYMIN(g_settings->getS16("max_block_generate_distance"), wanted_range);
  152. // Don't loop very much at a time
  153. s16 max_d_increment_at_time = 2;
  154. if(d_max > d_start + max_d_increment_at_time)
  155. d_max = d_start + max_d_increment_at_time;
  156. s32 nearest_emerged_d = -1;
  157. s32 nearest_emergefull_d = -1;
  158. s32 nearest_sent_d = -1;
  159. //bool queue_is_full = false;
  160. const v3s16 cam_pos_nodes = floatToInt(camera_pos, BS);
  161. const bool occ_cull = g_settings->getBool("server_side_occlusion_culling");
  162. s16 d;
  163. for(d = d_start; d <= d_max; d++) {
  164. /*
  165. Get the border/face dot coordinates of a "d-radiused"
  166. box
  167. */
  168. std::vector<v3s16> list = FacePositionCache::getFacePositions(d);
  169. std::vector<v3s16>::iterator li;
  170. for(li = list.begin(); li != list.end(); ++li) {
  171. v3s16 p = *li + center;
  172. /*
  173. Send throttling
  174. - Don't allow too many simultaneous transfers
  175. - EXCEPT when the blocks are very close
  176. Also, don't send blocks that are already flying.
  177. */
  178. // Start with the usual maximum
  179. u16 max_simul_dynamic = max_simul_sends_usually;
  180. // If block is very close, allow full maximum
  181. if(d <= BLOCK_SEND_DISABLE_LIMITS_MAX_D)
  182. max_simul_dynamic = max_simul_sends_setting;
  183. // Don't select too many blocks for sending
  184. if (num_blocks_selected >= max_simul_dynamic) {
  185. //queue_is_full = true;
  186. goto queue_full_break;
  187. }
  188. // Don't send blocks that are currently being transferred
  189. if (m_blocks_sending.find(p) != m_blocks_sending.end())
  190. continue;
  191. /*
  192. Do not go over max mapgen limit
  193. */
  194. if (blockpos_over_max_limit(p))
  195. continue;
  196. // If this is true, inexistent block will be made from scratch
  197. bool generate = d <= d_max_gen;
  198. /*
  199. Don't generate or send if not in sight
  200. FIXME This only works if the client uses a small enough
  201. FOV setting. The default of 72 degrees is fine.
  202. */
  203. f32 dist;
  204. if (!isBlockInSight(p, camera_pos, camera_dir, camera_fov, d_blocks_in_sight, &dist)) {
  205. continue;
  206. }
  207. /*
  208. Don't send already sent blocks
  209. */
  210. {
  211. if(m_blocks_sent.find(p) != m_blocks_sent.end())
  212. {
  213. continue;
  214. }
  215. }
  216. /*
  217. Check if map has this block
  218. */
  219. MapBlock *block = env->getMap().getBlockNoCreateNoEx(p);
  220. bool surely_not_found_on_disk = false;
  221. bool block_is_invalid = false;
  222. if (block) {
  223. // Reset usage timer, this block will be of use in the future.
  224. block->resetUsageTimer();
  225. // Block is dummy if data doesn't exist.
  226. // It means it has been not found from disk and not generated
  227. if (block->isDummy()) {
  228. surely_not_found_on_disk = true;
  229. }
  230. if (!block->isGenerated())
  231. block_is_invalid = true;
  232. /*
  233. If block is not close, don't send it unless it is near
  234. ground level.
  235. Block is near ground level if night-time mesh
  236. differs from day-time mesh.
  237. */
  238. if (d >= d_opt) {
  239. if (!block->getDayNightDiff())
  240. continue;
  241. }
  242. if (occ_cull && !block_is_invalid &&
  243. env->getMap().isBlockOccluded(block, cam_pos_nodes)) {
  244. continue;
  245. }
  246. }
  247. /*
  248. If block has been marked to not exist on disk (dummy)
  249. and generating new ones is not wanted, skip block.
  250. */
  251. if (!generate && surely_not_found_on_disk) {
  252. // get next one.
  253. continue;
  254. }
  255. /*
  256. Add inexistent block to emerge queue.
  257. */
  258. if(block == NULL || surely_not_found_on_disk || block_is_invalid)
  259. {
  260. if (emerge->enqueueBlockEmerge(peer_id, p, generate)) {
  261. if (nearest_emerged_d == -1)
  262. nearest_emerged_d = d;
  263. } else {
  264. if (nearest_emergefull_d == -1)
  265. nearest_emergefull_d = d;
  266. goto queue_full_break;
  267. }
  268. // get next one.
  269. continue;
  270. }
  271. if(nearest_sent_d == -1)
  272. nearest_sent_d = d;
  273. /*
  274. Add block to send queue
  275. */
  276. PrioritySortedBlockTransfer q((float)dist, p, peer_id);
  277. dest.push_back(q);
  278. num_blocks_selected += 1;
  279. }
  280. }
  281. queue_full_break:
  282. // If nothing was found for sending and nothing was queued for
  283. // emerging, continue next time browsing from here
  284. if(nearest_emerged_d != -1){
  285. new_nearest_unsent_d = nearest_emerged_d;
  286. } else if(nearest_emergefull_d != -1){
  287. new_nearest_unsent_d = nearest_emergefull_d;
  288. } else {
  289. if(d > full_d_max){
  290. new_nearest_unsent_d = 0;
  291. m_nothing_to_send_pause_timer = 2.0;
  292. } else {
  293. if(nearest_sent_d != -1)
  294. new_nearest_unsent_d = nearest_sent_d;
  295. else
  296. new_nearest_unsent_d = d;
  297. }
  298. }
  299. if(new_nearest_unsent_d != -1)
  300. m_nearest_unsent_d = new_nearest_unsent_d;
  301. }
  302. void RemoteClient::GotBlock(v3s16 p)
  303. {
  304. if (m_blocks_modified.find(p) == m_blocks_modified.end()) {
  305. if (m_blocks_sending.find(p) != m_blocks_sending.end())
  306. m_blocks_sending.erase(p);
  307. else
  308. m_excess_gotblocks++;
  309. m_blocks_sent.insert(p);
  310. }
  311. }
  312. void RemoteClient::SentBlock(v3s16 p)
  313. {
  314. if (m_blocks_modified.find(p) != m_blocks_modified.end())
  315. m_blocks_modified.erase(p);
  316. if(m_blocks_sending.find(p) == m_blocks_sending.end())
  317. m_blocks_sending[p] = 0.0;
  318. else
  319. infostream<<"RemoteClient::SentBlock(): Sent block"
  320. " already in m_blocks_sending"<<std::endl;
  321. }
  322. void RemoteClient::SetBlockNotSent(v3s16 p)
  323. {
  324. m_nearest_unsent_d = 0;
  325. m_nothing_to_send_pause_timer = 0;
  326. if(m_blocks_sending.find(p) != m_blocks_sending.end())
  327. m_blocks_sending.erase(p);
  328. if(m_blocks_sent.find(p) != m_blocks_sent.end())
  329. m_blocks_sent.erase(p);
  330. m_blocks_modified.insert(p);
  331. }
  332. void RemoteClient::SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks)
  333. {
  334. m_nearest_unsent_d = 0;
  335. m_nothing_to_send_pause_timer = 0;
  336. for (auto &block : blocks) {
  337. v3s16 p = block.first;
  338. m_blocks_modified.insert(p);
  339. if(m_blocks_sending.find(p) != m_blocks_sending.end())
  340. m_blocks_sending.erase(p);
  341. if(m_blocks_sent.find(p) != m_blocks_sent.end())
  342. m_blocks_sent.erase(p);
  343. }
  344. }
  345. void RemoteClient::notifyEvent(ClientStateEvent event)
  346. {
  347. std::ostringstream myerror;
  348. switch (m_state)
  349. {
  350. case CS_Invalid:
  351. //intentionally do nothing
  352. break;
  353. case CS_Created:
  354. switch (event) {
  355. case CSE_Hello:
  356. m_state = CS_HelloSent;
  357. break;
  358. case CSE_InitLegacy:
  359. m_state = CS_AwaitingInit2;
  360. break;
  361. case CSE_Disconnect:
  362. m_state = CS_Disconnecting;
  363. break;
  364. case CSE_SetDenied:
  365. m_state = CS_Denied;
  366. break;
  367. /* GotInit2 SetDefinitionsSent SetMediaSent */
  368. default:
  369. myerror << "Created: Invalid client state transition! " << event;
  370. throw ClientStateError(myerror.str());
  371. }
  372. break;
  373. case CS_Denied:
  374. /* don't do anything if in denied state */
  375. break;
  376. case CS_HelloSent:
  377. switch(event)
  378. {
  379. case CSE_AuthAccept:
  380. m_state = CS_AwaitingInit2;
  381. if ((chosen_mech == AUTH_MECHANISM_SRP)
  382. || (chosen_mech == AUTH_MECHANISM_LEGACY_PASSWORD))
  383. srp_verifier_delete((SRPVerifier *) auth_data);
  384. chosen_mech = AUTH_MECHANISM_NONE;
  385. break;
  386. case CSE_Disconnect:
  387. m_state = CS_Disconnecting;
  388. break;
  389. case CSE_SetDenied:
  390. m_state = CS_Denied;
  391. if ((chosen_mech == AUTH_MECHANISM_SRP)
  392. || (chosen_mech == AUTH_MECHANISM_LEGACY_PASSWORD))
  393. srp_verifier_delete((SRPVerifier *) auth_data);
  394. chosen_mech = AUTH_MECHANISM_NONE;
  395. break;
  396. default:
  397. myerror << "HelloSent: Invalid client state transition! " << event;
  398. throw ClientStateError(myerror.str());
  399. }
  400. break;
  401. case CS_AwaitingInit2:
  402. switch(event)
  403. {
  404. case CSE_GotInit2:
  405. confirmSerializationVersion();
  406. m_state = CS_InitDone;
  407. break;
  408. case CSE_Disconnect:
  409. m_state = CS_Disconnecting;
  410. break;
  411. case CSE_SetDenied:
  412. m_state = CS_Denied;
  413. break;
  414. /* Init SetDefinitionsSent SetMediaSent */
  415. default:
  416. myerror << "InitSent: Invalid client state transition! " << event;
  417. throw ClientStateError(myerror.str());
  418. }
  419. break;
  420. case CS_InitDone:
  421. switch(event)
  422. {
  423. case CSE_SetDefinitionsSent:
  424. m_state = CS_DefinitionsSent;
  425. break;
  426. case CSE_Disconnect:
  427. m_state = CS_Disconnecting;
  428. break;
  429. case CSE_SetDenied:
  430. m_state = CS_Denied;
  431. break;
  432. /* Init GotInit2 SetMediaSent */
  433. default:
  434. myerror << "InitDone: Invalid client state transition! " << event;
  435. throw ClientStateError(myerror.str());
  436. }
  437. break;
  438. case CS_DefinitionsSent:
  439. switch(event)
  440. {
  441. case CSE_SetClientReady:
  442. m_state = CS_Active;
  443. break;
  444. case CSE_Disconnect:
  445. m_state = CS_Disconnecting;
  446. break;
  447. case CSE_SetDenied:
  448. m_state = CS_Denied;
  449. break;
  450. /* Init GotInit2 SetDefinitionsSent */
  451. default:
  452. myerror << "DefinitionsSent: Invalid client state transition! " << event;
  453. throw ClientStateError(myerror.str());
  454. }
  455. break;
  456. case CS_Active:
  457. switch(event)
  458. {
  459. case CSE_SetDenied:
  460. m_state = CS_Denied;
  461. break;
  462. case CSE_Disconnect:
  463. m_state = CS_Disconnecting;
  464. break;
  465. case CSE_SudoSuccess:
  466. m_state = CS_SudoMode;
  467. if ((chosen_mech == AUTH_MECHANISM_SRP)
  468. || (chosen_mech == AUTH_MECHANISM_LEGACY_PASSWORD))
  469. srp_verifier_delete((SRPVerifier *) auth_data);
  470. chosen_mech = AUTH_MECHANISM_NONE;
  471. break;
  472. /* Init GotInit2 SetDefinitionsSent SetMediaSent SetDenied */
  473. default:
  474. myerror << "Active: Invalid client state transition! " << event;
  475. throw ClientStateError(myerror.str());
  476. break;
  477. }
  478. break;
  479. case CS_SudoMode:
  480. switch(event)
  481. {
  482. case CSE_SetDenied:
  483. m_state = CS_Denied;
  484. break;
  485. case CSE_Disconnect:
  486. m_state = CS_Disconnecting;
  487. break;
  488. case CSE_SudoLeave:
  489. m_state = CS_Active;
  490. break;
  491. default:
  492. myerror << "Active: Invalid client state transition! " << event;
  493. throw ClientStateError(myerror.str());
  494. break;
  495. }
  496. break;
  497. case CS_Disconnecting:
  498. /* we are already disconnecting */
  499. break;
  500. }
  501. }
  502. u64 RemoteClient::uptime() const
  503. {
  504. return porting::getTimeS() - m_connection_time;
  505. }
  506. ClientInterface::ClientInterface(const std::shared_ptr<con::Connection> & con)
  507. :
  508. m_con(con),
  509. m_env(NULL),
  510. m_print_info_timer(0.0)
  511. {
  512. }
  513. ClientInterface::~ClientInterface()
  514. {
  515. /*
  516. Delete clients
  517. */
  518. {
  519. MutexAutoLock clientslock(m_clients_mutex);
  520. for (auto &client_it : m_clients) {
  521. // Delete client
  522. delete client_it.second;
  523. }
  524. }
  525. }
  526. std::vector<u16> ClientInterface::getClientIDs(ClientState min_state)
  527. {
  528. std::vector<u16> reply;
  529. MutexAutoLock clientslock(m_clients_mutex);
  530. for (const auto &m_client : m_clients) {
  531. if (m_client.second->getState() >= min_state)
  532. reply.push_back(m_client.second->peer_id);
  533. }
  534. return reply;
  535. }
  536. /**
  537. * Verify if user limit was reached.
  538. * User limit count all clients from HelloSent state (MT protocol user) to Active state
  539. * @return true if user limit was reached
  540. */
  541. bool ClientInterface::isUserLimitReached()
  542. {
  543. return getClientIDs(CS_HelloSent).size() >= g_settings->getU16("max_users");
  544. }
  545. void ClientInterface::step(float dtime)
  546. {
  547. m_print_info_timer += dtime;
  548. if(m_print_info_timer >= 30.0)
  549. {
  550. m_print_info_timer = 0.0;
  551. UpdatePlayerList();
  552. }
  553. }
  554. void ClientInterface::UpdatePlayerList()
  555. {
  556. if (m_env) {
  557. std::vector<u16> clients = getClientIDs();
  558. m_clients_names.clear();
  559. if(!clients.empty())
  560. infostream<<"Players:"<<std::endl;
  561. for (u16 i : clients) {
  562. RemotePlayer *player = m_env->getPlayer(i);
  563. if (player == NULL)
  564. continue;
  565. infostream << "* " << player->getName() << "\t";
  566. {
  567. MutexAutoLock clientslock(m_clients_mutex);
  568. RemoteClient* client = lockedGetClientNoEx(i);
  569. if (client)
  570. client->PrintInfo(infostream);
  571. }
  572. m_clients_names.emplace_back(player->getName());
  573. }
  574. }
  575. }
  576. void ClientInterface::send(u16 peer_id, u8 channelnum,
  577. NetworkPacket* pkt, bool reliable)
  578. {
  579. m_con->Send(peer_id, channelnum, pkt, reliable);
  580. }
  581. void ClientInterface::sendToAll(NetworkPacket *pkt)
  582. {
  583. MutexAutoLock clientslock(m_clients_mutex);
  584. for (auto &client_it : m_clients) {
  585. RemoteClient *client = client_it.second;
  586. if (client->net_proto_version != 0) {
  587. m_con->Send(client->peer_id,
  588. clientCommandFactoryTable[pkt->getCommand()].channel, pkt,
  589. clientCommandFactoryTable[pkt->getCommand()].reliable);
  590. }
  591. }
  592. }
  593. void ClientInterface::sendToAllCompat(NetworkPacket *pkt, NetworkPacket *legacypkt,
  594. u16 min_proto_ver)
  595. {
  596. MutexAutoLock clientslock(m_clients_mutex);
  597. for (auto &client_it : m_clients) {
  598. RemoteClient *client = client_it.second;
  599. NetworkPacket *pkt_to_send = nullptr;
  600. if (client->net_proto_version >= min_proto_ver) {
  601. pkt_to_send = pkt;
  602. } else if (client->net_proto_version != 0) {
  603. pkt_to_send = legacypkt;
  604. } else {
  605. warningstream << "Client with unhandled version to handle: '"
  606. << client->net_proto_version << "'";
  607. continue;
  608. }
  609. m_con->Send(client->peer_id,
  610. clientCommandFactoryTable[pkt_to_send->getCommand()].channel,
  611. pkt_to_send,
  612. clientCommandFactoryTable[pkt_to_send->getCommand()].reliable);
  613. }
  614. }
  615. RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min)
  616. {
  617. MutexAutoLock clientslock(m_clients_mutex);
  618. RemoteClientMap::const_iterator n = m_clients.find(peer_id);
  619. // The client may not exist; clients are immediately removed if their
  620. // access is denied, and this event occurs later then.
  621. if (n == m_clients.end())
  622. return NULL;
  623. if (n->second->getState() >= state_min)
  624. return n->second;
  625. return NULL;
  626. }
  627. RemoteClient* ClientInterface::lockedGetClientNoEx(u16 peer_id, ClientState state_min)
  628. {
  629. RemoteClientMap::const_iterator n = m_clients.find(peer_id);
  630. // The client may not exist; clients are immediately removed if their
  631. // access is denied, and this event occurs later then.
  632. if (n == m_clients.end())
  633. return NULL;
  634. if (n->second->getState() >= state_min)
  635. return n->second;
  636. return NULL;
  637. }
  638. ClientState ClientInterface::getClientState(u16 peer_id)
  639. {
  640. MutexAutoLock clientslock(m_clients_mutex);
  641. RemoteClientMap::const_iterator n = m_clients.find(peer_id);
  642. // The client may not exist; clients are immediately removed if their
  643. // access is denied, and this event occurs later then.
  644. if (n == m_clients.end())
  645. return CS_Invalid;
  646. return n->second->getState();
  647. }
  648. void ClientInterface::setPlayerName(u16 peer_id,std::string name)
  649. {
  650. MutexAutoLock clientslock(m_clients_mutex);
  651. RemoteClientMap::iterator n = m_clients.find(peer_id);
  652. // The client may not exist; clients are immediately removed if their
  653. // access is denied, and this event occurs later then.
  654. if (n != m_clients.end())
  655. n->second->setName(name);
  656. }
  657. void ClientInterface::DeleteClient(u16 peer_id)
  658. {
  659. MutexAutoLock conlock(m_clients_mutex);
  660. // Error check
  661. RemoteClientMap::iterator n = m_clients.find(peer_id);
  662. // The client may not exist; clients are immediately removed if their
  663. // access is denied, and this event occurs later then.
  664. if (n == m_clients.end())
  665. return;
  666. /*
  667. Mark objects to be not known by the client
  668. */
  669. //TODO this should be done by client destructor!!!
  670. RemoteClient *client = n->second;
  671. // Handle objects
  672. for (u16 id : client->m_known_objects) {
  673. // Get object
  674. ServerActiveObject* obj = m_env->getActiveObject(id);
  675. if(obj && obj->m_known_by_count > 0)
  676. obj->m_known_by_count--;
  677. }
  678. // Delete client
  679. delete m_clients[peer_id];
  680. m_clients.erase(peer_id);
  681. }
  682. void ClientInterface::CreateClient(u16 peer_id)
  683. {
  684. MutexAutoLock conlock(m_clients_mutex);
  685. // Error check
  686. RemoteClientMap::iterator n = m_clients.find(peer_id);
  687. // The client shouldn't already exist
  688. if (n != m_clients.end()) return;
  689. // Create client
  690. RemoteClient *client = new RemoteClient();
  691. client->peer_id = peer_id;
  692. m_clients[client->peer_id] = client;
  693. }
  694. void ClientInterface::event(u16 peer_id, ClientStateEvent event)
  695. {
  696. {
  697. MutexAutoLock clientlock(m_clients_mutex);
  698. // Error check
  699. RemoteClientMap::iterator n = m_clients.find(peer_id);
  700. // No client to deliver event
  701. if (n == m_clients.end())
  702. return;
  703. n->second->notifyEvent(event);
  704. }
  705. if ((event == CSE_SetClientReady) ||
  706. (event == CSE_Disconnect) ||
  707. (event == CSE_SetDenied))
  708. {
  709. UpdatePlayerList();
  710. }
  711. }
  712. u16 ClientInterface::getProtocolVersion(u16 peer_id)
  713. {
  714. MutexAutoLock conlock(m_clients_mutex);
  715. // Error check
  716. RemoteClientMap::iterator n = m_clients.find(peer_id);
  717. // No client to get version
  718. if (n == m_clients.end())
  719. return 0;
  720. return n->second->net_proto_version;
  721. }
  722. void ClientInterface::setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch, std::string full)
  723. {
  724. MutexAutoLock conlock(m_clients_mutex);
  725. // Error check
  726. RemoteClientMap::iterator n = m_clients.find(peer_id);
  727. // No client to set versions
  728. if (n == m_clients.end())
  729. return;
  730. n->second->setVersionInfo(major,minor,patch,full);
  731. }