1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
4 // This file is part of Scorched3D.
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <dialogs/ConnectDialog.h>
22 #include <dialogs/PlayerDialog.h>
23 #include <dialogs/MsgBoxDialog.h>
24 #include <dialogs/ProgressDialog.h>
25 #include <client/ScorchedClient.h>
26 #include <client/ClientParams.h>
27 #include <client/ClientState.h>
28 #include <coms/ComsMessageSender.h>
29 #include <coms/ComsConnectMessage.h>
30 #include <net/NetInterface.h>
31 #include <common/Logger.h>
33 ConnectDialog
*ConnectDialog::instance_
= 0;
35 ConnectDialog
*ConnectDialog::instance()
39 instance_
= new ConnectDialog
;
44 ConnectDialog::ConnectDialog() :
45 GLWWindow("Connect", -100.0f
, 10.0f
, 20.0f
, 20.0f
, eNoDraw
| eNoTitle
,
47 connectionState_(eWaiting
),
48 tryCount_(0), lastTime_(0), idStore_(0)
50 connectionState_
= eWaiting
;
55 ConnectDialog::~ConnectDialog()
59 UniqueIdStore
&ConnectDialog::getIdStore()
63 idStore_
= new UniqueIdStore();
65 if (!idStore_
->loadStore())
67 Logger::log("Failed to load id store");
73 void ConnectDialog::windowInit(const unsigned state
)
78 void ConnectDialog::simulate(float frameTime
)
80 time_t currentTime
= time(0);
81 if (connectionState_
== eWaiting
)
83 if (currentTime
- lastTime_
> 3)
85 ScorchedClient::instance()->getNetInterface().stop();
90 lastTime_
= currentTime
;
95 connectionState_
= eFinished
;
97 LangString msg
= LANG_RESOURCE_2("FAILED_TO_CONNECT_TIMEOUT",
98 "Failed to connect to server \"{0}:{1}\", timeout.",
101 MsgBoxDialog::instance()->show(msg
);
103 ScorchedClient::instance()->getGameState().stimulate(
104 ClientState::StimOptions
);
110 void ConnectDialog::tryConnection()
112 const char *serverName
=
113 (ClientParams::instance()->getConnect()[0]?
114 ClientParams::instance()->getConnect():
118 port_
= S3D::ScorchedPort
;
120 char *colon
= strchr((char *)serverName
, ':');
126 port_
= strtol(colon
, &stop
, 10);
132 ProgressDialog::instance()->progressChange(
133 LANG_RESOURCE_3("CONNECTING_TO",
134 "Connecting to \"{0}:{1}\" ({3})....",
136 S3D::formatStringBuffer("%i", port_
),
137 S3D::formatStringBuffer("%i", tryCount_
)), 0);
139 connectionState_
= eTryingConnection
;
140 if (ClientParams::instance()->getConnectedToServer())
142 // Do in a thread so connect can block if it wants!
143 SDL_CreateThread(ConnectDialog::tryRemoteConnection
, 0);
148 tryLocalConnection();
152 int ConnectDialog::tryRemoteConnection(void *)
154 char *host
= (char *) instance_
->host_
.c_str();
155 int port
= instance_
->port_
;
157 // Try to connect to the server
158 ScorchedClient::instance()->getNetInterface().connect(host
, port
);
161 instance_
->connectionState_
= eWaiting
;
165 void ConnectDialog::tryLocalConnection()
167 // Try to connect localy
168 ScorchedClient::instance()->getNetInterface().connect("Local", 0);
171 connectionState_
= eWaiting
;;
174 void ConnectDialog::connected()
176 ProgressDialog::instance()->progressChange(LANG_RESOURCE("CONNECTED", "Connected"), 100);
178 // Wait for the coms to start
179 for (int i
=0; i
<10 && !ScorchedClient::instance()->getNetInterface().started(); i
++)
184 // If we connected then send our details to the server
185 ComsConnectMessage connectMessage
;
186 connectMessage
.setVersion(S3D::ScorchedVersion
.c_str());
187 connectMessage
.setProtocolVersion(S3D::ScorchedProtocolVersion
.c_str());
189 if (!ComsMessageSender::sendToServer(connectMessage
))
191 ScorchedClient::instance()->getNetInterface().stop();
193 LangString msg
= LANG_RESOURCE_2("FAILED_TO_CONNECT_SEND",
194 "Failed to connect to server \"{0}:{1}\", send failed.",
197 MsgBoxDialog::instance()->show(msg
);
199 ScorchedClient::instance()->getNetInterface().stop();
200 ScorchedClient::instance()->getGameState().stimulate(
201 ClientState::StimOptions
);
204 connectionState_
= eFinished
;