4 #include "connection.h"
5 #include "environment.h"
6 #include "common_irrlicht.h"
10 class ClientNotReadyException : public BaseException
13 ClientNotReadyException(const char *s):
20 class ClientUpdateThread : public JThread
29 ClientUpdateThread(Client *client) : JThread(), run(true), m_client(client)
39 bool run_cached = run;
43 void setRun(bool a_run)
59 IncomingPacket(const IncomingPacket &a)
62 m_datalen = a.m_datalen;
63 m_refcount = a.m_refcount;
64 if(m_refcount != NULL)
67 IncomingPacket(u8 *data, u32 datalen)
69 m_data = new u8[datalen];
70 memcpy(m_data, data, datalen);
72 m_refcount = new s32(1);
76 if(m_refcount != NULL){
77 assert(*m_refcount > 0);
85 /*IncomingPacket & operator=(IncomingPacket a)
88 m_datalen = a.m_datalen;
89 m_refcount = a.m_refcount;
101 LazyMeshUpdater(Environment *env)
108 TODO: This could be optimized. It will currently
109 double-update some blocks.
111 for(core::map<v3s16, bool>::Iterator
112 i = m_blocks.getIterator();
113 i.atEnd() == false; i++)
115 v3s16 p = i.getNode()->getKey();
116 m_env->getMap().updateMeshes(p);
122 m_blocks.insert(p, true);
126 core::map<v3s16, bool> m_blocks;
129 class Client : public con::PeerHandler
133 NOTE: Every public method should be thread-safe
135 Client(IrrlichtDevice *device, video::SMaterial *materials,
136 float delete_unused_sectors_timeout,
137 const char *playername);
140 The name of the local player should already be set when
141 calling this, as it is sent in the initialization.
143 void connect(Address address);
146 m_con.Connected() == true
147 AND m_server_ser_ver != SER_FMT_VER_INVALID
148 throws con::PeerNotFoundException if connection has been deleted,
151 bool connectedAndInitialized();
153 Stuff that references the environment is valid only as
154 long as this is not called. (eg. Players)
155 If this throws a PeerNotFoundException, the connection has
158 void step(float dtime);
160 // Called from updater thread
164 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
165 // Returns true if something was received
166 bool AsyncProcessPacket(LazyMeshUpdater &mesh_updater);
167 bool AsyncProcessData();
168 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
171 bool isFetchingBlocks();
173 // Pops out a packet from the packet queue
174 IncomingPacket getPacket();
176 /*void removeNode(v3s16 nodepos);
177 void addNodeFromInventory(v3s16 nodepos, u16 i);*/
178 void clickGround(u8 button, v3s16 nodepos_undersurface,
179 v3s16 nodepos_oversurface, u16 item);
180 void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
181 void release(u8 button);
183 void sendSignText(v3s16 blockpos, s16 id, std::string text);
185 void updateCamera(v3f pos, v3f dir);
187 // Returns InvalidPositionException if not found
188 MapNode getNode(v3s16 p);
189 // Returns InvalidPositionException if not found
190 //f32 getGroundHeight(v2s16 p);
191 // Returns InvalidPositionException if not found
192 bool isNodeUnderground(v3s16 p);
194 // Note: The players should not be exposed outside
195 // Return value is valid until client is destroyed
196 //Player * getLocalPlayer();
197 // Return value is valid until step()
198 //core::list<Player*> getPlayers();
199 v3f getPlayerPosition();
201 void setPlayerControl(PlayerControl &control);
203 // Returns true if the inventory of the local player has been
204 // updated from the server. If it is true, it is set to false.
205 bool getLocalInventoryUpdated();
206 // Copies the inventory of the local player to parameter
207 void getLocalInventory(Inventory &dst);
208 // TODO: Functions for sending inventory editing commands to
211 // Gets closest object pointed by the shootline
212 // Returns NULL if not found
213 MapBlockObject * getSelectedObject(
215 v3f from_pos_f_on_map,
216 core::line3d<f32> shootline_on_map
219 // Prints a line or two of info
220 void printDebugInfo(std::ostream &os);
224 // Virtual methods from con::PeerHandler
225 void peerAdded(con::Peer *peer);
226 void deletingPeer(con::Peer *peer, bool timeout);
231 void sendPlayerPos();
232 // This sends the player's current name etc to the server
233 void sendPlayerInfo();
235 ClientUpdateThread m_thread;
237 // NOTE: If connection and environment are both to be locked,
238 // environment shall be locked first.
243 con::Connection m_con;
246 /*core::map<v3s16, float> m_fetchblock_history;
247 JMutex m_fetchblock_mutex;*/
249 core::list<IncomingPacket> m_incoming_queue;
250 JMutex m_incoming_queue_mutex;
252 IrrlichtDevice *m_device;
255 v3f camera_direction;
257 // Server serialization version
261 JMutex m_step_dtime_mutex;
263 float m_delete_unused_sectors_timeout;
265 // This is behind m_env_mutex.
266 bool m_inventory_updated;
268 core::map<v3s16, bool> m_active_blocks;
270 PacketCounter m_packetcounter;