]> git.lizzy.rs Git - dragonfireclient.git/blob - src/clientiface.h
Revert "Add daemon support for linux like operating systems"
[dragonfireclient.git] / src / clientiface.h
1 /*
2 Minetest
3 Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19 #ifndef _CLIENTIFACE_H_
20 #define _CLIENTIFACE_H_
21
22 #include "irr_v3d.h"                   // for irrlicht datatypes
23
24 #include "constants.h"
25 #include "serialization.h"             // for SER_FMT_VER_INVALID
26 #include "jthread/jmutex.h"
27
28 #include <list>
29 #include <vector>
30 #include <map>
31 #include <set>
32
33 class MapBlock;
34 class ServerEnvironment;
35 class EmergeManager;
36
37 /*
38  * State Transitions
39
40       Start
41   (peer connect)
42         |
43         v
44       /-----------------\
45       |                 |
46       |    Created      |
47       |                 |
48       \-----------------/
49                |
50                |
51 +-----------------------------+            invalid playername, password
52 |IN:                          |                    or denied by mod
53 | TOSERVER_INIT               |------------------------------
54 +-----------------------------+                             |
55                |                                            |
56                | Auth ok                                    |
57                |                                            |
58 +-----------------------------+                             |
59 |OUT:                         |                             |
60 | TOCLIENT_INIT               |                             |
61 +-----------------------------+                             |
62                |                                            |
63                v                                            |
64       /-----------------\                                   |
65       |                 |                                   |
66       |    InitSent     |                                   |
67       |                 |                                   |
68       \-----------------/                                   +------------------
69                |                                            |                 |
70 +-----------------------------+             +-----------------------------+   |
71 |IN:                          |             |OUT:                         |   |
72 | TOSERVER_INIT2              |             | TOCLIENT_ACCESS_DENIED      |   |
73 +-----------------------------+             +-----------------------------+   |
74                |                                            |                 |
75                v                                            v                 |
76       /-----------------\                           /-----------------\       |
77       |                 |                           |                 |       |
78       |    InitDone     |                           |     Denied      |       |
79       |                 |                           |                 |       |
80       \-----------------/                           \-----------------/       |
81                |                                                              |
82 +-----------------------------+                                               |
83 |OUT:                         |                                               |
84 | TOCLIENT_MOVEMENT           |                                               |
85 | TOCLIENT_ITEMDEF            |                                               |
86 | TOCLIENT_NODEDEF            |                                               |
87 | TOCLIENT_ANNOUNCE_MEDIA     |                                               |
88 | TOCLIENT_DETACHED_INVENTORY |                                               |
89 | TOCLIENT_TIME_OF_DAY        |                                               |
90 +-----------------------------+                                               |
91                |                                                              |
92                |                                                              |
93                |      -----------------------------------                     |
94                v      |                                 |                     |
95       /-----------------\                               v                     |
96       |                 |                   +-----------------------------+   |
97       | DefinitionsSent |                   |IN:                          |   |
98       |                 |                   | TOSERVER_REQUEST_MEDIA      |   |
99       \-----------------/                   | TOSERVER_RECEIVED_MEDIA     |   |
100                |                            +-----------------------------+   |
101                |      ^                                 |                     |
102                |      -----------------------------------                     |
103                |                                                              |
104 +-----------------------------+                                               |
105 |IN:                          |                                               |
106 | TOSERVER_CLIENT_READY       |                                               |
107 +-----------------------------+                                               |
108                |                                                    async     |
109                v                                                  mod action  |
110 +-----------------------------+                                   (ban,kick)  |
111 |OUT:                         |                                               |
112 | TOCLIENT_MOVE_PLAYER        |                                               |
113 | TOCLIENT_PRIVILEGES         |                                               |
114 | TOCLIENT_INVENTORY_FORMSPEC |                                               |
115 | UpdateCrafting              |                                               |
116 | TOCLIENT_INVENTORY          |                                               |
117 | TOCLIENT_HP (opt)           |                                               |
118 | TOCLIENT_BREATH             |                                               |
119 | TOCLIENT_DEATHSCREEN        |                                               |
120 +-----------------------------+                                               |
121               |                                                               |
122               v                                                               |
123       /-----------------\                                                     |
124       |                 |------------------------------------------------------
125       |     Active      |
126       |                 |----------------------------------
127       \-----------------/      timeout                    |
128                |                            +-----------------------------+
129                |                            |OUT:                         |
130                |                            | TOCLIENT_DISCONNECT         |
131                |                            +-----------------------------+
132                |                                          |
133                |                                          v
134 +-----------------------------+                    /-----------------\
135 |IN:                          |                    |                 |
136 | TOSERVER_DISCONNECT         |------------------->|  Disconnecting  |
137 +-----------------------------+                    |                 |
138                                                    \-----------------/
139 */
140 namespace con {
141         class Connection;
142 }
143
144 #define CI_ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
145
146 enum ClientState
147 {
148         Invalid,
149         Disconnecting,
150         Denied,
151         Created,
152         InitSent,
153         InitDone,
154         DefinitionsSent,
155         Active
156 };
157
158 static const char* statenames[] = {
159         "Invalid",
160         "Disconnecting",
161         "Denied",
162         "Created",
163         "InitSent",
164         "InitDone",
165         "DefinitionsSent",
166         "Active"
167 };
168
169 enum ClientStateEvent
170 {
171         Init,
172         GotInit2,
173         SetDenied,
174         SetDefinitionsSent,
175         SetClientReady,
176         Disconnect
177 };
178
179 /*
180         Used for queueing and sorting block transfers in containers
181
182         Lower priority number means higher priority.
183 */
184 struct PrioritySortedBlockTransfer
185 {
186         PrioritySortedBlockTransfer(float a_priority, v3s16 a_pos, u16 a_peer_id)
187         {
188                 priority = a_priority;
189                 pos = a_pos;
190                 peer_id = a_peer_id;
191         }
192         bool operator < (const PrioritySortedBlockTransfer &other) const
193         {
194                 return priority < other.priority;
195         }
196         float priority;
197         v3s16 pos;
198         u16 peer_id;
199 };
200
201 class RemoteClient
202 {
203 public:
204         // peer_id=0 means this client has no associated peer
205         // NOTE: If client is made allowed to exist while peer doesn't,
206         //       this has to be set to 0 when there is no peer.
207         //       Also, the client must be moved to some other container.
208         u16 peer_id;
209         // The serialization version to use with the client
210         u8 serialization_version;
211         //
212         u16 net_proto_version;
213
214         RemoteClient():
215                 peer_id(PEER_ID_INEXISTENT),
216                 serialization_version(SER_FMT_VER_INVALID),
217                 net_proto_version(0),
218                 m_time_from_building(9999),
219                 m_pending_serialization_version(SER_FMT_VER_INVALID),
220                 m_state(Created),
221                 m_nearest_unsent_d(0),
222                 m_nearest_unsent_reset_timer(0.0),
223                 m_excess_gotblocks(0),
224                 m_nothing_to_send_pause_timer(0.0),
225                 m_name(""),
226                 m_version_major(0),
227                 m_version_minor(0),
228                 m_version_patch(0),
229                 m_full_version("unknown"),
230                 m_connection_time(getTime(PRECISION_SECONDS))
231         {
232         }
233         ~RemoteClient()
234         {
235         }
236
237         /*
238                 Finds block that should be sent next to the client.
239                 Environment should be locked when this is called.
240                 dtime is used for resetting send radius at slow interval
241         */
242         void GetNextBlocks(ServerEnvironment *env, EmergeManager* emerge,
243                         float dtime, std::vector<PrioritySortedBlockTransfer> &dest);
244
245         void GotBlock(v3s16 p);
246
247         void SentBlock(v3s16 p);
248
249         void SetBlockNotSent(v3s16 p);
250         void SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks);
251
252         s32 SendingCount()
253         {
254                 return m_blocks_sending.size();
255         }
256
257         // Increments timeouts and removes timed-out blocks from list
258         // NOTE: This doesn't fix the server-not-sending-block bug
259         //       because it is related to emerging, not sending.
260         //void RunSendingTimeouts(float dtime, float timeout);
261
262         void PrintInfo(std::ostream &o)
263         {
264                 o<<"RemoteClient "<<peer_id<<": "
265                                 <<"m_blocks_sent.size()="<<m_blocks_sent.size()
266                                 <<", m_blocks_sending.size()="<<m_blocks_sending.size()
267                                 <<", m_nearest_unsent_d="<<m_nearest_unsent_d
268                                 <<", m_excess_gotblocks="<<m_excess_gotblocks
269                                 <<std::endl;
270                 m_excess_gotblocks = 0;
271         }
272
273         // Time from last placing or removing blocks
274         float m_time_from_building;
275
276         /*
277                 List of active objects that the client knows of.
278                 Value is dummy.
279         */
280         std::set<u16> m_known_objects;
281
282         ClientState getState()
283                 { return m_state; }
284
285         std::string getName()
286                 { return m_name; }
287
288         void setName(std::string name)
289                 { m_name = name; }
290
291         /* update internal client state */
292         void notifyEvent(ClientStateEvent event);
293
294         /* set expected serialization version */
295         void setPendingSerializationVersion(u8 version)
296                 { m_pending_serialization_version = version; }
297
298         void confirmSerializationVersion()
299                 { serialization_version = m_pending_serialization_version; }
300
301         /* get uptime */
302         u32 uptime();
303
304
305         /* set version information */
306         void setVersionInfo(u8 major, u8 minor, u8 patch, std::string full) {
307                 m_version_major = major;
308                 m_version_minor = minor;
309                 m_version_patch = patch;
310                 m_full_version = full;
311         }
312
313         /* read version information */
314         u8 getMajor() { return m_version_major; }
315         u8 getMinor() { return m_version_minor; }
316         u8 getPatch() { return m_version_patch; }
317         std::string getVersion() { return m_full_version; }
318 private:
319         // Version is stored in here after INIT before INIT2
320         u8 m_pending_serialization_version;
321
322         /* current state of client */
323         ClientState m_state;
324
325         /*
326                 Blocks that have been sent to client.
327                 - These don't have to be sent again.
328                 - A block is cleared from here when client says it has
329                   deleted it from it's memory
330
331                 Key is position, value is dummy.
332                 No MapBlock* is stored here because the blocks can get deleted.
333         */
334         std::set<v3s16> m_blocks_sent;
335         s16 m_nearest_unsent_d;
336         v3s16 m_last_center;
337         float m_nearest_unsent_reset_timer;
338
339         /*
340                 Blocks that are currently on the line.
341                 This is used for throttling the sending of blocks.
342                 - The size of this list is limited to some value
343                 Block is added when it is sent with BLOCKDATA.
344                 Block is removed when GOTBLOCKS is received.
345                 Value is time from sending. (not used at the moment)
346         */
347         std::map<v3s16, float> m_blocks_sending;
348
349         /*
350                 Count of excess GotBlocks().
351                 There is an excess amount because the client sometimes
352                 gets a block so late that the server sends it again,
353                 and the client then sends two GOTBLOCKs.
354                 This is resetted by PrintInfo()
355         */
356         u32 m_excess_gotblocks;
357
358         // CPU usage optimization
359         float m_nothing_to_send_pause_timer;
360
361         /*
362                 name of player using this client
363         */
364         std::string m_name;
365
366         /*
367                 client information
368          */
369         u8 m_version_major;
370         u8 m_version_minor;
371         u8 m_version_patch;
372
373         std::string m_full_version;
374
375         /*
376                 time this client was created
377          */
378         const u32 m_connection_time;
379 };
380
381 class ClientInterface {
382 public:
383
384         friend class Server;
385
386         ClientInterface(con::Connection* con);
387         ~ClientInterface();
388
389         /* run sync step */
390         void step(float dtime);
391
392         /* get list of active client id's */
393         std::list<u16> getClientIDs(ClientState min_state=Active);
394
395         /* get list of client player names */
396         std::vector<std::string> getPlayerNames();
397
398         /* send message to client */
399         void send(u16 peer_id, u8 channelnum, SharedBuffer<u8> data, bool reliable);
400
401         /* send to all clients */
402         void sendToAll(u16 channelnum, SharedBuffer<u8> data, bool reliable);
403
404         /* delete a client */
405         void DeleteClient(u16 peer_id);
406
407         /* create client */
408         void CreateClient(u16 peer_id);
409
410         /* get a client by peer_id */
411         RemoteClient* getClientNoEx(u16 peer_id,  ClientState state_min=Active);
412
413         /* get client by peer_id (make sure you have list lock before!*/
414         RemoteClient* lockedGetClientNoEx(u16 peer_id,  ClientState state_min=Active);
415
416         /* get state of client by id*/
417         ClientState getClientState(u16 peer_id);
418
419         /* set client playername */
420         void setPlayerName(u16 peer_id,std::string name);
421
422         /* get protocol version of client */
423         u16 getProtocolVersion(u16 peer_id);
424
425         /* set client version */
426         void setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch, std::string full);
427
428         /* event to update client state */
429         void event(u16 peer_id, ClientStateEvent event);
430
431         /* set environment */
432         void setEnv(ServerEnvironment* env)
433         { assert(m_env == 0); m_env = env; }
434
435         static std::string state2Name(ClientState state) {
436                 assert((int) state < CI_ARRAYSIZE(statenames));
437                 return statenames[state];
438         }
439
440 protected:
441         //TODO find way to avoid this functions
442         void Lock()
443                 { m_clients_mutex.Lock(); }
444         void Unlock()
445                 { m_clients_mutex.Unlock(); }
446
447         std::map<u16, RemoteClient*>& getClientList()
448                 { return m_clients; }
449
450 private:
451         /* update internal player list */
452         void UpdatePlayerList();
453
454         // Connection
455         con::Connection* m_con;
456         JMutex m_clients_mutex;
457         // Connected clients (behind the con mutex)
458         std::map<u16, RemoteClient*> m_clients;
459         std::vector<std::string> m_clients_names; //for announcing masterserver
460
461         // Environment
462         ServerEnvironment *m_env;
463         JMutex m_env_mutex;
464
465         float m_print_info_timer;
466 };
467
468 #endif