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