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
netserver.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  NetServer - Network Server Interface - written by Stephane Vaxelaire <svax@free.fr>
19 */
20 
21 #ifndef __NETSERVER_H
22 #define __NETSERVER_H
23 
24 #include "config.h" // for HAVE_ZLIB
25 
26 #include <vector>
27 #include <list>
28 // WARNING : using std::queue gives conflicts under Solaris so we must use std::queue explicitly each time we need to
29 #include <queue>
30 #include <map>
31 #include <string.h>
32 #include <stdlib.h>
33 
34 #include <boost/shared_ptr.hpp>
35 #include "configxml.h"
36 #include "networking/accountsxml.h"
37 #include "networking/const.h"
41 #include "networking/client.h"
42 #include "networking/clientptr.h"
43 #include "gfx/quaternion.h"
44 #include "cmd/unit_generic.h"
45 #include "boost/smart_ptr.hpp"
46 
47 struct GFXColor;
48 
49 extern VegaConfig *vs_config;
50 //extern VegaSimpleConfig *server_config;
51 using std::vector;
52 // typedef list<Client *>::iterator LI;
53 typedef vector<Account *>::iterator VI;
54 
56 
57 struct ServerSocket;
58 class SocketSet;
59 
60 namespace VsnetDownload {
61  namespace Server {
62  class Manager;
63  };
64 };
65 class VsnetHTTPSocket;
67 class NetServer
68 {
69  SocketSet _sock_set; // Capsule for select()
70 
71  ServerSocket* tcpNetwork;
72  SOCKETALT *udpNetwork;
73  Packet packet; // Network data packet
74  Packet packeta; // Network data packet for account server
75 
76  SaveGame* globalsave; // Savegame class used to save dynamic universe
77  int nbclients; // Active client connections number
78  int nbaccts; // Number of registered accounts
79 
80  VsnetHTTPSocket* acct_sock; // Connection socket for account server
81  int keeprun; // Tells if we should keep running server
82  int snapchanged; // Tells the snapshot has changed and can be sent
83 
84  int acctserver; // Tells if we use an account server
85  // If we don't use an account server:
86  string server_password;
87 
88  timeval srvtimeout; // timer
89  std::queue<int> unused_players;
90 
91  // queue of units added to system but not sent to clients yet.
92  std::vector<UnitContainer> newUnits;
93 
94 // vector<Account *> Cltacct; // Client accounts
95  ClientList allClients; // Active TCP and UDP client connections
96  ClientList discList; // Client connections to be disconnected
97  ClientList logoutList; // Client connections that logged out
98 
99  struct WaitListEntry
100  {
101  enum EntryTypes{CONNECTING, JUMPING};
102  EntryTypes type;
103  bool tcp;
104  ClientPtr t; // Client connections waiting for login response
105  AddressIP u; // Client addresses waiting for login response
106  };
107 
108  std::map<string, WaitListEntry> waitList;
109 
110  boost::shared_ptr<VsnetDownload::Server::Manager> _downloadManagerServer;
111  static const char* _downloadSearchDirs[];
112 
113  public:
114  class ZoneMgr *zonemgr; // Zones/star systems Manager
115  private:
116 
117  bool updateTimestamps( ClientPtr clt, Packet & p);
118  //void loadConfig(); // Loads configuration from server.xml
119  void posUpdate( ClientPtr clt); // Update a client position
120  void addClient( ClientPtr clt); // Add the client in the game
121  void chooseShip( ClientPtr clt, Packet &p); // Client has chosen a ship to fly in.
122  void localLogin( ClientPtr clt, Packet &p); // No account server... client will choose a ship.
123  bool loginAccept( const std::string &inetbuf, ClientPtr clt, int newacct, char flags);
124  void serverTimeInitUDP( ClientPtr clt, NetBuffer &netbuf);
125  void removeClient( ClientPtr clt); // Remove the client from the game
126  ClientPtr newConnection_udp( const AddressIP& ipadr);
127  ClientPtr newConnection_tcp( );
128  void checkAcctMsg( SocketSet& set ); // Check for account server message to receive
129  void checkMsg( SocketSet& set ); // Check for network message to receive
130  void checkKey( SocketSet& set); // Check for keyboard input
131  void recvMsg_tcp( ClientPtr clt); // Receive network messages
132  void recvMsg_udp( ); // Receive network messages
133  void processPacket( ClientPtr clt, unsigned char cmd, const AddressIP& ipadr, Packet& packet ); // Process received packet info
134  void startMsg(); // Startup showing text
135  void disconnect( ClientPtr clt, const char* debug_from_file, int debug_from_line ); // Disconnect a client
136  void logout( ClientPtr clt); // Clean disconnect a client
137  void checkTimedoutClients_udp(); // Check for timed out clients
138 
139  ClientPtr addNewClient( SOCKETALT &sock ); // Adds a new client to listen for.
140  void sendLoginError( ClientPtr clt);
141  void sendLoginAlready( ClientPtr clt);
142  void sendLoginAccept( ClientPtr clt, Cockpit *cp);
143  //returns false if unwilling to load star system
144  void sendLoginUnavailable( ClientPtr clt);
145 
146  // loadCockpit will fail if a cockpit already exists when client state is CONNECTED.
147  Cockpit * loadCockpit(ClientPtr clt );
148  bool loadFromSavegame( ClientPtr clt, Cockpit *cp );
149  bool loadFromNewGame( ClientPtr clt, Cockpit *cp, string shipname );
150  ClientPtr getClientFromSerial( ObjSerial serial);
151  public:
152  NetServer();
153  ~NetServer();
154 
155  void start( int argc, char ** argv);
156  void save();
157 
158  void broadcast( NetBuffer & netbuf, ObjSerial serial, unsigned short zone, Cmd command, bool isTcp );
159 
160  void broadcastUnit( Unit* un, unsigned short zone);
161  void sendNewUnitQueue();
162  // WEAPON STUFF
163  void BroadcastCargoUpgrade( ObjSerial sender, ObjSerial buyer, ObjSerial seller, const std::string &cargo,
164  float price, float mass, float volume, bool mission, unsigned int quantity,
165  int mountOffset, int subunitOffset, unsigned short zone);
166  void BroadcastTarget( ObjSerial serial, ObjSerial oldtarg, ObjSerial target, unsigned short zone);
167  void BroadcastUnfire( ObjSerial serial, const vector<int> &weapon_indicies, unsigned short zone);
168  void BroadcastFire( ObjSerial serial, const vector<int> &weapon_indicies, ObjSerial missile_serial, float energy, unsigned short zone);
169  //void sendDamages( ObjSerial serial, int zone, string shields, float recharge, char leak, unsigned short ab, unsigned short af, unsigned short al, unsigned short ar, float ppercentage, float spercentage, float amt, Vector & pnt, Vector & normal, GFXColor & color);
170 
171  bool saveAccount(int cpnum );
172 
173  void sendCustom( int cp, const string &cmd, const string &args, const string &id);
174  void sendDamages( ObjSerial serial, unsigned short zone, float hull, const Shield &shields, const Armor &armor,
175  float ppercentage, float spercentage, float amt, Vector & pnt, Vector & normal, GFXColor & color);
176  //void sendDamages( ObjSerial serial, Vector & pnt, Vector & normal, float amt, GFXColor & color, float phasedamage);
177  void sendKill( ObjSerial serial, unsigned short zone);
178  void sendJump( Unit * src, Unit *jumppoint, const string &destination);
179  void sendJumpFinal( ClientPtr clt, const string &server, unsigned short port );
180  void sendForcePosition( ClientPtr clt );
181 
182  void invalidateSnapshot( ) { snapchanged = 1; }
183 
184  // DOCKING STUFF
185  void sendDockAuthorize( ObjSerial serial, ObjSerial utdwserial, int docknum, unsigned short zone);
186  void sendDockDeny( ObjSerial serial, unsigned short zone);
187  void sendUnDock( ObjSerial serial, ObjSerial utdwserial, unsigned short zone);
188  void sendCredits( ObjSerial serial, float creds);
189  void addUnitCargoSnapshot( const Unit *un, NetBuffer &netbuf);
190  void sendCargoSnapshot( ObjSerial serial, const UnitCollection &unitlist);
191 
192  void sendMessage(const string &from, const string &to, const string &message, float delay);
193  void sendCommunication(Unit *from, Unit *to, const class CommunicationMessage *c);
194  void sendSaveData( int cp, unsigned short packetType, int pos, const string *key,
195  Mission *miss, // Mission number for objectives.
196  const string *strValue, const float *floatValue);
197  void sendMission( int cp, unsigned short packetType, string mission, int pos);
198 
199  void addSystem( string & sysname, string & system);
200  friend class ZoneMgr;
201 
202  void closeAllSockets(); // Disconnect all clients for shutdown
203 };
204 
205 //void str_cat( char *res, char c, char *s);
206 
207 #endif
208