Merge pull request #188 from teamfisk/FriendlyHost/Join

User friendly server join
This commit is contained in:
Tobias Dahl
2016-03-09 17:49:39 +01:00
10 changed files with 63 additions and 12 deletions
+6 -1
View File
@@ -17,6 +17,7 @@
#include "Network/TCPClient.h" #include "Network/TCPClient.h"
#include "Network/SnapshotDefinitions.h" #include "Network/SnapshotDefinitions.h"
#include "Core/World.h" #include "Core/World.h"
#include "Core/EntityFile.h"
#include "Core/EventBroker.h" #include "Core/EventBroker.h"
#include "Core/ConfigFile.h" #include "Core/ConfigFile.h"
#include "Core/EPlayerDeath.h" #include "Core/EPlayerDeath.h"
@@ -30,7 +31,7 @@
#include "Network/ESearchForServers.h" #include "Network/ESearchForServers.h"
#include "../Game/Events/EDashAbility.h" #include "../Game/Events/EDashAbility.h"
#include "Network/EDisplayServerlist.h" #include "Network/EDisplayServerlist.h"
#include "Network/EConnectRequest.h"
class Client : public Network class Client : public Network
{ {
public: public:
@@ -108,6 +109,8 @@ private:
void sendLocalPlayerTransform(); void sendLocalPlayerTransform();
void becomePlayer(); void becomePlayer();
void displayServerlist(); void displayServerlist();
void removeWorld();
void createMainMenu();
// Mapping Logic // Mapping Logic
// Returns if local EntityID exist in map // Returns if local EntityID exist in map
bool clientServerMapsHasEntity(EntityID clientEntityID); bool clientServerMapsHasEntity(EntityID clientEntityID);
@@ -128,6 +131,8 @@ private:
bool OnDoubleJump(Events::DoubleJump & e); bool OnDoubleJump(Events::DoubleJump & e);
EventRelay<Client, Events::DashAbility> m_EDashAbility; EventRelay<Client, Events::DashAbility> m_EDashAbility;
bool OnDashAbility(const Events::DashAbility& e); bool OnDashAbility(const Events::DashAbility& e);
EventRelay<Client, Events::ConnectRequest> m_EConnectRequest;
bool OnConnectRequest(const Events::ConnectRequest& e);
bool OnSearchForServers(const Events::SearchForServers& e); bool OnSearchForServers(const Events::SearchForServers& e);
UDPClient m_ServerlistRequest; UDPClient m_ServerlistRequest;
+1 -1
View File
@@ -11,7 +11,7 @@ class NetworkClient
public: public:
NetworkClient(); NetworkClient();
virtual ~NetworkClient(); virtual ~NetworkClient();
virtual void Connect(std::string playerName, std::string address, int port) = 0; virtual bool Connect(std::string playerName, std::string address, int port) = 0;
virtual void Disconnect() = 0; virtual void Disconnect() = 0;
virtual void Receive(Packet& packet) = 0; virtual void Receive(Packet& packet) = 0;
virtual void Send(Packet & packet) = 0; virtual void Send(Packet & packet) = 0;
+1
View File
@@ -60,6 +60,7 @@ private:
std::vector<Events::InputCommand> m_InputCommandsToBroadcast; std::vector<Events::InputCommand> m_InputCommandsToBroadcast;
//Timers //Timers
std::clock_t m_StartPingTime; std::clock_t m_StartPingTime;
std::string m_ServerName = "";
// Packet loss logic // Packet loss logic
PacketID m_PacketID = 0; PacketID m_PacketID = 0;
+1 -1
View File
@@ -10,7 +10,7 @@ public:
TCPClient(); TCPClient();
~TCPClient(); ~TCPClient();
void Connect(std::string playerName, std::string address, int port); bool Connect(std::string playerName, std::string address, int port);
void Disconnect(); void Disconnect();
void Receive(Packet& packet); void Receive(Packet& packet);
void Send(Packet & packet); void Send(Packet & packet);
+1 -1
View File
@@ -10,7 +10,7 @@ public:
UDPClient(); UDPClient();
~UDPClient(); ~UDPClient();
void Connect(std::string playerName, std::string address, int port); bool Connect(std::string playerName, std::string address, int port);
void Disconnect(); void Disconnect();
void Receive(Packet& packet); void Receive(Packet& packet);
void Send(Packet & packet); void Send(Packet & packet);
+43 -3
View File
@@ -35,6 +35,7 @@ void Client::Connect(std::string address, int port)
EVENT_SUBSCRIBE_MEMBER(m_EDoubleJump, &Client::OnDoubleJump); EVENT_SUBSCRIBE_MEMBER(m_EDoubleJump, &Client::OnDoubleJump);
EVENT_SUBSCRIBE_MEMBER(m_EDashAbility, &Client::OnDashAbility); EVENT_SUBSCRIBE_MEMBER(m_EDashAbility, &Client::OnDashAbility);
EVENT_SUBSCRIBE_MEMBER(m_ESearchForServers, &Client::OnSearchForServers); EVENT_SUBSCRIBE_MEMBER(m_ESearchForServers, &Client::OnSearchForServers);
EVENT_SUBSCRIBE_MEMBER(m_EConnectRequest, &Client::OnConnectRequest);
auto config = ResourceManager::Load<ConfigFile>("Config.ini"); auto config = ResourceManager::Load<ConfigFile>("Config.ini");
m_Address = address; m_Address = address;
if (address.empty()) { if (address.empty()) {
@@ -180,8 +181,8 @@ void Client::parseTCPConnect(Packet& packet)
Packet UnreliablePacket(MessageType::Connect, m_SendPacketID); Packet UnreliablePacket(MessageType::Connect, m_SendPacketID);
// Add player id and other stuff // Add player id and other stuff
packet.WritePrimitive(m_PlayerID); packet.WritePrimitive(m_PlayerID);
// m_Unreliable.Send(packet); // m_Unreliable.Send(packet);
// LOG_INFO("Sent UDP Connect Server"); // LOG_INFO("Sent UDP Connect Server");
} }
void Client::parsePlayerConnected(Packet & packet) void Client::parsePlayerConnected(Packet & packet)
@@ -456,12 +457,14 @@ void Client::parseSnapshot(Packet& packet)
void Client::disconnect() void Client::disconnect()
{ {
removeWorld();
m_IsConnected = false; m_IsConnected = false;
m_PreviousPacketID = 0; m_PreviousPacketID = 0;
m_PacketID = 0; m_PacketID = 0;
Packet packet(MessageType::Disconnect, m_SendPacketID); Packet packet(MessageType::Disconnect, m_SendPacketID);
m_Reliable.Send(packet); m_Reliable.Send(packet);
m_Reliable.Disconnect(); m_Reliable.Disconnect();
createMainMenu();
} }
bool Client::OnInputCommand(const Events::InputCommand & e) bool Client::OnInputCommand(const Events::InputCommand & e)
@@ -472,7 +475,7 @@ bool Client::OnInputCommand(const Events::InputCommand & e)
if (e.Command == "ConnectToServer") { // Connect for now if (e.Command == "ConnectToServer") { // Connect for now
if (e.Value > 0) { if (e.Value > 0) {
m_Reliable.Connect(m_PlayerName, m_Address, m_Port); //m_Reliable.Connect(m_PlayerName, m_Address, m_Port);
// m_Unreliable.Connect(m_PlayerName, m_Address, m_Port); // m_Unreliable.Connect(m_PlayerName, m_Address, m_Port);
} }
//LOG_DEBUG("Client::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID); //LOG_DEBUG("Client::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID);
@@ -545,6 +548,23 @@ bool Client::OnDashAbility(const Events::DashAbility& e)
return true; return true;
} }
bool Client::OnConnectRequest(const Events::ConnectRequest& e)
{
removeWorld();
if (m_Reliable.Connect(m_PlayerName, e.IP, e.Port)) {
// The client sent a successful connect message
return true;
} else {
// The client could not send a successful connect message
createMainMenu();
// Load the main menu again ?
return false;
}
return false;
}
bool Client::OnSearchForServers(const Events::SearchForServers& e) bool Client::OnSearchForServers(const Events::SearchForServers& e)
{ {
m_SearchingForServers = true; m_SearchingForServers = true;
@@ -671,6 +691,26 @@ void Client::displayServerlist()
} }
} }
void Client::removeWorld()
{
std::vector<EntityID> childrenToBeDeleted;
auto rootEntites = m_World->GetDirectChildren(EntityID_Invalid);
for (auto it = rootEntites.first; it != rootEntites.second; it++) {
childrenToBeDeleted.push_back(it->second);
}
for (int i = 0; i < childrenToBeDeleted.size(); ++i) {
m_World->DeleteEntity(childrenToBeDeleted[i]);
}
}
void Client::createMainMenu()
{
auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/StartMenu.xml");
entityFile->MergeInto(m_World);
}
bool Client::clientServerMapsHasEntity(EntityID clientEntityID) bool Client::clientServerMapsHasEntity(EntityID clientEntityID)
{ {
if (m_ClientIDToServerID.find(clientEntityID) != m_ClientIDToServerID.end()) { if (m_ClientIDToServerID.find(clientEntityID) != m_ClientIDToServerID.end()) {
+3 -1
View File
@@ -7,6 +7,8 @@ Server::Server(World* world, EventBroker* eventBroker, int port)
ConfigFile* config = ResourceManager::Load<ConfigFile>("Config.ini"); ConfigFile* config = ResourceManager::Load<ConfigFile>("Config.ini");
snapshotInterval = 1000 * config->Get<float>("Networking.SnapshotInterval", 0.05f); snapshotInterval = 1000 * config->Get<float>("Networking.SnapshotInterval", 0.05f);
pingIntervalMs = config->Get<float>("Networking.PingIntervalMs", 1000); pingIntervalMs = config->Get<float>("Networking.PingIntervalMs", 1000);
m_ServerName = config->Get<std::string>("Networking.Name", "Unnamed");
// Subscribe to events // Subscribe to events
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Server::OnInputCommand); EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Server::OnInputCommand);
EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &Server::OnPlayerSpawned); EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &Server::OnPlayerSpawned);
@@ -410,7 +412,7 @@ void Server::parseServerlistRequest(boost::asio::ip::udp::endpoint endpoint)
Packet packet(MessageType::ServerlistRequest); Packet packet(MessageType::ServerlistRequest);
packet.WriteString(m_Reliable.Address()); packet.WriteString(m_Reliable.Address());
packet.WritePrimitive<int>(m_Reliable.Port()); packet.WritePrimitive<int>(m_Reliable.Port());
packet.WriteString("SERVERNAME"); packet.WriteString(m_ServerName);
packet.WritePrimitive<int>(m_ConnectedPlayers.size()); packet.WritePrimitive<int>(m_ConnectedPlayers.size());
m_ServerlistRequest.Send(packet); m_ServerlistRequest.Send(packet);
} }
+4 -1
View File
@@ -10,7 +10,7 @@ TCPClient::~TCPClient()
{ {
} }
void TCPClient::Connect(std::string playerName, std::string address, int port) bool TCPClient::Connect(std::string playerName, std::string address, int port)
{ {
if (m_Socket) { if (m_Socket) {
if (m_IsConnected) { if (m_IsConnected) {
@@ -19,6 +19,7 @@ void TCPClient::Connect(std::string playerName, std::string address, int port)
Send(packet); Send(packet);
LOG_INFO("Connect message sent again!"); LOG_INFO("Connect message sent again!");
} }
return true;
} }
else if (!m_IsConnected) { else if (!m_IsConnected) {
boost::system::error_code error = boost::asio::error::host_not_found; boost::system::error_code error = boost::asio::error::host_not_found;
@@ -34,11 +35,13 @@ void TCPClient::Connect(std::string playerName, std::string address, int port)
packet.WriteString(playerName); packet.WriteString(playerName);
Send(packet); Send(packet);
LOG_INFO("Connect message sent!"); LOG_INFO("Connect message sent!");
return true;
} }
// If error // If error
else { else {
m_Socket->close(); m_Socket->close();
m_Socket = nullptr; m_Socket = nullptr;
return false;
} }
} }
} }
+3 -2
View File
@@ -10,14 +10,15 @@ UDPClient::~UDPClient()
{ {
} }
void UDPClient::Connect(std::string playerName, std::string address, int port) bool UDPClient::Connect(std::string playerName, std::string address, int port)
{ {
if (m_Socket) { if (m_Socket) {
return; return false;
} }
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address().from_string(address), port); m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address().from_string(address), port);
m_Socket = boost::shared_ptr<boost::asio::ip::udp::socket>(new boost::asio::ip::udp::socket(m_IOService)); m_Socket = boost::shared_ptr<boost::asio::ip::udp::socket>(new boost::asio::ip::udp::socket(m_IOService));
m_Socket->open(boost::asio::ip::udp::v4()); m_Socket->open(boost::asio::ip::udp::v4());
return true;
} }
void UDPClient::Disconnect() void UDPClient::Disconnect()
-1
View File
@@ -49,6 +49,5 @@ bool ServerListSystem::OnServerListRecieved(const Events::DisplayServerlist& e)
(int&)cIdentity["PlayersConnected"] = e.Serverlist[i].PlayersConnected; (int&)cIdentity["PlayersConnected"] = e.Serverlist[i].PlayersConnected;
} }
} }
return 1; return 1;
} }