80 typedef vector< Client* >::iterator
VC;
81 typedef vector< ObjSerial >::iterator
ObjI;
87 extern bool isWeapon( std::string name );
102 if ( sernum == (*
i) )
120 latest_timestamp = 0;
133 lastsave.resize( 0 );
138 callsign = password =
"";
139 this->Clients = ClientsMap();
152 latest_timestamp = 0;
175 if (NetComm != NULL) {
179 if (clt_tcp_sock)
delete clt_tcp_sock;
189 int NetClient::checkAcctMsg()
199 if ( acct_sock->
recvstr( packeta ) !=
false && packeta.length() ) {
201 std::string netbuf = packeta;
207 COUT<<
">>> LOGIN DATA --------------------------------------"<<endl;
211 this->error_message = warning;
214 const char *srvport = srvportstr.c_str();
215 int porttemp = atoi( srvport );
216 if (porttemp > 65535) porttemp = 0;
217 if (porttemp < 0) porttemp = 0;
218 this->_serverport = (
unsigned short) porttemp;
219 COUT<<
"<<< LOGIN DATA --------------------------------------"<<endl;
223 COUT<<
">>> LOGIN ERROR =( DENIED )= --------------------------------------"<<endl;
224 lastsave.resize( 0 );
227 lastsave.push_back(
"" );
228 if ( !warning.empty() )
229 lastsave.push_back( warning );
231 lastsave.push_back(
"Failed to login with this password!" );
234 COUT<<
">>> LOGIN ERROR =( ALREADY LOGGED IN )= --------------------------------------"<<endl;
235 lastsave.resize( 0 );
238 lastsave.push_back(
"" );
239 if ( !warning.empty() )
240 lastsave.push_back( warning );
242 lastsave.push_back(
"The account is already logged in to a server!" );
245 COUT<<
">>> UNKNOWN COMMAND =( "<<std::hex<<packeta<<std::dec<<
" )= --------------------------------------"
247 lastsave.resize( 0 );
248 lastsave.push_back(
"" );
249 lastsave.push_back(
"!!! PROTOCOL ERROR : Unexpected command received !!!" );
253 sprintf( str,
"!!! NETWORK ERROR : Connection to account server lost (error number %d)!!!",
335 void NetClient::versionBuf(
NetBuffer &buf )
const
370 string jpeg_str(
"" );
375 ret = recvMsg( outpacket, &tv );
382 if ( NetComm != NULL && NetComm->
IsActive() )
384 NetComm->
SendSound( *this->clt_tcp_sock, this->serial );
392 unsigned int whichcp;
398 cerr<<
"Error could not find blank cockpit to respawn into\n";
404 vector< string > packedInfo;
419 string fullsysname = mysystem+
".system";
426 {NULL, NULL, NULL, NULL, NULL, NULL, NULL}
431 savedtextures = tmp->
Cache();
450 "" ), cp->
fg, fgsnumber, &lastsave[1], newserial );
453 cp->
SetParent( un, unkeyname.c_str(),
"", pos );
472 int NetClient::recvMsg(
Packet *outpacket, timeval *timeout )
480 using namespace VSFileSystem;
483 static vector< Mount::STATUS >backupMountStatus;
487 unsigned int mount_num;
490 if (this->game_unit.GetUnit() != NULL)
491 local_serial = this->game_unit.GetUnit()->
GetSerial();
499 static bool udpgetspriority =
true;
500 bool wasudp = udpgetspriority;
503 int recvbytes = (udpgetspriority ? clt_udp_sock : clt_tcp_sock)->recvbuf( &p1, &ipadr );
504 if (recvbytes <= 0) {
505 recvbytes = (udpgetspriority ==
false ? clt_udp_sock : clt_tcp_sock)->recvbuf( &p1, &ipadr );
506 wasudp = !udpgetspriority;
508 if (recvbytes <= 0) {
510 clt_tcp_sock->addToSet( _sock_set );
511 clt_udp_sock->addToSet( _sock_set );
512 int socketstat = _sock_set.wait( timeout );
513 if ( !clt_tcp_sock->valid() ) {
514 perror(
"Error socket closed " );
515 clt_tcp_sock->disconnect(
"socket error closed" );
519 if (socketstat < 0) {
520 perror(
"Error select -1 " );
521 clt_tcp_sock->disconnect(
"socket error recv err" );
531 recvbytes = (udpgetspriority ? clt_udp_sock : clt_tcp_sock)->recvbuf( &p1, &ipadr );
532 wasudp = udpgetspriority;
533 if (recvbytes <= 0) {
534 recvbytes = (udpgetspriority ==
false ? clt_udp_sock : clt_tcp_sock)->recvbuf( &p1, &ipadr );
535 wasudp = !udpgetspriority;
537 udpgetspriority = !udpgetspriority;
538 if (recvbytes <= 0) {
540 perror(
"Error recv -1 " );
541 clt_tcp_sock->disconnect(
"socket error recv" );
552 NetBuffer netbuf( p1.getData(), p1.getDataLength() );
553 versionBuf( netbuf );
557 Cmd cmd = p1.getCommand();
559 COUT<<
"Rcvd "<<(wasudp ?
"UDP" :
"TCP")<<
": "<<cmd<<
" from serial "<<packet_serial<<endl;
565 ObjSerial server_netversion = netbuf.getSerial();
566 this->netversion = server_netversion;
567 string ipaddress = netbuf.getString();
569 <<
"VegaServer "<<server_netversion<<
" from address "<<ipaddress<<endl;
574 cout<<
"Connected to an old server. Setting version to "<<server_netversion<<endl;
591 filename = netbuf.getString();
592 file = netbuf.getString();
594 if (packet_serial == local_serial) {
596 cerr<<
"RECEIVING file : "<<filename<<endl;
600 cerr<<
"!!! ERROR : opening received file !!!"<<endl;
603 if ( f.
Write( file ) != file.length() ) {
604 cerr<<
"!!! ERROR : writing received file !!!"<<endl;
616 if (_downloadManagerClient)
617 _downloadManagerClient->processCmdDownload( *clt_tcp_sock, netbuf );
621 COUT<<
">>> LOGIN ERROR =( DENIED )= ------------------------------------------------"<<endl;
623 lastsave.push_back(
"" );
624 lastsave.push_back(
"!!! ACCESS DENIED : Account does not exist with this password !!!" );
629 COUT<<
">>> ACCOUNT SERVER UNAVAILABLE ------------------------------------------------"<<endl;
630 lastsave.push_back(
"" );
631 lastsave.push_back(
"!!! ACCESS DENIED : Account server unavailable !!!" );
646 this->receiveLocations( &p1 );
650 string sender = netbuf.getString();
651 string message = netbuf.getString();
657 un = this->game_unit.GetUnit();
659 string cmd = netbuf.getString();
660 string args = netbuf.getString();
661 string id = netbuf.getString();
667 if (nostarsystem)
break;
672 unsigned int numUnits = p1.getSerial();
673 unsigned int timestamp = p1.getTimestamp();
674 double deltatime = netbuf.getFloat();
678 this->receivePositions( numUnits, timestamp, netbuf, deltatime );
682 if (nostarsystem)
break;
691 if (nostarsystem)
break;
692 COUT<<
">>> "<<local_serial<<
" >>> EXITING CLIENT =( serial #"
693 <<packet_serial<<
" )= --------------------------------------"<<endl;
694 this->removeClient( &p1 );
698 if (nostarsystem)
break;
699 COUT<<
">>> "<<local_serial<<
" >>> ADDED IN GAME =( serial #"
700 <<packet_serial<<
" )= --------------------------------------"<<endl;
702 if (this->game_unit.GetUnit() == NULL)
703 this->
Respawn( packet_serial );
704 this->game_unit.GetUnit()->curr_physical_state = netbuf.getTransformation();
708 COUT<<
">>> "<<local_serial<<
" >>> DISCONNECTED -> Client killed =( serial #"
709 <<packet_serial<<
" )= --------------------------------------"<<endl;
721 if (nostarsystem)
break;
723 float energy = netbuf.getFloat();
724 mis = netbuf.getSerial();
725 mount_num = netbuf.getInt32();
727 if (p1.getSerial() == local_serial)
728 un = this->game_unit.GetUnit();
734 ::iterator
i = un->mounts.begin();
735 if ( mount_num > un->mounts.size() )
736 mount_num = un->mounts.size();
740 for (j = backupMountStatus.size(); j < un->mounts.size(); ++j)
742 for (j = 0; i != un->mounts.end(); ++i, ++j) {
743 backupMountStatus[j] = (*i).status;
748 for (j = 0; j < mount_num; ++j) {
749 unsigned int mnt = (
unsigned int)netbuf.getInt32();
750 if (mnt < un->mounts.size() && mnt >= 0) {
755 un->mounts[mnt].serial = mis;
764 i = un->mounts.begin();
765 for (j = 0; i != un->mounts.end(); ++i, ++j)
766 (*i).status = backupMountStatus[j];
768 COUT<<
"!!! Problem -> CANNOT FIRE UNIT NOT FOUND !!!"<<endl;
773 if (nostarsystem)
break;
775 mount_num = netbuf.getInt32();
782 ::iterator i = un->
mounts.begin();
784 if ( mount_num > un->mounts.size() )
785 mount_num = un->mounts.size();
786 for (j = backupMountStatus.size(); j < un->mounts.size(); ++j)
788 for (j = 0; i != un->mounts.end(); ++i, ++j) {
789 backupMountStatus[j] = (*i).status;
793 for (j = 0; j < mount_num; ++j) {
794 unsigned int mnt = (
unsigned int)netbuf.getInt32();
795 if (mnt < un->mounts.size() && mnt >= 0) {
799 un->mounts[mnt].serial = 0;
805 i = un->mounts.begin();
806 for (j = 0; i != un->mounts.end(); ++i, ++j)
807 (*i).status = backupMountStatus[j];
809 COUT<<
"!!! Problem -> CANNOT UNFIRE UNIT NOT FOUND !!!"<<endl;
813 if (nostarsystem)
break;
816 unsigned short targserial = netbuf.getSerial();
819 COUT<<
"Confirmed targeting unit "<<target_un->
name<<
" ("<<targserial<<
")."<<endl;
821 if ( oldtarg && oldtarg->
GetSerial() == 0 && (target_un == NULL || target_un->
GetSerial() == 0) )
822 COUT<<
"Setting target from "<<oldtarg->
name<<
" to NULL."<<endl;
825 un->computer.target.SetUnit( target_un );
829 if (nostarsystem)
break;
840 if (nostarsystem)
break;
846 unsigned int nbupdates = packet_serial;
848 int offset = netbuf.getOffset();
849 for (
unsigned int i = 0; i < nbupdates; i++) {
850 serial = netbuf.getSerial();
851 int noffset = netbuf.getOffset();
852 if (noffset == offset) {
853 COUT<<
"ERROR Premature end of Snapshot buffer "<<std::hex<<std::string(
854 netbuf.getData(), netbuf.getSize() )<<std::dec<<std::endl;
859 receiveUnitDamage( netbuf, un );
866 if (nostarsystem)
break;
867 float amt = netbuf.getFloat();
868 float ppercentage = netbuf.getFloat();
869 float spercentage = netbuf.getFloat();
870 Vector pnt = netbuf.getVector();
871 Vector normal = netbuf.getVector();
874 float hul = netbuf.getFloat();
875 Shield sh = netbuf.getShield();
876 Armor ar = netbuf.getArmor();
880 un->ApplyNetDamage( pnt, normal, amt, ppercentage, spercentage, col );
888 COUT<<
"!!! Problem -> CANNOT APPLY DAMAGE UNIT NOT FOUND !!!"<<endl;
909 if (nostarsystem)
break;
910 ClientPtr clt = Clients.get( p1.getSerial() );
916 un->Kill(
true,
true );
918 COUT<<
"!!! Problem -> CANNOT KILL UNIT NOT FOUND !!!"<<endl;
921 un = clt->game_unit.GetUnit();
924 Clients.remove( p1.getSerial() );
931 un->Kill(
true,
true );
934 COUT<<
"Client #"<<p1.getSerial()<<
" killed - now "<<nbclients<<
" clients in system"<<endl;
935 if ( !clt->callsign.empty() ) {
936 string msg = clt->callsign+
" has died.";
944 Unit *un = this->game_unit.GetUnit();
947 if (cpnum >= 0 && this->lastsave.size() >= 2)
953 if (nostarsystem)
break;
955 std::string srvipadr( netbuf.getString() );
956 unsigned short port( netbuf.getShort() );
962 string newsystem = netbuf.getString();
964 ObjSerial jumpserial = netbuf.getSerial();
965 unsigned short zoneid = netbuf.getShort();
966 un = this->game_unit.GetUnit();
970 if ( !( sts = star_system_table.
Get( newsystem ) ) ) {
972 cerr<<
"!!! FATAL ERROR : Couldn't find destination Star system !!!"<<endl;
979 if ( packet_serial == un->
GetSerial() ) {
981 this->ingame =
false;
987 sts->
JumpTo( un, jumpun, newsystem,
true );
988 string sysfile( newsystem+
".system" );
990 _downloadManagerClient->addItem( &f );
992 timeval timeout = {10, 0};
994 if (recvMsg( NULL, &timeout ) <= 0) {
1004 sts->
JumpTo( un, jumpun, newsystem,
true );
1010 if (nostarsystem)
break;
1013 while ( ( ser = netbuf.getSerial() ) != 0 ) {
1024 float mass = netbuf.getFloat();
1025 float cargvol = netbuf.getFloat();
1026 float upgvol = netbuf.getFloat();
1032 unsigned int numcargo = (
unsigned int)netbuf.getInt32();
1036 numcargo = -numcargo;
1039 for (i = 0; i < numcargo; i++) {
1040 unsigned int mplind;
1041 unsigned int quantity = (
unsigned int)netbuf.getInt32();
1042 string str = netbuf.getString();
1054 carg.
SetPrice( netbuf.getFloat() );
1055 carg.
SetMass( netbuf.getFloat() );
1065 un = this->game_unit.GetUnit();
1067 if (cp == -1)
break;
1068 unsigned short type = netbuf.getShort();
1069 string qualname = netbuf.getString();
1070 int pos = netbuf.getInt32();
1076 fprintf( stderr,
"Failed to find and terminate mission %d for player %d\n", pos, cp );
1083 LoadMission(
"",
"import Director; temp=Director.Mission()",
false );
1084 string::size_type tpos = qualname.find(
'/' );
1085 string cat = qualname.substr( 0, tpos );
1095 if (nostarsystem)
break;
1097 Unit *to = game_unit.GetUnit();
1098 unsigned int curstate = netbuf.getInt32();
1100 COUT<<
"Received invalid comm message "<<curstate<<
" from "<<packet_serial<<endl;
1104 COUT<<
"Received comm message while dead."<<endl;
1108 if ( curstate >= 0 && curstate < fsm->nodes.size() ) {
1109 unsigned char sex = 0;
1113 c.SetCurrentState( curstate, NULL, sex );
1122 float relfrompilot = netbuf.getFloat();
1123 float reltopilot = netbuf.getFloat();
1127 (*i).second = relfrompilot;
1132 (*i).second = reltopilot;
1138 if (nostarsystem)
break;
1139 ObjSerial buyer_ser = netbuf.getSerial();
1140 ObjSerial seller_ser = netbuf.getSerial();
1141 int quantity = netbuf.getInt32();
1142 std::string cargoName = netbuf.getString();
1143 float price = netbuf.getFloat();
1144 float mass = netbuf.getFloat();
1145 float volume = netbuf.getFloat();
1146 int mountOffset = ( (
int) netbuf.getInt32() );
1147 int subunitOffset = ( (
int) netbuf.getInt32() );
1151 bool missioncarg =
false;
1153 unsigned int cargIndex = 0;
1154 Cargo *cargptr = NULL;
1158 cargptr = seller->
GetCargo( cargoName, cargIndex );
1164 Cargo carg = *cargptr;
1167 bool weapon =
false;
1172 else if (!quantity && buyer == sender)
1176 missioncarg = (mountOffset == 1 && subunitOffset == 1);
1189 if ( upgrade && !repair && (seller == sender || buyer == sender) ) {
1196 string templateName;
1198 if (seller == sender) {
1199 templateName = unitDir+
".blank";
1201 }
else if (buyer == sender) {
1203 templateName = unitDir+
".template";
1206 if (!unitCarg)
break;
1209 if (!templateUnit) {
1215 templateUnit = NULL;
1218 if (seller == sender) {
1220 if ( seller->
canDowngrade( unitCarg, mountOffset, subunitOffset, percent, templateUnit ) )
1221 seller->
Downgrade( unitCarg, mountOffset, subunitOffset, percent, templateUnit );
1222 }
else if (buyer == sender) {
1224 if ( buyer->
canUpgrade( unitCarg, mountOffset, subunitOffset, multAddMode,
true, percent, templateUnit ) )
1225 buyer->
Upgrade( unitCarg, mountOffset, subunitOffset, multAddMode,
true, percent, templateUnit );
1230 Unit *player = game_unit.GetUnit();
1234 || ( seller && seller->
isDocked( player ) ) || player == buyer || player == seller ) )
1241 cp->
credits = netbuf.getFloat();
1246 un = this->game_unit.GetUnit();
1248 if (cp == -1)
break;
1249 unsigned short type = netbuf.getShort();
1252 float floatValue = 0;
1257 key = netbuf.getString();
1258 pos = netbuf.getInt32();
1260 int missionnum = netbuf.getInt32();
1263 if (activeMis == NULL) activeMis =
mission;
1266 if ( (type&Subcmd::StringValue) || (type&Subcmd::Objective) )
1267 strValue = netbuf.getString();
1268 if ( (type&Subcmd::FloatValue) || (type&Subcmd::Objective) )
1269 floatValue = netbuf.getFloat();
1273 case (Subcmd::FloatValue|Subcmd::SetValue):
1274 while (
getSaveDataLength( cp, key ) <= (
unsigned int) pos && (
unsigned int) pos != UINT_MAX)
1285 case (Subcmd::StringValue|Subcmd::SetValue):
1286 while (
getSaveStringLength( cp, key ) <= (
unsigned int) pos && (
unsigned int) pos != UINT_MAX)
1297 case (Subcmd::Objective|Subcmd::SetValue):
1298 mission = activeMis;
1299 while (mission->objectives.size() <= (
unsigned int) pos && (
unsigned int) pos != UINT_MAX)
1307 mission = activeMis;
1308 if ( (
unsigned int) pos < mission->objectives.size() && (
unsigned int) pos >= 0 && (
unsigned int) pos
1322 float freq = netbuf.getFloat();
1323 char secured = netbuf.getChar();
1324 char webc = netbuf.getChar();
1325 char pa = netbuf.getChar();
1326 if (freq == current_freq) {
1327 if ( secured == NetComm->IsSecured() ) {
1330 if (packet_serial != this->serial) {
1332 clt = Clients.get( packet_serial );
1334 clt->portaudio = pa;
1335 NetComm->AddToSession( clt );
1338 cerr<<
"WARNING : Received a STARTCOMM from a channel not in the same mode"<<endl;
1341 cerr<<
"WARNING : Received a STARTCOMM from another frequency"<<endl;
1351 if (packet_serial != this->serial) {
1353 clt = Clients.get( packet_serial );
1354 NetComm->RemoveFromSession( clt );
1362 NetComm->RecvSound( p1.getData(), p1.getDataLength(), false );
1369 NetComm->RecvSound( p1.getData(), p1.getDataLength(), true );
1378 string msg( p1.getData() );
1379 NetComm->RecvMessage( msg,
false );
1386 string msg( p1.getData() );
1387 NetComm->RecvMessage( msg,
true );
1393 if (nostarsystem)
break;
1394 ObjSerial utdw_serial = netbuf.getSerial();
1397 unsigned int dockport;
1402 cerr<<
"CMD_DOCK: All docking ports used up! Kicking out port 0!"<<endl;
1406 cerr<<
"RECEIVED A DOCK AUTHORIZATION for unit "<<p1.getSerial()<<
" to unit "<<utdw_serial
1407 <<
" at docking port #"<<dockport<<endl;
1415 if (nostarsystem)
break;
1416 ObjSerial utdw_serial = netbuf.getSerial();
1417 cerr<<
"RECEIVED A UNDOCK ORDER for unit "<<p1.getSerial()<<
" to unit "<<utdw_serial<<endl;
1425 if (nostarsystem)
break;
1429 un = this->game_unit.GetUnit();
1440 COUT<<
">>> "<<local_serial<<
" >>> UNKNOWN COMMAND =( "<<std::hex<<cmd
1441 <<
" )= --------------------------------------"<<std::endl;
1456 void NetClient::disconnect()
1468 if (clt_tcp_sock->
valid() && clt_tcp_sock->
get_fd() != -1) {
1475 timeval tv = {10, 0};
1478 clt_tcp_sock->
disconnect(
"Closing connection to server" );
1487 else if (lossy_socket == clt_udp_sock)
1488 return clt_udp_sock;
1504 vector< string > usernames;
1505 vector< string > passwords;
1506 vector< SOCKETALT* >udp;
1511 usernames.push_back(
Network[i].callsign );
1512 passwords.push_back(
Network[i].password );
1515 udp.push_back( udpsocket );
1517 udp.push_back( NULL );
1528 if ( !srvipadr.empty() )
1533 if (response == 0) {
1534 COUT<<
"Network login error: \n"<<err<<endl;
1540 COUT<<
"Failed to get a ship";
1544 cout<<
" logged in !"<<endl;
1564 ClientPtr NetClient::ClientsMap::get(
int x )
1567 if ( it == _map.end() )
return ClientPtr();
1571 bool NetClient::ClientsMap::remove(
int x )
1573 size_t s = _map.erase( x );
1574 if (s == 0)
return false;
1587 clt->elapsed_since_packet += addtime;
1588 trans = clt->prediction->Interpolate( un, clt->elapsed_since_packet );