Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
netclient.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */
16 
17 /*
18  * NetClient - Network Client Interface - written by Stephane Vaxelaire <svax@free.fr>
19  */
20 
21 #ifndef __NetClient_H
22 #define __NetClient_H
23 
24 #include "config.h" //for NETCOMM & ZLIB
25 
26 #include <string>
27 #include <vector>
28 
29 #include <boost/shared_ptr.hpp>
30 #include "savegame.h"
31 #include "networking/const.h"
37 
38 #include "cmd/container.h" //for UnitContainer
39 #include "gfx/quaternion.h" //for Transformation
40 
41 #include "networking/clientptr.h"
42 
43 class Packet;
44 class Unit;
45 class Client;
46 class ClientState;
47 class NetUI;
49 class Prediction;
50 
51 namespace VsnetDownload
52 {
53 namespace Client
54 {
55 class Manager;
56 };
57 };
58 
59 using std::vector;
60 using std::string;
61 extern vector< ObjSerial >localSerials;
62 extern bool isLocalSerial( ObjSerial sernum );
63 
65 
66 class NetClient
67 {
68  class ClientsMap
69  {
70  ClientMap _map;
71 
72 public:
73  ClientPtr insert( int x, Client *c );
74  ClientPtr get( int x );
75  bool remove( int x );
76  };
77 
78  UnitContainer game_unit; //Unit struct from the game corresponding to that client
79 
80  string _serverip; //used during login
81  unsigned short _serverport; //used during login
82  SOCKETALT *clt_tcp_sock; //Comm. socket...memory allocated in class
83  SOCKETALT *clt_udp_sock; //Comm. socket...memory allocated in class
84  SOCKETALT *lossy_socket; //Usually points to the UDP socket, unless behind firewall... do not free this one
85  VsnetHTTPSocket *acct_sock; //Connection socket for account server
86  SocketSet _sock_set; //Encapsulates select()
87 //SaveGame save;
88  ObjSerial netversion;
89 public:
90  ObjSerial serial; //Serial # of client
91 private:
92  int nbclients; //Number of clients in the zone
93  int zone; //Zone id in universe
94  char keeprun; //Bool to test client stop
95  string callsign; //Callsign of the networked player
96  string password; //Callsign of the networked player
97  vector< string >ship_select_list;
98  int selected_ship;
99 
100  vector< string >lastsave; //If [0] is empty, [1] will contain a login error message.
101  string error_message; //Errors when logging in
102 
103  ClientsMap Clients; //Clients in the same zone
104 
105 //This unit array has to be changed into a map too !!
106 //Unit * Units[MAXOBJECTS]; // Server controlled units in the same zone
107 //a vector because always accessed by their IDs
108 
109  NetworkCommunication *NetComm;
110 
111  int enabled; //Bool to say network is enabled
112 //Time used for refresh - not sure still used
113 //int old_time;
114  double cur_time;
115 //unsigned int old_timestamp; // Previous timestamp
116  unsigned int latest_timestamp; //Last received
117  double deltatime; //Semi-ping value between this client and server in ms
118 //double elapsed_since_packet; // Time elapsed since we we received the last SNAPSHOT or PING packet
119  bool jumpok;
120  bool ingame;
121  float current_freq;
122  float selected_freq;
123 
124  boost::shared_ptr< VsnetDownload::Client::Manager >_downloadManagerClient;
125  static const char *_downloadSearchPaths[];
126 
127  void versionBuf( NetBuffer &buf ) const; //Sets the netbuffer to this version.
128  void createChar();
129  int recvMsg( Packet *outpacket, timeval *timeout = NULL );
130  void disconnect();
131  int checkAcctMsg();
132 
133  void receiveLocations( const Packet *packet );
134  void receiveUnitDamage( NetBuffer &netbuf, Unit *un );
135  void receivePositions( unsigned int numUnits, unsigned int timestamp, NetBuffer &netbuf, double deltat );
136  Unit * enterClient( NetBuffer &netbuf, ObjSerial ser );
137  void removeClient( const Packet *packet );
138 
139 public: NetClient();
140  void Reinitialize(); //sets all values back to defaults
141  ~NetClient();
142 
143  int connectLoad( string user, string pass, string &error ); //First step of loading... returns ships.
144  vector< string > * loginSavedGame( int ship = 0 ); //Second half of loading process... returns savegame
145  void startGame(); //Third half of loading process.
146 
147 /**** netclient_login.cpp stuff ****/
148  void SetConfigServerAddress( string &host, unsigned short &port ); //reads config vars, sets address/port and returns them.
149 
150  void GetCurrentServerAddress( string &host, unsigned short &port );
151  void SetCurrentServerAddress( string host, unsigned short port );
152 
153 //Returns the list of valid ships to use upon logging in.
154  const vector< string >& shipSelections()
155  {
156  return ship_select_list;
157  }
158  bool selectShip( unsigned int ship );
159 
160  int authenticate();
161  int loginAuth( string str_callsign, string str_passwd, string &error ); //Loops until receiving login response
162  int loginLoop( string &error );
163  vector< string >& loginAcctLoop( string str_callsign, string str_passwd );
164  void loginAccept( Packet &p1 );
165  void loginChooseShip( Packet &p1 );
166  SOCKETALT init( const char *addr, unsigned short port, string &error );
167  VsnetHTTPSocket * init_acct( const std::string &addr );
168  void synchronizeTime( SOCKETALT* ); //Sends time packets back and forth to find the actual double time on the server.
169 
170 //start() should not used... Use init() instead.
171 //void start( char * addr, unsigned short port);
172 //void checkKey();
173 
174  void setCallsign( char *calls )
175  {
176  this->callsign = string( calls );
177  }
178  void setCallsign( string calls )
179  {
180  this->callsign = calls;
181  }
182  string getCallsign()
183  {
184  return this->callsign;
185  }
186  void setUnit( Unit *un )
187  {
188  game_unit.SetUnit( un );
189  }
191  {
192  return game_unit.GetUnit();
193  }
194 
195 /********************* Network stuff **********************/
196 //Get the lag time between us and the server
197  unsigned int getLag()
198  {
199  return (unsigned int) (deltatime*1000);
200  }
201 //Check if it is time to send our update
202  int isTime();
203 //Warn the server we are leaving the game
204  SOCKETALT * logout( bool leaveUDP );
205  void Respawn( ObjSerial clientname );
206  void saveRequest();
207  void dieRequest();
208 
209 //Check if there are info incoming over the network
210  int checkMsg( Packet *outpacket );
211 //Send a position update
212 
213  void send( Cmd cmd, NetBuffer &netbuf, int mode, const char *file, int line );
214  void sendCustom( string cmd, string args, string id );
215  void sendPosition( const ClientState *cs );
216  void sendCloak( bool engage );
217 //Send a PING-like packet to say we are still alive (UDP)
218  void sendAlive();
219  void inGame(); //Tells the server we are ready to go in game
220  bool isInGame()
221  {
222  return this->ingame;
223  }
224 
225  Transformation Interpolate( Unit *un, double addtime );
226 
227 //void disable() { enabled=false;}
228 //int isEnabled() { return enabled; }
229 //void setNetworkedMode( bool mode) { enabled = mode;}
230 
231 /********************* Weapon stuff **********************/
232 //Functions called when to give a firing order to the server (only for player units).
233  void scanRequest( Unit *target );
234  void targetRequest( Unit *target );
235  void respawnRequest();
236  void textMessage( const string &data );
237  void fireRequest( ObjSerial serial, const vector< int > &mount_indicies, char mis );
238  void unfireRequest( ObjSerial serial, const vector< int > &mount_indicies );
239 
240  void cargoRequest( ObjSerial buyer,
241  ObjSerial seller,
242  const std::string &cargo,
243  unsigned int quantity,
244  int mountOffset,
245  int subunitOffset );
246  void shipRequest( const std::string &cargo, unsigned char type );
247  void missionRequest( unsigned short packetType, string mission, int pos );
248  void communicationRequest( const class CommunicationMessage &c, ObjSerial sendTo );
249  void downloadZoneInfo();
250  void AddObjects( NetBuffer &netbuf );
251  ClientPtr AddClientObject( Unit *un, ObjSerial cltserial = 0 );
252 
253  bool jumpRequest( string newsystem, ObjSerial jumpserial );
254  bool readyToJump();
255  void unreadyToJump();
256 
257 /********************* Docking stuff **********************/
258  void dockRequest( ObjSerial utdw_serial );
259  void undockRequest( ObjSerial utdw_serial );
260 
261 /********************* Communication stuff **********************/
262 private:
263 public:
264  void createNetComm( float minfreq, float maxfreq, bool video, bool secured, string method );
265  void destroyNetComm();
266  void startCommunication();
267  void stopCommunication();
268  void startWebcamTransfer();
269  void stopWebcamTransfer();
270  void sendWebcamPicture();
271  char * getWebcamCapture();
272  char * getWebcamFromNetwork( int &length );
273  void increaseFrequency();
274  void decreaseFrequency();
275  float getSelectedFrequency();
276  float getCurrentFrequency();
277  void switchSecured();
278  void switchWebcam();
279  bool hasWebcam();
280 
281  void sendTextMessage( string message );
282  bool IsNetcommActive() const;
283  bool IsNetcommSecured() const;
284  static void Reconnect( std::string srvipaddr, unsigned short port );
285  static void CleanUp();
286 private: NetClient( const NetClient& );
287  NetClient& operator=( const NetClient& );
288 };
289 
290 Unit * getNetworkUnit( ObjSerial cserial );
291 bool isLocalSerial( ObjSerial sernum );
292 
293 extern vector< ObjSerial >localSerials;
294 void Reconnect( std::string ip, std::string port );
295 #endif
296