From 4a3c4c552e206d4843d902887c203ad9e79fdfc5 Mon Sep 17 00:00:00 2001 From: hiker Date: Fri, 13 Nov 2015 08:50:03 +1100 Subject: [PATCH] LAN clients now start to connect to the server. --- src/network/protocols/connect_to_peer.cpp | 44 +++++++++++++++----- src/network/protocols/connect_to_peer.hpp | 11 ++--- src/network/protocols/connect_to_server.cpp | 14 ++++--- src/network/protocols/connect_to_server.hpp | 2 +- src/network/protocols/request_connection.cpp | 15 +++---- src/network/stk_host.cpp | 5 ++- 6 files changed, 61 insertions(+), 30 deletions(-) diff --git a/src/network/protocols/connect_to_peer.cpp b/src/network/protocols/connect_to_peer.cpp index fc63cd3ce..cb5599a02 100644 --- a/src/network/protocols/connect_to_peer.cpp +++ b/src/network/protocols/connect_to_peer.cpp @@ -31,12 +31,30 @@ #include "utils/log.hpp" // ---------------------------------------------------------------------------- - +/** Constructor for a WAN request. In this case we need to get the peer's + * ip address first. + * \param peer_id ID of the peer in the stk client table. + */ ConnectToPeer::ConnectToPeer(uint32_t peer_id) : Protocol(PROTOCOL_CONNECTION) { - m_peer_id = peer_id; - m_state = NONE; -} // ConnectToPeer + m_peer_address.clear(); + m_peer_id = peer_id; + m_state = NONE; + m_current_protocol = NULL; + m_is_lan = false; +} // ConnectToPeer(peer_id) + +// ---------------------------------------------------------------------------- +ConnectToPeer::ConnectToPeer(const TransportAddress &address) + : Protocol(PROTOCOL_CONNECTION) +{ + m_peer_address.copy(address); + // We don't need to find the peer address, so we can start + // with the state when we found the peer address. + m_state = RECEIVED_PEER_ADDRESS; + m_current_protocol = NULL; + m_is_lan = true; +} // ConnectToPeers(TransportAddress) // ---------------------------------------------------------------------------- @@ -44,13 +62,10 @@ ConnectToPeer::~ConnectToPeer() { } // ~ConnectToPeer - // ---------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- void ConnectToPeer::setup() { - m_peer_address.clear(); - m_current_protocol = NULL; - m_state = NONE; } // setup // ---------------------------------------------------------------------------- @@ -103,7 +118,8 @@ void ConnectToPeer::asynchronousUpdate() // the Ping protocol to keep the port available. We can't rely on // STKHost::isLAN(), since we might get a LAN connection even if // the server itself accepts connections from anywhere. - if (m_peer_address.getIP() != NetworkConfig::get() + if (!m_is_lan && + m_peer_address.getIP() != NetworkConfig::get() ->getMyAddress().getIP()) { m_current_protocol = new PingProtocol(m_peer_address, @@ -117,8 +133,14 @@ void ConnectToPeer::asynchronousUpdate() // Just send a broadcast packet with the string aloha_stk inside, // the client will know our ip address and will connect TransportAddress broadcast_address; - broadcast_address.setIP(-1); // 255.255.255.255 - broadcast_address.setPort(m_peer_address.getPort()); // 0b10101100000101101101111111111111; // for test + if(NetworkConfig::get()->isWAN()) + { + broadcast_address.setIP(-1); // 255.255.255.255 + broadcast_address.setPort(m_peer_address.getPort()); + } + else + broadcast_address.copy(m_peer_address); + char data[] = "aloha_stk\0"; STKHost::get()->sendRawPacket((uint8_t*)(data), 10, broadcast_address); Log::info("ConnectToPeer", "Broadcast aloha sent."); diff --git a/src/network/protocols/connect_to_peer.hpp b/src/network/protocols/connect_to_peer.hpp index b8e611d2f..29099ac4e 100644 --- a/src/network/protocols/connect_to_peer.hpp +++ b/src/network/protocols/connect_to_peer.hpp @@ -23,8 +23,6 @@ #include "network/transport_address.hpp" #include "utils/cpp2011.hpp" -#include - /** One instance of this is started for every peer who tries to * connect to this server. */ @@ -38,6 +36,9 @@ protected: /** Pointer to the protocol which is monitored for state changes. */ Protocol *m_current_protocol; + /** True if this is a LAN connection. */ + bool m_is_lan; + enum STATE { NONE, @@ -46,11 +47,11 @@ protected: CONNECTED, DONE, EXITING - }; - STATE m_state; + } m_state; public: - ConnectToPeer(uint32_t peer_id); + ConnectToPeer(uint32_t peer_id); + ConnectToPeer(const TransportAddress &address); virtual ~ConnectToPeer(); virtual bool notifyEventAsynchronous(Event* event); diff --git a/src/network/protocols/connect_to_server.cpp b/src/network/protocols/connect_to_server.cpp index df7e7ce13..55c2fdc5d 100644 --- a/src/network/protocols/connect_to_server.cpp +++ b/src/network/protocols/connect_to_server.cpp @@ -84,7 +84,7 @@ void ConnectToServer::setup() m_current_protocol = NULL; // In case of LAN we already have the server's and our ip address, // so we can immediately start requesting a connection. - m_state = NetworkConfig::get()->isLAN() ? GETTING_SERVER_ADDRESS : NONE; + m_state = NetworkConfig::get()->isLAN() ? GOT_SERVER_ADDRESS : NONE; } // setup // ---------------------------------------------------------------------------- @@ -135,14 +135,14 @@ void ConnectToServer::asynchronousUpdate() // Find its address m_current_protocol = new GetPeerAddress(m_host_id, this); m_current_protocol->requestStart(); - m_state = GETTING_SERVER_ADDRESS; + m_state = GOT_SERVER_ADDRESS; // Pause this protocol till GetPeerAddress finishes. // The callback then will unpause this protocol/ ProtocolManager::getInstance()->pauseProtocol(this); } } break; - case GETTING_SERVER_ADDRESS: + case GOT_SERVER_ADDRESS: { assert(!m_quick_join); delete m_current_protocol; @@ -163,9 +163,11 @@ void ConnectToServer::asynchronousUpdate() } case REQUESTING_CONNECTION: // In case of a LAN server, m_crrent_protocol is NULL - if (NetworkConfig::get()->isLAN() || + if (!m_current_protocol || m_current_protocol->getState() == PROTOCOL_STATE_TERMINATED) { + delete m_current_protocol; + m_current_protocol = NULL; // Server knows we want to connect Log::info("ConnectToServer", "Connection request made"); if (m_server_address.getIP() == 0 || @@ -253,7 +255,7 @@ void ConnectToServer::callback(Protocol *protocol) // STKHost, so we only need to unpause this protocol ProtocolManager::getInstance()->unpauseProtocol(this); break; - case GETTING_SERVER_ADDRESS: + case GOT_SERVER_ADDRESS: // Get the server address from the protocol. m_server_address.copy(((GetPeerAddress*)protocol)->getAddress()); ProtocolManager::getInstance()->unpauseProtocol(this); @@ -355,11 +357,13 @@ void ConnectToServer::handleSameLAN() STKHost* host = STKHost::get(); host->stopListening(); // stop the listening +#ifdef XX TransportAddress broadcast_address; broadcast_address.setIP(-1); // 255.255.255.255 broadcast_address.setPort(7321); char data2[] = "aloha_stk\0"; host->sendRawPacket((uint8_t*)(data2), 10, broadcast_address); +#endif Log::info("ConnectToServer", "Waiting broadcast message."); diff --git a/src/network/protocols/connect_to_server.hpp b/src/network/protocols/connect_to_server.hpp index 1a6922357..6d6c9597f 100644 --- a/src/network/protocols/connect_to_server.hpp +++ b/src/network/protocols/connect_to_server.hpp @@ -40,7 +40,7 @@ private: { NONE, GETTING_SELF_ADDRESS, - GETTING_SERVER_ADDRESS, + GOT_SERVER_ADDRESS, REQUESTING_CONNECTION, QUICK_JOIN, CONNECTING, diff --git a/src/network/protocols/request_connection.cpp b/src/network/protocols/request_connection.cpp index ec268218f..d27fe87fd 100644 --- a/src/network/protocols/request_connection.cpp +++ b/src/network/protocols/request_connection.cpp @@ -24,6 +24,7 @@ #include "network/network_config.hpp" #include "network/protocol_manager.hpp" #include "network/servers_manager.hpp" +#include "network/stk_host.hpp" using namespace Online; @@ -34,6 +35,7 @@ RequestConnection::RequestConnection(uint32_t server_id) : Protocol(PROTOCOL_SILENT) { m_server_id = server_id; + m_request = NULL; } // RequestConnection // ---------------------------------------------------------------------------- @@ -75,19 +77,18 @@ void RequestConnection::asynchronousUpdate() { if(NetworkConfig::get()->isLAN()) { - Network *broadcast = new Network(1, 1, 0, 0); - - NetworkString s(std::string("connection-request")); const Server *server = ServersManager::get()->getServerByID(m_server_id); - broadcast->sendRawPacket(s.getBytes(), s.size(), - server->getAddress()); - m_state = EXITING; + NetworkString s(std::string("connection-request")); + STKHost::get()->sendRawPacket(s.getBytes(), s.size(), + server->getAddress()); + m_state = DONE; } else { m_request = new ServerJoinRequest(); - PlayerManager::setUserDetails(m_request, "request-connection", Online::API::SERVER_PATH); + PlayerManager::setUserDetails(m_request, "request-connection", + Online::API::SERVER_PATH); m_request->addParameter("server_id", m_server_id); m_request->queue(); diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 30a641117..5b3390985 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -506,9 +506,12 @@ void STKHost::handleLANRequests() } // if message is server-requested else if (std::string(buffer, len) == "connection-request") { - Protocol *c = new ConnectToPeer(0); + Protocol *c = new ConnectToPeer(sender); c->requestStart(); } + else + Log::info("STKHost", "Received unknown command '%s'", + std::string(buffer, len).c_str()); } // handleLANRequests