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 "connection.h"
28 #include "environment.h"
29 #include "common_irrlicht.h"
34 #include "inventory.h"
39 v3f findSpawnPos(ServerMap &map);
42 A structure containing the data needed for queueing the fetching
45 struct QueuedBlockEmerge
48 // key = peer_id, value = flags
49 core::map<u16, u8> peer_ids;
53 This is a thread-safe class.
55 class BlockEmergeQueue
65 JMutexAutoLock lock(m_mutex);
67 core::list<QueuedBlockEmerge*>::Iterator i;
68 for(i=m_queue.begin(); i!=m_queue.end(); i++)
70 QueuedBlockEmerge *q = *i;
76 peer_id=0 adds with nobody to send to
78 void addBlock(u16 peer_id, v3s16 pos, u8 flags)
80 DSTACK(__FUNCTION_NAME);
82 JMutexAutoLock lock(m_mutex);
87 Find if block is already in queue.
88 If it is, update the peer to it and quit.
90 core::list<QueuedBlockEmerge*>::Iterator i;
91 for(i=m_queue.begin(); i!=m_queue.end(); i++)
93 QueuedBlockEmerge *q = *i;
96 q->peer_ids[peer_id] = flags;
105 QueuedBlockEmerge *q = new QueuedBlockEmerge;
108 q->peer_ids[peer_id] = flags;
109 m_queue.push_back(q);
112 // Returned pointer must be deleted
113 // Returns NULL if queue is empty
114 QueuedBlockEmerge * pop()
116 JMutexAutoLock lock(m_mutex);
118 core::list<QueuedBlockEmerge*>::Iterator i = m_queue.begin();
119 if(i == m_queue.end())
121 QueuedBlockEmerge *q = *i;
128 JMutexAutoLock lock(m_mutex);
129 return m_queue.size();
132 u32 peerItemCount(u16 peer_id)
134 JMutexAutoLock lock(m_mutex);
138 core::list<QueuedBlockEmerge*>::Iterator i;
139 for(i=m_queue.begin(); i!=m_queue.end(); i++)
141 QueuedBlockEmerge *q = *i;
142 if(q->peer_ids.find(peer_id) != NULL)
150 core::list<QueuedBlockEmerge*> m_queue;
156 class ServerThread : public SimpleThread
162 ServerThread(Server *server):
171 class EmergeThread : public SimpleThread
177 EmergeThread(Server *server):
188 if(IsRunning() == false)
198 char name[PLAYERNAME_SIZE];
204 void PrintLine(std::ostream *s);
207 u32 PIChecksum(core::list<PlayerInfo> &l);
210 Used for queueing and sorting block transfers in containers
212 Lower priority number means higher priority.
214 struct PrioritySortedBlockTransfer
216 PrioritySortedBlockTransfer(float a_priority, v3s16 a_pos, u16 a_peer_id)
218 priority = a_priority;
222 bool operator < (PrioritySortedBlockTransfer &other)
224 return priority < other.priority;
234 // peer_id=0 means this client has no associated peer
235 // NOTE: If client is made allowed to exist while peer doesn't,
236 // this has to be set to 0 when there is no peer.
237 // Also, the client must be moved to some other container.
239 // The serialization version to use with the client
240 u8 serialization_version;
241 // Version is stored in here after INIT before INIT2
242 u8 pending_serialization_version;
245 m_time_from_building(9999),
246 m_excess_gotblocks(0)
249 serialization_version = SER_FMT_VER_INVALID;
250 pending_serialization_version = SER_FMT_VER_INVALID;
251 m_nearest_unsent_d = 0;
252 m_nearest_unsent_reset_timer = 0.0;
259 Finds block that should be sent next to the client.
260 Environment should be locked when this is called.
261 dtime is used for resetting send radius at slow interval
263 void GetNextBlocks(Server *server, float dtime,
264 core::array<PrioritySortedBlockTransfer> &dest);
267 Connection and environment should be locked when this is called.
268 steps() objects of blocks not found in active_blocks, then
269 adds those blocks to active_blocks
274 core::map<v3s16, bool> &stepped_blocks
277 void GotBlock(v3s16 p);
279 void SentBlock(v3s16 p);
281 void SetBlockNotSent(v3s16 p);
282 void SetBlocksNotSent(core::map<v3s16, MapBlock*> &blocks);
286 return m_blocks_sending.size();
289 // Increments timeouts and removes timed-out blocks from list
290 // NOTE: This doesn't fix the server-not-sending-block bug
291 // because it is related to emerging, not sending.
292 //void RunSendingTimeouts(float dtime, float timeout);
294 void PrintInfo(std::ostream &o)
296 o<<"RemoteClient "<<peer_id<<": "
297 <<"m_blocks_sent.size()="<<m_blocks_sent.size()
298 <<", m_blocks_sending.size()="<<m_blocks_sending.size()
299 <<", m_nearest_unsent_d="<<m_nearest_unsent_d
300 <<", m_excess_gotblocks="<<m_excess_gotblocks
302 m_excess_gotblocks = 0;
305 // Time from last placing or removing blocks
306 float m_time_from_building;
308 /*JMutex m_dig_mutex;
309 float m_dig_time_remaining;
312 v3s16 m_dig_position;*/
315 List of active objects that the client knows of.
318 core::map<u16, bool> m_known_objects;
322 Blocks that have been sent to client.
323 - These don't have to be sent again.
324 - A block is cleared from here when client says it has
325 deleted it from it's memory
327 Key is position, value is dummy.
328 No MapBlock* is stored here because the blocks can get deleted.
330 core::map<v3s16, bool> m_blocks_sent;
331 s16 m_nearest_unsent_d;
333 float m_nearest_unsent_reset_timer;
336 Blocks that are currently on the line.
337 This is used for throttling the sending of blocks.
338 - The size of this list is limited to some value
339 Block is added when it is sent with BLOCKDATA.
340 Block is removed when GOTBLOCKS is received.
341 Value is time from sending. (not used at the moment)
343 core::map<v3s16, float> m_blocks_sending;
346 Count of excess GotBlocks().
347 There is an excess amount because the client sometimes
348 gets a block so late that the server sends it again,
349 and the client then sends two GOTBLOCKs.
350 This is resetted by PrintInfo()
352 u32 m_excess_gotblocks;
355 class Server : public con::PeerHandler, public MapEventReceiver,
356 public InventoryManager
360 NOTE: Every public method should be thread-safe
364 std::string mapsavedir
367 void start(unsigned short port);
369 // This is mainly a way to pass the time to the server.
370 // Actual processing is done in an another thread.
371 void step(float dtime);
372 // This is run by ServerThread and does the actual processing
375 void ProcessData(u8 *data, u32 datasize, u16 peer_id);
377 core::list<PlayerInfo> getPlayerInfo();
379 u32 getDayNightRatio()
381 return time_to_daynight_ratio(m_time_of_day.get());
384 bool getShutdownRequested()
386 return m_shutdown_requested.get();
390 Shall be called with the environment locked.
391 This is accessed by the map, which is inside the environment,
392 so it shouldn't be a problem.
394 void onMapEditEvent(MapEditEvent *event);
397 Shall be called with the environment and the connection locked.
399 Inventory* getInventory(InventoryContext *c, std::string id);
400 void inventoryModified(InventoryContext *c, std::string id);
404 // Virtual methods from con::PeerHandler.
405 // As of now, these create and remove clients and players.
406 void peerAdded(con::Peer *peer);
407 void deletingPeer(con::Peer *peer, bool timeout);
413 static void SendHP(con::Connection &con, u16 peer_id, u8 hp);
416 Non-static send methods
419 // Envlock and conlock should be locked when calling these
420 void SendObjectData(float dtime);
421 void SendPlayerInfos();
422 void SendInventory(u16 peer_id);
423 void SendChatMessage(u16 peer_id, const std::wstring &message);
424 void BroadcastChatMessage(const std::wstring &message);
425 void SendPlayerHP(Player *player);
426 void SendMovePlayer(Player *player);
428 Send a node removal/addition event to all clients except ignore_id.
429 Additionally, if far_players!=NULL, players further away than
430 far_d_nodes are ignored and their peer_ids are added to far_players
432 void sendRemoveNode(v3s16 p, u16 ignore_id=0,
433 core::list<u16> *far_players=NULL, float far_d_nodes=100);
434 void sendAddNode(v3s16 p, MapNode n, u16 ignore_id=0,
435 core::list<u16> *far_players=NULL, float far_d_nodes=100);
437 // Environment and Connection must be locked when called
438 void SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver);
440 // Sends blocks to clients
441 void SendBlocks(float dtime);
447 void UpdateCrafting(u16 peer_id);
449 // When called, connection mutex should be locked
450 RemoteClient* getClient(u16 peer_id);
452 // Connection must be locked when called
453 std::wstring getStatusString();
456 Get a player from memory or creates one.
457 If player is already connected, return NULL
459 Call with env and con locked.
461 Player *emergePlayer(const char *name, const char *password,
465 Update water pressure.
466 This also adds suitable nodes to active_nodes.
468 environment has to be locked when calling.
470 /*void UpdateBlockWaterPressure(MapBlock *block,
471 core::map<v3s16, MapBlock*> &modified_blocks);*/
473 // Locks environment and connection by its own
475 void handlePeerChange(PeerChange &c);
476 void handlePeerChanges();
478 //float m_flowwater_timer;
479 float m_liquid_transform_timer;
480 float m_print_info_timer;
481 float m_objectdata_timer;
482 float m_emergethread_trigger_timer;
483 float m_savemap_timer;
485 // NOTE: If connection and environment are both to be locked,
486 // environment shall be locked first.
488 ServerEnvironment m_env;
491 con::Connection m_con;
492 core::map<u16, RemoteClient*> m_clients; // Behind the con mutex
495 JMutex m_step_dtime_mutex;
497 ServerThread m_thread;
498 EmergeThread m_emergethread;
500 BlockEmergeQueue m_emerge_queue;
502 // Nodes that are destinations of flowing liquid at the moment
503 //core::map<v3s16, u8> m_flow_active_nodes;
506 MutexedVariable<u32> m_time_of_day;
507 // Used to buffer dtime for adding to m_time_of_day
508 float m_time_counter;
509 float m_time_of_day_send_timer;
511 MutexedVariable<double> m_uptime;
526 Queue<PeerChange> m_peer_change_queue;
528 std::string m_mapsavedir;
530 MutexedVariable<bool> m_shutdown_requested;
533 Queue of map edits from the environment for sending to the clients
534 This is behind m_env_mutex
536 Queue<MapEditEvent*> m_unsent_map_edit_queue;
538 Set to true when the server itself is modifying the map and does
539 all sending of information by itself.
540 This is behind m_env_mutex
542 bool m_ignore_map_edit_events;
544 If set to !=0, the incoming MapEditEvents are modified to have
545 this peed id as the disabled recipient
546 This is behind m_env_mutex
548 u16 m_ignore_map_edit_events_peer_id;
550 friend class EmergeThread;
551 friend class RemoteClient;
555 Runs a simple dedicated server loop.
557 Shuts down when run is set to false.
559 void dedicated_server_loop(Server &server, bool &run);