28 COUT<<
" enter "<<__PRETTY_FUNCTION__<<endl;
31 string str_callsign, str_passwd;
43 if ( str_callsign.length() && str_passwd.length() ) {
51 COUT<<
"Send login for player <"<<str_callsign<<
">:<*******"
55 cerr<<
"Callsign and/or password not specified in vegastrike.config, please check this."<<endl<<endl;
69 COUT<<
"enter "<<
"NetClient::loginLoop"<<endl;
71 ship_select_list.clear();
84 COUT<<
"Sent login for player <"<<str_callsign<<
">:<*******"
87 this->callsign = str_callsign;
88 this->password = str_passwd;
95 int timeout = 0,
recv = 0;
101 timeval tv = {atoi( login_tostr.c_str() ), 0};
103 recv = this->recvMsg( &packet, &tv );
105 error =
"NETWORK ERROR : Login procedeure timed out.";
107 }
else if (
recv < 0) {
109 sprintf( str,
"NETWORK ERROR in recieving login data (error number %d)!!!",
122 COUT<<
"End of login loop"<<endl;
123 if (lastsave.empty() || lastsave[0] ==
"") {
124 if ( ship_select_list.empty() )
125 error =
"Login failure!";
126 return ship_select_list.size();
141 COUT<<
"enter "<<
"NetClient::loginAcctLoop"<<endl;
143 this->error_message = string();
151 COUT<<
"Buffering to send with LOGIN_DATA for "<<str_callsign<<endl;
162 int timeout = 0,
recv = 0;
171 int login_to = atoi( login_tostr.c_str() );
172 while (!timeout && !
recv) {
175 elapsed = newtime-initial;
177 if (elapsed > login_to) {
179 COUT<<
"!!! NETWORK ERROR : Connection to account server timed out !!!"<<endl;
183 recv = checkAcctMsg();
186 COUT<<
"End of loginAcct loop"<<endl;
188 if (lastsave.empty() || lastsave[0] !=
"") {
191 COUT<<
"Trying to connect to game server..."<<endl
192 <<
"\tIP="<<_serverip<<
":"<<_serverport<<endl;
200 ship_select_list.clear();
201 unsigned short numShips = netbuf.getShort();
202 ship_select_list.reserve( numShips );
203 for (
int i = 0;
i < numShips;
i++)
204 ship_select_list.push_back( netbuf.getString() );
209 using namespace VSFileSystem;
214 COUT<<
">>> LOGIN ACCEPTED =( serial #"<<
serial<<
" )= --------------------------------------"<<endl;
217 sprintf( msg,
"#cc66ffNETWORK: Login Accepted. Serial number is %d. Downloading system file...",
serial );
223 string datestr = netbuf.getString();
225 cerr<<
"Stardate initialized"<<endl;
226 cerr<<
"WE ARE ON STARDATE "<<datestr<<
" - converted : "
228 lastsave.push_back( netbuf.getString() );
229 lastsave.push_back( netbuf.getString() );
230 unsigned char *digest = 0;
231 unsigned short digest_length;
263 string sysfile = netbuf.getString();
264 bool downloadsystem =
true;
267 if ( fullsys.empty() ) fullsys = sysfile;
268 digest_length = netbuf.getShort();
269 COUT<<
"Initial system = "<<fullsys;
271 digest = netbuf.getBuffer( digest_length );
273 cerr<<
" - File Hash = "<<digest;
275 downloadsystem =
false;
279 this->zone = netbuf.getShort();
281 if (downloadsystem) {
282 cerr<<
": Downloading system from server..."<<endl;
284 _downloadManagerClient->addItem( &f );
285 timeval timeout = {10, 0};
287 if (recvMsg( NULL, &timeout ) <= 0)
318 addr = this->_serverip;
319 port = this->_serverport;
324 this->_serverip = addr;
325 this->_serverport = port;
331 if (use_acctserver) {
332 this->_serverport = port = 0;
334 "http://localhost/cgi-bin/accountserver.py" );
335 cout<<endl<<
"Account Server URL : "<<addr<<endl<<endl;
340 port_tmp = atoi( srvport.c_str() );
341 if (port_tmp > 65535 || port_tmp < 0)
343 port = (
unsigned short) port_tmp;
346 this->_serverip = addr;
347 this->_serverport = port;
348 cout<<endl<<
"Server IP : "<<addr<<
" - port : "<<srvport<<endl<<endl;
358 COUT<<
" enter "<<__PRETTY_FUNCTION__
359 <<
" with "<<addr<<endl;
363 cout<<
"Initializing connection to account server..."<<endl;
365 COUT<<
"accountserver on socket "<<acct_sock<<
" done."<<endl;
378 if ( clt_tcp_sock && clt_tcp_sock->
valid() ) clt_tcp_sock->
disconnect(
"NC_init_tcp" );
383 addr = _serverip.c_str();
386 COUT<<
" enter "<<__PRETTY_FUNCTION__
387 <<
" with "<<addr<<
":"<<port<<endl;
393 if (strnetatom ==
"")
398 this->lossy_socket = this->clt_tcp_sock;
399 if ( !clt_tcp_sock->
valid() )
400 return *this->clt_tcp_sock;
401 COUT<<
"created TCP socket ("<<addr<<
","<<port<<
") -> "<<this->clt_tcp_sock<<endl;
416 timeval tv = {atoi( login_tostr.c_str() ), 0};
420 int recvd = this->recvMsg( &packet, &tv );
422 error =
"Connection to game server timed out!";
424 }
else if (recvd < 0) {
426 sprintf( str,
"NETWORK ERROR in recieving socket (error number %d)!!!",
435 }
else if (this->netversion) {
439 if (!this->netversion) {
441 error =
"Unable to receive a valid version from this server.";
445 clt_tcp_sock->
disconnect(
"NCinit_timedout" );
446 return *this->clt_tcp_sock;
471 std::map< double, double >times;
476 if (clt_port_read > 65535 || clt_port_read <= 0)
479 if (clt_port_read_max > 65535 || clt_port_read_max <= 0)
480 clt_port_read_max = clt_port_read;
481 unsigned short clt_port = (
unsigned short) clt_port_read;
482 unsigned short clt_port_max = (
unsigned short) clt_port_read_max;
483 if (clt_port_max < clt_port)
484 clt_port_max = clt_port;
488 unsigned short port = this->_serverport;
493 while ( ( !this->clt_udp_sock->
valid() ) && (clt_port++) );
495 this->clt_udp_sock = udpsock;
497 COUT<<
"created UDP socket ("<<this->_serverip<<
","<<port<<
", listen on "<<clt_port<<
") -> "<<this->clt_udp_sock<<endl;
498 if (nettransport ==
"udp") {
500 COUT<<
"Default lossy transport configured to UDP."<<endl;
501 this->lossy_socket = clt_udp_sock;
503 COUT<<
"Default lossy transport configured to TCP (behind firewall)."<<endl;
504 this->lossy_socket = clt_tcp_sock;
511 while (i < NUM_TIMES && timeout < UDP_TIMEOUT) {
519 recv = this->recvMsg( &packet, &tv );
522 COUT<<
"synchronizeTime() Timed out"<<endl;
524 if (timeout >= UDP_TIMEOUT) {
525 if (this->lossy_socket->
isTcp() ==
false) {
526 if (clt_port < clt_port_max && !udpsock) {
530 COUT<<
"Trying UDP port "<<clt_port<<
"."<<endl;
533 this->lossy_socket = this->clt_tcp_sock;
535 COUT<<
"Setting default lossy transport to TCP (UDP timeout)."<<endl;
544 if (ping > 0 && ping < 1.) {
549 serverTime += initialTime-currentTime;
550 times.insert( std::multimap< double, double >::value_type( ping, serverTime-ping ) );
560 if (i >= NUM_TIMES) {
565 std::map< double, double >::const_iterator iter;
566 for (iter = times.begin(); iter != times.end(); ++iter) {
567 if (location == mid) {
568 median = iter->first;
571 median += iter->first;
579 for (iter = times.begin(); iter != times.end(); ++iter) {
580 double wdiff =
exp( -10*(median-iter->first)*(median-iter->first) );
581 pingavg += wdiff*iter->first;
582 timeavg += wdiff*iter->second;
588 COUT<<
"Error in time synchronization: connection ended or timed out.";
590 this->deltatime = pingavg;
591 double newTime = timeavg+
queryTime()-initialTime;
592 COUT<<
"Setting time to: New time: "<<newTime<<endl;
609 void NetClient::receiveLocations(
const Packet* )
614 COUT<<
"Nb start locations : "<<nblocs<<endl;
629 if (lastsave.empty() || lastsave[0] ==
"") {
632 netbuf.
addShort( (
unsigned short) ship );
633 if ( ship < ship_select_list.size() )
634 shipname = ship_select_list[ship];
641 if (ret != 1 || lastsave.size() < 2 || lastsave[0] ==
"") {
642 cout<<
"Error in CHOOSEHIP: "<<err
643 <<
"choice="<<ship<<
"("<<shipname<<
"), max="<<ret<<endl;
650 void NetClient::createChar()
657 cout<<
"NETWORK: Initializing..."<<endl;
665 string srvipadr =
vs_config->
getVariable(
"network",
"account_server_url",
"http://localhost/cgi-bin/accountserver.py" );
666 bootstrap_draw(
"#cc66ffNETWORK: Connecting to account server.", NULL );
667 cout<<
"NETWORK: Connecting to account server."<<endl;
669 vector< string > &savetmp =
loginAcctLoop( username, passwd );
671 if ( savetmp.size() >= 2 && savetmp[0].empty() ) {
676 bootstrap_draw(
"#cc66ffNETWORK: Connecting to VegaServer.", NULL );
677 cout<<
"NETWORK: Connecting to VegaServer."<<endl;
681 bootstrap_draw(
"#cc66ffNETWORK: Connecting to VegaServer.", NULL );
682 cout<<
"NETWORK: Connecting to VegaServer."<<endl;
683 ret =
init( srvipadr.c_str(), port,
error ).valid();
687 if ( error.empty() ) error =
"Network connection error";
688 if ( !this->error_message.empty() )
689 error +=
"\n"+this->error_message;
690 cout<<
"Error: "<<error<<endl;
693 cout<<
"Successfully connected!";
695 cout<<
"Waiting for player "<<username<<
": login response..."<<endl;
696 bootstrap_draw(
"#cc66ffNETWORK: Successful connection! Waiting to log in.", NULL );
697 int loggedin =
loginAuth( username, passwd, error );
698 if ( !this->error_message.empty() ) {
699 cout<<
"Warning: "<<this->error_message<<endl;
700 if ( !error.empty() )
702 error += this->error_message;
717 vector< string >savedships;
722 bool setplayerXloc =
false;
724 vector< StarSystem* >ss;
725 vector< QVector > playerNloc;
726 vector< string >playersaveunit;
727 vector< vector< string > >vecstr;
729 bootstrap_draw(
"#cc66ffNETWORK: Checking for UDP connection.", NULL );
730 cout<<
"NETWORK: Checking for UDP connection."<<endl;
732 cout<<
" logged in !"<<endl;
735 cout<<
"NETWORK: Loading player ship."<<endl;
754 playersaveunit.push_back( savedships[0] );
756 playerNloc.push_back( pos );
758 playerNloc.push_back(
QVector( FLT_MAX, FLT_MAX, FLT_MAX ) );
759 vecstr.push_back( lastsave );
762 cout<<
"NETWORK: Loading system."<<endl;