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