Adjust connect-now for new lobby

This commit is contained in:
Benau 2018-03-31 16:14:22 +08:00
parent 7f17d7ab25
commit 852c78729f
7 changed files with 61 additions and 64 deletions

View File

@ -210,7 +210,8 @@
#include "modes/cutscene_world.hpp"
#include "modes/demo_world.hpp"
#include "modes/profile_world.hpp"
#include "network/protocols/lobby_protocol.hpp"
#include "network/protocols/connect_to_server.hpp"
#include "network/protocols/client_lobby.hpp"
#include "network/game_setup.hpp"
#include "network/network_config.hpp"
#include "network/network_string.hpp"
@ -590,7 +591,7 @@ void cmdLineHelp()
" --server-password= Sets a password for a server (both client&server).\n"
" --connect-now=ip Connect to a server with IP known now\n"
" (in format x.x.x.x:xxx(port)), the port should be its\n"
" private port.\n"
" public port.\n"
" --login=s Automatically log in (set the login).\n"
" --password=s Automatically log in (set the password).\n"
" --port=n Port number to use.\n"
@ -1083,17 +1084,33 @@ int handleCmdLine()
}
if (CommandLine::has("--connect-now", &s))
{
TransportAddress ip(s);
NetworkConfig::get()->setIsLAN();
TransportAddress server_addr(s);
NetworkConfig::get()->setIsWAN();
NetworkConfig::get()->setIsServer(false);
Log::info("main", "Try to connect to server '%s'.",
ip.toString().c_str() );
irr::core::stringw name = StringUtils::utf8ToWide(ip.toString());
auto server = std::make_shared<Server>(0, name,
NetworkConfig::get()->getMaxPlayers(), 0,
race_manager->getDifficulty(), 0, ip, !server_password.empty());
NetworkingLobby::getInstance()->setJoinedServer(server);
STKHost::create(server);
auto server = std::make_shared<Server>(0, L"", 0, 0, 0, 0, server_addr,
!server_password.empty());
NetworkConfig::get()->addNetworkPlayer(
input_manager->getDeviceManager()->getLatestUsedDevice(),
PlayerManager::getCurrentPlayer(), false/*handicap*/);
NetworkConfig::get()->doneAddingNetworkPlayers();
STKHost::create();
auto cts = std::make_shared<ConnectToServer>(server);
cts->setup();
Log::info("main", "Trying to connect to server '%s'.",
server_addr.toString().c_str());
if (!cts->handleDirectConnect(10000))
{
Log::error("main", "Timeout trying to connect to server '%s'.",
server_addr.toString().c_str());
STKHost::get()->shutdown();
exit(0);
}
else
{
auto cl = LobbyProtocol::create<ClientLobby>();
cl->setAddress(server_addr);
cl->requestStart();
}
}
std::shared_ptr<LobbyProtocol> server_lobby;

View File

@ -297,7 +297,7 @@ void ConnectToServer::update(int ticks)
} // update
// ----------------------------------------------------------------------------
bool ConnectToServer::handleDirectConnect()
bool ConnectToServer::handleDirectConnect(int timeout)
{
// Direct connection to server should only possbile if public and private
// ports of server are the same
@ -316,7 +316,7 @@ bool ConnectToServer::handleDirectConnect()
ENetPeer* p = dc->connectTo(m_server_address);
if (p)
{
while (enet_host_service(dc->getENetHost(), &event, 2000) != 0)
while (enet_host_service(dc->getENetHost(), &event, timeout) != 0)
{
if (event.type == ENET_EVENT_TYPE_CONNECT)
{

View File

@ -56,7 +56,6 @@ private:
void registerWithSTKServer();
void waitingAloha(bool is_wan);
bool handleDirectConnect();
public:
ConnectToServer(std::shared_ptr<Server> server);
virtual ~ConnectToServer();
@ -65,6 +64,8 @@ public:
virtual void setup() OVERRIDE;
virtual void asynchronousUpdate() OVERRIDE;
virtual void update(int ticks) OVERRIDE;
bool handleDirectConnect(int timeout = 2000);
}; // class ConnectToServer
#endif // CONNECT_TO_SERVER_HPP

View File

@ -67,20 +67,20 @@
STKHost *STKHost::m_stk_host = NULL;
bool STKHost::m_enable_console = false;
std::shared_ptr<LobbyProtocol> STKHost::create(std::shared_ptr<Server> server,
SeparateProcess* p)
std::shared_ptr<LobbyProtocol> STKHost::create(SeparateProcess* p)
{
assert(m_stk_host == NULL);
std::shared_ptr<LobbyProtocol> lp;
if (NetworkConfig::get()->isServer())
{
lp = LobbyProtocol::create<ServerLobby>();
m_stk_host = new STKHost(NetworkConfig::get()->getServerName());
m_stk_host = new STKHost(true/*server*/);
}
else
{
m_stk_host = new STKHost(server);
m_stk_host = new STKHost(false/*server*/);
}
// Separate process for client-server gui if exists
m_stk_host->m_separate_process = p;
if (!m_stk_host->m_network)
{
@ -260,57 +260,40 @@ std::shared_ptr<LobbyProtocol> STKHost::create(std::shared_ptr<Server> server,
*/
// ============================================================================
/** Constructor for a client
/** The constructor for a server or client.
*/
STKHost::STKHost(std::shared_ptr<Server> server)
{
// Will be overwritten with the correct value once a connection with the
// server is made.
m_host_id = 0;
init();
ENetAddress ea;
ea.host = STKHost::HOST_ANY;
ea.port = NetworkConfig::get()->getClientPort();
m_network = new Network(/*peer_count*/1, /*channel_limit*/2,
/*max_in_bandwidth*/0, /*max_out_bandwidth*/0,
&ea, true/*change_port_if_bound*/);
if (!m_network)
{
Log::fatal ("STKHost", "An error occurred while trying to create "
"an ENet client host.");
}
setPrivatePort();
} // STKHost
// ----------------------------------------------------------------------------
/** The constructor for a server.
* The server control flow starts with the ServerLobby.
*/
STKHost::STKHost(const irr::core::stringw &server_name)
STKHost::STKHost(bool server)
{
init();
m_host_id = 0; // indicates a server host.
ENetAddress addr;
addr.host = STKHost::HOST_ANY;
addr.port = NetworkConfig::get()->getServerPort();
// Reserver 1 peer to handle full server message
m_network = new Network(NetworkConfig::get()->getMaxPlayers() + 1,
/*channel_limit*/2,
/*max_in_bandwidth*/0,
/*max_out_bandwidth*/ 0, &addr,
true/*change_port_if_bound*/);
if (server)
{
addr.port = NetworkConfig::get()->getServerPort();
// Reserve 1 peer to deliver full server message
m_network = new Network(NetworkConfig::get()->getMaxPlayers() + 1,
/*channel_limit*/2, /*max_in_bandwidth*/0,
/*max_out_bandwidth*/ 0, &addr, true/*change_port_if_bound*/);
}
else
{
addr.port = NetworkConfig::get()->getClientPort();
// Client only has 1 peer
m_network = new Network(/*peer_count*/1, /*channel_limit*/2,
/*max_in_bandwidth*/0, /*max_out_bandwidth*/0, &addr,
true/*change_port_if_bound*/);
}
if (!m_network)
{
Log::fatal("STKHost", "An error occurred while trying to create an "
"ENet server host.");
}
setPrivatePort();
} // STKHost(server_name)
} // STKHost
// ----------------------------------------------------------------------------
/** Initialises the internal data structures and starts the protocol manager

View File

@ -133,9 +133,7 @@ private:
uint16_t m_private_port;
// ------------------------------------------------------------------------
STKHost(std::shared_ptr<Server> server);
// ------------------------------------------------------------------------
STKHost(const irr::core::stringw &server_name);
STKHost(bool server);
// ------------------------------------------------------------------------
~STKHost();
// ------------------------------------------------------------------------
@ -155,9 +153,7 @@ public:
/** Creates the STKHost. It takes all confifguration parameters from
* NetworkConfig. This STKHost can either be a client or a server.
*/
static std::shared_ptr<LobbyProtocol>
create(std::shared_ptr<Server> server = nullptr,
SeparateProcess* p = NULL);
static std::shared_ptr<LobbyProtocol> create(SeparateProcess* p = NULL);
// ------------------------------------------------------------------------
/** Returns the instance of STKHost. */
static STKHost *get()

View File

@ -331,7 +331,7 @@ void CreateServerScreen::createServer()
SeparateProcess* sp =
new SeparateProcess(SeparateProcess::getCurrentExecutableLocation(),
server_cfg.str() + password);
STKHost::create(server, sp);
STKHost::create(sp);
NetworkingLobby::getInstance()->setJoinedServer(server);
#endif
} // createServer

View File

@ -104,7 +104,7 @@ void ServerInfoDialog::requestJoin()
{
NetworkConfig::get()->setPassword("");
}
STKHost::create(m_server);
STKHost::create();
NetworkingLobby::getInstance()->setJoinedServer(m_server);
ModalDialog::dismiss();
NetworkingLobby::getInstance()->push();