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