WIP connect from menu

This commit is contained in:
stiffly
2016-03-09 14:33:20 +01:00
parent 67ccb5bee5
commit 09171a1a18
8 changed files with 31 additions and 8 deletions
+3
View File
@@ -30,6 +30,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
{ {
@@ -128,6 +129,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 -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);
+18 -1
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()) {
@@ -472,7 +473,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 +546,22 @@ bool Client::OnDashAbility(const Events::DashAbility& e)
return true; return true;
} }
bool Client::OnConnectRequest(const Events::ConnectRequest& e)
{
if (m_Reliable.Connect(m_PlayerName, e.IP, e.Port)) {
// The client send a successful connect message
auto rootEntities = m_World->GetDirectChildren(EntityID_Invalid);
for (auto it = rootEntities.first; it != rootEntities.second; it++) {
m_World->DeleteEntity(it->first);
}
} else {
// The client could not send a successful connect message
}
return false;
}
bool Client::OnSearchForServers(const Events::SearchForServers& e) bool Client::OnSearchForServers(const Events::SearchForServers& e)
{ {
m_SearchingForServers = true; m_SearchingForServers = true;
+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
@@ -48,6 +48,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;
} }