diff --git a/source/glest_game/menu/menu_state_join_game.cpp b/source/glest_game/menu/menu_state_join_game.cpp index 60fff5a12..477912cef 100644 --- a/source/glest_game/menu/menu_state_join_game.cpp +++ b/source/glest_game/menu/menu_state_join_game.cpp @@ -264,10 +264,19 @@ void MenuStateJoinGame::DiscoveredServers(std::vector serverList) { buttonAutoFindServers.setEnabled(true); buttonConnect.setEnabled(true); if(serverList.empty() == false) { + Config &config= Config::getInstance(); string bestIPMatch = ""; + int serverGamePort = config.getInt("ServerPort",intToStr(GameConstants::serverPort).c_str()); std::vector localIPList = Socket::getLocalIPAddressList(); for(int idx = 0; idx < serverList.size(); idx++) { + + vector paramPartPortsTokens; + Tokenize(serverList[idx],paramPartPortsTokens,":"); + if(paramPartPortsTokens.size() >= 2 && paramPartPortsTokens[1].length() > 0) { + serverGamePort = strToInt(paramPartPortsTokens[1]); + } + bestIPMatch = serverList[idx]; if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestIPMatch = [%s] localIPList[0] = [%s]\n",__FILE__,__FUNCTION__,__LINE__,bestIPMatch.c_str(),localIPList[0].c_str()); @@ -277,6 +286,9 @@ void MenuStateJoinGame::DiscoveredServers(std::vector serverList) { } } + if(bestIPMatch != "") { + bestIPMatch += ":" + intToStr(serverGamePort); + } labelServerIp.setText(bestIPMatch); if(serverList.size() > 1) { diff --git a/source/shared_lib/include/platform/posix/socket.h b/source/shared_lib/include/platform/posix/socket.h index 09b96a6b5..a4569710a 100644 --- a/source/shared_lib/include/platform/posix/socket.h +++ b/source/shared_lib/include/platform/posix/socket.h @@ -220,9 +220,10 @@ class BroadCastSocketThread : public BaseThread private: Mutex *mutexPauseBroadcast; bool pauseBroadcast; + int boundPort; public: - BroadCastSocketThread(); + BroadCastSocketThread(int boundPort); virtual ~BroadCastSocketThread(); virtual void execute(); virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false); diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index 1b4c509c2..eaea69ae7 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -1999,8 +1999,17 @@ void BroadCastClientSocketThread::execute() { string fromIP = szHostFrom; if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"broadcast message received: [%s] from: [%s]\n", buff,fromIP.c_str() ); + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Client got broadcast from server: [%s] data [%s]\n",fromIP.c_str(),buff); + + int serverGamePort = Socket::getBroadCastPort(); + vector paramPartPortsTokens; + Tokenize(buff,paramPartPortsTokens,":"); + if(paramPartPortsTokens.size() >= 3 && paramPartPortsTokens[2].length() > 0) { + serverGamePort = strToInt(paramPartPortsTokens[2]); + } + if(std::find(foundServers.begin(),foundServers.end(),fromIP) == foundServers.end()) { - foundServers.push_back(fromIP); + foundServers.push_back(fromIP + ":" + intToStr(serverGamePort)); } // For now break as soon as we find a server @@ -2144,7 +2153,7 @@ void ServerSocket::startBroadCastThread() { stopBroadCastThread(); - broadCastThread = new BroadCastSocketThread(); + broadCastThread = new BroadCastSocketThread(this->boundPort); //printf("Start broadcast thread [%p]\n",broadCastThread); @@ -2672,10 +2681,11 @@ void UPNP_Tools::NETremRedirects(int ext_port) { // Description: To be forked in its own thread to send out a broadcast to the local subnet // the current broadcast message is // -BroadCastSocketThread::BroadCastSocketThread() : BaseThread() { +BroadCastSocketThread::BroadCastSocketThread(int boundPort) : BaseThread() { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); mutexPauseBroadcast = new Mutex(); setPauseBroadcast(false); + this->boundPort = boundPort; //printf("new broadcast thread [%p]\n",this); } @@ -2792,6 +2802,8 @@ void BroadCastSocketThread::execute() { //sprintf(buff,"%s:%s",buff,ipList[idx1].c_str()); strcat(buff,":"); strcat(buff,ipList[idx1].c_str()); + strcat(buff,":"); + strcat(buff,intToStr(this->boundPort).c_str()); } if(difftime((long int)time(NULL),elapsed) >= 1 && getQuitStatus() == false) { @@ -2801,6 +2813,9 @@ void BroadCastSocketThread::execute() { if(pauseBroadCast == false) { // Broadcast the packet to the subnet //if( sendto( bcfd, buff, sizeof(buff) + 1, 0 , (struct sockaddr *)&bcaddr, sizeof(struct sockaddr_in) ) != sizeof(buff) + 1 ) + + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Server is sending broadcast data [%s]\n",buff); + ssize_t send_res = sendto( bcfd[idx], buff, buffMaxSize, 0 , (struct sockaddr *)&bcLocal[idx], sizeof(struct sockaddr_in) ); if( send_res != buffMaxSize ) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Sendto error: %s\n", getLastSocketErrorFormattedText().c_str());