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 class Client : public con::PeerHandler
108 NOTE: Every public method should be thread-safe
111 IrrlichtDevice *device,
112 const char *playername,
114 float &viewing_range_nodes,
115 bool &viewing_range_all
120 The name of the local player should already be set when
121 calling this, as it is sent in the initialization.
123 void connect(Address address);
126 m_con.Connected() == true
127 AND m_server_ser_ver != SER_FMT_VER_INVALID
128 throws con::PeerNotFoundException if connection has been deleted,
131 bool connectedAndInitialized();
133 Stuff that references the environment is valid only as
134 long as this is not called. (eg. Players)
135 If this throws a PeerNotFoundException, the connection has
138 void step(float dtime);
140 // Called from updater thread
144 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
145 // Returns true if something was received
146 bool AsyncProcessPacket();
147 bool AsyncProcessData();
148 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
151 bool isFetchingBlocks();
153 // Pops out a packet from the packet queue
154 IncomingPacket getPacket();
156 void groundAction(u8 action, v3s16 nodepos_undersurface,
157 v3s16 nodepos_oversurface, u16 item);
158 void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
160 void sendSignText(v3s16 blockpos, s16 id, std::string text);
161 void sendInventoryAction(InventoryAction *a);
162 void sendChatMessage(const std::wstring &message);
165 void removeNode(v3s16 p);
167 void addNode(v3s16 p, MapNode n);
169 void updateCamera(v3f pos, v3f dir);
171 // Returns InvalidPositionException if not found
172 MapNode getNode(v3s16 p);
173 // Returns InvalidPositionException if not found
174 //void setNode(v3s16 p, MapNode n);
176 // Returns InvalidPositionException if not found
177 //f32 getGroundHeight(v2s16 p);
179 v3f getPlayerPosition();
181 void setPlayerControl(PlayerControl &control);
183 // Returns true if the inventory of the local player has been
184 // updated from the server. If it is true, it is set to false.
185 bool getLocalInventoryUpdated();
186 // Copies the inventory of the local player to parameter
187 void getLocalInventory(Inventory &dst);
189 // Gets closest object pointed by the shootline
190 // Returns NULL if not found
191 MapBlockObject * getSelectedObject(
193 v3f from_pos_f_on_map,
194 core::line3d<f32> shootline_on_map
197 // Prints a line or two of info
198 void printDebugInfo(std::ostream &os);
200 //s32 getDayNightIndex();
201 u32 getDayNightRatio();
203 //void updateSomeExpiredMeshes();
205 void setTempMod(v3s16 p, NodeMod mod)
207 JMutexAutoLock envlock(m_env_mutex);
208 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
209 v3s16 blockpos = ((ClientMap&)m_env.getMap()).setTempMod(p, mod);
210 m_env.getMap().updateMeshes(blockpos, m_env.getDayNightRatio());
212 void clearTempMod(v3s16 p)
214 JMutexAutoLock envlock(m_env_mutex);
215 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
216 v3s16 blockpos = ((ClientMap&)m_env.getMap()).clearTempMod(p);
217 m_env.getMap().updateMeshes(blockpos, m_env.getDayNightRatio());
222 JMutexAutoLock lock(m_con_mutex);
223 con::Peer *peer = m_con.GetPeerNoEx(PEER_ID_SERVER);
226 return peer->avg_rtt;
229 bool getChatMessage(std::wstring &message)
231 if(m_chat_queue.size() == 0)
233 message = m_chat_queue.pop_front();
237 void addChatMessage(const std::wstring &message)
239 JMutexAutoLock envlock(m_env_mutex);
240 LocalPlayer *player = m_env.getLocalPlayer();
241 assert(player != NULL);
242 std::wstring name = narrow_to_wide(player->getName());
243 m_chat_queue.push_back(
244 (std::wstring)L"<"+name+L"> "+message);
249 // Virtual methods from con::PeerHandler
250 void peerAdded(con::Peer *peer);
251 void deletingPeer(con::Peer *peer, bool timeout);
256 void sendPlayerPos();
257 // This sends the player's current name etc to the server
258 void sendPlayerInfo();
260 float m_packetcounter_timer;
261 float m_delete_unused_sectors_timer;
262 float m_connection_reinit_timer;
263 float m_avg_rtt_timer;
264 float m_playerpos_send_timer;
266 ClientUpdateThread m_thread;
268 // NOTE: If connection and environment are both to be locked,
269 // environment shall be locked first.
274 con::Connection m_con;
277 /*core::map<v3s16, float> m_fetchblock_history;
278 JMutex m_fetchblock_mutex;*/
280 core::list<IncomingPacket> m_incoming_queue;
281 JMutex m_incoming_queue_mutex;
283 IrrlichtDevice *m_device;
286 v3f camera_direction;
288 // Server serialization version
292 JMutex m_step_dtime_mutex;
294 // This is behind m_env_mutex.
295 bool m_inventory_updated;
297 core::map<v3s16, bool> m_active_blocks;
299 PacketCounter m_packetcounter;
301 // Received from the server. 0-23999
302 MutexedVariable<u32> m_time_of_day;
304 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
306 //u32 m_daynight_ratio;
308 Queue<std::wstring> m_chat_queue;
313 #endif // !CLIENT_HEADER