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 #define sleep_ms(x) Sleep(x)
37 #define sleep_ms(x) usleep(x*1000)
40 struct QueuedBlockEmerge
43 // key = peer_id, value = flags
44 core::map<u16, u8> peer_ids;
48 This is a thread-safe class.
50 class BlockEmergeQueue
60 JMutexAutoLock lock(m_mutex);
62 core::list<QueuedBlockEmerge*>::Iterator i;
63 for(i=m_queue.begin(); i!=m_queue.end(); i++)
65 QueuedBlockEmerge *q = *i;
71 peer_id=0 adds with nobody to send to
73 void addBlock(u16 peer_id, v3s16 pos, u8 flags)
75 DSTACK(__FUNCTION_NAME);
77 JMutexAutoLock lock(m_mutex);
82 Find if block is already in queue.
83 If it is, update the peer to it and quit.
85 core::list<QueuedBlockEmerge*>::Iterator i;
86 for(i=m_queue.begin(); i!=m_queue.end(); i++)
88 QueuedBlockEmerge *q = *i;
91 q->peer_ids[peer_id] = flags;
100 QueuedBlockEmerge *q = new QueuedBlockEmerge;
103 q->peer_ids[peer_id] = flags;
104 m_queue.push_back(q);
107 // Returned pointer must be deleted
108 // Returns NULL if queue is empty
109 QueuedBlockEmerge * pop()
111 JMutexAutoLock lock(m_mutex);
113 core::list<QueuedBlockEmerge*>::Iterator i = m_queue.begin();
114 if(i == m_queue.end())
116 QueuedBlockEmerge *q = *i;
123 JMutexAutoLock lock(m_mutex);
124 return m_queue.size();
127 u32 peerItemCount(u16 peer_id)
129 JMutexAutoLock lock(m_mutex);
133 core::list<QueuedBlockEmerge*>::Iterator i;
134 for(i=m_queue.begin(); i!=m_queue.end(); i++)
136 QueuedBlockEmerge *q = *i;
137 if(q->peer_ids.find(peer_id) != NULL)
145 core::list<QueuedBlockEmerge*> m_queue;
149 class SimpleThread : public JThread
163 virtual ~SimpleThread()
166 virtual void * Thread() = 0;
170 JMutexAutoLock lock(run_mutex);
173 void setRun(bool a_run)
175 JMutexAutoLock lock(run_mutex);
189 class ServerThread : public SimpleThread
195 ServerThread(Server *server):
204 class EmergeThread : public SimpleThread
210 EmergeThread(Server *server):
221 if(IsRunning() == false)
231 char name[PLAYERNAME_SIZE];
237 void PrintLine(std::ostream *s);
240 u32 PIChecksum(core::list<PlayerInfo> &l);
243 Used for queueing and sorting block transfers in containers
245 Lower priority number means higher priority.
247 struct PrioritySortedBlockTransfer
249 PrioritySortedBlockTransfer(float a_priority, v3s16 a_pos, u16 a_peer_id)
251 priority = a_priority;
255 bool operator < (PrioritySortedBlockTransfer &other)
257 return priority < other.priority;
267 // peer_id=0 means this client has no associated peer
268 // NOTE: If client is made allowed to exist while peer doesn't,
269 // this has to be set to 0 when there is no peer.
270 // Also, the client must be moved to some other container.
272 // The serialization version to use with the client
273 u8 serialization_version;
274 // Version is stored in here after INIT before INIT2
275 u8 pending_serialization_version;
278 m_time_from_building(0.0)
279 //m_num_blocks_in_emerge_queue(0)
282 serialization_version = SER_FMT_VER_INVALID;
283 pending_serialization_version = SER_FMT_VER_INVALID;
284 m_nearest_unsent_d = 0;
286 m_blocks_sent_mutex.Init();
287 m_blocks_sending_mutex.Init();
294 Finds block that should be sent next to the client.
295 Environment should be locked when this is called.
296 dtime is used for resetting send radius at slow interval
298 void GetNextBlocks(Server *server, float dtime,
299 core::array<PrioritySortedBlockTransfer> &dest);
301 // Connection and environment should be locked when this is called
302 // steps() objects of blocks not found in active_blocks, then
303 // adds those blocks to active_blocks
307 core::map<v3s16, bool> &stepped_blocks
310 void GotBlock(v3s16 p);
312 void SentBlock(v3s16 p);
314 void SetBlockNotSent(v3s16 p);
315 void SetBlocksNotSent(core::map<v3s16, MapBlock*> &blocks);
317 //void BlockEmerged();
319 /*bool IsSendingBlock(v3s16 p)
321 JMutexAutoLock lock(m_blocks_sending_mutex);
322 return (m_blocks_sending.find(p) != NULL);
327 JMutexAutoLock lock(m_blocks_sending_mutex);
328 return m_blocks_sending.size();
331 // Increments timeouts and removes timed-out blocks from list
332 // NOTE: This doesn't fix the server-not-sending-block bug
333 // because it is related to emerging, not sending.
334 //void RunSendingTimeouts(float dtime, float timeout);
336 void PrintInfo(std::ostream &o)
338 JMutexAutoLock l2(m_blocks_sent_mutex);
339 JMutexAutoLock l3(m_blocks_sending_mutex);
340 o<<"RemoteClient "<<peer_id<<": "
341 /*<<"m_num_blocks_in_emerge_queue="
342 <<m_num_blocks_in_emerge_queue.get()*/
343 <<", m_blocks_sent.size()="<<m_blocks_sent.size()
344 <<", m_blocks_sending.size()="<<m_blocks_sending.size()
345 <<", m_nearest_unsent_d="<<m_nearest_unsent_d
349 // Time from last placing or removing blocks
350 MutexedVariable<float> m_time_from_building;
354 All members that are accessed by many threads should
355 obviously be behind a mutex. The threads include:
356 - main thread (calls step())
357 - server thread (calls AsyncRunStep() and Receive())
361 //TODO: core::map<v3s16, MapBlock*> m_active_blocks
362 //NOTE: Not here, it should be server-wide!
364 // Number of blocks in the emerge queue that have this client as
365 // a receiver. Used for throttling network usage.
366 //MutexedVariable<s16> m_num_blocks_in_emerge_queue;
369 Blocks that have been sent to client.
370 - These don't have to be sent again.
371 - A block is cleared from here when client says it has
372 deleted it from it's memory
374 Key is position, value is dummy.
375 No MapBlock* is stored here because the blocks can get deleted.
377 core::map<v3s16, bool> m_blocks_sent;
378 s16 m_nearest_unsent_d;
380 JMutex m_blocks_sent_mutex;
382 Blocks that are currently on the line.
383 This is used for throttling the sending of blocks.
384 - The size of this list is limited to some value
385 Block is added when it is sent with BLOCKDATA.
386 Block is removed when GOTBLOCKS is received.
387 Value is time from sending. (not used at the moment)
389 core::map<v3s16, float> m_blocks_sending;
390 JMutex m_blocks_sending_mutex;
393 /*struct ServerSettings
397 creative_mode = false;
402 class Server : public con::PeerHandler
406 NOTE: Every public method should be thread-safe
409 std::string mapsavedir,
414 void start(unsigned short port);
416 // This is mainly a way to pass the time to the server.
417 // Actual processing is done in an another thread.
418 void step(float dtime);
419 // This is run by ServerThread and does the actual processing
422 void ProcessData(u8 *data, u32 datasize, u16 peer_id);
424 /*void Send(u16 peer_id, u16 channelnum,
425 SharedBuffer<u8> data, bool reliable);*/
427 // Environment and Connection must be locked when called
428 void SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver);
429 //TODO: Sending of many blocks in a single packet
431 // Environment and Connection must be locked when called
432 //void SendSectorMeta(u16 peer_id, core::list<v2s16> ps, u8 ver);
434 core::list<PlayerInfo> getPlayerInfo();
438 // Virtual methods from con::PeerHandler.
439 // As of now, these create and remove clients and players.
440 // TODO: Make it possible to leave players on server.
441 void peerAdded(con::Peer *peer);
442 void deletingPeer(con::Peer *peer, bool timeout);
444 // Envlock and conlock should be locked when calling these
445 void SendObjectData(float dtime);
446 void SendPlayerInfos();
447 void SendInventory(u16 peer_id);
448 // Sends blocks to clients
449 void SendBlocks(float dtime);
451 // When called, connection mutex should be locked
452 RemoteClient* getClient(u16 peer_id);
454 // NOTE: If connection and environment are both to be locked,
455 // environment shall be locked first.
461 con::Connection m_con;
462 core::map<u16, RemoteClient*> m_clients; // Behind the con mutex
465 JMutex m_step_dtime_mutex;
467 ServerThread m_thread;
468 EmergeThread m_emergethread;
470 BlockEmergeQueue m_emerge_queue;
472 // Nodes that are destinations of flowing liquid at the moment
473 core::map<v3s16, u8> m_flow_active_nodes;
475 friend class EmergeThread;
476 friend class RemoteClient;