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"
27 #include "network/networkpacket.h"
35 class ServerEnvironment;
52 +-----------------------------+ invalid playername, password
53 |IN: | or denied by mod
54 | TOSERVER_INIT |------------------------------
55 +-----------------------------+ |
59 +-----------------------------+ |
62 +-----------------------------+ |
69 \-----------------/ +------------------
71 +-----------------------------+ +-----------------------------+ |
73 | TOSERVER_INIT2 | | TOCLIENT_ACCESS_DENIED | |
74 +-----------------------------+ +-----------------------------+ |
77 /-----------------\ /-----------------\ |
79 | InitDone | | Denied | |
81 \-----------------/ \-----------------/ |
83 +-----------------------------+ |
85 | TOCLIENT_MOVEMENT | |
86 | TOCLIENT_ITEMDEF | |
87 | TOCLIENT_NODEDEF | |
88 | TOCLIENT_ANNOUNCE_MEDIA | |
89 | TOCLIENT_DETACHED_INVENTORY | |
90 | TOCLIENT_TIME_OF_DAY | |
91 +-----------------------------+ |
94 | ----------------------------------- |
96 /-----------------\ v |
97 | | +-----------------------------+ |
98 | DefinitionsSent | |IN: | |
99 | | | TOSERVER_REQUEST_MEDIA | |
100 \-----------------/ | TOSERVER_RECEIVED_MEDIA | |
101 | +-----------------------------+ |
103 | ----------------------------------- |
105 +-----------------------------+ |
107 | TOSERVER_CLIENT_READY | |
108 +-----------------------------+ |
111 +-----------------------------+ (ban,kick) |
113 | TOCLIENT_MOVE_PLAYER | |
114 | TOCLIENT_PRIVILEGES | |
115 | TOCLIENT_INVENTORY_FORMSPEC | |
117 | TOCLIENT_INVENTORY | |
118 | TOCLIENT_HP (opt) | |
119 | TOCLIENT_BREATH | |
120 | TOCLIENT_DEATHSCREEN | |
121 +-----------------------------+ |
124 /-----------------\ |
125 | |------------------------------------------------------
127 | |----------------------------------
128 \-----------------/ timeout |
129 | +-----------------------------+
131 | | TOCLIENT_DISCONNECT |
132 | +-----------------------------+
135 +-----------------------------+ /-----------------\
137 | TOSERVER_DISCONNECT |------------------->| Disconnecting |
138 +-----------------------------+ | |
145 #define CI_ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
159 enum ClientStateEvent
164 CSE_SetDefinitionsSent,
170 Used for queueing and sorting block transfers in containers
172 Lower priority number means higher priority.
174 struct PrioritySortedBlockTransfer
176 PrioritySortedBlockTransfer(float a_priority, v3s16 a_pos, u16 a_peer_id)
178 priority = a_priority;
182 bool operator < (const PrioritySortedBlockTransfer &other) const
184 return priority < other.priority;
194 // peer_id=0 means this client has no associated peer
195 // NOTE: If client is made allowed to exist while peer doesn't,
196 // this has to be set to 0 when there is no peer.
197 // Also, the client must be moved to some other container.
199 // The serialization version to use with the client
200 u8 serialization_version;
202 u16 net_proto_version;
205 peer_id(PEER_ID_INEXISTENT),
206 serialization_version(SER_FMT_VER_INVALID),
207 net_proto_version(0),
208 m_time_from_building(9999),
209 m_pending_serialization_version(SER_FMT_VER_INVALID),
211 m_nearest_unsent_d(0),
212 m_nearest_unsent_reset_timer(0.0),
213 m_excess_gotblocks(0),
214 m_nothing_to_send_pause_timer(0.0),
219 m_full_version("unknown"),
220 m_supported_compressions(0),
221 m_connection_time(getTime(PRECISION_SECONDS))
229 Finds block that should be sent next to the client.
230 Environment should be locked when this is called.
231 dtime is used for resetting send radius at slow interval
233 void GetNextBlocks(ServerEnvironment *env, EmergeManager* emerge,
234 float dtime, std::vector<PrioritySortedBlockTransfer> &dest);
236 void GotBlock(v3s16 p);
238 void SentBlock(v3s16 p);
240 void SetBlockNotSent(v3s16 p);
241 void SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks);
244 * tell client about this block being modified right now.
245 * this information is required to requeue the block in case it's "on wire"
246 * while modification is processed by server
247 * @param p position of modified block
249 void ResendBlockIfOnWire(v3s16 p);
253 return m_blocks_sending.size();
256 // Increments timeouts and removes timed-out blocks from list
257 // NOTE: This doesn't fix the server-not-sending-block bug
258 // because it is related to emerging, not sending.
259 //void RunSendingTimeouts(float dtime, float timeout);
261 void PrintInfo(std::ostream &o)
263 o<<"RemoteClient "<<peer_id<<": "
264 <<"m_blocks_sent.size()="<<m_blocks_sent.size()
265 <<", m_blocks_sending.size()="<<m_blocks_sending.size()
266 <<", m_nearest_unsent_d="<<m_nearest_unsent_d
267 <<", m_excess_gotblocks="<<m_excess_gotblocks
269 m_excess_gotblocks = 0;
272 // Time from last placing or removing blocks
273 float m_time_from_building;
276 List of active objects that the client knows of.
279 std::set<u16> m_known_objects;
281 ClientState getState()
284 std::string getName()
287 void setName(std::string name)
290 /* update internal client state */
291 void notifyEvent(ClientStateEvent event);
293 /* set expected serialization version */
294 void setPendingSerializationVersion(u8 version)
295 { m_pending_serialization_version = version; }
297 void setSupportedCompressionModes(u8 byteFlag)
298 { m_supported_compressions = byteFlag; }
300 void confirmSerializationVersion()
301 { serialization_version = m_pending_serialization_version; }
307 /* set version information */
308 void setVersionInfo(u8 major, u8 minor, u8 patch, std::string full) {
309 m_version_major = major;
310 m_version_minor = minor;
311 m_version_patch = patch;
312 m_full_version = full;
315 /* read version information */
316 u8 getMajor() { return m_version_major; }
317 u8 getMinor() { return m_version_minor; }
318 u8 getPatch() { return m_version_patch; }
319 std::string getVersion() { return m_full_version; }
321 // Version is stored in here after INIT before INIT2
322 u8 m_pending_serialization_version;
324 /* current state of client */
328 Blocks that have been sent to client.
329 - These don't have to be sent again.
330 - A block is cleared from here when client says it has
331 deleted it from it's memory
333 Key is position, value is dummy.
334 No MapBlock* is stored here because the blocks can get deleted.
336 std::set<v3s16> m_blocks_sent;
337 s16 m_nearest_unsent_d;
339 float m_nearest_unsent_reset_timer;
342 Blocks that are currently on the line.
343 This is used for throttling the sending of blocks.
344 - The size of this list is limited to some value
345 Block is added when it is sent with BLOCKDATA.
346 Block is removed when GOTBLOCKS is received.
347 Value is time from sending. (not used at the moment)
349 std::map<v3s16, float> m_blocks_sending;
352 Count of excess GotBlocks().
353 There is an excess amount because the client sometimes
354 gets a block so late that the server sends it again,
355 and the client then sends two GOTBLOCKs.
356 This is resetted by PrintInfo()
358 u32 m_excess_gotblocks;
360 // CPU usage optimization
361 float m_nothing_to_send_pause_timer;
364 name of player using this client
375 std::string m_full_version;
377 u8 m_supported_compressions;
380 time this client was created
382 const u32 m_connection_time;
385 class ClientInterface {
390 ClientInterface(con::Connection* con);
394 void step(float dtime);
396 /* get list of active client id's */
397 std::vector<u16> getClientIDs(ClientState min_state=CS_Active);
399 /* get list of client player names */
400 std::vector<std::string> getPlayerNames();
402 /* send message to client */
403 void send(u16 peer_id, u8 channelnum, NetworkPacket* pkt, bool reliable);
405 /* send to all clients */
406 void sendToAll(u16 channelnum, NetworkPacket* pkt, bool reliable);
408 /* delete a client */
409 void DeleteClient(u16 peer_id);
412 void CreateClient(u16 peer_id);
414 /* get a client by peer_id */
415 RemoteClient* getClientNoEx(u16 peer_id, ClientState state_min=CS_Active);
417 /* get client by peer_id (make sure you have list lock before!*/
418 RemoteClient* lockedGetClientNoEx(u16 peer_id, ClientState state_min=CS_Active);
420 /* get state of client by id*/
421 ClientState getClientState(u16 peer_id);
423 /* set client playername */
424 void setPlayerName(u16 peer_id,std::string name);
426 /* get protocol version of client */
427 u16 getProtocolVersion(u16 peer_id);
429 /* set client version */
430 void setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch, std::string full);
432 /* event to update client state */
433 void event(u16 peer_id, ClientStateEvent event);
435 /* Set environment. Do not call this function if environment is already set */
436 void setEnv(ServerEnvironment *env)
438 assert(m_env == NULL); // pre-condition
442 static std::string state2Name(ClientState state);
445 //TODO find way to avoid this functions
447 { m_clients_mutex.Lock(); }
449 { m_clients_mutex.Unlock(); }
451 std::map<u16, RemoteClient*>& getClientList()
452 { return m_clients; }
455 /* update internal player list */
456 void UpdatePlayerList();
459 con::Connection* m_con;
460 JMutex m_clients_mutex;
461 // Connected clients (behind the con mutex)
462 std::map<u16, RemoteClient*> m_clients;
463 std::vector<std::string> m_clients_names; //for announcing masterserver
466 ServerEnvironment *m_env;
469 float m_print_info_timer;
471 static const char *statenames[];