LAN clients now start to connect to the server.

This commit is contained in:
hiker 2015-11-13 08:50:03 +11:00
parent af9018ea64
commit 4a3c4c552e
6 changed files with 61 additions and 30 deletions

View File

@ -31,12 +31,30 @@
#include "utils/log.hpp" #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) ConnectToPeer::ConnectToPeer(uint32_t peer_id) : Protocol(PROTOCOL_CONNECTION)
{ {
m_peer_id = peer_id; m_peer_address.clear();
m_state = NONE; m_peer_id = peer_id;
} // ConnectToPeer 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 } // ~ConnectToPeer
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void ConnectToPeer::setup() void ConnectToPeer::setup()
{ {
m_peer_address.clear();
m_current_protocol = NULL;
m_state = NONE;
} // setup } // setup
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -103,7 +118,8 @@ void ConnectToPeer::asynchronousUpdate()
// the Ping protocol to keep the port available. We can't rely on // the Ping protocol to keep the port available. We can't rely on
// STKHost::isLAN(), since we might get a LAN connection even if // STKHost::isLAN(), since we might get a LAN connection even if
// the server itself accepts connections from anywhere. // 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()) ->getMyAddress().getIP())
{ {
m_current_protocol = new PingProtocol(m_peer_address, 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, // Just send a broadcast packet with the string aloha_stk inside,
// the client will know our ip address and will connect // the client will know our ip address and will connect
TransportAddress broadcast_address; TransportAddress broadcast_address;
broadcast_address.setIP(-1); // 255.255.255.255 if(NetworkConfig::get()->isWAN())
broadcast_address.setPort(m_peer_address.getPort()); // 0b10101100000101101101111111111111; // for test {
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"; char data[] = "aloha_stk\0";
STKHost::get()->sendRawPacket((uint8_t*)(data), 10, broadcast_address); STKHost::get()->sendRawPacket((uint8_t*)(data), 10, broadcast_address);
Log::info("ConnectToPeer", "Broadcast aloha sent."); Log::info("ConnectToPeer", "Broadcast aloha sent.");

View File

@ -23,8 +23,6 @@
#include "network/transport_address.hpp" #include "network/transport_address.hpp"
#include "utils/cpp2011.hpp" #include "utils/cpp2011.hpp"
#include <string>
/** One instance of this is started for every peer who tries to /** One instance of this is started for every peer who tries to
* connect to this server. * connect to this server.
*/ */
@ -38,6 +36,9 @@ protected:
/** Pointer to the protocol which is monitored for state changes. */ /** Pointer to the protocol which is monitored for state changes. */
Protocol *m_current_protocol; Protocol *m_current_protocol;
/** True if this is a LAN connection. */
bool m_is_lan;
enum STATE enum STATE
{ {
NONE, NONE,
@ -46,11 +47,11 @@ protected:
CONNECTED, CONNECTED,
DONE, DONE,
EXITING EXITING
}; } m_state;
STATE m_state;
public: public:
ConnectToPeer(uint32_t peer_id); ConnectToPeer(uint32_t peer_id);
ConnectToPeer(const TransportAddress &address);
virtual ~ConnectToPeer(); virtual ~ConnectToPeer();
virtual bool notifyEventAsynchronous(Event* event); virtual bool notifyEventAsynchronous(Event* event);

View File

@ -84,7 +84,7 @@ void ConnectToServer::setup()
m_current_protocol = NULL; m_current_protocol = NULL;
// In case of LAN we already have the server's and our ip address, // In case of LAN we already have the server's and our ip address,
// so we can immediately start requesting a connection. // 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 } // setup
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -135,14 +135,14 @@ void ConnectToServer::asynchronousUpdate()
// Find its address // Find its address
m_current_protocol = new GetPeerAddress(m_host_id, this); m_current_protocol = new GetPeerAddress(m_host_id, this);
m_current_protocol->requestStart(); m_current_protocol->requestStart();
m_state = GETTING_SERVER_ADDRESS; m_state = GOT_SERVER_ADDRESS;
// Pause this protocol till GetPeerAddress finishes. // Pause this protocol till GetPeerAddress finishes.
// The callback then will unpause this protocol/ // The callback then will unpause this protocol/
ProtocolManager::getInstance()->pauseProtocol(this); ProtocolManager::getInstance()->pauseProtocol(this);
} }
} }
break; break;
case GETTING_SERVER_ADDRESS: case GOT_SERVER_ADDRESS:
{ {
assert(!m_quick_join); assert(!m_quick_join);
delete m_current_protocol; delete m_current_protocol;
@ -163,9 +163,11 @@ void ConnectToServer::asynchronousUpdate()
} }
case REQUESTING_CONNECTION: case REQUESTING_CONNECTION:
// In case of a LAN server, m_crrent_protocol is NULL // 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) m_current_protocol->getState() == PROTOCOL_STATE_TERMINATED)
{ {
delete m_current_protocol;
m_current_protocol = NULL;
// Server knows we want to connect // Server knows we want to connect
Log::info("ConnectToServer", "Connection request made"); Log::info("ConnectToServer", "Connection request made");
if (m_server_address.getIP() == 0 || if (m_server_address.getIP() == 0 ||
@ -253,7 +255,7 @@ void ConnectToServer::callback(Protocol *protocol)
// STKHost, so we only need to unpause this protocol // STKHost, so we only need to unpause this protocol
ProtocolManager::getInstance()->unpauseProtocol(this); ProtocolManager::getInstance()->unpauseProtocol(this);
break; break;
case GETTING_SERVER_ADDRESS: case GOT_SERVER_ADDRESS:
// Get the server address from the protocol. // Get the server address from the protocol.
m_server_address.copy(((GetPeerAddress*)protocol)->getAddress()); m_server_address.copy(((GetPeerAddress*)protocol)->getAddress());
ProtocolManager::getInstance()->unpauseProtocol(this); ProtocolManager::getInstance()->unpauseProtocol(this);
@ -355,11 +357,13 @@ void ConnectToServer::handleSameLAN()
STKHost* host = STKHost::get(); STKHost* host = STKHost::get();
host->stopListening(); // stop the listening host->stopListening(); // stop the listening
#ifdef XX
TransportAddress broadcast_address; TransportAddress broadcast_address;
broadcast_address.setIP(-1); // 255.255.255.255 broadcast_address.setIP(-1); // 255.255.255.255
broadcast_address.setPort(7321); broadcast_address.setPort(7321);
char data2[] = "aloha_stk\0"; char data2[] = "aloha_stk\0";
host->sendRawPacket((uint8_t*)(data2), 10, broadcast_address); host->sendRawPacket((uint8_t*)(data2), 10, broadcast_address);
#endif
Log::info("ConnectToServer", "Waiting broadcast message."); Log::info("ConnectToServer", "Waiting broadcast message.");

View File

@ -40,7 +40,7 @@ private:
{ {
NONE, NONE,
GETTING_SELF_ADDRESS, GETTING_SELF_ADDRESS,
GETTING_SERVER_ADDRESS, GOT_SERVER_ADDRESS,
REQUESTING_CONNECTION, REQUESTING_CONNECTION,
QUICK_JOIN, QUICK_JOIN,
CONNECTING, CONNECTING,

View File

@ -24,6 +24,7 @@
#include "network/network_config.hpp" #include "network/network_config.hpp"
#include "network/protocol_manager.hpp" #include "network/protocol_manager.hpp"
#include "network/servers_manager.hpp" #include "network/servers_manager.hpp"
#include "network/stk_host.hpp"
using namespace Online; using namespace Online;
@ -34,6 +35,7 @@ RequestConnection::RequestConnection(uint32_t server_id)
: Protocol(PROTOCOL_SILENT) : Protocol(PROTOCOL_SILENT)
{ {
m_server_id = server_id; m_server_id = server_id;
m_request = NULL;
} // RequestConnection } // RequestConnection
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -75,19 +77,18 @@ void RequestConnection::asynchronousUpdate()
{ {
if(NetworkConfig::get()->isLAN()) if(NetworkConfig::get()->isLAN())
{ {
Network *broadcast = new Network(1, 1, 0, 0);
NetworkString s(std::string("connection-request"));
const Server *server = const Server *server =
ServersManager::get()->getServerByID(m_server_id); ServersManager::get()->getServerByID(m_server_id);
broadcast->sendRawPacket(s.getBytes(), s.size(), NetworkString s(std::string("connection-request"));
server->getAddress()); STKHost::get()->sendRawPacket(s.getBytes(), s.size(),
m_state = EXITING; server->getAddress());
m_state = DONE;
} }
else else
{ {
m_request = new ServerJoinRequest(); 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->addParameter("server_id", m_server_id);
m_request->queue(); m_request->queue();

View File

@ -506,9 +506,12 @@ void STKHost::handleLANRequests()
} // if message is server-requested } // if message is server-requested
else if (std::string(buffer, len) == "connection-request") else if (std::string(buffer, len) == "connection-request")
{ {
Protocol *c = new ConnectToPeer(0); Protocol *c = new ConnectToPeer(sender);
c->requestStart(); c->requestStart();
} }
else
Log::info("STKHost", "Received unknown command '%s'",
std::string(buffer, len).c_str());
} // handleLANRequests } // handleLANRequests