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 Class Reference

#include <netserver.h>

Public Member Functions

 NetServer ()
 
 ~NetServer ()
 
void start (int argc, char **argv)
 
void save ()
 
void broadcast (NetBuffer &netbuf, ObjSerial serial, unsigned short zone, Cmd command, bool isTcp)
 
void broadcastUnit (Unit *un, unsigned short zone)
 
void sendNewUnitQueue ()
 
void BroadcastCargoUpgrade (ObjSerial sender, ObjSerial buyer, ObjSerial seller, const std::string &cargo, float price, float mass, float volume, bool mission, unsigned int quantity, int mountOffset, int subunitOffset, unsigned short zone)
 
void BroadcastTarget (ObjSerial serial, ObjSerial oldtarg, ObjSerial target, unsigned short zone)
 
void BroadcastUnfire (ObjSerial serial, const vector< int > &weapon_indicies, unsigned short zone)
 
void BroadcastFire (ObjSerial serial, const vector< int > &weapon_indicies, ObjSerial missile_serial, float energy, unsigned short zone)
 
bool saveAccount (int cpnum)
 
void sendCustom (int cp, const string &cmd, const string &args, const string &id)
 
void sendDamages (ObjSerial serial, unsigned short zone, float hull, const Shield &shields, const Armor &armor, float ppercentage, float spercentage, float amt, Vector &pnt, Vector &normal, GFXColor &color)
 
void sendKill (ObjSerial serial, unsigned short zone)
 
void sendJump (Unit *src, Unit *jumppoint, const string &destination)
 
void sendJumpFinal (ClientPtr clt, const string &server, unsigned short port)
 
void sendForcePosition (ClientPtr clt)
 
void invalidateSnapshot ()
 
void sendDockAuthorize (ObjSerial serial, ObjSerial utdwserial, int docknum, unsigned short zone)
 
void sendDockDeny (ObjSerial serial, unsigned short zone)
 
void sendUnDock (ObjSerial serial, ObjSerial utdwserial, unsigned short zone)
 
void sendCredits (ObjSerial serial, float creds)
 
void addUnitCargoSnapshot (const Unit *un, NetBuffer &netbuf)
 
void sendCargoSnapshot (ObjSerial serial, const UnitCollection &unitlist)
 
void sendMessage (const string &from, const string &to, const string &message, float delay)
 
void sendCommunication (Unit *from, Unit *to, const class CommunicationMessage *c)
 
void sendSaveData (int cp, unsigned short packetType, int pos, const string *key, Mission *miss, const string *strValue, const float *floatValue)
 
void sendMission (int cp, unsigned short packetType, string mission, int pos)
 
void addSystem (string &sysname, string &system)
 
void closeAllSockets ()
 

Public Attributes

class ZoneMgrzonemgr
 

Friends

class ZoneMgr
 

Detailed Description

Class Netserver : runs the "game server"

Definition at line 67 of file netserver.h.

Constructor & Destructor Documentation

NetServer::NetServer ( )

Definition at line 114 of file netserver.cpp.

References getNewTime(), serial_seed, UpdateTime(), and FileUtil::use_crypto.

115 {
116  udpNetwork = new SOCKETALT();
117  this->nbclients = 0;
118  this->nbaccts = 0;
119  this->keeprun = 1;
120  this->acctserver = 0;
121  this->srvtimeout.tv_sec = 0;
122  this->srvtimeout.tv_usec = 0;
123  this->snapchanged = 0;
124  /***** number of zones should be determined as server loads zones files *****/
125  zonemgr = new ZoneMgr();
126  UpdateTime();
127  srand( (unsigned int) getNewTime() );
128  //Here 500 could be something else between 1 and 0xFFFF
129  serial_seed = (ObjSerial) ( rand()*( 500./( ( (double) (RAND_MAX) )+1 ) ) );
130  globalsave = new SaveGame( "" );
131 
132  _downloadManagerServer.reset( new VsnetDownload::Server::Manager( _sock_set ) );
133  _sock_set.addDownloadManager( _downloadManagerServer );
134 #ifdef CRYPTO
135  FileUtil::use_crypto = true;
136 #endif
137 }
NetServer::~NetServer ( )

Definition at line 139 of file netserver.cpp.

140 {
141  delete zonemgr;
142  delete globalsave;
143  delete udpNetwork;
144 }

Member Function Documentation

void NetServer::addSystem ( string &  sysname,
string &  system 
)

Definition at line 1568 of file netserver.cpp.

Referenced by UniverseUtil::ComputeSystemSerials().

1569 {
1570  zonemgr->addSystem( sysname, system );
1571 }
void NetServer::addUnitCargoSnapshot ( const Unit un,
NetBuffer netbuf 
)

Definition at line 143 of file netserver_devices.cpp.

References NetBuffer::addFloat(), NetBuffer::addInt32(), NetBuffer::addSerial(), NetBuffer::addString(), UnitImages< BOGUS >::CargoVolume, Unit::GetCargo(), Cargo::GetContent(), Cargo::GetMass(), Cargo::GetMissionFlag(), Cargo::GetPrice(), Cargo::GetQuantity(), Unit::GetSerial(), Cargo::GetVolume(), i, Unit::Mass, Unit::numCargo(), Unit::pImage, and UnitImages< BOGUS >::UpgradeVolume.

Referenced by sendCargoSnapshot().

144 {
145  unsigned int numCargo = un->numCargo();
146  if (!numCargo) return;
147  netbuf.addSerial( un->GetSerial() );
148  netbuf.addFloat( un->Mass );
149  netbuf.addFloat( un->pImage->CargoVolume );
150  netbuf.addFloat( un->pImage->UpgradeVolume );
151  netbuf.addInt32( numCargo );
152  for (unsigned int i = 0; i < numCargo; i++) {
153  const Cargo &carg = un->GetCargo( i );
154  netbuf.addInt32( carg.GetQuantity()*(carg.GetMissionFlag() ? -1 : 1) );
155  netbuf.addString( carg.GetContent() );
156  netbuf.addFloat( carg.GetPrice() );
157  netbuf.addFloat( carg.GetMass() );
158  netbuf.addFloat( carg.GetVolume() );
159  }
160 }
void NetServer::broadcast ( NetBuffer netbuf,
ObjSerial  serial,
unsigned short  zone,
Cmd  command,
bool  isTcp 
)

Definition at line 1543 of file netserver.cpp.

References Packet::bc_create(), NetBuffer::getData(), NetBuffer::getDataLength(), PSEUDO__LINE__, and SENDRELIABLE.

1544 {
1545  Packet p;
1546  p.bc_create( command, serial,
1547  netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
1548  __FILE__, PSEUDO__LINE__( 902 ) );
1549  zonemgr->broadcast( zone, 0, &p, isTcp );
1550 }
void NetServer::BroadcastCargoUpgrade ( ObjSerial  sender,
ObjSerial  buyer,
ObjSerial  seller,
const std::string &  cargo,
float  price,
float  mass,
float  volume,
bool  mission,
unsigned int  quantity,
int  mountOffset,
int  subunitOffset,
unsigned short  zone 
)

Definition at line 93 of file netserver_devices.cpp.

References NetBuffer::addFloat(), NetBuffer::addInt32(), NetBuffer::addSerial(), NetBuffer::addString(), Packet::bc_create(), ZoneMgr::broadcast(), CMD_CARGOUPGRADE, NetBuffer::getData(), NetBuffer::getDataLength(), PSEUDO__LINE__, SENDRELIABLE, and zonemgr.

Referenced by Unit::AddCargo(), Unit::RemoveCargo(), and Unit::UpAndDownGrade().

105 {
106  Packet p;
107  NetBuffer netbuf;
108 
109  netbuf.addSerial( buyer ); //If the buyer is the player, it is buying cargo.
110  netbuf.addSerial( seller ); //If seller is the player, it is selling cargo.
111  netbuf.addInt32( quantity );
112  netbuf.addString( cargo );
113  netbuf.addFloat( price );
114  netbuf.addFloat( mass );
115  netbuf.addFloat( volume );
116  if (mission) {
117  netbuf.addInt32( 1 );
118  netbuf.addInt32( 1 );
119  } else {
120  netbuf.addInt32( (unsigned int) (mountOffset) );
121  netbuf.addInt32( (unsigned int) (subunitOffset) );
122  }
123  p.bc_create( CMD_CARGOUPGRADE, sender,
124  netbuf.getData(), netbuf.getDataLength(),
125  SENDRELIABLE,
126  __FILE__, PSEUDO__LINE__( 87 ) );
127  zonemgr->broadcast( zone, sender, &p, true ); //NETFIXME: Should unfire be TCP?
128 }
void NetServer::BroadcastFire ( ObjSerial  serial,
const vector< int > &  weapon_indicies,
ObjSerial  missile_serial,
float  energy,
unsigned short  zone 
)

Definition at line 70 of file netserver_devices.cpp.

References NetBuffer::addFloat(), NetBuffer::addInt32(), NetBuffer::addSerial(), Packet::bc_create(), ZoneMgr::broadcast(), CMD_FIREREQUEST, NetBuffer::getData(), NetBuffer::getDataLength(), i, PSEUDO__LINE__, SENDRELIABLE, and zonemgr.

Referenced by Unit::Fire().

75 {
76  Packet p;
77  NetBuffer netbuf;
78  //bool found = false;
79 
80  netbuf.addFloat( player_energy );
81  netbuf.addSerial( missile_serial );
82  netbuf.addInt32( weapon_indicies.size() );
83  for (unsigned int i = 0; i < weapon_indicies.size(); i++)
84  netbuf.addInt32( weapon_indicies[i] );
85  p.bc_create( CMD_FIREREQUEST, serial,
86  netbuf.getData(), netbuf.getDataLength(),
88  __FILE__, PSEUDO__LINE__( 1710 ) );
89  //WARNING : WE WILL SEND THE INFO BACK TO THE CLIENT THAT HAS FIRED -> SHOULD USE broadcastNoSelf instead if we dont want that
90  zonemgr->broadcast( zone, serial, &p, true ); //NETFIXME: Should unfire be TCP?
91 }
void NetServer::BroadcastTarget ( ObjSerial  serial,
ObjSerial  oldtarg,
ObjSerial  target,
unsigned short  zone 
)

Definition at line 18 of file netserver_devices.cpp.

References NetBuffer::addSerial(), CMD_TARGET, NetBuffer::getData(), NetBuffer::getDataLength(), PSEUDO__LINE__, NetBuffer::Reset(), Packet::send(), and SENDRELIABLE.

Referenced by Unit::Target().

19 {
20  Packet p;
21  NetBuffer netbuf;
22  netbuf.addSerial( target );
23  ClientPtr clt = this->getClientFromSerial( target );
24  if (clt)
25  p.send( CMD_TARGET, serial, netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
26  NULL, clt->tcp_sock, __FILE__, PSEUDO__LINE__( 24 ) );
27  netbuf.Reset();
28  netbuf.addSerial( 0 );
29  clt = this->getClientFromSerial( oldtarget );
30  if (clt)
31  p.send( CMD_TARGET, serial, netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
32  NULL, clt->tcp_sock, __FILE__, PSEUDO__LINE__( 34 ) );
33  clt = this->getClientFromSerial( serial );
34  if (clt && clt->netversion < 4951) {
35  //Old clients needed confirmation to target...
36  netbuf.Reset();
37  netbuf.addSerial( target );
38  p.send( CMD_TARGET, serial, netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
39  NULL, clt->tcp_sock, __FILE__, PSEUDO__LINE__( 34 ) );
40  }
41  /*
42  * netbuf.addSerial( target);
43  * p.bc_create( CMD_TARGET, serial,
44  * netbuf.getData(), netbuf.getDataLength(),
45  * SENDRELIABLE,
46  * __FILE__, PSEUDO__LINE__(26) );
47  * zonemgr->broadcast( zone, serial, &p, true ); // NETFIXME: Should unfire be TCP?
48  */
49 }
void NetServer::BroadcastUnfire ( ObjSerial  serial,
const vector< int > &  weapon_indicies,
unsigned short  zone 
)

Definition at line 51 of file netserver_devices.cpp.

References NetBuffer::addInt32(), Packet::bc_create(), ZoneMgr::broadcast(), CMD_UNFIREREQUEST, NetBuffer::getData(), NetBuffer::getDataLength(), i, PSEUDO__LINE__, SENDRELIABLE, and zonemgr.

Referenced by Unit::Fire(), and Unit::UnFire().

52 {
53  Packet p;
54  NetBuffer netbuf;
55 
56  //netbuf.addSerial( serial);
57  netbuf.addInt32( weapon_indicies.size() );
58  for (unsigned int i = 0; i < weapon_indicies.size(); i++)
59  netbuf.addInt32( weapon_indicies[i] );
60  //p.send( CMD_UNFIREREQUEST, serial, netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE, NULL, this->clt_sock, __FILE__, __LINE__);
61  p.bc_create( CMD_UNFIREREQUEST, serial,
62  netbuf.getData(), netbuf.getDataLength(),
64  __FILE__, PSEUDO__LINE__( 1695 ) );
65  zonemgr->broadcast( zone, serial, &p, true ); //NETFIXME: Should unfire be TCP?
66 }
void NetServer::broadcastUnit ( Unit un,
unsigned short  zone 
)

Definition at line 39 of file netserver_clients.cpp.

40 {
41  newUnits.push_back( un );
42 }
void NetServer::closeAllSockets ( )

Definition at line 1558 of file netserver.cpp.

Referenced by VSExit().

1559 {
1560  tcpNetwork->disconnect( "Closing sockettcp" );
1561  udpNetwork->disconnect( "Closing socketudp" );
1562  for (LI i = allClients.begin(); i != allClients.end(); i++) {
1563  ClientPtr cl = *i;
1564  cl->tcp_sock.disconnect( cl->callsign.c_str() );
1565  }
1566 }
void NetServer::invalidateSnapshot ( )
inline

Definition at line 182 of file netserver.h.

Referenced by sendNewUnitQueue().

182 { snapchanged = 1; }
void NetServer::save ( )

Definition at line 109 of file netserver_acct.cpp.

References ZoneMgr::displayNPCs(), f, VSFileSystem::Ok, VSFileSystem::UnknownFile, and zonemgr.

110 {
111  using namespace VSFileSystem;
112 
113  Packet pckt;
114  string xmlstr, savestr;
115  //unsigned int xmllen, savelen, nxmllen, nsavelen;
116  NetBuffer netbuf;
117 
118  //Save the Dynamic Universe in the data dir for now
119  string dynuniv_path = "dynaverse.dat";
120  VSFile f;
121  VSError err = f.OpenCreateWrite( dynuniv_path, ::VSFileSystem::UnknownFile );
122  if (err > Ok) {
123  cerr<<"FATAL ERROR: Error opening dynamic universe file"<<endl;
124  } else {
125  string dyn_univ = globalsave->WriteDynamicUniverse();
126  f.Write( dyn_univ );
127  f.Close();
128  }
129  zonemgr->displayNPCs();
130 }
bool NetServer::saveAccount ( int  cpnum)

Definition at line 132 of file netserver_acct.cpp.

References _Universe, Universe::AccessCockpit(), acct_con, ACCT_SAVE, addSimpleChar(), addSimpleString(), CMD_SAVEACCOUNTS, COUT, NetBuffer::getData(), NetBuffer::getDataLength(), Cockpit::GetParent(), SaveNetUtil::GetSaveStrings(), Unit::GetSerial(), Client::INGAME, Packet::send(), SENDRELIABLE, and VsnetHTTPSocket::sendstr().

133 {
134  string xmlstr, savestr;
135  //unsigned int xmllen, savelen, nxmllen, nsavelen;
136  Cockpit *cp = _Universe->AccessCockpit( i );
137  //Write the save and xml unit
138  //FileUtil::WriteSaveFiles( savestr, xmlstr, VSFileSystem::datadir+"/serversaves", cp->savegame->GetCallsign());
139  //SEND THE BUFFERS TO ACCOUNT SERVER
140  if (cp && acctserver && acct_con) {
141  Unit *un = cp->GetParent();
142  ClientPtr clt;
143  if (un)
144  clt = getClientFromSerial( un->GetSerial() );
145  if (!clt || !un) {
146  cerr<<"Error client/unit for "<<(clt ? clt->callsign : "")<<", serial "<<(un ? un->GetSerial() : 0)
147  <<" not found in save process !!!!"<<endl;
148  return false;
149  }
150  if (clt->loginstate < Client::INGAME)
151  return false; //Cannot save at this point.
152 
153  SaveNetUtil::GetSaveStrings( i, savestr, xmlstr, true );
154  if ( savestr.empty() || xmlstr.empty() )
155  //cerr<<"Unable to generate CSV and Save data for player."<<endl;
156  return false;
157  std::string snetbuf;
158  //Loop through clients to find the one corresponding to the unit (we need its serial)
159  //Fix CMD_RESPAWN. I expect the client to do the same thing here.
160  clt->savegame.clear();
161  clt->savegame.push_back( savestr );
162  clt->savegame.push_back( xmlstr );
163 
164  addSimpleChar( snetbuf, ACCT_SAVE );
165  addSimpleString( snetbuf, clt->callsign );
166  addSimpleString( snetbuf, clt->passwd );
167  addSimpleString( snetbuf, savestr );
168  addSimpleString( snetbuf, xmlstr );
169  if ( !acct_sock->sendstr( snetbuf ) ) {
170  COUT<<"ERROR sending SAVE to account server for "<<clt->callsign<<" ("<<un->GetSerial()<<")"<<endl;
171  return false;
172  }
173  {
174  //Tell client that we saved the game.
175  Packet p1;
176  NetBuffer netbuf;
177  p1.send( CMD_SAVEACCOUNTS, un->GetSerial(), netbuf.getData(), netbuf.getDataLength(),
178  SENDRELIABLE, NULL, clt->tcp_sock, __FILE__, __LINE__ );
179  }
180  return true;
181  }
182  return false;
183 }
void NetServer::sendCargoSnapshot ( ObjSerial  serial,
const UnitCollection unitlist 
)

Definition at line 162 of file netserver_devices.cpp.

References NetBuffer::addSerial(), addUnitCargoSnapshot(), CMD_SNAPCARGO, UnitCollection::constIterator(), NetBuffer::getData(), NetBuffer::getDataLength(), Unit::GetSerial(), PSEUDO__LINE__, Packet::send(), and SENDRELIABLE.

163 {
164  ClientPtr clt = this->getClientFromSerial( cltser );
165  if (!clt) return;
166  NetBuffer netbuf;
167  Packet p2;
168  const Unit *un;
169  for (un_kiter iter = list.constIterator(); (un = *iter); ++iter) {
170  if (!un->GetSerial() || un->GetSerial() == cltser) continue;
171  addUnitCargoSnapshot( un, netbuf );
172  }
173  netbuf.addSerial( 0 );
174  p2.send( CMD_SNAPCARGO, 0, netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
175  NULL, clt->tcp_sock, __FILE__, PSEUDO__LINE__( 196 ) );
176 }
void NetServer::sendCommunication ( Unit from,
Unit to,
const class CommunicationMessage c 
)

Definition at line 330 of file netserver_devices.cpp.

References NetBuffer::addFloat(), NetBuffer::addInt32(), CMD_COMM, CommunicationMessage::curstate, Pilot::effective_relationship, NetBuffer::getData(), NetBuffer::getDataLength(), UnitUtil::getFactionRelation(), Unit::GetSerial(), i, Unit::pilot, Packet::send(), SENDRELIABLE, and Client::tcp_sock.

Referenced by Order::Communicate().

331 {
332  NetBuffer netbuf;
333  Client *clt = this->getClientFromSerial( to->GetSerial() ).get();
334  if (!clt) return;
335  if (c)
336  netbuf.addInt32( c->curstate );
337  else
338  netbuf.addInt32( -1 ); //don't actually say anything...
339  netbuf.addFloat( UnitUtil::getFactionRelation( from, to ) );
340  netbuf.addFloat( UnitUtil::getFactionRelation( to, from ) );
341  if (from->pilot) {
342  Pilot::relationmap::iterator i = from->pilot->effective_relationship.find( to );
343  if ( i == from->pilot->effective_relationship.end() )
344  netbuf.addFloat( 0.0 );
345  else
346  netbuf.addFloat( (*i).second );
347  } else {
348  netbuf.addFloat( 0.0 );
349  }
350  if (to->pilot) {
351  Pilot::relationmap::iterator i = to->pilot->effective_relationship.find( to );
352  if ( i == to->pilot->effective_relationship.end() )
353  netbuf.addFloat( 0.0 );
354  else
355  netbuf.addFloat( (*i).second );
356  } else {
357  netbuf.addFloat( 0.0 );
358  }
359  Packet p2;
360  p2.send( CMD_COMM, from->GetSerial(), netbuf.getData(), netbuf.getDataLength(),
361  SENDRELIABLE, NULL, clt->tcp_sock, __FILE__, __LINE__ );
362 }
void NetServer::sendCredits ( ObjSerial  serial,
float  creds 
)

Definition at line 130 of file netserver_devices.cpp.

References NetBuffer::addFloat(), CMD_CREDITS, NetBuffer::getData(), NetBuffer::getDataLength(), PSEUDO__LINE__, Packet::send(), and SENDRELIABLE.

Referenced by UnitUtil::addCredits(), Unit::BuyCargo(), and Unit::SellCargo().

131 {
132  Packet p;
133  NetBuffer netbuf;
134  netbuf.addFloat( creds );
135  ClientPtr clt = this->getClientFromSerial( serial );
136  if (!clt) return;
137  p.send( CMD_CREDITS, serial,
138  netbuf.getData(), netbuf.getDataLength(),
139  SENDRELIABLE, NULL, clt->tcp_sock,
140  __FILE__, PSEUDO__LINE__( 97 ) );
141 }
void NetServer::sendCustom ( int  cp,
const string &  cmd,
const string &  args,
const string &  id 
)

Definition at line 274 of file netserver_devices.cpp.

References _Universe, Universe::AccessCockpit(), NetBuffer::addString(), CMD_CUSTOM, fprintf, NetBuffer::getData(), NetBuffer::getDataLength(), Cockpit::GetParent(), Unit::GetSerial(), Packet::send(), and SENDRELIABLE.

275 {
276  Packet p2;
277  NetBuffer netbuf;
278 
279  Unit *un = _Universe->AccessCockpit( cp )->GetParent();
280  if (!un) {
281  fprintf( stderr, "Attempt to sendCustom NULL player %d ; CMD %s %s ; ID %s\n",
282  cp, command.c_str(), args.c_str(), id.c_str() );
283  return;
284  }
285  ClientPtr clt = this->getClientFromSerial( un->GetSerial() );
286  if (!clt) return;
287  netbuf.addString( command );
288  netbuf.addString( args );
289  netbuf.addString( id );
290 
291  p2.send( CMD_CUSTOM, un->GetSerial(), netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
292  NULL, clt->tcp_sock, __FILE__, __LINE__ );
293 }
void NetServer::sendDamages ( ObjSerial  serial,
unsigned short  zone,
float  hull,
const Shield shields,
const Armor armor,
float  ppercentage,
float  spercentage,
float  amt,
Vector pnt,
Vector normal,
GFXColor color 
)

Definition at line 232 of file netserver_devices.cpp.

References NetBuffer::addArmor(), NetBuffer::addColor(), NetBuffer::addFloat(), NetBuffer::addShield(), NetBuffer::addVector(), Packet::bc_create(), ZoneMgr::broadcast(), CMD_DAMAGE, NetBuffer::getData(), NetBuffer::getDataLength(), getNewTime(), VegaConfig::getVariable(), XMLSupport::parse_float(), PSEUDO__LINE__, SENDRELIABLE, vs_config, and zonemgr.

Referenced by Unit::ApplyLocalDamage().

243 {
244  static ObjSerial lastserial;
245  static float timestamp = getNewTime();
246  float curtime = getNewTime();
247  static float mintime =
248  XMLSupport::parse_float( vs_config->getVariable( "network", "min_time_btw_damage_updates", ".25" ) );
249  if (lastserial != serial || curtime-timestamp > mintime) {
250  timestamp = curtime;
251  lastserial = serial;
252  Packet p;
253  NetBuffer netbuf;
254 
255  netbuf.addFloat( amt );
256  netbuf.addFloat( ppercentage );
257  netbuf.addFloat( spercentage );
258  netbuf.addVector( pnt );
259  netbuf.addVector( normal );
260  netbuf.addColor( color );
261  netbuf.addFloat( hull );
262  netbuf.addShield( shield );
263  netbuf.addArmor( armor );
264 
265  p.bc_create( CMD_DAMAGE, serial,
266  netbuf.getData(), netbuf.getDataLength(),
267  SENDRELIABLE,
268  __FILE__, PSEUDO__LINE__( 1729 ) );
269  //WARNING : WE WILL SEND THE INFO BACK TO THE CLIENT THAT HAS FIRED
270  zonemgr->broadcast( zone, serial, &p, false ); //NETFIXME: Should damages be TCP? NO..we have alternate method to deal with it
271  }
272 }
void NetServer::sendDockAuthorize ( ObjSerial  serial,
ObjSerial  utdwserial,
int  docknum,
unsigned short  zone 
)

Definition at line 506 of file netserver_devices.cpp.

References NetBuffer::addInt32(), NetBuffer::addSerial(), Packet::bc_create(), ZoneMgr::broadcast(), CMD_DOCK, NetBuffer::getData(), NetBuffer::getDataLength(), PSEUDO__LINE__, SENDRELIABLE, and zonemgr.

507 {
508  //Set client not ingame while docked
509  ClientPtr clt = this->getClientFromSerial( serial );
510  if (!clt) {
511  cerr<<"Client "<<serial<<" for dock autorization is NULL."<<endl;
512  return;
513  }
514  Unit *un = clt->game_unit.GetUnit();
515  if (!un)
516  return;
517 //StarSystem * currentsys = un->getStarSystem();
518  //Remove the unit from the system list
519 //currentsys->RemoveUnit( un );
520 
521  NetBuffer netbuf;
522  Packet p;
523  //Send a CMD_DOCK with serial, an ObjSerial = unit_to_dock_with_serial and an int = docking port num
524  netbuf.addSerial( utdw_serial );
525  netbuf.addInt32( 0 ); //docknum);
526  p.bc_create( CMD_DOCK, serial,
527  netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
528  __FILE__, PSEUDO__LINE__( 118 ) );
529  zonemgr->broadcast( zone, serial, &p, true );
530 
531  //Whoops... Make sure to de-ingame the client *AFTER* the CMD_DOCK has been broadcast!
532  clt->ingame = false;
533  //Set timestamps to 0 so we won't have prediction problem when undocking
534  clt->clearLatestTimestamp();
535 }
void NetServer::sendDockDeny ( ObjSerial  serial,
unsigned short  zone 
)

Definition at line 537 of file netserver_devices.cpp.

538 {
539  //In fact do nothing
540 }
void NetServer::sendForcePosition ( ClientPtr  clt)

Definition at line 542 of file netserver_devices.cpp.

References NetBuffer::addClientState(), NetBuffer::addQVector(), CMD_POSUPDATE, NetBuffer::getData(), NetBuffer::getDataLength(), ClientState::getPosition(), Unit::GetSerial(), PSEUDO__LINE__, Packet::send(), and SENDRELIABLE.

Referenced by sendUnDock().

543 {
544  Unit *un = clt->game_unit.GetUnit();
545  if (!un)
546  return;
547  ClientState cs( un );
548 
549  Packet p;
550  NetBuffer netbuf;
551  if (clt->netversion < 4951)
552  netbuf.addQVector( cs.getPosition() );
553  else
554  netbuf.addClientState( cs );
555  p.send( CMD_POSUPDATE, un->GetSerial(), netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
556  NULL, clt->tcp_sock, __FILE__, PSEUDO__LINE__( 457 ) );
557 }
void NetServer::sendJump ( Unit src,
Unit jumppoint,
const string &  destination 
)

Definition at line 417 of file netserver_devices.cpp.

References _Universe, ACCT_SAVE_LOGOUT, Universe::activeStarSystem(), addSimpleChar(), addSimpleString(), StarSystem::getFileName(), getSaveStringLength(), SaveNetUtil::GetSaveStrings(), Unit::GetSerial(), Client::LOGGEDIN, pushSaveString(), putSaveString(), VsnetHTTPSocket::sendstr(), and Universe::whichPlayerStarship().

Referenced by Unit::jumpReactToCollision().

418 {
419  ClientPtr clt = this->getClientFromSerial( un->GetSerial() );
420  if (!clt) return;
421  int cpnum = -1;
422  if (clt && ( cpnum = _Universe->whichPlayerStarship( un ) ) >= 0 && un != NULL) {
423  WaitListEntry entry;
424  if ( waitList.find( clt->callsign ) != waitList.end() )
425  return;
426  std::string netbuf;
427  std::string fn = dststr;
428  if (getSaveStringLength( cpnum, "jump_from" ) < 1)
429  pushSaveString( cpnum, "jump_from", _Universe->activeStarSystem()->getFileName() );
430  else
431  putSaveString( cpnum, "jump_from", 0, _Universe->activeStarSystem()->getFileName() );
432  std::string savestr;
433  std::string csvstr;
434  SaveNetUtil::GetSaveStrings( cpnum, savestr, csvstr, true );
435  /*
436  * savestr = cp->savegame->WriteSaveGame (fn.c_str(),un->LocalPosition(),cp->credits,cp->unitfilename,-1,FactionUtil::GetFactionName(un->faction), false);
437  * csvstr = un->WriteUnitString();
438  */
439  std::string::size_type where = savestr.find( "^" );
440  addSimpleChar( netbuf, ACCT_SAVE_LOGOUT );
441  if (where != std::string::npos)
442  if ( dststr != savestr.substr( 0, where ) )
443  savestr = dststr+savestr.substr( where );
444  addSimpleString( netbuf, clt->callsign );
445  addSimpleString( netbuf, clt->passwd );
446  addSimpleString( netbuf, savestr );
447  addSimpleString( netbuf, csvstr );
448  clt->jumpok = 1;
449 
450  entry.type = WaitListEntry::JUMPING;
451  entry.t = clt;
452  entry.tcp = true;
453  waitList[clt->callsign] = entry;
454  clt->loginstate = Client::LOGGEDIN;
455  if (acct_sock)
456  acct_sock->sendstr( netbuf );
457  } else {
458  //do something intelligent for NPCs
459  }
460  /*
461  * if (0) {
462  * Packet p2;
463  * NetBuffer netbuf;
464  * string file_content;
465  * ClientPtr clt = this->getClientFromSerial( serial);
466  * if (!clt) {
467  * cerr<<"Client "<< serial<<" Trying to jump but cannot retrieve client struct \n";
468  * }
469  * // Send a CMD_JUMP to tell the client if the jump is allowed
470  * netbuf.addString( clt->jumpfile );
471  * netbuf.addSerial( serial);
472  * netbuf.addSerial( jumpserial);
473  *
474  * // DO NOT DO THAT : the client itself will ask to be removed and also to be added when it is allowed to do so
475  * // And remove the player from its old starsystem and set it out of game
476  * //this->removeClient( clt );
477  * // Have to set new starsystem here
478  * Cockpit * cp = _Universe->isPlayerStarship( clt->game_unit.GetUnit());
479  * cp->savegame->SetStarSystem( clt->jumpfile);
480  * StarSystem* sts = GetLoadedStarSystem( clt->jumpfile.c_str());
481  * unsigned short zoneid = _Universe->StarSystemIndex( sts);
482  * netbuf.addShort( zoneid);
483  *
484  * // Test whether the jump was accepted or not by server
485  * if( ok)
486  * {
487  * // If jumpfile is empty the hash was correct
488  * if( clt->jumpokOLD==1 )
489  * p2.bc_create( CMD_JUMP, serial,
490  * netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
491  * __FILE__, PSEUDO__LINE__(1164) );
492  * // New system file HASH is wrong tell the client with serial != player serial so he can ask for a new download
493  * else if( clt->jumpokOLD==2)
494  * p2.bc_create( CMD_JUMP, serial+1,
495  * netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
496  * __FILE__, PSEUDO__LINE__(1164) );
497  * zonemgr->broadcast( clt, &p2, true );
498  * }
499  */
500 
501  //Should broadcast JUMP so other client display jump anim too ?
502 }
void NetServer::sendJumpFinal ( ClientPtr  clt,
const string &  server,
unsigned short  port 
)

Definition at line 405 of file netserver_devices.cpp.

References NetBuffer::addShort(), NetBuffer::addString(), CMD_JUMP, NetBuffer::getData(), NetBuffer::getDataLength(), Packet::send(), and SENDRELIABLE.

406 {
407  Packet p2;
408  NetBuffer buf;
409  buf.addString( server_ip );
410  buf.addShort( server_port );
411 
412  p2.send( CMD_JUMP, 0, buf.getData(), buf.getDataLength(), SENDRELIABLE, NULL, clt->tcp_sock, __FILE__, 148 );
413  if (clt && 0 /*dont discon until client requests it*/)
414  logoutList.push_back( clt );
415 }
void NetServer::sendKill ( ObjSerial  serial,
unsigned short  zone 
)

Definition at line 367 of file netserver_devices.cpp.

References _Universe, Packet::bc_create(), ZoneMgr::broadcast(), CMD_KILL, displayUnitInfo(), Mission::getNthPlayerMission(), ZoneMgr::getUnit(), PSEUDO__LINE__, ZoneMgr::removeClient(), SENDRELIABLE, Mission::terminateMission(), Universe::whichPlayerStarship(), and zonemgr.

Referenced by Unit::Destroy(), and Unit::Kill().

368 {
369  Packet p;
370  Unit *un;
371 
372  //cerr<<"SENDING A KILL for serial "<<serial<<" in zone "<<zone<<endl;
373  //Find the client in the udp & tcp client lists in order to set it out of the game (not delete it yet)
374  ClientPtr clt = this->getClientFromSerial( serial );
375  if (clt)
376  un = clt->game_unit.GetUnit();
377  else
378  un = zonemgr->getUnit( serial, zone );
379  //It's the server's responsibility to kill missions in this case.
380  //NETFIXME: Note that the client might not hear the request to terminate missions.
381  int cp = _Universe->whichPlayerStarship( un );
382  if (cp >= 0) {
383  //Note 1 not 0 is to avoid killing the player's main mission.
384  Mission *mis;
385  while ( ( mis = Mission::getNthPlayerMission( cp, 1 ) ) )
386  mis->terminateMission();
387  }
388  p.bc_create( CMD_KILL, serial,
389  NULL, 0, SENDRELIABLE,
390  __FILE__, PSEUDO__LINE__( 1771 ) );
391  //WARNING : WE WILL SEND THE INFO BACK TO THE CLIENT THAT HAS FIRED
392  zonemgr->broadcast( zone, serial, &p, true );
393  if (!clt)
394  displayUnitInfo( un, string(), " *** KILLED a npc " );
395  /*
396  * un = zonemgr->getUnit( serial, zone);
397  * if (un) {
398  * zonemgr->removeUnit( un, zone);
399  * }
400  */
401  else
402  zonemgr->removeClient( clt );
403 }
void NetServer::sendMessage ( const string &  from,
const string &  to,
const string &  message,
float  delay 
)

Definition at line 295 of file netserver_devices.cpp.

References _Universe, Universe::AccessCockpit(), Universe::activeStarSystem(), NetBuffer::addString(), Packet::bc_create(), ZoneMgr::broadcast(), CMD_TXTMESSAGE, NetBuffer::getData(), NetBuffer::getDataLength(), Cockpit::GetParent(), Unit::GetSerial(), StarSystem::GetZone(), PSEUDO__LINE__, Packet::send(), SENDRELIABLE, zonemgr, and ZoneMgr::zones.

Referenced by MessageCenter::add().

296 {
297  NetBuffer netbuf;
298  netbuf.addString( from );
299  netbuf.addString( message );
300  Packet p2;
301 
302  const char*tostr( to.c_str() );
303  if (to[0] == 'p') {
304  int playerto = -1;
305  sscanf( tostr+1, "%d", &playerto );
306  if ( playerto != -1 && (unsigned int) playerto < _Universe->numPlayers() ) {
307  Cockpit *cp = _Universe->AccessCockpit( playerto );
308  Unit *un = cp->GetParent();
309  if (!un) return;
310  ClientPtr clt = this->getClientFromSerial( un->GetSerial() );
311  if (!clt) return;
312  p2.send( CMD_TXTMESSAGE, 0, netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
313  NULL, clt->tcp_sock, __FILE__, PSEUDO__LINE__( 223 ) );
314  }
315  } else {
316  p2.bc_create( CMD_TXTMESSAGE, 0,
317  netbuf.getData(), netbuf.getDataLength(),
318  SENDRELIABLE,
319  __FILE__, PSEUDO__LINE__( 229 ) );
320  if (to == "broadcast") {
321  for (ZoneMap::const_iterator iter = zonemgr->zones.begin();
322  iter != zonemgr->zones.end(); ++iter)
323  zonemgr->broadcast( (*iter).first, 0, &p2, false );
324  } else {
325  zonemgr->broadcast( _Universe->activeStarSystem()->GetZone(), 0, &p2, false );
326  }
327  }
328 }
void NetServer::sendMission ( int  cp,
unsigned short  packetType,
string  mission,
int  pos 
)

Definition at line 216 of file netserver_devices.cpp.

References _Universe, Universe::AccessCockpit(), NetBuffer::addInt32(), NetBuffer::addShort(), NetBuffer::addString(), CMD_MISSION, NetBuffer::getData(), NetBuffer::getDataLength(), Cockpit::GetParent(), Unit::GetSerial(), PSEUDO__LINE__, Packet::send(), and SENDRELIABLE.

Referenced by LoadMission(), and Mission::terminateMission().

217 {
218  Packet p;
219  NetBuffer netbuf;
220  Unit *un = _Universe->AccessCockpit( cp )->GetParent();
221  if (!un) return;
222  ClientPtr clt = this->getClientFromSerial( un->GetSerial() );
223  if (!clt) return;
224  netbuf.addShort( packetType );
225  netbuf.addString( mission );
226  netbuf.addInt32( pos );
227 
228  p.send( CMD_MISSION, 0, netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
229  NULL, clt->tcp_sock, __FILE__, PSEUDO__LINE__( 186 ) );
230 }
void NetServer::sendNewUnitQueue ( )

Definition at line 44 of file netserver_clients.cpp.

References UnitFactory::addBuffer(), Packet::bc_create(), ZoneMgr::broadcast(), CMD_ENTERCLIENT, COUT, displayUnitInfo(), UnitFactory::endBuffer(), NetBuffer::getData(), NetBuffer::getDataLength(), StarSystem::getFileName(), Unit::getStarSystem(), StarSystem::GetZone(), invalidateSnapshot(), PSEUDO__LINE__, SENDRELIABLE, NetBuffer::setVersion(), zonemgr, and ZoneMgr::zones.

45 {
46  unsigned short vers[4] = {0, 4951, 4952, 65535};
47  for (ZoneMap::const_iterator zoneiter = zonemgr->zones.begin();
48  zoneiter != zonemgr->zones.end(); ++zoneiter) {
49  unsigned short zone = (*zoneiter).first;
50  for (int verind = 0; verind < 4; verind += 2) {
51  unsigned short minver = vers[verind], maxver = vers[verind+1];
52  NetBuffer netbuf;
53  if (maxver != 65535) netbuf.setVersion( maxver );
54  bool added = false;
55  for (std::vector< UnitContainer >::iterator iter = newUnits.begin();
56  iter != newUnits.end();
57  ++iter) {
58  Unit *un = (*iter).GetUnit();
59  if (un) {
60  StarSystem *sys = un->getStarSystem();
61  if (sys) {
62  unsigned short unzone = sys->GetZone();
63  if (unzone == zone) {
64  added = true;
65  UnitFactory::addBuffer( netbuf, un, true );
66  if (verind == 0)
67  displayUnitInfo( un, string(),
68  ( " * CREATED "+un->getStarSystem()->getFileName() ).c_str() );
69  }
70  }
71  } else {
72  COUT<<"New Unit already killed before being sent!!!"<<endl;
73  }
74  }
75  if (added) {
76  UnitFactory::endBuffer( netbuf );
77  Packet p2;
78  p2.bc_create( CMD_ENTERCLIENT, 0, netbuf.getData(), netbuf.getDataLength(),
79  SENDRELIABLE, __FILE__, PSEUDO__LINE__( 66 ) );
80  zonemgr->broadcast( zone, 0, &p2, true, minver, maxver );
82  }
83  }
84  }
85  newUnits.clear();
86 }
void NetServer::sendSaveData ( int  cp,
unsigned short  packetType,
int  pos,
const string *  key,
Mission miss,
const string *  strValue,
const float floatValue 
)

Definition at line 178 of file netserver_devices.cpp.

References _Universe, Universe::AccessCockpit(), NetBuffer::addFloat(), NetBuffer::addInt32(), NetBuffer::addShort(), NetBuffer::addString(), CMD_SAVEDATA, Subcmd::FloatValue, NetBuffer::getData(), NetBuffer::getDataLength(), Cockpit::GetParent(), Mission::getPlayerMissionNumber(), Unit::GetSerial(), Subcmd::Objective, PSEUDO__LINE__, Packet::send(), SENDRELIABLE, Subcmd::SetValue, and Subcmd::StringValue.

Referenced by UniverseUtil::addObjective(), UniverseUtil::clearObjectives(), clearSaveData(), clearSaveString(), UniverseUtil::eraseObjective(), eraseSaveData(), eraseSaveString(), pushSaveData(), pushSaveString(), putSaveData(), putSaveString(), saveDataList(), saveStringList(), UniverseUtil::setCompleteness(), and UniverseUtil::setObjective().

185 {
186  /* Note to self: This function will do absolutely nothing
187  * until it is implemented. */
188  NetBuffer netbuf;
189  Packet p2;
190  Unit *un = _Universe->AccessCockpit( cp )->GetParent();
191  if (!un) return;
192  ClientPtr clt = this->getClientFromSerial( un->GetSerial() );
193  if (!clt) return;
194  netbuf.addShort( type );
196  if (*key == "active_scripts" || *key == "mission_scripts")
197  return; //Don't send these to clients since they are huge.
198 
199  netbuf.addString( *key );
200  }
201  netbuf.addInt32( pos );
202  if (type&Subcmd::Objective) {
203  int num = miss->getPlayerMissionNumber();
204  netbuf.addInt32( num-1 );
205  }
206  if (type&Subcmd::SetValue) {
207  if ( (type&Subcmd::StringValue) || (type&Subcmd::Objective) )
208  netbuf.addString( *strValue );
209  if ( (type&Subcmd::FloatValue) || (type&Subcmd::Objective) )
210  netbuf.addFloat( *floatValue );
211  }
212  p2.send( CMD_SAVEDATA, 0, netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
213  NULL, clt->tcp_sock, __FILE__, PSEUDO__LINE__( 164 ) );
214 }
void NetServer::sendUnDock ( ObjSerial  serial,
ObjSerial  utdwserial,
unsigned short  zone 
)

Definition at line 559 of file netserver_devices.cpp.

References NetBuffer::addSerial(), Packet::bc_create(), ZoneMgr::broadcast(), CMD_UNDOCK, NetBuffer::getData(), NetBuffer::getDataLength(), VegaConfig::getVariable(), XMLSupport::parse_bool(), PSEUDO__LINE__, sendForcePosition(), SENDRELIABLE, vs_config, and zonemgr.

560 {
561  //Set client ingame
562  ClientPtr clt = this->getClientFromSerial( serial );
563  if (!clt) {
564  cerr<<"Client "<<serial<<" for dock autorization is NULL."<<endl;
565  return;
566  }
567  Unit *un = clt->game_unit.GetUnit();
568  if (!un)
569  return;
570  clt->ingame = true;
571  //Add the unit back into the system list
572 //StarSystem * currentsys = un->getStarSystem();
573 //currentsys->AddUnit( un );
574 
575  //SEND A CMD_UNDOCK TO OTHER CLIENTS IN THE ZONE with utdw serial
576  NetBuffer netbuf;
577  Packet p;
578  netbuf.addSerial( utdwserial );
579  p.bc_create( CMD_UNDOCK, serial,
580  netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE,
581  __FILE__, PSEUDO__LINE__( 134 ) );
582  zonemgr->broadcast( zone, serial, &p, true );
583 
584  static bool autolanding_enable = XMLSupport::parse_bool( vs_config->getVariable( "physics", "AutoLandingEnable", "false" ) );
585  if (!autolanding_enable)
586  sendForcePosition( clt );
587 }
void NetServer::start ( int  argc,
char **  argv 
)

Definition at line 167 of file netserver.cpp.

References _Universe, acct_con, ACCT_PORT, ACCT_RESYNC, active_missions, addSimpleChar(), addSimpleString(), clienttimeout, VSFileSystem::VSFile::Close(), CONFIGFILE, COUT, NetUITCP::createServerSocket(), NetUIUDP::createServerSocket(), DAMAGE_ATOM, game_data_t::difficulty, ExecuteDirector(), f, g_game, game_options, ROLES::getAllRolePriorities(), getNewTime(), VegaConfig::getVariable(), i, vs_options::init(), Mission::initMission(), VSFileSystem::InitPaths(), InitTime(), InitUnitTables(), input_buffer, j, logintimeout, MAXINPUT, mission, nbchars, NETWORK_ATOM, Universe::numPlayers(), VSFileSystem::Ok, VSFileSystem::VSFile::OpenReadOnly(), XMLSupport::parse_bool(), PLANET_ATOM, StarSystem::ProcessPendingJumps(), PSEUDO__LINE__, VSFileSystem::VSFile::ReadFull(), run_only_player_starsystem, SAVE_ATOM, Universe::star_system, test, universe_file, universe_path, VSFileSystem::UnknownFile, UpdateTime(), vs_config, and VSExit().

Referenced by main().

168 {
169  const char *serverport = NULL;
170  int i;
171  for (i = 0; i < argc; ++i) {
172  char match = 1;
173  int j;
174  if (strncmp( argv[i], "-p", 2 ) == 0)
175  serverport = argv[i]+2;
176  else
177  match = 0;
178  if (match) {
179  for (j = i+1; j < argc; ++j)
180  argv[j-1] = argv[j];
181  argc--;
182  i--;
183  }
184  }
185  string strperiod, strtimeout, strlogintimeout, strnetatom;
186  int periodrecon;
187  keeprun = 1;
188  double savetime = 0;
189  double reconnect_time = 0;
190  double curtime = 0;
191  double snaptime = 0;
192  double planettime = 0;
193  acct_con = 1;
194  nbchars = 0;
195  memset( input_buffer, 0, MAXINPUT );
196  Packet p2;
197 
198  _sock_set.start();
199 
200  startMsg();
201  if (argc == 2) {
202  CONFIGFILE = argv[1];
203  } else {
204  CONFIGFILE = new char[42];
205  strcpy( CONFIGFILE, "vegastrike.config" );
206  }
207  cout<<"Loading server config...";
209 
210  game_options.init();
211 
212  InitUnitTables(); //universe_generic.cpp
213  //Here we say we want to only handle activity in all starsystems
215  //vs_config = new VegaConfig( SERVERCONFIGFILE);
216  cout<<" config loaded"<<endl;
217  //Save period in seconds
218  strperiod = vs_config->getVariable( "server", "saveperiod", "7200" );
219  SAVE_ATOM = atoi( strperiod.c_str() );
220  string strperiodrecon = vs_config->getVariable( "server", "reconnectperiod", "60" );
221  periodrecon = atoi( strperiodrecon.c_str() );
222  strtimeout = vs_config->getVariable( "server", "clienttimeout", "180" );
223  clienttimeout = atoi( strtimeout.c_str() );
224  strlogintimeout = vs_config->getVariable( "server", "logintimeout", "60" );
225  logintimeout = atoi( strlogintimeout.c_str() );
226 
227  this->server_password = vs_config->getVariable( "server", "server_password", "" );
228 
229  strnetatom = vs_config->getVariable( "network", "network_atom", "0.2" );
230  NETWORK_ATOM = (double) atof( strnetatom.c_str() );
231  strnetatom = vs_config->getVariable( "network", "damage_atom", "1" );
232  DAMAGE_ATOM = (double) atof( strnetatom.c_str() );
233  strnetatom = vs_config->getVariable( "network", "planet_atom", "10" );
234  PLANET_ATOM = (double) atof( strnetatom.c_str() );
235 
236  strnetatom = vs_config->getVariable( "server", "difficulty", "1" );
237  g_game.difficulty = atof( strnetatom.c_str() );
238  InitTime();
239  UpdateTime();
240  savetime = getNewTime();
241  reconnect_time = getNewTime()+periodrecon;
242  std::string configport = vs_config->getVariable( "network", "server_port", "6777" );
243  if ( configport.empty() )
244  configport = vs_config->getVariable( "network", "serverport", "6777" );
245  if (serverport == NULL)
246  serverport = configport.c_str();
247  string tmp;
248  acctserver = XMLSupport::parse_bool( vs_config->getVariable( "server", "useaccountserver",
249  vs_config->getVariable( "network", "use_account_server",
250  "false" ) ) );
251 
252  //Create and bind sockets
253  COUT<<"Initializing TCP server ..."<<endl;
254  tcpNetwork = NetUITCP::createServerSocket( atoi( serverport ), _sock_set );
255  if (tcpNetwork == NULL) {
256  COUT<<"Couldn't create TCP server - quitting"<<endl;
257  exit( -100 );
258  }
259  COUT<<"Initializing UDP server ..."<<endl;
260  *udpNetwork = NetUIUDP::createServerSocket( atoi( serverport ), _sock_set );
261  if (*udpNetwork == NULL) {
262  COUT<<"Couldn't create UDP server - quitting"<<endl;
263  exit( -100 );
264  }
265  COUT<<"done."<<endl;
266  std::string acctsrv = vs_config->getVariable( "network", "account_server_url", "" );
267  if ( acctsrv.empty() )
268  acctsrv = vs_config->getVariable( "network", "accountsrv", "" );
269  if (!acctserver) {
270  /*
271  * // Read data files ;)
272  * cout<<"Loading accounts data... ";
273  * LoadAccounts( "accounts.xml");
274  * // Gets hashtable accounts elements and put them in vector Cltacct
275  * Cltacct = getAllAccounts();
276  * cout<<Cltacct.size()<<" accounts loaded."<<endl;
277  */
278  cout<<"Not connecting to account server."<<endl;
279  } else {
280  cout<<"Initializing connection to account server..."<<endl;
281  if ( acctsrv.empty() ) {
282  cout<<"Account server IP not specified, exiting"<<endl;
283  VSExit( 1 );
284  }
285  if (acctsrv.find( '/' ) == std::string::npos) {
286  int acctport = atoi( vs_config->getVariable( "network", "accountsrvport", "" ).c_str() );
287  if (!acctport)
288  acctport = ACCT_PORT;
289  //acct_sock = NetUITCP::createSocket( acctsrv.c_str(), acctport, _sock_set );
290  } else {
291  acct_sock = new VsnetHTTPSocket( acctsrv, _sock_set );
292  }
293  if (acct_sock == NULL)
294  cerr<<"Invalid Accountserver URL... "<<endl;
295 //VSExit(1);
296  else
297  COUT<<"accountserver on socket "<<acct_sock<<" done."<<endl;
298  }
299  //Create the _Universe telling it we are on server side
300  universe_path = "";
301  universe_file = vs_config->getVariable( "server", "galaxy", "milky_way.xml" );
302  cout<<"LOADING Universe file : "<<universe_file<<endl;
303  _Universe = new Universe( argc, argv, universe_file.c_str(), true );
304  cout<<"Universe LOADED"<<endl;
305  string strmission = vs_config->getVariable( "server", "missionfile", "networking.mission" );
306  Python::init();
307  Python::test();
308  active_missions.push_back( mission = new Mission( strmission.c_str() ) );
309  mission->initMission( true );
310 
311  //Loads dynamic universe
312  string dynpath = "dynaverse.dat";
313  VSFile f;
314  VSError err = f.OpenReadOnly( dynpath, ::VSFileSystem::UnknownFile );
315  if (err > Ok) {
316  cerr<<"!!! ERROR : opening dynamic universe file "<<dynpath.c_str()<<" !!!"<<endl;
317  } else {
318  string dynaverse = f.ReadFull();
319  char *dynchar = strdup( dynaverse.c_str() );
320  globalsave->ReadSavedPackets( dynchar, true );
321  f.Close();
322  }
323  std::vector< std::vector< char > >temp = ROLES::getAllRolePriorities();
324  {
325  char hostName[128];
326  hostName[0] = '\0';
327  gethostname( hostName, 128 );
328  hostent *local = NULL;
329  cout<<endl<<endl<<" ======== SERVER IS NOW RUNNING ========"<<endl;
330  const AddressIP &adr = this->tcpNetwork->get_adr();
331  cout<<" Server Port: "<<ntohs( adr.sin_port )<<endl;
332  cout<<" Server IP Addresses: "<<endl;
333 //cout << " localhost (Local computer only)" << endl;
334  int num = 0;
335  if (hostName[0])
336 //cout << " " << hostName << " (requires DNS lookup) "
337  local = gethostbyname( hostName );
338  if (local) {
339  in_addr **localaddr = (in_addr**) local->h_addr_list;
340  for (int i = 0; i < 5 && localaddr[i]; i++) {
341  string ipaddr = inet_ntoa( *(localaddr[i]) );
342  if (ipaddr.substr( 0, 4 ) == "127.") {
343  continue;
344 //cout << " (Local computer only)";
345  } else {
346  cout<<" "<<ipaddr;
347  num++;
348  if (ipaddr.substr( 0, 8 ) == "169.254.")
349  cout<<" (Ethernet connection)";
350  else if (ipaddr.substr( 0, 8 ) == "192.168."
351  || ipaddr.substr( 0, 3 ) == "10.")
352  cout<<" (Local Area Network)";
353  else
354  cout<<" (Internet Connection)";
355  }
356  cout<<endl;
357  }
358  }
359  if (!num) {
360  cout<<" No network interfaces found associated to your hostname."<<endl;
361 #ifdef _WIN32
362  cout<<" (Consult Start-> Run-> 'cmd /k ipconfig' for your IP.)"<<endl;
363 #else
364  cout<<" (Consult the '/sbin/ifconfig' command-line tool for your IP.)"<<endl;
365 #endif
366  }
367  cout<<" You can also connect locally using 'localhost'"<<endl;
368  if (acctserver) {
369  cout<<" Public Server: "<<endl<<" ";
370  if (acctsrv.length() > 75)
371  cout<<acctsrv.substr( 0, 50 )<<"..."<<acctsrv.substr( acctsrv.length()-20, 20 )<<endl;
372  else
373  cout<<acctsrv<<endl;
374  } else {
375  if ( this->server_password.empty() )
376  cout<<" Private Server"<<endl;
377  else
378  cout<<" Private Server, Password Protected: <"<<this->server_password<<">"<<endl;
379  }
380  cout<<" --------------------------------------- "<<endl;
381  cout<<"To stop this server, hit Ctrl-C, Ctrl-\\, Ctrl-Break, or close this window."<<endl;
382  cout<<endl<<"Have fun!"<<endl<<endl;
383  }
384  //Server loop
385  while (keeprun) {
386  //int nb;
387 
388  UpdateTime();
389  if (_Universe->numPlayers() > 0)
390  ExecuteDirector();
391  //Check a key press
392  //keyset.setReadAlwaysTrue( 0);
393  //this->checkKey( keyset);
394 
395  //Check received communications
396  checkMsg( _sock_set );
397  if (acctserver && acct_con)
398  //Listen for account server answers
399  checkAcctMsg( _sock_set );
400  //And send to it the login request we received
401  //Then send clients confirmations or errors
402  curtime = getNewTime();
403  if (acctserver && !acct_con && (curtime-reconnect_time) > periodrecon) {
404  std::string netbuf;
405  reconnect_time = curtime+periodrecon;
406  //We previously lost connection to account server
407  //We try to reconnect
408  if (acct_sock)
409  delete acct_sock;
410  acct_sock = new VsnetHTTPSocket( acctsrv, _sock_set );
411  if ( acct_sock->valid() ) {
412  LI i;
413  int j = 0;
414  COUT<<">>> Reconnected accountserver on socket "<<*acct_sock<<" done."<<endl;
415  //Send a list of ingame clients
416  //Build a buffer with number of clients and client serials
417  //Put first the number of clients
418  //netbuf.addShort( nbclients);
419  addSimpleChar( netbuf, ACCT_RESYNC );
420  for (j = 0, i = allClients.begin(); i != allClients.end(); i++, j++)
421  //Add the current client's serial to the buffer
422  addSimpleString( netbuf, (*i)->callsign );
423  //Passing NULL to AddressIP arg because between servers -> only TCP
424  //Use the serial packet's field to send the number of clients
425  if ( !acct_sock->sendstr( netbuf ) )
426  COUT<<"Failure to resync, SOCKET was : "<<*acct_sock<<endl;
427  } else {
428  cerr<<">>> Reconnection to account server failed."<<endl;
429  }
430  }
431  //See if we have some timed out clients and disconnect them
432  this->checkTimedoutClients_udp();
433 
434  //Remove all clients to be disconnected
435  LI j;
436  for (j = discList.begin(); j != discList.end(); j++)
437  disconnect( (*j), __FILE__, PSEUDO__LINE__( 328 ) );
438  discList.clear();
439  //Remove all clients that logged out
440  for (j = logoutList.begin(); j != logoutList.end(); j++)
441  this->logout( (*j) );
442  logoutList.clear();
443 
444  /****************************** VS STUFF TO DO ************************************/
445  //UPDATE STAR SYSTEM -> TO INTEGRATE WITH NETWORKING
446  //PROCESS JUMPS -> MAKE UNITS CHANGE THEIR STAR SYSTEM
447 
448 //NETFIXME: Why was StarSystem->Update() commented out?
449 
450  unsigned int i;
451  /*
452  * static float nonactivesystemtime = XMLSupport::parse_float (vs_config->getVariable ("physics","InactiveSystemTime",".3"));
453  * static unsigned int numrunningsystems = XMLSupport::parse_int (vs_config->getVariable ("physics","NumRunningSystems","4"));
454  * float systime=nonactivesystemtime;
455  */
456  for (i = 0; i < _Universe->star_system.size(); i++)
457 //NETFIXME: No Director for you!
458  _Universe->star_system[i]->Update( 1, true /*need to run python serverside*/ );
460  /****************************** VS STUFF TO DO ************************************/
461  if (snapchanged && (curtime-snaptime) > NETWORK_ATOM) {
462  //COUT<<"SENDING SNAPSHOT ----------"<<end;
463  //If planet time we send planet and nebula info
464  if ( (curtime-planettime) > PLANET_ATOM ) {
465  zonemgr->broadcastSnapshots( true );
466  planettime = curtime;
467  }
468  //Otherwise we just send ships/bases... info
469  else {
470  zonemgr->broadcastSnapshots( false );
471  }
472  snapchanged = 0;
473  snaptime = curtime;
474  }
476  //Check for automatic server status save time (in seconds)
477  //curtime = getNewTime();
478  if ( (curtime-savetime) > SAVE_ATOM ) {
479  //Not implemented
480  cout<<">>> Saving server status... Time="<<curtime<<endl;
481  this->save();
482  savetime = curtime;
483  cout<<"<<< Finished saving."<<endl;
484  }
485  _sock_set.waste_time( 0, 10000 );
486  }
487  delete CONFIGFILE;
488  delete vs_config;
489  vs_config = NULL;
490  this->closeAllSockets();
491 }

Friends And Related Function Documentation

friend class ZoneMgr
friend

Definition at line 200 of file netserver.h.

Member Data Documentation


The documentation for this class was generated from the following files: