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.
25 #include "connection.h"
26 #include "environment.h"
27 #include "common_irrlicht.h"
31 class ClientNotReadyException : public BaseException
34 ClientNotReadyException(const char *s):
41 class ClientUpdateThread : public SimpleThread
47 ClientUpdateThread(Client *client):
64 IncomingPacket(const IncomingPacket &a)
67 m_datalen = a.m_datalen;
68 m_refcount = a.m_refcount;
69 if(m_refcount != NULL)
72 IncomingPacket(u8 *data, u32 datalen)
74 m_data = new u8[datalen];
75 memcpy(m_data, data, datalen);
77 m_refcount = new s32(1);
81 if(m_refcount != NULL){
82 assert(*m_refcount > 0);
91 /*IncomingPacket & operator=(IncomingPacket a)
94 m_datalen = a.m_datalen;
95 m_refcount = a.m_refcount;
104 // TODO: Remove this. It is not used as supposed.
105 class LazyMeshUpdater
108 LazyMeshUpdater(Environment *env)
115 TODO: This could be optimized. It will currently
116 double-update some blocks.
118 for(core::map<v3s16, bool>::Iterator
119 i = m_blocks.getIterator();
120 i.atEnd() == false; i++)
122 v3s16 p = i.getNode()->getKey();
123 m_env->updateMeshes(p);
129 m_blocks.insert(p, true);
133 core::map<v3s16, bool> m_blocks;
136 class Client : public con::PeerHandler
140 NOTE: Every public method should be thread-safe
143 IrrlichtDevice *device,
144 const char *playername,
146 s16 &viewing_range_nodes,
147 bool &viewing_range_all
152 The name of the local player should already be set when
153 calling this, as it is sent in the initialization.
155 void connect(Address address);
158 m_con.Connected() == true
159 AND m_server_ser_ver != SER_FMT_VER_INVALID
160 throws con::PeerNotFoundException if connection has been deleted,
163 bool connectedAndInitialized();
165 Stuff that references the environment is valid only as
166 long as this is not called. (eg. Players)
167 If this throws a PeerNotFoundException, the connection has
170 void step(float dtime);
172 // Called from updater thread
176 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
177 // Returns true if something was received
178 bool AsyncProcessPacket(LazyMeshUpdater &mesh_updater);
179 bool AsyncProcessData();
180 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
183 bool isFetchingBlocks();
185 // Pops out a packet from the packet queue
186 IncomingPacket getPacket();
188 void groundAction(u8 action, v3s16 nodepos_undersurface,
189 v3s16 nodepos_oversurface, u16 item);
190 void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
192 void sendSignText(v3s16 blockpos, s16 id, std::string text);
193 void sendInventoryAction(InventoryAction *a);
195 void updateCamera(v3f pos, v3f dir);
197 // Returns InvalidPositionException if not found
198 MapNode getNode(v3s16 p);
199 // Returns InvalidPositionException if not found
200 //void setNode(v3s16 p, MapNode n);
202 // Returns InvalidPositionException if not found
203 //f32 getGroundHeight(v2s16 p);
204 // Returns InvalidPositionException if not found
205 //bool isNodeUnderground(v3s16 p);
207 // Note: The players should not be exposed outside
208 // Return value is valid until client is destroyed
209 //Player * getLocalPlayer();
210 // Return value is valid until step()
211 //core::list<Player*> getPlayers();
212 v3f getPlayerPosition();
214 void setPlayerControl(PlayerControl &control);
216 // Returns true if the inventory of the local player has been
217 // updated from the server. If it is true, it is set to false.
218 bool getLocalInventoryUpdated();
219 // Copies the inventory of the local player to parameter
220 void getLocalInventory(Inventory &dst);
221 // TODO: Functions for sending inventory editing commands to
224 // Gets closest object pointed by the shootline
225 // Returns NULL if not found
226 MapBlockObject * getSelectedObject(
228 v3f from_pos_f_on_map,
229 core::line3d<f32> shootline_on_map
232 // Prints a line or two of info
233 void printDebugInfo(std::ostream &os);
235 //s32 getDayNightIndex();
236 u32 getDayNightRatio();
238 //void updateSomeExpiredMeshes();
240 void setTempMod(v3s16 p, NodeMod mod)
242 JMutexAutoLock envlock(m_env_mutex);
243 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
244 v3s16 blockpos = ((ClientMap&)m_env.getMap()).setTempMod(p, mod);
245 m_env.getMap().updateMeshes(blockpos, m_env.getDayNightRatio());
247 void clearTempMod(v3s16 p)
249 JMutexAutoLock envlock(m_env_mutex);
250 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
251 v3s16 blockpos = ((ClientMap&)m_env.getMap()).clearTempMod(p);
252 m_env.getMap().updateMeshes(blockpos, m_env.getDayNightRatio());
257 JMutexAutoLock lock(m_con_mutex);
258 con::Peer *peer = m_con.GetPeerNoEx(PEER_ID_SERVER);
261 return peer->avg_rtt;
266 // Virtual methods from con::PeerHandler
267 void peerAdded(con::Peer *peer);
268 void deletingPeer(con::Peer *peer, bool timeout);
273 void sendPlayerPos();
274 // This sends the player's current name etc to the server
275 void sendPlayerInfo();
277 float m_packetcounter_timer;
278 float m_delete_unused_sectors_timer;
279 float m_connection_reinit_timer;
280 float m_avg_rtt_timer;
281 float m_playerpos_send_timer;
283 ClientUpdateThread m_thread;
285 // NOTE: If connection and environment are both to be locked,
286 // environment shall be locked first.
291 con::Connection m_con;
294 /*core::map<v3s16, float> m_fetchblock_history;
295 JMutex m_fetchblock_mutex;*/
297 core::list<IncomingPacket> m_incoming_queue;
298 JMutex m_incoming_queue_mutex;
300 IrrlichtDevice *m_device;
303 v3f camera_direction;
305 // Server serialization version
309 JMutex m_step_dtime_mutex;
311 // This is behind m_env_mutex.
312 bool m_inventory_updated;
314 core::map<v3s16, bool> m_active_blocks;
316 PacketCounter m_packetcounter;
318 // Received from the server. 0-23999
319 MutexedVariable<u32> m_time_of_day;
321 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
323 //u32 m_daynight_ratio;
328 #endif // !CLIENT_HEADER