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