]> git.lizzy.rs Git - minetest.git/blob - src/clientiface.cpp
Log deprecated Lua function calls (#7491)
[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         float prop_zoom_fov = sao->getZoomFOV() * core::DEGTORAD;
203
204         const s16 full_d_max = std::min(adjustDist(m_max_send_distance, prop_zoom_fov),
205                 wanted_range);
206         const s16 d_opt = std::min(adjustDist(m_block_optimize_distance, prop_zoom_fov),
207                 wanted_range);
208         const s16 d_blocks_in_sight = full_d_max * BS * MAP_BLOCKSIZE;
209
210         s16 d_max = full_d_max;
211         s16 d_max_gen = std::min(adjustDist(m_max_gen_distance, prop_zoom_fov),
212                 wanted_range);
213
214         // Don't loop very much at a time, adjust with distance,
215         // do more work per RTT with greater distances.
216         s16 max_d_increment_at_time = full_d_max / 9 + 1;
217         if (d_max > d_start + max_d_increment_at_time)
218                 d_max = d_start + max_d_increment_at_time;
219
220         // cos(angle between velocity and camera) * |velocity|
221         // Limit to 0.0f in case player moves backwards.
222         f32 dot = rangelim(camera_dir.dotProduct(playerspeed), 0.0f, 300.0f);
223
224         // Reduce the field of view when a player moves and looks forward.
225         // limit max fov effect to 50%, 60% at 20n/s fly speed
226         camera_fov = camera_fov / (1 + dot / 300.0f);
227
228         s32 nearest_emerged_d = -1;
229         s32 nearest_emergefull_d = -1;
230         s32 nearest_sent_d = -1;
231         //bool queue_is_full = false;
232
233         const v3s16 cam_pos_nodes = floatToInt(camera_pos, BS);
234
235         s16 d;
236         for (d = d_start; d <= d_max; d++) {
237                 /*
238                         Get the border/face dot coordinates of a "d-radiused"
239                         box
240                 */
241                 std::vector<v3s16> list = FacePositionCache::getFacePositions(d);
242
243                 std::vector<v3s16>::iterator li;
244                 for (li = list.begin(); li != list.end(); ++li) {
245                         v3s16 p = *li + center;
246
247                         /*
248                                 Send throttling
249                                 - Don't allow too many simultaneous transfers
250                                 - EXCEPT when the blocks are very close
251
252                                 Also, don't send blocks that are already flying.
253                         */
254
255                         // Start with the usual maximum
256                         u16 max_simul_dynamic = max_simul_sends_usually;
257
258                         // If block is very close, allow full maximum
259                         if (d <= BLOCK_SEND_DISABLE_LIMITS_MAX_D)
260                                 max_simul_dynamic = m_max_simul_sends;
261
262                         // Don't select too many blocks for sending
263                         if (num_blocks_selected >= max_simul_dynamic) {
264                                 //queue_is_full = true;
265                                 goto queue_full_break;
266                         }
267
268                         // Don't send blocks that are currently being transferred
269                         if (m_blocks_sending.find(p) != m_blocks_sending.end())
270                                 continue;
271
272                         /*
273                                 Do not go over max mapgen limit
274                         */
275                         if (blockpos_over_max_limit(p))
276                                 continue;
277
278                         // If this is true, inexistent block will be made from scratch
279                         bool generate = d <= d_max_gen;
280
281                         /*
282                                 Don't generate or send if not in sight
283                                 FIXME This only works if the client uses a small enough
284                                 FOV setting. The default of 72 degrees is fine.
285                                 Also retrieve a smaller view cone in the direction of the player's
286                                 movement.
287                                 (0.1 is about 4 degrees)
288                         */
289                         f32 dist;
290                         if (!(isBlockInSight(p, camera_pos, camera_dir, camera_fov,
291                                                 d_blocks_in_sight, &dist) ||
292                                         (playerspeed.getLength() > 1.0f * BS &&
293                                         isBlockInSight(p, camera_pos, playerspeeddir, 0.1f,
294                                                 d_blocks_in_sight)))) {
295                                 continue;
296                         }
297
298                         /*
299                                 Don't send already sent blocks
300                         */
301                         if (m_blocks_sent.find(p) != m_blocks_sent.end())
302                                 continue;
303
304                         /*
305                                 Check if map has this block
306                         */
307                         MapBlock *block = env->getMap().getBlockNoCreateNoEx(p);
308
309                         bool surely_not_found_on_disk = false;
310                         bool block_is_invalid = false;
311                         if (block) {
312                                 // Reset usage timer, this block will be of use in the future.
313                                 block->resetUsageTimer();
314
315                                 // Block is dummy if data doesn't exist.
316                                 // It means it has been not found from disk and not generated
317                                 if (block->isDummy()) {
318                                         surely_not_found_on_disk = true;
319                                 }
320
321                                 if (!block->isGenerated())
322                                         block_is_invalid = true;
323
324                                 /*
325                                         If block is not close, don't send it unless it is near
326                                         ground level.
327
328                                         Block is near ground level if night-time mesh
329                                         differs from day-time mesh.
330                                 */
331                                 if (d >= d_opt) {
332                                         if (!block->getDayNightDiff())
333                                                 continue;
334                                 }
335
336                                 if (m_occ_cull && !block_is_invalid &&
337                                                 env->getMap().isBlockOccluded(block, cam_pos_nodes)) {
338                                         continue;
339                                 }
340                         }
341
342                         /*
343                                 If block has been marked to not exist on disk (dummy)
344                                 and generating new ones is not wanted, skip block.
345                         */
346                         if (!generate && surely_not_found_on_disk) {
347                                 // get next one.
348                                 continue;
349                         }
350
351                         /*
352                                 Add inexistent block to emerge queue.
353                         */
354                         if (block == NULL || surely_not_found_on_disk || block_is_invalid) {
355                                 if (emerge->enqueueBlockEmerge(peer_id, p, generate)) {
356                                         if (nearest_emerged_d == -1)
357                                                 nearest_emerged_d = d;
358                                 } else {
359                                         if (nearest_emergefull_d == -1)
360                                                 nearest_emergefull_d = d;
361                                         goto queue_full_break;
362                                 }
363
364                                 // get next one.
365                                 continue;
366                         }
367
368                         if (nearest_sent_d == -1)
369                                 nearest_sent_d = d;
370
371                         /*
372                                 Add block to send queue
373                         */
374                         PrioritySortedBlockTransfer q((float)dist, p, peer_id);
375
376                         dest.push_back(q);
377
378                         num_blocks_selected += 1;
379                 }
380         }
381 queue_full_break:
382
383         // If nothing was found for sending and nothing was queued for
384         // emerging, continue next time browsing from here
385         if (nearest_emerged_d != -1) {
386                 new_nearest_unsent_d = nearest_emerged_d;
387         } else if (nearest_emergefull_d != -1) {
388                 new_nearest_unsent_d = nearest_emergefull_d;
389         } else {
390                 if (d > full_d_max) {
391                         new_nearest_unsent_d = 0;
392                         m_nothing_to_send_pause_timer = 2.0f;
393                 } else {
394                         if (nearest_sent_d != -1)
395                                 new_nearest_unsent_d = nearest_sent_d;
396                         else
397                                 new_nearest_unsent_d = d;
398                 }
399         }
400
401         if (new_nearest_unsent_d != -1)
402                 m_nearest_unsent_d = new_nearest_unsent_d;
403 }
404
405 void RemoteClient::GotBlock(v3s16 p)
406 {
407         if (m_blocks_modified.find(p) == m_blocks_modified.end()) {
408                 if (m_blocks_sending.find(p) != m_blocks_sending.end())
409                         m_blocks_sending.erase(p);
410                 else
411                         m_excess_gotblocks++;
412
413                 m_blocks_sent.insert(p);
414         }
415 }
416
417 void RemoteClient::SentBlock(v3s16 p)
418 {
419         if (m_blocks_modified.find(p) != m_blocks_modified.end())
420                 m_blocks_modified.erase(p);
421
422         if (m_blocks_sending.find(p) == m_blocks_sending.end())
423                 m_blocks_sending[p] = 0.0f;
424         else
425                 infostream<<"RemoteClient::SentBlock(): Sent block"
426                                 " already in m_blocks_sending"<<std::endl;
427 }
428
429 void RemoteClient::SetBlockNotSent(v3s16 p)
430 {
431         m_nearest_unsent_d = 0;
432         m_nothing_to_send_pause_timer = 0;
433
434         if (m_blocks_sending.find(p) != m_blocks_sending.end())
435                 m_blocks_sending.erase(p);
436         if (m_blocks_sent.find(p) != m_blocks_sent.end())
437                 m_blocks_sent.erase(p);
438         m_blocks_modified.insert(p);
439 }
440
441 void RemoteClient::SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks)
442 {
443         m_nearest_unsent_d = 0;
444         m_nothing_to_send_pause_timer = 0;
445
446         for (auto &block : blocks) {
447                 v3s16 p = block.first;
448                 m_blocks_modified.insert(p);
449
450                 if (m_blocks_sending.find(p) != m_blocks_sending.end())
451                         m_blocks_sending.erase(p);
452                 if (m_blocks_sent.find(p) != m_blocks_sent.end())
453                         m_blocks_sent.erase(p);
454         }
455 }
456
457 void RemoteClient::notifyEvent(ClientStateEvent event)
458 {
459         std::ostringstream myerror;
460         switch (m_state)
461         {
462         case CS_Invalid:
463                 //intentionally do nothing
464                 break;
465         case CS_Created:
466                 switch (event) {
467                 case CSE_Hello:
468                         m_state = CS_HelloSent;
469                         break;
470                 case CSE_InitLegacy:
471                         m_state = CS_AwaitingInit2;
472                         break;
473                 case CSE_Disconnect:
474                         m_state = CS_Disconnecting;
475                         break;
476                 case CSE_SetDenied:
477                         m_state = CS_Denied;
478                         break;
479                 /* GotInit2 SetDefinitionsSent SetMediaSent */
480                 default:
481                         myerror << "Created: Invalid client state transition! " << event;
482                         throw ClientStateError(myerror.str());
483                 }
484                 break;
485         case CS_Denied:
486                 /* don't do anything if in denied state */
487                 break;
488         case CS_HelloSent:
489                 switch(event)
490                 {
491                 case CSE_AuthAccept:
492                         m_state = CS_AwaitingInit2;
493                         if (chosen_mech == AUTH_MECHANISM_SRP ||
494                                         chosen_mech == AUTH_MECHANISM_LEGACY_PASSWORD)
495                                 srp_verifier_delete((SRPVerifier *) auth_data);
496                         chosen_mech = AUTH_MECHANISM_NONE;
497                         break;
498                 case CSE_Disconnect:
499                         m_state = CS_Disconnecting;
500                         break;
501                 case CSE_SetDenied:
502                         m_state = CS_Denied;
503                         if (chosen_mech == AUTH_MECHANISM_SRP ||
504                                         chosen_mech == AUTH_MECHANISM_LEGACY_PASSWORD)
505                                 srp_verifier_delete((SRPVerifier *) auth_data);
506                         chosen_mech = AUTH_MECHANISM_NONE;
507                         break;
508                 default:
509                         myerror << "HelloSent: Invalid client state transition! " << event;
510                         throw ClientStateError(myerror.str());
511                 }
512                 break;
513         case CS_AwaitingInit2:
514                 switch(event)
515                 {
516                 case CSE_GotInit2:
517                         confirmSerializationVersion();
518                         m_state = CS_InitDone;
519                         break;
520                 case CSE_Disconnect:
521                         m_state = CS_Disconnecting;
522                         break;
523                 case CSE_SetDenied:
524                         m_state = CS_Denied;
525                         break;
526
527                 /* Init SetDefinitionsSent SetMediaSent */
528                 default:
529                         myerror << "InitSent: Invalid client state transition! " << event;
530                         throw ClientStateError(myerror.str());
531                 }
532                 break;
533
534         case CS_InitDone:
535                 switch(event)
536                 {
537                 case CSE_SetDefinitionsSent:
538                         m_state = CS_DefinitionsSent;
539                         break;
540                 case CSE_Disconnect:
541                         m_state = CS_Disconnecting;
542                         break;
543                 case CSE_SetDenied:
544                         m_state = CS_Denied;
545                         break;
546
547                 /* Init GotInit2 SetMediaSent */
548                 default:
549                         myerror << "InitDone: Invalid client state transition! " << event;
550                         throw ClientStateError(myerror.str());
551                 }
552                 break;
553         case CS_DefinitionsSent:
554                 switch(event)
555                 {
556                 case CSE_SetClientReady:
557                         m_state = CS_Active;
558                         break;
559                 case CSE_Disconnect:
560                         m_state = CS_Disconnecting;
561                         break;
562                 case CSE_SetDenied:
563                         m_state = CS_Denied;
564                         break;
565                 /* Init GotInit2 SetDefinitionsSent */
566                 default:
567                         myerror << "DefinitionsSent: Invalid client state transition! " << event;
568                         throw ClientStateError(myerror.str());
569                 }
570                 break;
571         case CS_Active:
572                 switch(event)
573                 {
574                 case CSE_SetDenied:
575                         m_state = CS_Denied;
576                         break;
577                 case CSE_Disconnect:
578                         m_state = CS_Disconnecting;
579                         break;
580                 case CSE_SudoSuccess:
581                         m_state = CS_SudoMode;
582                         if (chosen_mech == AUTH_MECHANISM_SRP)
583                                 srp_verifier_delete((SRPVerifier *) auth_data);
584                         chosen_mech = AUTH_MECHANISM_NONE;
585                         break;
586                 /* Init GotInit2 SetDefinitionsSent SetMediaSent SetDenied */
587                 default:
588                         myerror << "Active: Invalid client state transition! " << event;
589                         throw ClientStateError(myerror.str());
590                         break;
591                 }
592                 break;
593         case CS_SudoMode:
594                 switch(event)
595                 {
596                 case CSE_SetDenied:
597                         m_state = CS_Denied;
598                         break;
599                 case CSE_Disconnect:
600                         m_state = CS_Disconnecting;
601                         break;
602                 case CSE_SudoLeave:
603                         m_state = CS_Active;
604                         break;
605                 default:
606                         myerror << "Active: Invalid client state transition! " << event;
607                         throw ClientStateError(myerror.str());
608                         break;
609                 }
610                 break;
611         case CS_Disconnecting:
612                 /* we are already disconnecting */
613                 break;
614         }
615 }
616
617 u64 RemoteClient::uptime() const
618 {
619         return porting::getTimeS() - m_connection_time;
620 }
621
622 ClientInterface::ClientInterface(const std::shared_ptr<con::Connection> & con)
623 :
624         m_con(con),
625         m_env(NULL),
626         m_print_info_timer(0.0f)
627 {
628
629 }
630 ClientInterface::~ClientInterface()
631 {
632         /*
633                 Delete clients
634         */
635         {
636                 MutexAutoLock clientslock(m_clients_mutex);
637
638                 for (auto &client_it : m_clients) {
639                         // Delete client
640                         delete client_it.second;
641                 }
642         }
643 }
644
645 std::vector<session_t> ClientInterface::getClientIDs(ClientState min_state)
646 {
647         std::vector<session_t> reply;
648         MutexAutoLock clientslock(m_clients_mutex);
649
650         for (const auto &m_client : m_clients) {
651                 if (m_client.second->getState() >= min_state)
652                         reply.push_back(m_client.second->peer_id);
653         }
654
655         return reply;
656 }
657
658 void ClientInterface::markBlockposAsNotSent(const v3s16 &pos)
659 {
660         MutexAutoLock clientslock(m_clients_mutex);
661         for (const auto &client : m_clients) {
662                 if (client.second->getState() >= CS_Active)
663                         client.second->SetBlockNotSent(pos);
664         }
665 }
666
667 /**
668  * Verify if user limit was reached.
669  * User limit count all clients from HelloSent state (MT protocol user) to Active state
670  * @return true if user limit was reached
671  */
672 bool ClientInterface::isUserLimitReached()
673 {
674         return getClientIDs(CS_HelloSent).size() >= g_settings->getU16("max_users");
675 }
676
677 void ClientInterface::step(float dtime)
678 {
679         m_print_info_timer += dtime;
680         if (m_print_info_timer >= 30.0f) {
681                 m_print_info_timer = 0.0f;
682                 UpdatePlayerList();
683         }
684 }
685
686 void ClientInterface::UpdatePlayerList()
687 {
688         if (m_env) {
689                 std::vector<session_t> clients = getClientIDs();
690                 m_clients_names.clear();
691
692
693                 if (!clients.empty())
694                         infostream<<"Players:"<<std::endl;
695
696                 for (session_t i : clients) {
697                         RemotePlayer *player = m_env->getPlayer(i);
698
699                         if (player == NULL)
700                                 continue;
701
702                         infostream << "* " << player->getName() << "\t";
703
704                         {
705                                 MutexAutoLock clientslock(m_clients_mutex);
706                                 RemoteClient* client = lockedGetClientNoEx(i);
707                                 if (client)
708                                         client->PrintInfo(infostream);
709                         }
710
711                         m_clients_names.emplace_back(player->getName());
712                 }
713         }
714 }
715
716 void ClientInterface::send(session_t peer_id, u8 channelnum,
717                 NetworkPacket *pkt, bool reliable)
718 {
719         m_con->Send(peer_id, channelnum, pkt, reliable);
720 }
721
722 void ClientInterface::sendToAll(NetworkPacket *pkt)
723 {
724         MutexAutoLock clientslock(m_clients_mutex);
725         for (auto &client_it : m_clients) {
726                 RemoteClient *client = client_it.second;
727
728                 if (client->net_proto_version != 0) {
729                         m_con->Send(client->peer_id,
730                                         clientCommandFactoryTable[pkt->getCommand()].channel, pkt,
731                                         clientCommandFactoryTable[pkt->getCommand()].reliable);
732                 }
733         }
734 }
735
736 void ClientInterface::sendToAllCompat(NetworkPacket *pkt, NetworkPacket *legacypkt,
737                 u16 min_proto_ver)
738 {
739         MutexAutoLock clientslock(m_clients_mutex);
740         for (auto &client_it : m_clients) {
741                 RemoteClient *client = client_it.second;
742                 NetworkPacket *pkt_to_send = nullptr;
743
744                 if (client->net_proto_version >= min_proto_ver) {
745                         pkt_to_send = pkt;
746                 } else if (client->net_proto_version != 0) {
747                         pkt_to_send = legacypkt;
748                 } else {
749                         warningstream << "Client with unhandled version to handle: '"
750                                 << client->net_proto_version << "'";
751                         continue;
752                 }
753
754                 m_con->Send(client->peer_id,
755                         clientCommandFactoryTable[pkt_to_send->getCommand()].channel,
756                         pkt_to_send,
757                         clientCommandFactoryTable[pkt_to_send->getCommand()].reliable);
758         }
759 }
760
761 RemoteClient* ClientInterface::getClientNoEx(session_t peer_id, ClientState state_min)
762 {
763         MutexAutoLock clientslock(m_clients_mutex);
764         RemoteClientMap::const_iterator n = m_clients.find(peer_id);
765         // The client may not exist; clients are immediately removed if their
766         // access is denied, and this event occurs later then.
767         if (n == m_clients.end())
768                 return NULL;
769
770         if (n->second->getState() >= state_min)
771                 return n->second;
772
773         return NULL;
774 }
775
776 RemoteClient* ClientInterface::lockedGetClientNoEx(session_t peer_id, ClientState state_min)
777 {
778         RemoteClientMap::const_iterator n = m_clients.find(peer_id);
779         // The client may not exist; clients are immediately removed if their
780         // access is denied, and this event occurs later then.
781         if (n == m_clients.end())
782                 return NULL;
783
784         if (n->second->getState() >= state_min)
785                 return n->second;
786
787         return NULL;
788 }
789
790 ClientState ClientInterface::getClientState(session_t peer_id)
791 {
792         MutexAutoLock clientslock(m_clients_mutex);
793         RemoteClientMap::const_iterator n = m_clients.find(peer_id);
794         // The client may not exist; clients are immediately removed if their
795         // access is denied, and this event occurs later then.
796         if (n == m_clients.end())
797                 return CS_Invalid;
798
799         return n->second->getState();
800 }
801
802 void ClientInterface::setPlayerName(session_t peer_id, const std::string &name)
803 {
804         MutexAutoLock clientslock(m_clients_mutex);
805         RemoteClientMap::iterator n = m_clients.find(peer_id);
806         // The client may not exist; clients are immediately removed if their
807         // access is denied, and this event occurs later then.
808         if (n != m_clients.end())
809                 n->second->setName(name);
810 }
811
812 void ClientInterface::DeleteClient(session_t peer_id)
813 {
814         MutexAutoLock conlock(m_clients_mutex);
815
816         // Error check
817         RemoteClientMap::iterator n = m_clients.find(peer_id);
818         // The client may not exist; clients are immediately removed if their
819         // access is denied, and this event occurs later then.
820         if (n == m_clients.end())
821                 return;
822
823         /*
824                 Mark objects to be not known by the client
825         */
826         //TODO this should be done by client destructor!!!
827         RemoteClient *client = n->second;
828         // Handle objects
829         for (u16 id : client->m_known_objects) {
830                 // Get object
831                 ServerActiveObject* obj = m_env->getActiveObject(id);
832
833                 if(obj && obj->m_known_by_count > 0)
834                         obj->m_known_by_count--;
835         }
836
837         // Delete client
838         delete m_clients[peer_id];
839         m_clients.erase(peer_id);
840 }
841
842 void ClientInterface::CreateClient(session_t peer_id)
843 {
844         MutexAutoLock conlock(m_clients_mutex);
845
846         // Error check
847         RemoteClientMap::iterator n = m_clients.find(peer_id);
848         // The client shouldn't already exist
849         if (n != m_clients.end()) return;
850
851         // Create client
852         RemoteClient *client = new RemoteClient();
853         client->peer_id = peer_id;
854         m_clients[client->peer_id] = client;
855 }
856
857 void ClientInterface::event(session_t peer_id, ClientStateEvent event)
858 {
859         {
860                 MutexAutoLock clientlock(m_clients_mutex);
861
862                 // Error check
863                 RemoteClientMap::iterator n = m_clients.find(peer_id);
864
865                 // No client to deliver event
866                 if (n == m_clients.end())
867                         return;
868                 n->second->notifyEvent(event);
869         }
870
871         if ((event == CSE_SetClientReady) ||
872                 (event == CSE_Disconnect)     ||
873                 (event == CSE_SetDenied))
874         {
875                 UpdatePlayerList();
876         }
877 }
878
879 u16 ClientInterface::getProtocolVersion(session_t peer_id)
880 {
881         MutexAutoLock conlock(m_clients_mutex);
882
883         // Error check
884         RemoteClientMap::iterator n = m_clients.find(peer_id);
885
886         // No client to get version
887         if (n == m_clients.end())
888                 return 0;
889
890         return n->second->net_proto_version;
891 }
892
893 void ClientInterface::setClientVersion(session_t peer_id, u8 major, u8 minor, u8 patch,
894                 const std::string &full)
895 {
896         MutexAutoLock conlock(m_clients_mutex);
897
898         // Error check
899         RemoteClientMap::iterator n = m_clients.find(peer_id);
900
901         // No client to set versions
902         if (n == m_clients.end())
903                 return;
904
905         n->second->setVersionInfo(major, minor, patch, full);
906 }