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"
36 struct QueuedBlockEmerge
39 // key = peer_id, value = flags
40 core::map<u16, u8> peer_ids;
44 This is a thread-safe class.
46 class BlockEmergeQueue
56 JMutexAutoLock lock(m_mutex);
58 core::list<QueuedBlockEmerge*>::Iterator i;
59 for(i=m_queue.begin(); i!=m_queue.end(); i++)
61 QueuedBlockEmerge *q = *i;
67 peer_id=0 adds with nobody to send to
69 void addBlock(u16 peer_id, v3s16 pos, u8 flags)
71 DSTACK(__FUNCTION_NAME);
73 JMutexAutoLock lock(m_mutex);
78 Find if block is already in queue.
79 If it is, update the peer to it and quit.
81 core::list<QueuedBlockEmerge*>::Iterator i;
82 for(i=m_queue.begin(); i!=m_queue.end(); i++)
84 QueuedBlockEmerge *q = *i;
87 q->peer_ids[peer_id] = flags;
96 QueuedBlockEmerge *q = new QueuedBlockEmerge;
99 q->peer_ids[peer_id] = flags;
100 m_queue.push_back(q);
103 // Returned pointer must be deleted
104 // Returns NULL if queue is empty
105 QueuedBlockEmerge * pop()
107 JMutexAutoLock lock(m_mutex);
109 core::list<QueuedBlockEmerge*>::Iterator i = m_queue.begin();
110 if(i == m_queue.end())
112 QueuedBlockEmerge *q = *i;
119 JMutexAutoLock lock(m_mutex);
120 return m_queue.size();
123 u32 peerItemCount(u16 peer_id)
125 JMutexAutoLock lock(m_mutex);
129 core::list<QueuedBlockEmerge*>::Iterator i;
130 for(i=m_queue.begin(); i!=m_queue.end(); i++)
132 QueuedBlockEmerge *q = *i;
133 if(q->peer_ids.find(peer_id) != NULL)
141 core::list<QueuedBlockEmerge*> m_queue;
147 class ServerThread : public SimpleThread
153 ServerThread(Server *server):
162 class EmergeThread : public SimpleThread
168 EmergeThread(Server *server):
179 if(IsRunning() == false)
189 char name[PLAYERNAME_SIZE];
195 void PrintLine(std::ostream *s);
198 u32 PIChecksum(core::list<PlayerInfo> &l);
201 Used for queueing and sorting block transfers in containers
203 Lower priority number means higher priority.
205 struct PrioritySortedBlockTransfer
207 PrioritySortedBlockTransfer(float a_priority, v3s16 a_pos, u16 a_peer_id)
209 priority = a_priority;
213 bool operator < (PrioritySortedBlockTransfer &other)
215 return priority < other.priority;
225 // peer_id=0 means this client has no associated peer
226 // NOTE: If client is made allowed to exist while peer doesn't,
227 // this has to be set to 0 when there is no peer.
228 // Also, the client must be moved to some other container.
230 // The serialization version to use with the client
231 u8 serialization_version;
232 // Version is stored in here after INIT before INIT2
233 u8 pending_serialization_version;
236 m_time_from_building(9999),
237 m_excess_gotblocks(0)
240 serialization_version = SER_FMT_VER_INVALID;
241 pending_serialization_version = SER_FMT_VER_INVALID;
242 m_nearest_unsent_d = 0;
243 m_nearest_unsent_reset_timer = 0.0;
250 Finds block that should be sent next to the client.
251 Environment should be locked when this is called.
252 dtime is used for resetting send radius at slow interval
254 void GetNextBlocks(Server *server, float dtime,
255 core::array<PrioritySortedBlockTransfer> &dest);
258 Connection and environment should be locked when this is called.
259 steps() objects of blocks not found in active_blocks, then
260 adds those blocks to active_blocks
265 core::map<v3s16, bool> &stepped_blocks
268 void GotBlock(v3s16 p);
270 void SentBlock(v3s16 p);
272 void SetBlockNotSent(v3s16 p);
273 void SetBlocksNotSent(core::map<v3s16, MapBlock*> &blocks);
277 return m_blocks_sending.size();
280 // Increments timeouts and removes timed-out blocks from list
281 // NOTE: This doesn't fix the server-not-sending-block bug
282 // because it is related to emerging, not sending.
283 //void RunSendingTimeouts(float dtime, float timeout);
285 void PrintInfo(std::ostream &o)
287 o<<"RemoteClient "<<peer_id<<": "
288 <<", m_blocks_sent.size()="<<m_blocks_sent.size()
289 <<", m_blocks_sending.size()="<<m_blocks_sending.size()
290 <<", m_nearest_unsent_d="<<m_nearest_unsent_d
291 <<", m_excess_gotblocks="<<m_excess_gotblocks
293 m_excess_gotblocks = 0;
296 // Time from last placing or removing blocks
297 float m_time_from_building;
299 /*JMutex m_dig_mutex;
300 float m_dig_time_remaining;
303 v3s16 m_dig_position;*/
306 List of active objects that the client knows of.
309 core::map<u16, bool> m_known_objects;
313 Blocks that have been sent to client.
314 - These don't have to be sent again.
315 - A block is cleared from here when client says it has
316 deleted it from it's memory
318 Key is position, value is dummy.
319 No MapBlock* is stored here because the blocks can get deleted.
321 core::map<v3s16, bool> m_blocks_sent;
322 s16 m_nearest_unsent_d;
324 float m_nearest_unsent_reset_timer;
327 Blocks that are currently on the line.
328 This is used for throttling the sending of blocks.
329 - The size of this list is limited to some value
330 Block is added when it is sent with BLOCKDATA.
331 Block is removed when GOTBLOCKS is received.
332 Value is time from sending. (not used at the moment)
334 core::map<v3s16, float> m_blocks_sending;
337 Count of excess GotBlocks().
338 There is an excess amount because the client sometimes
339 gets a block so late that the server sends it again,
340 and the client then sends two GOTBLOCKs.
341 This is resetted by PrintInfo()
343 u32 m_excess_gotblocks;
346 class Server : public con::PeerHandler, public MapEventReceiver,
347 public InventoryManager
351 NOTE: Every public method should be thread-safe
355 std::string mapsavedir
358 void start(unsigned short port);
360 // This is mainly a way to pass the time to the server.
361 // Actual processing is done in an another thread.
362 void step(float dtime);
363 // This is run by ServerThread and does the actual processing
366 void ProcessData(u8 *data, u32 datasize, u16 peer_id);
368 core::list<PlayerInfo> getPlayerInfo();
370 u32 getDayNightRatio()
372 return time_to_daynight_ratio(m_time_of_day.get());
375 bool getShutdownRequested()
377 return m_shutdown_requested.get();
381 Shall be called with the environment locked.
382 This is accessed by the map, which is inside the environment,
383 so it shouldn't be a problem.
385 void onMapEditEvent(MapEditEvent *event);
388 Shall be called with the environment and the connection locked.
390 Inventory* getInventory(InventoryContext *c, std::string id);
391 void inventoryModified(InventoryContext *c, std::string id);
395 // Virtual methods from con::PeerHandler.
396 // As of now, these create and remove clients and players.
397 void peerAdded(con::Peer *peer);
398 void deletingPeer(con::Peer *peer, bool timeout);
400 // Envlock and conlock should be locked when calling these
401 void SendObjectData(float dtime);
402 void SendPlayerInfos();
403 void SendInventory(u16 peer_id);
404 void SendChatMessage(u16 peer_id, const std::wstring &message);
405 void BroadcastChatMessage(const std::wstring &message);
406 void sendRemoveNode(v3s16 p, u16 ignore_id=0);
407 void sendAddNode(v3s16 p, MapNode n, u16 ignore_id=0);
409 // Environment and Connection must be locked when called
410 void SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver);
412 // Sends blocks to clients
413 void SendBlocks(float dtime);
415 // When called, connection mutex should be locked
416 RemoteClient* getClient(u16 peer_id);
418 // Connection must be locked when called
419 std::wstring getStatusString();
422 Get a player from memory or creates one.
423 If player is already connected, return NULL
425 Call with env and con locked.
427 Player *emergePlayer(const char *name, const char *password,
431 Update water pressure.
432 This also adds suitable nodes to active_nodes.
434 environment has to be locked when calling.
436 /*void UpdateBlockWaterPressure(MapBlock *block,
437 core::map<v3s16, MapBlock*> &modified_blocks);*/
439 // Locks environment and connection by its own
441 void handlePeerChange(PeerChange &c);
442 void handlePeerChanges();
444 //float m_flowwater_timer;
445 float m_liquid_transform_timer;
446 float m_print_info_timer;
447 float m_objectdata_timer;
448 float m_emergethread_trigger_timer;
449 float m_savemap_timer;
451 // NOTE: If connection and environment are both to be locked,
452 // environment shall be locked first.
454 ServerEnvironment m_env;
457 con::Connection m_con;
458 core::map<u16, RemoteClient*> m_clients; // Behind the con mutex
461 JMutex m_step_dtime_mutex;
463 ServerThread m_thread;
464 EmergeThread m_emergethread;
466 BlockEmergeQueue m_emerge_queue;
468 // Nodes that are destinations of flowing liquid at the moment
469 //core::map<v3s16, u8> m_flow_active_nodes;
472 MutexedVariable<u32> m_time_of_day;
473 // Used to buffer dtime for adding to m_time_of_day
474 float m_time_counter;
475 float m_time_of_day_send_timer;
477 MutexedVariable<double> m_uptime;
492 Queue<PeerChange> m_peer_change_queue;
494 std::string m_mapsavedir;
496 MutexedVariable<bool> m_shutdown_requested;
499 Queue of map edits from the environment for sending to the clients
500 This is behind m_env_mutex
502 Queue<MapEditEvent*> m_unsent_map_edit_queue;
504 Set to true when the server itself is modifying the map and does
505 all sending of information by itself.
506 This is behind m_env_mutex
508 bool m_ignore_map_edit_events;
510 If set to !=0, the incoming MapEditEvents are modified to have
511 this peed id as the disabled recipient
512 This is behind m_env_mutex
514 u16 m_ignore_map_edit_events_peer_id;
516 friend class EmergeThread;
517 friend class RemoteClient;
521 Runs a simple dedicated server loop.
523 Shuts down when run is set to false.
525 void dedicated_server_loop(Server &server, bool &run);