3 Copyright (C) 2010-2014 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 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.
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.
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.
19 #ifndef _CLIENTIFACE_H_
20 #define _CLIENTIFACE_H_
22 #include "irr_v3d.h" // for irrlicht datatypes
24 #include "constants.h"
25 #include "serialization.h" // for SER_FMT_VER_INVALID
26 #include "jthread/jmutex.h"
34 class ServerEnvironment;
51 +-----------------------------+ invalid playername, password
52 |IN: | or denied by mod
53 | TOSERVER_INIT |------------------------------
54 +-----------------------------+ |
58 +-----------------------------+ |
61 +-----------------------------+ |
68 \-----------------/ +------------------
70 +-----------------------------+ +-----------------------------+ |
72 | TOSERVER_INIT2 | | TOCLIENT_ACCESS_DENIED | |
73 +-----------------------------+ +-----------------------------+ |
76 /-----------------\ /-----------------\ |
78 | InitDone | | Denied | |
80 \-----------------/ \-----------------/ |
82 +-----------------------------+ |
84 | TOCLIENT_MOVEMENT | |
85 | TOCLIENT_ITEMDEF | |
86 | TOCLIENT_NODEDEF | |
87 | TOCLIENT_ANNOUNCE_MEDIA | |
88 | TOCLIENT_DETACHED_INVENTORY | |
89 | TOCLIENT_TIME_OF_DAY | |
90 +-----------------------------+ |
93 | ----------------------------------- |
95 /-----------------\ v |
96 | | +-----------------------------+ |
97 | DefinitionsSent | |IN: | |
98 | | | TOSERVER_REQUEST_MEDIA | |
99 \-----------------/ | TOSERVER_RECEIVED_MEDIA | |
100 | +-----------------------------+ |
102 | ----------------------------------- |
104 +-----------------------------+ |
106 | TOSERVER_CLIENT_READY | |
107 +-----------------------------+ |
110 +-----------------------------+ (ban,kick) |
112 | TOCLIENT_MOVE_PLAYER | |
113 | TOCLIENT_PRIVILEGES | |
114 | TOCLIENT_INVENTORY_FORMSPEC | |
116 | TOCLIENT_INVENTORY | |
117 | TOCLIENT_HP (opt) | |
118 | TOCLIENT_BREATH | |
119 | TOCLIENT_DEATHSCREEN | |
120 +-----------------------------+ |
123 /-----------------\ |
124 | |------------------------------------------------------
126 | |----------------------------------
127 \-----------------/ timeout |
128 | +-----------------------------+
130 | | TOCLIENT_DISCONNECT |
131 | +-----------------------------+
134 +-----------------------------+ /-----------------\
136 | TOSERVER_DISCONNECT |------------------->| Disconnecting |
137 +-----------------------------+ | |
144 #define CI_ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
158 enum ClientStateEvent
163 CSE_SetDefinitionsSent,
169 Used for queueing and sorting block transfers in containers
171 Lower priority number means higher priority.
173 struct PrioritySortedBlockTransfer
175 PrioritySortedBlockTransfer(float a_priority, v3s16 a_pos, u16 a_peer_id)
177 priority = a_priority;
181 bool operator < (const PrioritySortedBlockTransfer &other) const
183 return priority < other.priority;
193 // peer_id=0 means this client has no associated peer
194 // NOTE: If client is made allowed to exist while peer doesn't,
195 // this has to be set to 0 when there is no peer.
196 // Also, the client must be moved to some other container.
198 // The serialization version to use with the client
199 u8 serialization_version;
201 u16 net_proto_version;
204 peer_id(PEER_ID_INEXISTENT),
205 serialization_version(SER_FMT_VER_INVALID),
206 net_proto_version(0),
207 m_time_from_building(9999),
208 m_pending_serialization_version(SER_FMT_VER_INVALID),
210 m_nearest_unsent_d(0),
211 m_nearest_unsent_reset_timer(0.0),
212 m_excess_gotblocks(0),
213 m_nothing_to_send_pause_timer(0.0),
218 m_full_version("unknown"),
219 m_connection_time(getTime(PRECISION_SECONDS))
227 Finds block that should be sent next to the client.
228 Environment should be locked when this is called.
229 dtime is used for resetting send radius at slow interval
231 void GetNextBlocks(ServerEnvironment *env, EmergeManager* emerge,
232 float dtime, std::vector<PrioritySortedBlockTransfer> &dest);
234 void GotBlock(v3s16 p);
236 void SentBlock(v3s16 p);
238 void SetBlockNotSent(v3s16 p);
239 void SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks);
242 * tell client about this block being modified right now.
243 * this information is required to requeue the block in case it's "on wire"
244 * while modification is processed by server
245 * @param p position of modified block
247 void ResendBlockIfOnWire(v3s16 p);
251 return m_blocks_sending.size();
254 // Increments timeouts and removes timed-out blocks from list
255 // NOTE: This doesn't fix the server-not-sending-block bug
256 // because it is related to emerging, not sending.
257 //void RunSendingTimeouts(float dtime, float timeout);
259 void PrintInfo(std::ostream &o)
261 o<<"RemoteClient "<<peer_id<<": "
262 <<"m_blocks_sent.size()="<<m_blocks_sent.size()
263 <<", m_blocks_sending.size()="<<m_blocks_sending.size()
264 <<", m_nearest_unsent_d="<<m_nearest_unsent_d
265 <<", m_excess_gotblocks="<<m_excess_gotblocks
267 m_excess_gotblocks = 0;
270 // Time from last placing or removing blocks
271 float m_time_from_building;
274 List of active objects that the client knows of.
277 std::set<u16> m_known_objects;
279 ClientState getState()
282 std::string getName()
285 void setName(std::string name)
288 /* update internal client state */
289 void notifyEvent(ClientStateEvent event);
291 /* set expected serialization version */
292 void setPendingSerializationVersion(u8 version)
293 { m_pending_serialization_version = version; }
295 void confirmSerializationVersion()
296 { serialization_version = m_pending_serialization_version; }
302 /* set version information */
303 void setVersionInfo(u8 major, u8 minor, u8 patch, std::string full) {
304 m_version_major = major;
305 m_version_minor = minor;
306 m_version_patch = patch;
307 m_full_version = full;
310 /* read version information */
311 u8 getMajor() { return m_version_major; }
312 u8 getMinor() { return m_version_minor; }
313 u8 getPatch() { return m_version_patch; }
314 std::string getVersion() { return m_full_version; }
316 // Version is stored in here after INIT before INIT2
317 u8 m_pending_serialization_version;
319 /* current state of client */
323 Blocks that have been sent to client.
324 - These don't have to be sent again.
325 - A block is cleared from here when client says it has
326 deleted it from it's memory
328 Key is position, value is dummy.
329 No MapBlock* is stored here because the blocks can get deleted.
331 std::set<v3s16> m_blocks_sent;
332 s16 m_nearest_unsent_d;
334 float m_nearest_unsent_reset_timer;
337 Blocks that are currently on the line.
338 This is used for throttling the sending of blocks.
339 - The size of this list is limited to some value
340 Block is added when it is sent with BLOCKDATA.
341 Block is removed when GOTBLOCKS is received.
342 Value is time from sending. (not used at the moment)
344 std::map<v3s16, float> m_blocks_sending;
347 Count of excess GotBlocks().
348 There is an excess amount because the client sometimes
349 gets a block so late that the server sends it again,
350 and the client then sends two GOTBLOCKs.
351 This is resetted by PrintInfo()
353 u32 m_excess_gotblocks;
355 // CPU usage optimization
356 float m_nothing_to_send_pause_timer;
359 name of player using this client
370 std::string m_full_version;
373 time this client was created
375 const u32 m_connection_time;
378 class ClientInterface {
383 ClientInterface(con::Connection* con);
387 void step(float dtime);
389 /* get list of active client id's */
390 std::list<u16> getClientIDs(ClientState min_state=CS_Active);
392 /* get list of client player names */
393 std::vector<std::string> getPlayerNames();
395 /* send message to client */
396 void send(u16 peer_id, u8 channelnum, SharedBuffer<u8> data, bool reliable);
398 /* send to all clients */
399 void sendToAll(u16 channelnum, SharedBuffer<u8> data, bool reliable);
401 /* delete a client */
402 void DeleteClient(u16 peer_id);
405 void CreateClient(u16 peer_id);
407 /* get a client by peer_id */
408 RemoteClient* getClientNoEx(u16 peer_id, ClientState state_min=CS_Active);
410 /* get client by peer_id (make sure you have list lock before!*/
411 RemoteClient* lockedGetClientNoEx(u16 peer_id, ClientState state_min=CS_Active);
413 /* get state of client by id*/
414 ClientState getClientState(u16 peer_id);
416 /* set client playername */
417 void setPlayerName(u16 peer_id,std::string name);
419 /* get protocol version of client */
420 u16 getProtocolVersion(u16 peer_id);
422 /* set client version */
423 void setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch, std::string full);
425 /* event to update client state */
426 void event(u16 peer_id, ClientStateEvent event);
428 /* set environment */
429 void setEnv(ServerEnvironment* env)
430 { assert(m_env == 0); m_env = env; }
432 static std::string state2Name(ClientState state);
435 //TODO find way to avoid this functions
437 { m_clients_mutex.Lock(); }
439 { m_clients_mutex.Unlock(); }
441 std::map<u16, RemoteClient*>& getClientList()
442 { return m_clients; }
445 /* update internal player list */
446 void UpdatePlayerList();
449 con::Connection* m_con;
450 JMutex m_clients_mutex;
451 // Connected clients (behind the con mutex)
452 std::map<u16, RemoteClient*> m_clients;
453 std::vector<std::string> m_clients_names; //for announcing masterserver
456 ServerEnvironment *m_env;
459 float m_print_info_timer;
461 static const char *statenames[];