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.
23 #include "connection.h"
24 #include "environment.h"
25 #include "common_irrlicht.h"
29 class ClientNotReadyException : public BaseException
32 ClientNotReadyException(const char *s):
39 class ClientUpdateThread : public JThread
48 ClientUpdateThread(Client *client) : JThread(), run(true), m_client(client)
58 bool run_cached = run;
62 void setRun(bool a_run)
78 IncomingPacket(const IncomingPacket &a)
81 m_datalen = a.m_datalen;
82 m_refcount = a.m_refcount;
83 if(m_refcount != NULL)
86 IncomingPacket(u8 *data, u32 datalen)
88 m_data = new u8[datalen];
89 memcpy(m_data, data, datalen);
91 m_refcount = new s32(1);
95 if(m_refcount != NULL){
96 assert(*m_refcount > 0);
104 /*IncomingPacket & operator=(IncomingPacket a)
107 m_datalen = a.m_datalen;
108 m_refcount = a.m_refcount;
117 // TODO: Remove this. It is not used as supposed.
118 class LazyMeshUpdater
121 LazyMeshUpdater(Environment *env)
128 TODO: This could be optimized. It will currently
129 double-update some blocks.
131 for(core::map<v3s16, bool>::Iterator
132 i = m_blocks.getIterator();
133 i.atEnd() == false; i++)
135 v3s16 p = i.getNode()->getKey();
136 m_env->getMap().updateMeshes(p);
142 m_blocks.insert(p, true);
146 core::map<v3s16, bool> m_blocks;
149 class Client : public con::PeerHandler
153 NOTE: Every public method should be thread-safe
155 Client(IrrlichtDevice *device, const char *playername);
158 The name of the local player should already be set when
159 calling this, as it is sent in the initialization.
161 void connect(Address address);
164 m_con.Connected() == true
165 AND m_server_ser_ver != SER_FMT_VER_INVALID
166 throws con::PeerNotFoundException if connection has been deleted,
169 bool connectedAndInitialized();
171 Stuff that references the environment is valid only as
172 long as this is not called. (eg. Players)
173 If this throws a PeerNotFoundException, the connection has
176 void step(float dtime);
178 // Called from updater thread
182 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
183 // Returns true if something was received
184 bool AsyncProcessPacket(LazyMeshUpdater &mesh_updater);
185 bool AsyncProcessData();
186 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
189 bool isFetchingBlocks();
191 // Pops out a packet from the packet queue
192 IncomingPacket getPacket();
194 /*void removeNode(v3s16 nodepos);
195 void addNodeFromInventory(v3s16 nodepos, u16 i);*/
196 void pressGround(u8 button, v3s16 nodepos_undersurface,
197 v3s16 nodepos_oversurface, u16 item);
198 void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
201 void sendSignText(v3s16 blockpos, s16 id, std::string text);
203 void updateCamera(v3f pos, v3f dir);
205 // Returns InvalidPositionException if not found
206 MapNode getNode(v3s16 p);
207 // Returns InvalidPositionException if not found
208 //f32 getGroundHeight(v2s16 p);
209 // Returns InvalidPositionException if not found
210 bool isNodeUnderground(v3s16 p);
212 // Note: The players should not be exposed outside
213 // Return value is valid until client is destroyed
214 //Player * getLocalPlayer();
215 // Return value is valid until step()
216 //core::list<Player*> getPlayers();
217 v3f getPlayerPosition();
219 void setPlayerControl(PlayerControl &control);
221 // Returns true if the inventory of the local player has been
222 // updated from the server. If it is true, it is set to false.
223 bool getLocalInventoryUpdated();
224 // Copies the inventory of the local player to parameter
225 void getLocalInventory(Inventory &dst);
226 // TODO: Functions for sending inventory editing commands to
229 // Gets closest object pointed by the shootline
230 // Returns NULL if not found
231 MapBlockObject * getSelectedObject(
233 v3f from_pos_f_on_map,
234 core::line3d<f32> shootline_on_map
237 // Prints a line or two of info
238 void printDebugInfo(std::ostream &os);
242 // Virtual methods from con::PeerHandler
243 void peerAdded(con::Peer *peer);
244 void deletingPeer(con::Peer *peer, bool timeout);
249 void sendPlayerPos();
250 // This sends the player's current name etc to the server
251 void sendPlayerInfo();
253 ClientUpdateThread m_thread;
255 // NOTE: If connection and environment are both to be locked,
256 // environment shall be locked first.
261 con::Connection m_con;
264 /*core::map<v3s16, float> m_fetchblock_history;
265 JMutex m_fetchblock_mutex;*/
267 core::list<IncomingPacket> m_incoming_queue;
268 JMutex m_incoming_queue_mutex;
270 IrrlichtDevice *m_device;
273 v3f camera_direction;
275 // Server serialization version
279 JMutex m_step_dtime_mutex;
281 // This is behind m_env_mutex.
282 bool m_inventory_updated;
284 core::map<v3s16, bool> m_active_blocks;
286 PacketCounter m_packetcounter;