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