clientiface.cpp 22 KB

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