Push to lobby for enter server ip address dialog too

This commit is contained in:
Benau 2018-11-22 02:42:37 +08:00
parent cd1b401cc8
commit f14daeca26
2 changed files with 51 additions and 27 deletions

View File

@ -27,8 +27,8 @@
#include "guiengine/widgets/ribbon_widget.hpp" #include "guiengine/widgets/ribbon_widget.hpp"
#include "guiengine/widgets/text_box_widget.hpp" #include "guiengine/widgets/text_box_widget.hpp"
#include "input/device_manager.hpp" #include "input/device_manager.hpp"
#include "network/protocols/connect_to_server.hpp" #include "network/event.hpp"
#include "network/protocols/client_lobby.hpp" #include "network/network.hpp"
#include "network/network_config.hpp" #include "network/network_config.hpp"
#include "network/server.hpp" #include "network/server.hpp"
#include "network/server_config.hpp" #include "network/server_config.hpp"
@ -130,9 +130,14 @@ void OnlineScreen::onUpdate(float delta)
m_online->setLabel(PlayerManager::getCurrentOnlineId() ? m_online_string m_online->setLabel(PlayerManager::getCurrentOnlineId() ? m_online_string
: m_login_string); : m_login_string);
// In case for entering server address finished // In case for entering server address finished
if (auto lb = LobbyProtocol::get<LobbyProtocol>()) if (m_entered_server)
{ {
NetworkingLobby::getInstance()->setJoinedServer(nullptr); NetworkConfig::get()->setIsLAN();
NetworkConfig::get()->setIsServer(false);
ServerConfig::m_private_server_password = "";
STKHost::create();
NetworkingLobby::getInstance()->setJoinedServer(m_entered_server);
m_entered_server = nullptr;
StateManager::get()->resetAndSetStack( StateManager::get()->resetAndSetStack(
NetworkConfig::get()->getResetScreens(true/*lobby*/).data()); NetworkConfig::get()->getResetScreens(true/*lobby*/).data());
} }
@ -220,6 +225,7 @@ void OnlineScreen::eventCallback(Widget* widget, const std::string& name,
} }
else if (selection == "enter-address") else if (selection == "enter-address")
{ {
m_entered_server = nullptr;
if (NetworkConfig::get()->isAddingNetworkPlayers()) if (NetworkConfig::get()->isAddingNetworkPlayers())
{ {
core::stringw msg = core::stringw msg =
@ -244,31 +250,44 @@ void OnlineScreen::eventCallback(Widget* widget, const std::string& name,
lw->setText(err, true); lw->setText(err, true);
return false; return false;
} }
NetworkConfig::get()->setIsWAN(); if (server_addr.getPort() == 0)
NetworkConfig::get()->setIsServer(false);
ServerConfig::m_private_server_password = "";
auto server = std::make_shared<Server>(0, L"", 0, 0, 0, 0,
server_addr, false, false);
STKHost::create();
auto cts = std::make_shared<ConnectToServer>(server);
cts->setup();
Log::info("OnlineScreen", "Trying to connect to server '%s'.",
server_addr.toString().c_str());
if (!cts->tryConnect(2000, 15))
{ {
core::stringw err = _("Cannot connect to server %s.", ENetAddress ea;
server_addr.toString().c_str()); ea.host = STKHost::HOST_ANY;
STKHost::get()->shutdown(); ea.port = STKHost::PORT_ANY;
NetworkConfig::get()->unsetNetworking(); Network* nw = new Network(/*peer_count*/1,
lw->setText(err, true); /*channel_limit*/EVENT_CHANNEL_COUNT,
return false; /*max_in_bandwidth*/0, /*max_out_bandwidth*/0, &ea,
true/*change_port_if_bound*/);
BareNetworkString s(std::string("stk-server-port"));
TransportAddress address(server_addr.getIP(),
stk_config->m_server_discovery_port);
nw->sendRawPacket(s, address);
TransportAddress sender;
const int LEN = 2048;
char buffer[LEN];
int len = nw->receiveRawPacket(buffer, LEN, &sender, 2000);
if (len != 2)
{
//I18N: In enter server ip address dialog
core::stringw err = _("Failed to detect port number.");
lw->setText(err, true);
delete nw;
return false;
}
BareNetworkString server_port(buffer, len);
uint16_t port = server_port.getUInt16();
server_addr.setPort(port);
Log::info("OnlineScreen",
"Detected port %d for server address: %s.",
port, server_addr.toString(false).c_str());
delete nw;
} }
auto server = std::make_shared<Server>(0,
m_entered_server_address = StringUtils::utf8ToWide(server_addr.toString()), 0, 0, 0, 0,
STKHost::get()->getServerPeerForClient()->getAddress(); server_addr, false, false);
auto cl = LobbyProtocol::create<ClientLobby> m_entered_server = server;
(m_entered_server_address, server); m_entered_server_address = server_addr;
cl->requestStart();
return true; return true;
}); });
if (!m_entered_server_address.isUnset()) if (!m_entered_server_address.isUnset())

View File

@ -21,6 +21,9 @@
#include "guiengine/screen.hpp" #include "guiengine/screen.hpp"
#include "network/transport_address.hpp" #include "network/transport_address.hpp"
#include <memory>
class Server;
namespace GUIEngine { class CheckBoxWidget; class ListWidget; namespace GUIEngine { class CheckBoxWidget; class ListWidget;
class ButtonWidget; class IconButtonWidget; } class ButtonWidget; class IconButtonWidget; }
@ -46,6 +49,8 @@ private:
GUIEngine::CheckBoxWidget* m_enable_splitscreen; GUIEngine::CheckBoxWidget* m_enable_splitscreen;
std::shared_ptr<Server> m_entered_server;
TransportAddress m_entered_server_address; TransportAddress m_entered_server_address;
OnlineScreen(); OnlineScreen();