3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU 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.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
27 #include "clientserver.h"
29 #include "jmutexautolock.h"
31 #include "constants.h"
34 void * ServerThread::Thread()
38 DSTACK(__FUNCTION_NAME);
43 m_server->AsyncRunStep();
45 //dout_server<<"Running m_server->Receive()"<<std::endl;
48 catch(con::NoIncomingDataException &e)
51 #if CATCH_UNHANDLED_EXCEPTIONS
53 This is what has to be done in threads to get suitable debug info
55 catch(std::exception &e)
57 dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
58 <<e.what()<<std::endl;
68 void * EmergeThread::Thread()
72 DSTACK(__FUNCTION_NAME);
75 #if CATCH_UNHANDLED_EXCEPTIONS
81 Get block info from queue, emerge them and send them
84 After queue is empty, exit.
88 QueuedBlockEmerge *qptr = m_server->m_emerge_queue.pop();
92 SharedPtr<QueuedBlockEmerge> q(qptr);
96 //derr_server<<"EmergeThread::Thread(): running"<<std::endl;
98 //TimeTaker timer("block emerge", g_device);
101 Try to emerge it from somewhere.
103 If it is only wanted as optional, only loading from disk
108 Check if any peer wants it as non-optional. In that case it
111 Also decrement the emerge queue count in clients.
114 bool optional = true;
117 core::map<u16, u8>::Iterator i;
118 for(i=q->peer_ids.getIterator(); i.atEnd()==false; i++)
120 //u16 peer_id = i.getNode()->getKey();
123 u8 flags = i.getNode()->getValue();
124 if((flags & TOSERVER_GETBLOCK_FLAG_OPTIONAL) == false)
130 /*dstream<<"EmergeThread: p="
131 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<") "
132 <<"optional="<<optional<<std::endl;*/
134 ServerMap &map = ((ServerMap&)m_server->m_env.getMap());
136 core::map<v3s16, MapBlock*> changed_blocks;
137 core::map<v3s16, MapBlock*> lighting_invalidated_blocks;
139 MapBlock *block = NULL;
140 bool got_block = true;
141 core::map<v3s16, MapBlock*> modified_blocks;
145 JMutexAutoLock envlock(m_server->m_env_mutex);
147 //TimeTaker timer("block emerge envlock", g_device);
150 bool only_from_disk = false;
153 only_from_disk = true;
155 block = map.emergeBlock(
159 lighting_invalidated_blocks);
161 // If it is a dummy, block was not found on disk
164 //dstream<<"EmergeThread: Got a dummy block"<<std::endl;
168 catch(InvalidPositionException &e)
171 // This happens when position is over limit.
177 if(debug && changed_blocks.size() > 0)
179 dout_server<<DTIME<<"Got changed_blocks: ";
180 for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
181 i.atEnd() == false; i++)
183 MapBlock *block = i.getNode()->getValue();
184 v3s16 p = block->getPos();
185 dout_server<<"("<<p.X<<","<<p.Y<<","<<p.Z<<") ";
187 dout_server<<std::endl;
191 Update water pressure
194 m_server->UpdateBlockWaterPressure(block, modified_blocks);
196 for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
197 i.atEnd() == false; i++)
199 MapBlock *block = i.getNode()->getValue();
200 m_server->UpdateBlockWaterPressure(block, modified_blocks);
201 //v3s16 p = i.getNode()->getKey();
202 //m_server->UpdateBlockWaterPressure(p, modified_blocks);
206 Collect a list of blocks that have been modified in
207 addition to the fetched one.
210 // Add all the "changed blocks" to modified_blocks
211 for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
212 i.atEnd() == false; i++)
214 MapBlock *block = i.getNode()->getValue();
215 modified_blocks.insert(block->getPos(), block);
218 /*dstream<<"lighting "<<lighting_invalidated_blocks.size()
219 <<" blocks"<<std::endl;
220 TimeTaker timer("** updateLighting", g_device);*/
222 // Update lighting without locking the environment mutex,
223 // add modified blocks to changed blocks
224 map.updateLighting(lighting_invalidated_blocks, modified_blocks);
226 // If we got no block, there should be no invalidated blocks
229 assert(lighting_invalidated_blocks.size() == 0);
235 Set sent status of modified blocks on clients
238 // NOTE: Server's clients are also behind the connection mutex
239 JMutexAutoLock lock(m_server->m_con_mutex);
242 Add the originally fetched block to the modified list
246 modified_blocks.insert(p, block);
250 Set the modified blocks unsent for all the clients
253 for(core::map<u16, RemoteClient*>::Iterator
254 i = m_server->m_clients.getIterator();
255 i.atEnd() == false; i++)
257 RemoteClient *client = i.getNode()->getValue();
259 if(modified_blocks.size() > 0)
261 // Remove block from sent history
262 client->SetBlocksNotSent(modified_blocks);
267 #if CATCH_UNHANDLED_EXCEPTIONS
270 This is what has to be done in threads to get suitable debug info
272 catch(std::exception &e)
274 dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
275 <<e.what()<<std::endl;
283 void RemoteClient::GetNextBlocks(Server *server, float dtime,
284 core::array<PrioritySortedBlockTransfer> &dest)
286 DSTACK(__FUNCTION_NAME);
288 // Won't send anything if already sending
290 JMutexAutoLock lock(m_blocks_sending_mutex);
292 if(m_blocks_sending.size() >= g_settings.getU16
293 ("max_simultaneous_block_sends_per_client"))
295 //dstream<<"Not sending any blocks, Queue full."<<std::endl;
300 Player *player = server->m_env.getPlayer(peer_id);
302 v3f playerpos = player->getPosition();
303 v3f playerspeed = player->getSpeed();
305 v3s16 center_nodepos = floatToInt(playerpos);
307 v3s16 center = getNodeBlockPos(center_nodepos);
310 Get the starting value of the block finder radius.
312 s16 last_nearest_unsent_d;
315 JMutexAutoLock lock(m_blocks_sent_mutex);
317 if(m_last_center != center)
319 m_nearest_unsent_d = 0;
320 m_last_center = center;
323 static float reset_counter = 0;
324 reset_counter += dtime;
325 if(reset_counter > 5.0)
328 m_nearest_unsent_d = 0;
331 last_nearest_unsent_d = m_nearest_unsent_d;
333 d_start = m_nearest_unsent_d;
336 u16 maximum_simultaneous_block_sends_setting = g_settings.getU16
337 ("max_simultaneous_block_sends_per_client");
338 u16 maximum_simultaneous_block_sends =
339 maximum_simultaneous_block_sends_setting;
342 Check the time from last addNode/removeNode.
344 Decrease send rate if player is building stuff.
347 SharedPtr<JMutexAutoLock> lock(m_time_from_building.getLock());
348 m_time_from_building.m_value += dtime;
349 if(m_time_from_building.m_value
350 < FULL_BLOCK_SEND_ENABLE_MIN_TIME_FROM_BUILDING)
352 maximum_simultaneous_block_sends
353 = LIMITED_MAX_SIMULTANEOUS_BLOCK_SENDS;
357 // Serialization version used
358 //u8 ser_version = serialization_version;
360 //bool has_incomplete_blocks = false;
362 s16 d_max = g_settings.getS16("max_block_send_distance");
363 s16 d_max_gen = g_settings.getS16("max_block_generate_distance");
365 //dstream<<"Starting from "<<d_start<<std::endl;
367 for(s16 d = d_start; d <= d_max; d++)
369 //dstream<<"RemoteClient::SendBlocks(): d="<<d<<std::endl;
371 //if(has_incomplete_blocks == false)
373 JMutexAutoLock lock(m_blocks_sent_mutex);
375 If m_nearest_unsent_d was changed by the EmergeThread
376 (it can change it to 0 through SetBlockNotSent),
378 Else update m_nearest_unsent_d
380 if(m_nearest_unsent_d != last_nearest_unsent_d)
382 d = m_nearest_unsent_d;
386 m_nearest_unsent_d = d;
388 last_nearest_unsent_d = m_nearest_unsent_d;
392 Get the border/face dot coordinates of a "d-radiused"
395 core::list<v3s16> list;
396 getFacePositions(list, d);
398 core::list<v3s16>::Iterator li;
399 for(li=list.begin(); li!=list.end(); li++)
401 v3s16 p = *li + center;
405 - Don't allow too many simultaneous transfers
406 - EXCEPT when the blocks are very close
408 Also, don't send blocks that are already flying.
411 u16 maximum_simultaneous_block_sends_now =
412 maximum_simultaneous_block_sends;
414 if(d <= BLOCK_SEND_DISABLE_LIMITS_MAX_D)
416 maximum_simultaneous_block_sends_now =
417 maximum_simultaneous_block_sends_setting;
421 JMutexAutoLock lock(m_blocks_sending_mutex);
423 // Limit is dynamically lowered when building
424 if(m_blocks_sending.size()
425 >= maximum_simultaneous_block_sends_now)
427 /*dstream<<"Not sending more blocks. Queue full. "
428 <<m_blocks_sending.size()
433 if(m_blocks_sending.find(p) != NULL)
440 if(p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
441 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
442 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
443 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
444 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
445 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
448 bool generate = d <= d_max_gen;
450 // Limit the generating area vertically to half
451 if(abs(p.Y - center.Y) > d_max_gen / 2)
455 Don't send already sent blocks
458 JMutexAutoLock lock(m_blocks_sent_mutex);
460 if(m_blocks_sent.find(p) != NULL)
465 Check if map has this block
467 MapBlock *block = NULL;
470 block = server->m_env.getMap().getBlockNoCreate(p);
472 catch(InvalidPositionException &e)
476 bool surely_not_found_on_disk = false;
479 /*if(block->isIncomplete())
481 has_incomplete_blocks = true;
487 surely_not_found_on_disk = true;
492 If block has been marked to not exist on disk (dummy)
493 and generating new ones is not wanted, skip block.
495 if(generate == false && surely_not_found_on_disk == true)
502 Add inexistent block to emerge queue.
504 if(block == NULL || surely_not_found_on_disk)
506 /*SharedPtr<JMutexAutoLock> lock
507 (m_num_blocks_in_emerge_queue.getLock());*/
509 //TODO: Get value from somewhere
510 // Allow only one block in emerge queue
511 if(server->m_emerge_queue.peerItemCount(peer_id) < 1)
513 // Add it to the emerge queue and trigger the thread
516 if(generate == false)
517 flags |= TOSERVER_GETBLOCK_FLAG_OPTIONAL;
519 server->m_emerge_queue.addBlock(peer_id, p, flags);
520 server->m_emergethread.trigger();
531 PrioritySortedBlockTransfer q((float)d, p, peer_id);
537 // Don't add anything here. The loop breaks by returning.
540 void RemoteClient::SendObjectData(
543 core::map<v3s16, bool> &stepped_blocks
546 DSTACK(__FUNCTION_NAME);
548 // Can't send anything without knowing version
549 if(serialization_version == SER_FMT_VER_INVALID)
551 dstream<<"RemoteClient::SendObjectData(): Not sending, no version."
557 Send a TOCLIENT_OBJECTDATA packet.
561 u16 number of player positions
572 std::ostringstream os(std::ios_base::binary);
576 writeU16(buf, TOCLIENT_OBJECTDATA);
577 os.write((char*)buf, 2);
580 Get and write player data
583 core::list<Player*> players = server->m_env.getPlayers();
585 // Write player count
586 u16 playercount = players.size();
587 writeU16(buf, playercount);
588 os.write((char*)buf, 2);
590 core::list<Player*>::Iterator i;
591 for(i = players.begin();
592 i != players.end(); i++)
596 v3f pf = player->getPosition();
597 v3f sf = player->getSpeed();
599 v3s32 position_i(pf.X*100, pf.Y*100, pf.Z*100);
600 v3s32 speed_i (sf.X*100, sf.Y*100, sf.Z*100);
601 s32 pitch_i (player->getPitch() * 100);
602 s32 yaw_i (player->getYaw() * 100);
604 writeU16(buf, player->peer_id);
605 os.write((char*)buf, 2);
606 writeV3S32(buf, position_i);
607 os.write((char*)buf, 12);
608 writeV3S32(buf, speed_i);
609 os.write((char*)buf, 12);
610 writeS32(buf, pitch_i);
611 os.write((char*)buf, 4);
612 writeS32(buf, yaw_i);
613 os.write((char*)buf, 4);
617 Get and write object data
623 For making players to be able to build to their nearby
624 environment (building is not possible on blocks that are not
627 - Add blocks to emerge queue if they are not found
629 SUGGESTION: These could be ignored from the backside of the player
631 TODO: Keep track of total size of packet and stop when it is too big
634 Player *player = server->m_env.getPlayer(peer_id);
636 v3f playerpos = player->getPosition();
637 v3f playerspeed = player->getSpeed();
639 v3s16 center_nodepos = floatToInt(playerpos);
640 v3s16 center = getNodeBlockPos(center_nodepos);
642 //s16 d_max = ACTIVE_OBJECT_D_BLOCKS;
643 s16 d_max = g_settings.getS16("active_object_range");
645 // Number of blocks whose objects were written to bos
648 //core::map<v3s16, MapBlock*> blocks;
649 std::ostringstream bos(std::ios_base::binary);
651 for(s16 d = 0; d <= d_max; d++)
653 core::list<v3s16> list;
654 getFacePositions(list, d);
656 core::list<v3s16>::Iterator li;
657 for(li=list.begin(); li!=list.end(); li++)
659 v3s16 p = *li + center;
662 Ignore blocks that haven't been sent to the client
665 JMutexAutoLock sentlock(m_blocks_sent_mutex);
666 if(m_blocks_sent.find(p) == NULL)
670 // Try stepping block and add it to a send queue
675 MapBlock *block = server->m_env.getMap().getBlockNoCreate(p);
677 // Skip block if there are no objects
678 if(block->getObjectCount() == 0)
681 // Step block if not in stepped_blocks and add to stepped_blocks
682 if(stepped_blocks.find(p) == NULL)
684 block->stepObjects(dtime, true);
685 stepped_blocks.insert(p, true);
686 block->setChangedFlag();
695 bos.write((char*)buf, 6);
698 block->serializeObjects(bos, serialization_version);
703 Stop collecting objects if data is already too big
705 // Sum of player and object data sizes
706 s32 sum = (s32)os.tellp() + 2 + (s32)bos.tellp();
707 // break out if data too big
708 if(sum > MAX_OBJECTDATA_SIZE)
710 goto skip_subsequent;
714 catch(InvalidPositionException &e)
717 // Add it to the emerge queue and trigger the thread.
718 // Fetch the block only if it is on disk.
720 // Grab and increment counter
721 /*SharedPtr<JMutexAutoLock> lock
722 (m_num_blocks_in_emerge_queue.getLock());
723 m_num_blocks_in_emerge_queue.m_value++;*/
725 // Add to queue as an anonymous fetch from disk
726 u8 flags = TOSERVER_GETBLOCK_FLAG_OPTIONAL;
727 server->m_emerge_queue.addBlock(0, p, flags);
728 server->m_emergethread.trigger();
736 writeU16(buf, blockcount);
737 os.write((char*)buf, 2);
739 // Write block objects
746 //dstream<<"Server: Sending object data to "<<peer_id<<std::endl;
749 std::string s = os.str();
750 SharedBuffer<u8> data((u8*)s.c_str(), s.size());
751 // Send as unreliable
752 server->m_con.Send(peer_id, 0, data, false);
755 void RemoteClient::GotBlock(v3s16 p)
757 JMutexAutoLock lock(m_blocks_sending_mutex);
758 JMutexAutoLock lock2(m_blocks_sent_mutex);
759 if(m_blocks_sending.find(p) != NULL)
760 m_blocks_sending.remove(p);
762 dstream<<"RemoteClient::GotBlock(): Didn't find in"
763 " m_blocks_sending"<<std::endl;
764 m_blocks_sent.insert(p, true);
767 void RemoteClient::SentBlock(v3s16 p)
769 JMutexAutoLock lock(m_blocks_sending_mutex);
770 if(m_blocks_sending.size() > 15)
772 dstream<<"RemoteClient::SentBlock(): "
773 <<"m_blocks_sending.size()="
774 <<m_blocks_sending.size()<<std::endl;
776 if(m_blocks_sending.find(p) == NULL)
777 m_blocks_sending.insert(p, 0.0);
779 dstream<<"RemoteClient::SentBlock(): Sent block"
780 " already in m_blocks_sending"<<std::endl;
783 void RemoteClient::SetBlockNotSent(v3s16 p)
785 JMutexAutoLock sendinglock(m_blocks_sending_mutex);
786 JMutexAutoLock sentlock(m_blocks_sent_mutex);
788 m_nearest_unsent_d = 0;
790 if(m_blocks_sending.find(p) != NULL)
791 m_blocks_sending.remove(p);
792 if(m_blocks_sent.find(p) != NULL)
793 m_blocks_sent.remove(p);
796 void RemoteClient::SetBlocksNotSent(core::map<v3s16, MapBlock*> &blocks)
798 JMutexAutoLock sendinglock(m_blocks_sending_mutex);
799 JMutexAutoLock sentlock(m_blocks_sent_mutex);
801 m_nearest_unsent_d = 0;
803 for(core::map<v3s16, MapBlock*>::Iterator
804 i = blocks.getIterator();
805 i.atEnd()==false; i++)
807 v3s16 p = i.getNode()->getKey();
809 if(m_blocks_sending.find(p) != NULL)
810 m_blocks_sending.remove(p);
811 if(m_blocks_sent.find(p) != NULL)
812 m_blocks_sent.remove(p);
816 /*void RemoteClient::BlockEmerged()
818 SharedPtr<JMutexAutoLock> lock(m_num_blocks_in_emerge_queue.getLock());
819 assert(m_num_blocks_in_emerge_queue.m_value > 0);
820 m_num_blocks_in_emerge_queue.m_value--;
823 /*void RemoteClient::RunSendingTimeouts(float dtime, float timeout)
825 JMutexAutoLock sendinglock(m_blocks_sending_mutex);
827 core::list<v3s16> remove_queue;
828 for(core::map<v3s16, float>::Iterator
829 i = m_blocks_sending.getIterator();
830 i.atEnd()==false; i++)
832 v3s16 p = i.getNode()->getKey();
833 float t = i.getNode()->getValue();
835 i.getNode()->setValue(t);
839 remove_queue.push_back(p);
842 for(core::list<v3s16>::Iterator
843 i = remove_queue.begin();
844 i != remove_queue.end(); i++)
846 m_blocks_sending.remove(*i);
854 PlayerInfo::PlayerInfo()
859 void PlayerInfo::PrintLine(std::ostream *s)
861 (*s)<<id<<": \""<<name<<"\" ("
862 <<position.X<<","<<position.Y
863 <<","<<position.Z<<") ";
865 (*s)<<" avg_rtt="<<avg_rtt;
869 u32 PIChecksum(core::list<PlayerInfo> &l)
871 core::list<PlayerInfo>::Iterator i;
874 for(i=l.begin(); i!=l.end(); i++)
876 checksum += a * (i->id+1);
877 checksum ^= 0x435aafcd;
888 std::string mapsavedir,
892 m_env(new ServerMap(mapsavedir, hm_params, map_params), dout_server),
893 m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this),
899 m_step_dtime_mutex.Init();
908 JMutexAutoLock clientslock(m_con_mutex);
910 for(core::map<u16, RemoteClient*>::Iterator
911 i = m_clients.getIterator();
912 i.atEnd() == false; i++)
914 u16 peer_id = i.getNode()->getKey();
918 JMutexAutoLock envlock(m_env_mutex);
919 m_env.removePlayer(peer_id);
923 delete i.getNode()->getValue();
927 void Server::start(unsigned short port)
929 DSTACK(__FUNCTION_NAME);
930 // Stop thread if already running
933 // Initialize connection
934 m_con.setTimeoutMs(30);
938 m_thread.setRun(true);
941 dout_server<<"Server started on port "<<port<<std::endl;
946 DSTACK(__FUNCTION_NAME);
947 // Stop threads (set run=false first so both start stopping)
948 m_thread.setRun(false);
949 m_emergethread.setRun(false);
951 m_emergethread.stop();
953 dout_server<<"Server threads stopped"<<std::endl;
956 void Server::step(float dtime)
958 DSTACK(__FUNCTION_NAME);
963 JMutexAutoLock lock(m_step_dtime_mutex);
964 m_step_dtime += dtime;
968 void Server::AsyncRunStep()
970 DSTACK(__FUNCTION_NAME);
974 JMutexAutoLock lock1(m_step_dtime_mutex);
975 dtime = m_step_dtime;
978 // Send blocks to clients
985 JMutexAutoLock lock1(m_step_dtime_mutex);
989 //dstream<<"Server steps "<<dtime<<std::endl;
991 //dstream<<"Server::AsyncRunStep(): dtime="<<dtime<<std::endl;
993 // Has to be locked for peerAdded/Removed
994 JMutexAutoLock lock1(m_env_mutex);
995 // Process connection's timeouts
996 JMutexAutoLock lock2(m_con_mutex);
997 m_con.RunTimeouts(dtime);
1001 // This also runs Map's timers
1002 JMutexAutoLock lock(m_env_mutex);
1016 if(g_settings.getBool("endless_water") == false)
1021 static float counter = 0.0;
1023 if(counter >= 0.25 && m_flow_active_nodes.size() > 0)
1028 core::map<v3s16, MapBlock*> modified_blocks;
1032 JMutexAutoLock envlock(m_env_mutex);
1034 MapVoxelManipulator v(&m_env.getMap());
1035 v.m_disable_water_climb =
1036 g_settings.getBool("disable_water_climb");
1038 if(g_settings.getBool("endless_water") == false)
1039 v.flowWater(m_flow_active_nodes, 0, false, 250);
1041 v.flowWater(m_flow_active_nodes, 0, false, 50);
1043 v.blitBack(modified_blocks);
1045 ServerMap &map = ((ServerMap&)m_env.getMap());
1048 core::map<v3s16, MapBlock*> lighting_modified_blocks;
1049 map.updateLighting(modified_blocks, lighting_modified_blocks);
1051 // Add blocks modified by lighting to modified_blocks
1052 for(core::map<v3s16, MapBlock*>::Iterator
1053 i = lighting_modified_blocks.getIterator();
1054 i.atEnd() == false; i++)
1056 MapBlock *block = i.getNode()->getValue();
1057 modified_blocks.insert(block->getPos(), block);
1062 Set the modified blocks unsent for all the clients
1065 JMutexAutoLock lock2(m_con_mutex);
1067 for(core::map<u16, RemoteClient*>::Iterator
1068 i = m_clients.getIterator();
1069 i.atEnd() == false; i++)
1071 RemoteClient *client = i.getNode()->getValue();
1073 if(modified_blocks.size() > 0)
1075 // Remove block from sent history
1076 client->SetBlocksNotSent(modified_blocks);
1080 } // interval counter
1083 // Periodically print some info
1085 static float counter = 0.0;
1091 JMutexAutoLock lock2(m_con_mutex);
1093 for(core::map<u16, RemoteClient*>::Iterator
1094 i = m_clients.getIterator();
1095 i.atEnd() == false; i++)
1097 //u16 peer_id = i.getNode()->getKey();
1098 RemoteClient *client = i.getNode()->getValue();
1099 client->PrintInfo(std::cout);
1104 // Run time- and client- related stuff
1105 // NOTE: If you intend to add something here, check that it
1106 // doesn't fit in RemoteClient::GetNextBlocks for example.
1108 // Clients are behind connection lock
1109 JMutexAutoLock lock(m_con_mutex);
1111 for(core::map<u16, RemoteClient*>::Iterator
1112 i = m_clients.getIterator();
1113 i.atEnd() == false; i++)
1115 RemoteClient *client = i.getNode()->getValue();
1116 //con::Peer *peer = m_con.GetPeer(client->peer_id);
1117 //client->RunSendingTimeouts(dtime, peer->resend_timeout);
1121 // Send object positions
1123 static float counter = 0.0;
1125 if(counter >= g_settings.getFloat("objectdata_interval"))
1127 JMutexAutoLock lock1(m_env_mutex);
1128 JMutexAutoLock lock2(m_con_mutex);
1129 SendObjectData(counter);
1135 // Trigger emergethread (it gets somehow gets to a
1136 // non-triggered but bysy state sometimes)
1138 static float counter = 0.0;
1144 m_emergethread.trigger();
1150 static float counter = 0.0;
1152 if(counter >= SERVER_MAP_SAVE_INTERVAL)
1156 JMutexAutoLock lock(m_env_mutex);
1157 // Save only changed parts
1158 m_env.getMap().save(true);
1163 void Server::Receive()
1165 DSTACK(__FUNCTION_NAME);
1166 u32 data_maxsize = 10000;
1167 Buffer<u8> data(data_maxsize);
1172 JMutexAutoLock lock(m_con_mutex);
1173 datasize = m_con.Receive(peer_id, *data, data_maxsize);
1175 ProcessData(*data, datasize, peer_id);
1177 catch(con::InvalidIncomingDataException &e)
1179 derr_server<<"Server::Receive(): "
1180 "InvalidIncomingDataException: what()="
1181 <<e.what()<<std::endl;
1183 catch(con::PeerNotFoundException &e)
1185 //NOTE: This is not needed anymore
1187 // The peer has been disconnected.
1188 // Find the associated player and remove it.
1190 /*JMutexAutoLock envlock(m_env_mutex);
1192 dout_server<<"ServerThread: peer_id="<<peer_id
1193 <<" has apparently closed connection. "
1194 <<"Removing player."<<std::endl;
1196 m_env.removePlayer(peer_id);*/
1200 void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
1202 DSTACK(__FUNCTION_NAME);
1203 // Environment is locked first.
1204 JMutexAutoLock envlock(m_env_mutex);
1205 JMutexAutoLock conlock(m_con_mutex);
1209 peer = m_con.GetPeer(peer_id);
1211 catch(con::PeerNotFoundException &e)
1213 derr_server<<DTIME<<"Server::ProcessData(): Cancelling: peer "
1214 <<peer_id<<" not found"<<std::endl;
1218 //u8 peer_ser_ver = peer->serialization_version;
1219 u8 peer_ser_ver = getClient(peer->id)->serialization_version;
1227 ToServerCommand command = (ToServerCommand)readU16(&data[0]);
1229 if(command == TOSERVER_INIT)
1231 // [0] u16 TOSERVER_INIT
1232 // [2] u8 SER_FMT_VER_HIGHEST
1233 // [3] u8[20] player_name
1238 derr_server<<DTIME<<"Server: Got TOSERVER_INIT from "
1239 <<peer->id<<std::endl;
1241 // First byte after command is maximum supported
1242 // serialization version
1243 u8 client_max = data[2];
1244 u8 our_max = SER_FMT_VER_HIGHEST;
1245 // Use the highest version supported by both
1246 u8 deployed = core::min_(client_max, our_max);
1247 // If it's lower than the lowest supported, give up.
1248 if(deployed < SER_FMT_VER_LOWEST)
1249 deployed = SER_FMT_VER_INVALID;
1251 //peer->serialization_version = deployed;
1252 getClient(peer->id)->pending_serialization_version = deployed;
1254 if(deployed == SER_FMT_VER_INVALID)
1256 derr_server<<DTIME<<"Server: Cannot negotiate "
1257 "serialization version with peer "
1258 <<peer_id<<std::endl;
1266 Player *player = m_env.getPlayer(peer_id);
1268 // Check if player doesn't exist
1270 throw con::InvalidIncomingDataException
1271 ("Server::ProcessData(): INIT: Player doesn't exist");
1273 // update name if it was supplied
1274 if(datasize >= 20+3)
1277 player->updateName((const char*)&data[3]);
1280 // Now answer with a TOCLIENT_INIT
1282 SharedBuffer<u8> reply(2+1+6);
1283 writeU16(&reply[0], TOCLIENT_INIT);
1284 writeU8(&reply[2], deployed);
1285 writeV3S16(&reply[3], floatToInt(player->getPosition()+v3f(0,BS/2,0)));
1287 m_con.Send(peer_id, 0, reply, true);
1291 if(command == TOSERVER_INIT2)
1293 derr_server<<DTIME<<"Server: Got TOSERVER_INIT2 from "
1294 <<peer->id<<std::endl;
1297 getClient(peer->id)->serialization_version
1298 = getClient(peer->id)->pending_serialization_version;
1301 Send some initialization data
1304 // Send player info to all players
1307 // Send inventory to player
1308 SendInventory(peer->id);
1313 if(peer_ser_ver == SER_FMT_VER_INVALID)
1315 derr_server<<DTIME<<"Server::ProcessData(): Cancelling: Peer"
1316 " serialization format invalid or not initialized."
1317 " Skipping incoming command="<<command<<std::endl;
1321 Player *player = m_env.getPlayer(peer_id);
1324 derr_server<<"Server::ProcessData(): Cancelling: "
1325 "No player for peer_id="<<peer_id
1329 if(command == TOSERVER_PLAYERPOS)
1331 if(datasize < 2+12+12+4+4)
1335 v3s32 ps = readV3S32(&data[start+2]);
1336 v3s32 ss = readV3S32(&data[start+2+12]);
1337 f32 pitch = (f32)readS32(&data[2+12+12]) / 100.0;
1338 f32 yaw = (f32)readS32(&data[2+12+12+4]) / 100.0;
1339 v3f position((f32)ps.X/100., (f32)ps.Y/100., (f32)ps.Z/100.);
1340 v3f speed((f32)ss.X/100., (f32)ss.Y/100., (f32)ss.Z/100.);
1341 pitch = wrapDegrees(pitch);
1342 yaw = wrapDegrees(yaw);
1343 player->setPosition(position);
1344 player->setSpeed(speed);
1345 player->setPitch(pitch);
1346 player->setYaw(yaw);
1348 /*dout_server<<"Server::ProcessData(): Moved player "<<peer_id<<" to "
1349 <<"("<<position.X<<","<<position.Y<<","<<position.Z<<")"
1350 <<" pitch="<<pitch<<" yaw="<<yaw<<std::endl;*/
1352 else if(command == TOSERVER_GOTBLOCKS)
1365 u16 count = data[2];
1366 for(u16 i=0; i<count; i++)
1368 if((s16)datasize < 2+1+(i+1)*6)
1369 throw con::InvalidIncomingDataException
1370 ("GOTBLOCKS length is too short");
1371 v3s16 p = readV3S16(&data[2+1+i*6]);
1372 /*dstream<<"Server: GOTBLOCKS ("
1373 <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
1374 RemoteClient *client = getClient(peer_id);
1375 client->GotBlock(p);
1378 else if(command == TOSERVER_DELETEDBLOCKS)
1391 u16 count = data[2];
1392 for(u16 i=0; i<count; i++)
1394 if((s16)datasize < 2+1+(i+1)*6)
1395 throw con::InvalidIncomingDataException
1396 ("DELETEDBLOCKS length is too short");
1397 v3s16 p = readV3S16(&data[2+1+i*6]);
1398 /*dstream<<"Server: DELETEDBLOCKS ("
1399 <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
1400 RemoteClient *client = getClient(peer_id);
1401 client->SetBlockNotSent(p);
1404 else if(command == TOSERVER_CLICK_OBJECT)
1411 [2] u8 button (0=left, 1=right)
1416 u8 button = readU8(&data[2]);
1418 p.X = readS16(&data[3]);
1419 p.Y = readS16(&data[5]);
1420 p.Z = readS16(&data[7]);
1421 s16 id = readS16(&data[9]);
1422 //u16 item_i = readU16(&data[11]);
1424 MapBlock *block = NULL;
1427 block = m_env.getMap().getBlockNoCreate(p);
1429 catch(InvalidPositionException &e)
1431 derr_server<<"PICK_OBJECT block not found"<<std::endl;
1435 MapBlockObject *obj = block->getObject(id);
1439 derr_server<<"PICK_OBJECT object not found"<<std::endl;
1443 //TODO: Check that object is reasonably close
1448 if(g_settings.getBool("creative_mode") == false)
1451 // Skip if inventory has no free space
1452 if(player->inventory.getUsedSlots() == player->inventory.getSize())
1454 dout_server<<"Player inventory has no free space"<<std::endl;
1458 // Add to inventory and send inventory
1459 InventoryItem *item = new MapBlockObjectItem
1460 (obj->getInventoryString());
1461 player->inventory.addItem(item);
1462 SendInventory(player->peer_id);
1465 // Remove from block
1466 block->removeObject(id);
1469 else if(command == TOSERVER_CLICK_GROUND)
1476 [2] u8 button (0=left, 1=right)
1477 [3] v3s16 nodepos_undersurface
1478 [9] v3s16 nodepos_abovesurface
1481 u8 button = readU8(&data[2]);
1483 p_under.X = readS16(&data[3]);
1484 p_under.Y = readS16(&data[5]);
1485 p_under.Z = readS16(&data[7]);
1487 p_over.X = readS16(&data[9]);
1488 p_over.Y = readS16(&data[11]);
1489 p_over.Z = readS16(&data[13]);
1490 u16 item_i = readU16(&data[15]);
1492 //TODO: Check that target is reasonably close
1495 Left button digs ground
1500 core::map<v3s16, MapBlock*> modified_blocks;
1506 // Get material at position
1507 material = m_env.getMap().getNode(p_under).d;
1508 // If it's not diggable, do nothing
1509 if(content_diggable(material) == false)
1514 catch(InvalidPositionException &e)
1516 derr_server<<"Server: Ignoring REMOVENODE: Node not found"
1521 // Reset build time counter
1522 getClient(peer->id)->m_time_from_building.set(0.0);
1526 SharedBuffer<u8> reply(replysize);
1527 writeU16(&reply[0], TOCLIENT_REMOVENODE);
1528 writeS16(&reply[2], p_under.X);
1529 writeS16(&reply[4], p_under.Y);
1530 writeS16(&reply[6], p_under.Z);
1532 m_con.SendToAll(0, reply, true);
1534 if(g_settings.getBool("creative_mode") == false)
1536 // Add to inventory and send inventory
1537 InventoryItem *item = new MaterialItem(material, 1);
1538 player->inventory.addItem(item);
1539 SendInventory(player->peer_id);
1544 (this takes some time so it is done after the quick stuff)
1546 m_env.getMap().removeNodeAndUpdate(p_under, modified_blocks);
1552 // Update water pressure around modification
1553 // This also adds it to m_flow_active_nodes if appropriate
1555 MapVoxelManipulator v(&m_env.getMap());
1556 v.m_disable_water_climb =
1557 g_settings.getBool("disable_water_climb");
1559 VoxelArea area(p_under-v3s16(1,1,1), p_under+v3s16(1,1,1));
1563 v.updateAreaWaterPressure(area, m_flow_active_nodes);
1565 catch(ProcessingLimitException &e)
1567 dstream<<"Processing limit reached (1)"<<std::endl;
1570 v.blitBack(modified_blocks);
1572 // Add the node to m_flow_active_nodes.
1573 //m_flow_active_nodes[p_under] = 1;
1577 Right button places blocks and stuff
1579 else if(button == 1)
1583 InventoryItem *item = player->inventory.getItem(item_i);
1585 // If there is no item, it is not possible to add it anywhere
1590 Handle material items
1592 if(std::string("MaterialItem") == item->getName())
1595 // Don't add a node if this is not a free space
1596 MapNode n2 = m_env.getMap().getNode(p_over);
1597 if(content_buildable_to(n2.d) == false)
1600 catch(InvalidPositionException &e)
1602 derr_server<<"Server: Ignoring ADDNODE: Node not found"
1607 // Reset build time counter
1608 getClient(peer->id)->m_time_from_building.set(0.0);
1611 MaterialItem *mitem = (MaterialItem*)item;
1613 n.d = mitem->getMaterial();
1614 if(content_directional(n.d))
1615 n.dir = packDir(p_under - p_over);
1619 u32 replysize = 8 + MapNode::serializedLength(peer_ser_ver);
1620 SharedBuffer<u8> reply(replysize);
1621 writeU16(&reply[0], TOCLIENT_ADDNODE);
1622 writeS16(&reply[2], p_over.X);
1623 writeS16(&reply[4], p_over.Y);
1624 writeS16(&reply[6], p_over.Z);
1625 n.serialize(&reply[8], peer_ser_ver);
1627 m_con.SendToAll(0, reply, true);
1632 if(g_settings.getBool("creative_mode") == false)
1634 // Remove from inventory and send inventory
1635 if(mitem->getCount() == 1)
1636 player->inventory.deleteItem(item_i);
1640 SendInventory(peer_id);
1646 This takes some time so it is done after the quick stuff
1648 core::map<v3s16, MapBlock*> modified_blocks;
1649 m_env.getMap().addNodeAndUpdate(p_over, n, modified_blocks);
1655 if(g_settings.getBool("creative_mode") == false)
1657 // Remove from inventory and send inventory
1658 if(mitem->getCount() == 1)
1659 player->inventory.deleteItem(item_i);
1663 SendInventory(peer_id);
1669 This takes some time so it is done after the quick stuff
1671 core::map<v3s16, MapBlock*> modified_blocks;
1672 m_env.getMap().addNodeAndUpdate(p_over, n, modified_blocks);
1675 Set the modified blocks unsent for all the clients
1678 //JMutexAutoLock lock2(m_con_mutex);
1680 for(core::map<u16, RemoteClient*>::Iterator
1681 i = m_clients.getIterator();
1682 i.atEnd() == false; i++)
1684 RemoteClient *client = i.getNode()->getValue();
1686 if(modified_blocks.size() > 0)
1688 // Remove block from sent history
1689 client->SetBlocksNotSent(modified_blocks);
1698 // Update water pressure around modification
1699 // This also adds it to m_flow_active_nodes if appropriate
1701 MapVoxelManipulator v(&m_env.getMap());
1702 v.m_disable_water_climb =
1703 g_settings.getBool("disable_water_climb");
1705 VoxelArea area(p_over-v3s16(1,1,1), p_over+v3s16(1,1,1));
1709 v.updateAreaWaterPressure(area, m_flow_active_nodes);
1711 catch(ProcessingLimitException &e)
1713 dstream<<"Processing limit reached (1)"<<std::endl;
1716 v.blitBack(modified_blocks);
1719 Handle block object items
1721 else if(std::string("MBOItem") == item->getName())
1723 MapBlockObjectItem *oitem = (MapBlockObjectItem*)item;
1725 /*dout_server<<"Trying to place a MapBlockObjectItem: "
1726 "inventorystring=\""
1727 <<oitem->getInventoryString()
1728 <<"\""<<std::endl;*/
1730 v3s16 blockpos = getNodeBlockPos(p_over);
1732 MapBlock *block = NULL;
1735 block = m_env.getMap().getBlockNoCreate(blockpos);
1737 catch(InvalidPositionException &e)
1739 derr_server<<"Error while placing object: "
1740 "block not found"<<std::endl;
1744 v3s16 block_pos_i_on_map = block->getPosRelative();
1745 v3f block_pos_f_on_map = intToFloat(block_pos_i_on_map);
1747 v3f pos = intToFloat(p_over);
1748 pos -= block_pos_f_on_map;
1750 /*dout_server<<"pos="
1751 <<"("<<pos.X<<","<<pos.Y<<","<<pos.Z<<")"
1755 MapBlockObject *obj = oitem->createObject
1756 (pos, player->getYaw(), player->getPitch());
1759 derr_server<<"WARNING: oitem created NULL object"
1762 block->addObject(obj);
1764 //dout_server<<"Placed object"<<std::endl;
1766 if(g_settings.getBool("creative_mode") == false)
1768 // Remove from inventory and send inventory
1769 player->inventory.deleteItem(item_i);
1771 SendInventory(peer_id);
1777 Catch invalid buttons
1781 derr_server<<"WARNING: Server: Invalid button "
1782 <<button<<std::endl;
1785 else if(command == TOSERVER_RELEASE)
1796 else if(command == TOSERVER_SIGNTEXT)
1805 std::string datastring((char*)&data[2], datasize-2);
1806 std::istringstream is(datastring, std::ios_base::binary);
1809 is.read((char*)buf, 6);
1810 v3s16 blockpos = readV3S16(buf);
1811 is.read((char*)buf, 2);
1812 s16 id = readS16(buf);
1813 is.read((char*)buf, 2);
1814 u16 textlen = readU16(buf);
1816 for(u16 i=0; i<textlen; i++)
1818 is.read((char*)buf, 1);
1819 text += (char)buf[0];
1822 MapBlock *block = NULL;
1825 block = m_env.getMap().getBlockNoCreate(blockpos);
1827 catch(InvalidPositionException &e)
1829 derr_server<<"Error while setting sign text: "
1830 "block not found"<<std::endl;
1834 MapBlockObject *obj = block->getObject(id);
1837 derr_server<<"Error while setting sign text: "
1838 "object not found"<<std::endl;
1842 if(obj->getTypeId() != MAPBLOCKOBJECT_TYPE_SIGN)
1844 derr_server<<"Error while setting sign text: "
1845 "object is not a sign"<<std::endl;
1849 ((SignObject*)obj)->setText(text);
1851 obj->getBlock()->setChangedFlag();
1855 derr_server<<"WARNING: Server::ProcessData(): Ignoring "
1856 "unknown command "<<command<<std::endl;
1860 catch(SendFailedException &e)
1862 derr_server<<"Server::ProcessData(): SendFailedException: "
1868 /*void Server::Send(u16 peer_id, u16 channelnum,
1869 SharedBuffer<u8> data, bool reliable)
1871 JMutexAutoLock lock(m_con_mutex);
1872 m_con.Send(peer_id, channelnum, data, reliable);
1875 void Server::SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver)
1877 DSTACK(__FUNCTION_NAME);
1879 Create a packet with the block in the right format
1882 std::ostringstream os(std::ios_base::binary);
1883 block->serialize(os, ver);
1884 std::string s = os.str();
1885 SharedBuffer<u8> blockdata((u8*)s.c_str(), s.size());
1887 u32 replysize = 8 + blockdata.getSize();
1888 SharedBuffer<u8> reply(replysize);
1889 v3s16 p = block->getPos();
1890 writeU16(&reply[0], TOCLIENT_BLOCKDATA);
1891 writeS16(&reply[2], p.X);
1892 writeS16(&reply[4], p.Y);
1893 writeS16(&reply[6], p.Z);
1894 memcpy(&reply[8], *blockdata, blockdata.getSize());
1896 /*dstream<<"Sending block ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
1897 <<": \tpacket size: "<<replysize<<std::endl;*/
1902 m_con.Send(peer_id, 1, reply, true);
1905 core::list<PlayerInfo> Server::getPlayerInfo()
1907 DSTACK(__FUNCTION_NAME);
1908 JMutexAutoLock envlock(m_env_mutex);
1909 JMutexAutoLock conlock(m_con_mutex);
1911 core::list<PlayerInfo> list;
1913 core::list<Player*> players = m_env.getPlayers();
1915 core::list<Player*>::Iterator i;
1916 for(i = players.begin();
1917 i != players.end(); i++)
1921 Player *player = *i;
1923 con::Peer *peer = m_con.GetPeer(player->peer_id);
1925 info.address = peer->address;
1926 info.avg_rtt = peer->avg_rtt;
1928 catch(con::PeerNotFoundException &e)
1930 // Outdated peer info
1932 info.address = Address(0,0,0,0,0);
1936 snprintf(info.name, PLAYERNAME_SIZE, "%s", player->getName());
1937 info.position = player->getPosition();
1939 list.push_back(info);
1945 void Server::peerAdded(con::Peer *peer)
1947 DSTACK(__FUNCTION_NAME);
1948 dout_server<<"Server::peerAdded(): peer->id="
1949 <<peer->id<<std::endl;
1951 // Connection is already locked when this is called.
1952 //JMutexAutoLock lock(m_con_mutex);
1955 core::map<u16, RemoteClient*>::Node *n;
1956 n = m_clients.find(peer->id);
1957 // The client shouldn't already exist
1961 RemoteClient *client = new RemoteClient();
1962 client->peer_id = peer->id;
1963 m_clients.insert(client->peer_id, client);
1967 // Already locked when called
1968 //JMutexAutoLock envlock(m_env_mutex);
1970 Player *player = m_env.getPlayer(peer->id);
1972 // The player shouldn't already exist
1973 assert(player == NULL);
1975 player = new RemotePlayer();
1976 player->peer_id = peer->id;
1982 // Get zero sector (it could have been unloaded to disk)
1983 m_env.getMap().emergeSector(v2s16(0,0));
1984 // Get ground height at origin
1985 f32 groundheight = m_env.getMap().getGroundHeight(v2s16(0,0), true);
1986 // The zero sector should have been generated
1987 assert(groundheight > GROUNDHEIGHT_VALID_MINVALUE);
1988 // Don't go underwater
1989 if(groundheight < WATER_LEVEL)
1990 groundheight = WATER_LEVEL;
1992 player->setPosition(intToFloat(v3s16(
1999 Add player to environment
2002 m_env.addPlayer(player);
2005 Add stuff to inventory
2008 if(g_settings.getBool("creative_mode"))
2010 // Give all materials
2011 assert(USEFUL_CONTENT_COUNT <= PLAYER_INVENTORY_SIZE);
2012 for(u16 i=0; i<USEFUL_CONTENT_COUNT; i++)
2014 // Skip some materials
2015 if(i == CONTENT_OCEAN)
2018 InventoryItem *item = new MaterialItem(i, 1);
2019 player->inventory.addItem(item);
2023 InventoryItem *item = new MapBlockObjectItem("Sign Example text");
2024 bool r = player->inventory.addItem(item);
2029 InventoryItem *item = new MapBlockObjectItem("Rat");
2030 bool r = player->inventory.addItem(item);
2038 InventoryItem *item = new MaterialItem(3, 999);
2039 bool r = player->inventory.addItem(item);
2043 for(u16 i=0; i<4; i++)
2045 InventoryItem *item = new MapBlockObjectItem("Sign Example text");
2046 bool r = player->inventory.addItem(item);
2050 for(u16 i=0; i<4; i++)
2052 InventoryItem *item = new MapBlockObjectItem("Rat");
2053 bool r = player->inventory.addItem(item);
2060 void Server::deletingPeer(con::Peer *peer, bool timeout)
2062 DSTACK(__FUNCTION_NAME);
2063 dout_server<<"Server::deletingPeer(): peer->id="
2064 <<peer->id<<", timeout="<<timeout<<std::endl;
2066 // Connection is already locked when this is called.
2067 //JMutexAutoLock lock(m_con_mutex);
2070 core::map<u16, RemoteClient*>::Node *n;
2071 n = m_clients.find(peer->id);
2072 // The client should exist
2077 // Already locked when called
2078 //JMutexAutoLock envlock(m_env_mutex);
2079 m_env.removePlayer(peer->id);
2083 delete m_clients[peer->id];
2084 m_clients.remove(peer->id);
2086 // Send player info to all clients
2090 void Server::SendObjectData(float dtime)
2092 DSTACK(__FUNCTION_NAME);
2094 core::map<v3s16, bool> stepped_blocks;
2096 for(core::map<u16, RemoteClient*>::Iterator
2097 i = m_clients.getIterator();
2098 i.atEnd() == false; i++)
2100 u16 peer_id = i.getNode()->getKey();
2101 RemoteClient *client = i.getNode()->getValue();
2102 assert(client->peer_id == peer_id);
2104 if(client->serialization_version == SER_FMT_VER_INVALID)
2107 client->SendObjectData(this, dtime, stepped_blocks);
2111 void Server::SendPlayerInfos()
2113 DSTACK(__FUNCTION_NAME);
2115 //JMutexAutoLock envlock(m_env_mutex);
2117 core::list<Player*> players = m_env.getPlayers();
2119 u32 player_count = players.getSize();
2120 u32 datasize = 2+(2+PLAYERNAME_SIZE)*player_count;
2122 SharedBuffer<u8> data(datasize);
2123 writeU16(&data[0], TOCLIENT_PLAYERINFO);
2126 core::list<Player*>::Iterator i;
2127 for(i = players.begin();
2128 i != players.end(); i++)
2130 Player *player = *i;
2132 /*dstream<<"Server sending player info for player with "
2133 "peer_id="<<player->peer_id<<std::endl;*/
2135 writeU16(&data[start], player->peer_id);
2136 snprintf((char*)&data[start+2], PLAYERNAME_SIZE, "%s", player->getName());
2137 start += 2+PLAYERNAME_SIZE;
2140 //JMutexAutoLock conlock(m_con_mutex);
2143 m_con.SendToAll(0, data, true);
2146 void Server::SendInventory(u16 peer_id)
2148 DSTACK(__FUNCTION_NAME);
2150 //JMutexAutoLock envlock(m_env_mutex);
2152 Player* player = m_env.getPlayer(peer_id);
2154 std::ostringstream os;
2155 //os.imbue(std::locale("C"));
2157 player->inventory.serialize(os);
2159 std::string s = os.str();
2161 SharedBuffer<u8> data(s.size()+2);
2162 writeU16(&data[0], TOCLIENT_INVENTORY);
2163 memcpy(&data[2], s.c_str(), s.size());
2165 //JMutexAutoLock conlock(m_con_mutex);
2168 m_con.Send(peer_id, 0, data, true);
2171 void Server::SendBlocks(float dtime)
2173 DSTACK(__FUNCTION_NAME);
2175 JMutexAutoLock envlock(m_env_mutex);
2177 core::array<PrioritySortedBlockTransfer> queue;
2179 s32 total_sending = 0;
2181 for(core::map<u16, RemoteClient*>::Iterator
2182 i = m_clients.getIterator();
2183 i.atEnd() == false; i++)
2185 RemoteClient *client = i.getNode()->getValue();
2186 assert(client->peer_id == i.getNode()->getKey());
2188 total_sending += client->SendingCount();
2190 if(client->serialization_version == SER_FMT_VER_INVALID)
2193 client->GetNextBlocks(this, dtime, queue);
2197 // Lowest priority number comes first.
2198 // Lowest is most important.
2201 JMutexAutoLock conlock(m_con_mutex);
2203 for(u32 i=0; i<queue.size(); i++)
2205 //TODO: Calculate limit dynamically
2206 if(total_sending >= g_settings.getS32
2207 ("max_simultaneous_block_sends_server_total"))
2210 PrioritySortedBlockTransfer q = queue[i];
2212 MapBlock *block = NULL;
2215 block = m_env.getMap().getBlockNoCreate(q.pos);
2217 catch(InvalidPositionException &e)
2222 RemoteClient *client = getClient(q.peer_id);
2224 SendBlockNoLock(q.peer_id, block, client->serialization_version);
2226 client->SentBlock(q.pos);
2233 RemoteClient* Server::getClient(u16 peer_id)
2235 DSTACK(__FUNCTION_NAME);
2236 //JMutexAutoLock lock(m_con_mutex);
2237 core::map<u16, RemoteClient*>::Node *n;
2238 n = m_clients.find(peer_id);
2239 // A client should exist for all peers
2241 return n->getValue();
2244 void Server::UpdateBlockWaterPressure(MapBlock *block,
2245 core::map<v3s16, MapBlock*> &modified_blocks)
2247 MapVoxelManipulator v(&m_env.getMap());
2248 v.m_disable_water_climb =
2249 g_settings.getBool("disable_water_climb");
2251 VoxelArea area(block->getPosRelative(),
2252 block->getPosRelative() + v3s16(1,1,1)*(MAP_BLOCKSIZE-1));
2256 v.updateAreaWaterPressure(area, m_flow_active_nodes);
2258 catch(ProcessingLimitException &e)
2260 dstream<<"Processing limit reached (1)"<<std::endl;
2263 v.blitBack(modified_blocks);