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_acct.cpp
Go to the documentation of this file.
1 #include "networking/netserver.h"
2 #include "networking/zonemgr.h"
4 #include "vsfilesystem.h"
7 #include "networking/fileutil.h"
9 extern int acct_con;
10 
11 /*
12  *************************************************************
13  **** Check account server activity ***
14  *************************************************************
15  */
16 
17 void NetServer::checkAcctMsg( SocketSet &sets )
18 {
19  AddressIP ipadr;
20  ClientPtr clt;
21  unsigned char cmd = 0;
22  //Watch account server socket
23  //Get the number of active clients
24  if ( !acct_sock || acct_sock->isActive() ) {
25  //COUT<<"Net activity !"<<endl;
26  //Receive packet and process according to command
27 
28  std::string p;
29  if (acct_sock && acct_sock->recvstr( p ) != 0 && p.length() != 0) {
30  //Maybe copy that in a "else" condition too if when it fails we
31  //have to disconnect a client
32  //Here we get the latest client which asked for a login
33  //Since coms between game servers and account server are TCP
34  //the order of request/answers
35  //should be ok and we can use a "queue" for waiting clients
36  if ( !p.empty() ) cmd = getSimpleChar( p );
37  string ptemp( p );
38  string username = getSimpleString( ptemp );
39  string server, port;
40  if (cmd == ACCT_SUCCESS) {
41  server = getSimpleString( ptemp );
42  port = getSimpleString( ptemp );
43  }
44  std::map< std::string, WaitListEntry >::iterator iter = waitList.find( username );
45  if (waitList.end() != iter) {
46  WaitListEntry entry( (*iter).second );
47  char flags = 0;
48  if (entry.tcp) {
49  clt = entry.t;
50  COUT<<"Got response for TCP client"<<endl;
51  } else {
52  ipadr = entry.u;
53  COUT<<"Got response for client IP : "<<ipadr<<endl;
54  }
55  if (entry.type == (int) WaitListEntry::CONNECTING && cmd != ACCT_SUCCESS && cmd != 0) {
56  waitList.erase( iter );
57  if (clt) clt->loginstate = Client::CONNECTED;
58  ObjSerial serial = 0;
59  switch (cmd)
60  {
61  case ACCT_LOGIN_NEW:
62  COUT<<">>> NEW LOGIN =( serial #"<<serial<<" )= --------------------------------------"<<endl;
63  //We received a login authorization for a new account (no ship created)
64  this->loginAccept( p, clt, 1, flags );
65  COUT<<"<<< NEW LOGIN ----------------------------------------------------------------"<<endl;
66  break;
67  case ACCT_LOGIN_ACCEPT:
68  //Login is ok
69  COUT<<">>> LOGIN ACCEPTED =( serial #"<<serial<<" )= --------------------------------------"<<endl;
70  loginAccept( p, clt, 0, flags );
71  COUT<<"<<< LOGIN ACCEPTED -----------------------------------------------------------"<<endl;
72  break;
73  case ACCT_LOGIN_ERROR:
74  COUT<<">>> LOGIN ERROR =( DENIED )= --------------------------------------"<<endl;
75  //Login error -> disconnect
76  this->sendLoginError( clt );
77  COUT<<"<<< LOGIN ERROR ---------------------------------------------------"<<endl;
78  break;
79  case ACCT_LOGIN_ALREADY:
80  COUT<<">>> LOGIN ALREADY =( ALREADY LOGGED IN -> serial #"<<serial
81  <<" )= --------------------------------------"<<endl;
82  //Client already logged in -> disconnect
83  this->sendLoginAlready( clt );
84  COUT<<"<<< LOGIN ALREADY --------------------------------------------------------------"<<endl;
85  break;
86  default:
87  COUT<<">>> UNKNOWN COMMAND =( "<<(unsigned int) cmd<<" )= --------------------------------------"
88  <<endl<<"Full datastream was:"<<p<<endl;
89  }
90  } else if (entry.type == (int) WaitListEntry::JUMPING) {
91  waitList.erase( iter );
92  sendJumpFinal( clt, server, (unsigned short) ( atoi( port.c_str() ) ) );
93  }
94  }
95  } else {
96  cerr<<"Connection to account server lost !!"<<endl;
97 //acct_con = 0;
98  }
99  }
100 }
101 
102 /*
103  *************************************************************
104  **** Save the server state ***
105  *************************************************************
106  */
107 
108 //For now it only save units and player saves
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 }
131 
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 }
184