00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <time.h>
00022 #include <server/ServerRegistration.h>
00023 #include <server/ScorchedServer.h>
00024 #include <common/Logger.h>
00025 #include <common/OptionsMasterListServer.h>
00026 #include <common/OptionsScorched.h>
00027 #include <common/Defines.h>
00028
00029 ServerRegistration *ServerRegistration::instance_ = 0;
00030
00031 ServerRegistration *ServerRegistration::instance()
00032 {
00033 if (!instance_)
00034 {
00035 instance_ = new ServerRegistration();
00036 }
00037 return instance_;
00038 }
00039
00040 ServerRegistration::ServerRegistration() :
00041 mainServer_(
00042 OptionsMasterListServer::instance()->getMasterListServer(),
00043 OptionsMasterListServer::instance()->getMasterListServerURI()),
00044 backupServer_(
00045 OptionsMasterListServer::instance()->getMasterListBackupServer(),
00046 OptionsMasterListServer::instance()->getMasterListBackupServerURI())
00047 {
00048 }
00049
00050 ServerRegistration::~ServerRegistration()
00051 {
00052 }
00053
00054 void ServerRegistration::start()
00055 {
00056 mainServer_.start();
00057 backupServer_.start();
00058 }
00059
00060 ServerRegistrationEntry::ServerRegistrationEntry(
00061 const char *masterListServer,
00062 const char *masterListServerURI) :
00063 netServer_(new NetServerHTTPProtocolSend),
00064 masterListServer_(masterListServer)
00065 {
00066 std::string buffer = S3D::formatStringBuffer(
00067 "GET %s/servers.php?register=%s&port=%i HTTP/1.0\r\n"
00068 "User-Agent: Scorched3D\r\n"
00069 "Host: %s\r\n"
00070 "Connection: close\r\n"
00071 "\r\n"
00072 "\r\n",
00073 masterListServerURI,
00074 ScorchedServer::instance()->getOptionsGame().getPublishAddress(),
00075 ScorchedServer::instance()->getOptionsGame().getPortNo(),
00076 masterListServer);
00077 sendNetBuffer_.addDataToBuffer(buffer.c_str(), (int) buffer.size());
00078
00079 netServer_.setMessageHandler(this);
00080 }
00081
00082 ServerRegistrationEntry::~ServerRegistrationEntry()
00083 {
00084 }
00085
00086 void ServerRegistrationEntry::start()
00087 {
00088 SDL_CreateThread(ServerRegistrationEntry::threadFunc, (void *) this);
00089 }
00090
00091 int ServerRegistrationEntry::threadFunc(void *param)
00092 {
00093 ServerRegistrationEntry *entry =
00094 (ServerRegistrationEntry *) param;
00095
00096 entry->actualThreadFunc();
00097 return 0;
00098 }
00099
00100 void ServerRegistrationEntry::actualThreadFunc()
00101 {
00102 const int TimeBetweenRegistrations = 540;
00103
00104 for (;;)
00105 {
00106
00107 netServer_.disconnectAllClients();
00108 netServer_.processMessages();
00109
00110
00111 success_ = false;
00112 finished_ = false;
00113
00114 Logger::log(S3D::formatStringBuffer(
00115 "Connecting to registration server %s...", masterListServer_));
00116 if (registerGame())
00117 {
00118 time_t lastTime = time(0);
00119 for (;;)
00120 {
00121
00122 SDL_Delay(1000);
00123 netServer_.processMessages();
00124
00125
00126 if (finished_) break;
00127
00128
00129 int timeOut =
00130 OptionsMasterListServer::instance()->getMasterListServerTimeout();
00131 time_t currentTime = time(0);
00132 if (currentTime - lastTime > timeOut)
00133 {
00134 break;
00135 }
00136 }
00137
00138 Logger::log(S3D::formatStringBuffer("Registration to %s %s.",
00139 masterListServer_,
00140 (success_?"was successfull":"failed")));
00141 }
00142 else
00143 {
00144 Logger::log(S3D::formatStringBuffer(
00145 "Failed to connect to registration server %s", masterListServer_));
00146 }
00147
00148
00149
00150 int waitTime = (success_?TimeBetweenRegistrations:30);
00151 SDL_Delay(1000 * waitTime);
00152 }
00153 }
00154
00155 bool ServerRegistrationEntry::registerGame()
00156 {
00157
00158 if (!netServer_.connect(masterListServer_, 80)) return false;
00159
00160
00161 netServer_.sendMessageServer(sendNetBuffer_);
00162 return true;
00163 }
00164
00165 void ServerRegistrationEntry::processMessage(NetMessage &message)
00166 {
00167
00168 if (message.getMessageType() == NetMessage::BufferMessage)
00169 {
00170 message.getBuffer().addToBuffer("");
00171 success_ = (strstr(message.getBuffer().getBuffer(), "success") != 0);
00172 }
00173 else if (message.getMessageType() == NetMessage::DisconnectMessage)
00174 {
00175 finished_ = true;
00176 }
00177 }
00178