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"
// ----------------------------------------------------------------------------
/** 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_address.clear();
m_peer_id = peer_id;
m_state = NONE;
} // ConnectToPeer
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)
// ----------------------------------------------------------------------------
@ -48,9 +66,6 @@ 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;
if(NetworkConfig::get()->isWAN())
{
broadcast_address.setIP(-1); // 255.255.255.255
broadcast_address.setPort(m_peer_address.getPort()); // 0b10101100000101101101111111111111; // for test
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.");

View File

@ -23,8 +23,6 @@
#include "network/transport_address.hpp"
#include "utils/cpp2011.hpp"
#include <string>
/** 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(const TransportAddress &address);
virtual ~ConnectToPeer();
virtual bool notifyEventAsynchronous(Event* event);

View File

@ -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.");

View File

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

View File

@ -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(),
NetworkString s(std::string("connection-request"));
STKHost::get()->sendRawPacket(s.getBytes(), s.size(),
server->getAddress());
m_state = EXITING;
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();

View File

@ -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