begin player auth
This commit is contained in:
@@ -13,7 +13,7 @@ class Client : public StateMachine<Client, void, float> {
|
||||
std::shared_ptr<IClientSocket> m_Socket;
|
||||
|
||||
public:
|
||||
Client(const std::shared_ptr<IClientSocket>& a_Socket) : m_Socket(a_Socket) {}
|
||||
Client(const std::shared_ptr<IClientSocket>& a_Socket, const std::string& a_PlayerName);
|
||||
|
||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||
|
||||
|
||||
@@ -6,14 +6,13 @@
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class ClientState : public Client::State, private utils::SlotGuard {
|
||||
class ClientState : public Client::State, public protocol::PacketHandler, private utils::SlotGuard {
|
||||
public:
|
||||
ClientState(Client& a_Client);
|
||||
virtual ~ClientState() {}
|
||||
|
||||
protected:
|
||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) {}
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
|
||||
@@ -23,8 +23,8 @@ class GameState : public ClientState {
|
||||
return m_CurrentLerp;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Handle(const protocol::packets::LockStepsPacket& a_LockStep) override;
|
||||
virtual void Handle(const protocol::packets::LockStepResponsePacket& a_LockStep) override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
|
||||
21
include/client/state/LoggingState.h
Normal file
21
include/client/state/LoggingState.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/ClientState.h>
|
||||
#include <td/game/World.h>
|
||||
#include <td/simulation/ClientSimulation.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class LoggingState : public ClientState {
|
||||
public:
|
||||
LoggingState(Client& a_Client, const std::string& a_PlayerName);
|
||||
~LoggingState();
|
||||
|
||||
virtual void Update(float a_Delta) override {}
|
||||
|
||||
virtual void Handle(const protocol::packets::PlayerJoinPacket& a_Packet) override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
24
include/server/ConnectionHandler.h
Normal file
24
include/server/ConnectionHandler.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/Server.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class IServerSocket;
|
||||
|
||||
class ConnectionHandler : public protocol::PacketHandler {
|
||||
private:
|
||||
IServerSocket& m_Server;
|
||||
PlayerID m_Player;
|
||||
|
||||
public:
|
||||
ConnectionHandler(IServerSocket& a_Server, PlayerID a_Player);
|
||||
~ConnectionHandler() = default;
|
||||
|
||||
virtual void Handle(const protocol::packets::PlayerLoginPacket& a_Packet) override;
|
||||
virtual void Handle(const protocol::packets::DisconnectPacket& a_Packet) override;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -9,13 +9,15 @@ namespace server {
|
||||
|
||||
class IServerSocket {
|
||||
public:
|
||||
utils::Signal<PlayerID> OnConnect;
|
||||
utils::Signal<PlayerID> OnDisconnect;
|
||||
utils::Signal<PlayerID, const protocol::PlayerInfo&> OnPlayerJoin;
|
||||
utils::Signal<PlayerID> OnPlayerLeave;
|
||||
utils::Signal<PlayerID, const protocol::PacketBase&> OnReceive;
|
||||
|
||||
void Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet);
|
||||
void Broadcast(const protocol::PacketBase& a_Packet);
|
||||
|
||||
void Disconnect(PlayerID a_PlayerId);
|
||||
|
||||
IServerSocket() {}
|
||||
virtual ~IServerSocket() {}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ class ServerState : public Server::State, private utils::SlotGuard {
|
||||
public:
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) = 0;
|
||||
virtual void Update(float a_Delta) = 0;
|
||||
virtual void OnPlayerJoin(PlayerID a_Id, const td::protocol::PlayerInfo& a_Info) {}
|
||||
virtual void OnPlayerLeave(PlayerID a_Id) {}
|
||||
|
||||
ServerState(Server& a_Server);
|
||||
virtual ~ServerState();
|
||||
|
||||
@@ -66,12 +66,18 @@ using WorldDataPacket = PacketMessage<pdata::WorldData, PacketID::WorldData>;
|
||||
|
||||
} // namespace packets
|
||||
|
||||
using AllPackets = std::tuple<packets::BeginGamePacket, packets::ChatMessagePacket, packets::DisconnectPacket,
|
||||
packets::KeepAlivePacket, packets::LockStepRequestPacket, packets::LockStepResponsePacket, packets::LockStepsPacket, packets::LoggingSuccessPacket, packets::PlaceTowerPacket,
|
||||
packets::PlayerJoinPacket, packets::PlayerLeavePacket, packets::PlayerLoginPacket, packets::PredictCommandPacket,
|
||||
packets::SpawnTroopPacket, packets::WorldHeaderPacket, packets::WorldDataPacket>;
|
||||
using AllPackets =
|
||||
std::tuple<packets::BeginGamePacket, packets::ChatMessagePacket, packets::DisconnectPacket, packets::KeepAlivePacket,
|
||||
packets::LockStepRequestPacket, packets::LockStepResponsePacket, packets::LockStepsPacket, packets::LoggingSuccessPacket,
|
||||
packets::PlaceTowerPacket, packets::PlayerJoinPacket, packets::PlayerLeavePacket, packets::PlayerLoginPacket,
|
||||
packets::PredictCommandPacket, packets::SpawnTroopPacket, packets::WorldHeaderPacket, packets::WorldDataPacket>;
|
||||
|
||||
class PacketHandler : public sp::GenericHandler<AllPackets> {};
|
||||
class PacketHandler : public sp::GenericHandler<AllPackets> {
|
||||
public:
|
||||
void HandleBase(const PacketBase& a_Packet) {
|
||||
a_Packet.Dispatch(*this);
|
||||
}
|
||||
};
|
||||
|
||||
using PacketDispatcher = sp::MessageDispatcher<PacketBase>;
|
||||
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
#include <client/Client.h>
|
||||
|
||||
#include <client/state/LoggingState.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
Client::Client(const std::shared_ptr<IClientSocket>& a_Socket, const std::string& a_PlayerName) : m_Socket(a_Socket) {
|
||||
ChangeState<LoggingState>(a_PlayerName);
|
||||
}
|
||||
|
||||
void Client::SendPacket(const protocol::PacketBase& a_Packet) {
|
||||
m_Socket->Send(a_Packet);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace td {
|
||||
namespace client {
|
||||
|
||||
ClientState::ClientState(Client& a_Client) : Client::State(a_Client) {
|
||||
Connect(m_StateMachine.m_Socket->OnReceive, std::bind(&ClientState::HandlePacket, this, std::placeholders::_1));
|
||||
Connect(m_StateMachine.m_Socket->OnReceive, std::bind(&ClientState::HandleBase, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void ClientState::SendPacket(const protocol::PacketBase& a_Packet) {
|
||||
|
||||
@@ -3,24 +3,6 @@
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class ClientHandler : public protocol::PacketHandler {
|
||||
private:
|
||||
sim::ClientSimulation& m_Simulation;
|
||||
|
||||
using protocol::PacketHandler::Handle;
|
||||
|
||||
public:
|
||||
ClientHandler(sim::ClientSimulation& a_Simulation) : m_Simulation(a_Simulation) {}
|
||||
|
||||
void Handle(const protocol::packets::LockStepsPacket& a_LockStep) {
|
||||
m_Simulation.Handle(a_LockStep);
|
||||
}
|
||||
|
||||
void Handle(const protocol::packets::LockStepResponsePacket& a_LockStep) {
|
||||
m_Simulation.Handle(a_LockStep);
|
||||
}
|
||||
};
|
||||
|
||||
GameState::GameState(Client& a_Client, const std::shared_ptr<game::World>& a_World, std::uint64_t a_StepTime) :
|
||||
ClientState(a_Client), m_World(a_World), m_Simulation(a_World, a_StepTime) {
|
||||
m_Simulation.OnMissingLockSteps.Connect([this](const std::vector<td::StepTime>& a_MissingSteps) {
|
||||
@@ -28,9 +10,12 @@ GameState::GameState(Client& a_Client, const std::shared_ptr<game::World>& a_Wor
|
||||
});
|
||||
}
|
||||
|
||||
void GameState::HandlePacket(const protocol::PacketBase& a_Packet) {
|
||||
ClientHandler handler(m_Simulation);
|
||||
a_Packet.Dispatch(handler);
|
||||
void GameState::Handle(const protocol::packets::LockStepsPacket& a_LockStep) {
|
||||
m_Simulation.Handle(a_LockStep);
|
||||
}
|
||||
|
||||
void GameState::Handle(const protocol::packets::LockStepResponsePacket& a_LockStep) {
|
||||
m_Simulation.Handle(a_LockStep);
|
||||
}
|
||||
|
||||
void GameState::Update(float a_Delta) {
|
||||
|
||||
19
src/client/state/LoggingState.cpp
Normal file
19
src/client/state/LoggingState.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <client/state/LoggingState.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
LoggingState::LoggingState(Client& a_Client, const std::string& a_PlayerName) : ClientState(a_Client) {
|
||||
SendPacket(td::protocol::packets::PlayerLoginPacket(a_PlayerName));
|
||||
}
|
||||
|
||||
LoggingState::~LoggingState() {}
|
||||
|
||||
void LoggingState::Handle(const protocol::packets::PlayerJoinPacket& a_Packet) {
|
||||
std::cout << "[Client] " << a_Packet->m_Player.m_PlayerName << "(" << +a_Packet->m_Player.m_PlayerId << ") joined !\n";
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
24
src/server/ConnectionHandler.cpp
Normal file
24
src/server/ConnectionHandler.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <server/ConnectionHandler.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
ConnectionHandler::ConnectionHandler(IServerSocket& a_Server, PlayerID a_Player) : m_Server(a_Server), m_Player(a_Player) {}
|
||||
|
||||
void ConnectionHandler::Handle(const protocol::packets::PlayerLoginPacket& a_Packet) {
|
||||
std::cout << "[Server] " << a_Packet->m_PlayerName << " tried to join !\n";
|
||||
protocol::PlayerInfo pInfo{m_Player, a_Packet->m_PlayerName};
|
||||
m_Server.OnPlayerJoin(m_Player, pInfo);
|
||||
m_Server.Broadcast(protocol::packets::PlayerJoinPacket(pInfo));
|
||||
// TODO: send already existing players
|
||||
}
|
||||
|
||||
void ConnectionHandler::Handle(const protocol::packets::DisconnectPacket& a_Packet) {
|
||||
std::cout << "[Server] " << +m_Player << " wants to disconnect !\n";
|
||||
m_Server.Disconnect(m_Player);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -1,20 +1,25 @@
|
||||
#include <server/IServerSocket.h>
|
||||
|
||||
#include <server/ConnectionHandler.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
void IServerSocket::OnConnectPeer(PeerID a_PeerId) {
|
||||
// here, the client is not a player yet (we need to wait for auth)
|
||||
m_Ids.AddConnection(a_PeerId);
|
||||
OnConnect(m_Ids.GetPlayerId(a_PeerId));
|
||||
}
|
||||
|
||||
void IServerSocket::OnDisconnectPeer(PeerID a_PeerId) {
|
||||
OnDisconnect(m_Ids.GetPlayerId(a_PeerId));
|
||||
OnPlayerLeave(m_Ids.GetPlayerId(a_PeerId));
|
||||
m_Ids.RemovePeer(a_PeerId);
|
||||
}
|
||||
|
||||
void IServerSocket::OnReceivePeer(PeerID a_PeerId, const protocol::PacketBase& a_Packet) {
|
||||
OnReceive(m_Ids.GetPlayerId(a_PeerId), a_Packet);
|
||||
auto playerId = m_Ids.GetPlayerId(a_PeerId);
|
||||
ConnectionHandler handler(*this, playerId);
|
||||
a_Packet.Dispatch(handler);
|
||||
OnReceive(playerId, a_Packet);
|
||||
}
|
||||
|
||||
void IServerSocket::Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet) {
|
||||
@@ -27,5 +32,9 @@ void IServerSocket::Broadcast(const protocol::PacketBase& a_Packet) {
|
||||
}
|
||||
}
|
||||
|
||||
void IServerSocket::Disconnect(PlayerID a_PlayerId) {
|
||||
OnDisconnectPeer(m_Ids.GetPeerId(a_PlayerId));
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <server/ServerState.h>
|
||||
#include <server/Server.h>
|
||||
#include <server/ServerState.h>
|
||||
|
||||
#include <td/common/StateMachine.h>
|
||||
|
||||
@@ -8,6 +8,8 @@ namespace server {
|
||||
|
||||
ServerState::ServerState(Server& a_Server) : Server::State(a_Server) {
|
||||
Connect(m_StateMachine.m_Socket->OnReceive, std::bind(&ServerState::HandlePacket, this, std::placeholders::_1, std::placeholders::_2));
|
||||
Connect(m_StateMachine.m_Socket->OnPlayerJoin, std::bind(&ServerState::OnPlayerJoin, this, std::placeholders::_1, std::placeholders::_2));
|
||||
Connect(m_StateMachine.m_Socket->OnPlayerLeave, std::bind(&ServerState::OnPlayerLeave, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
ServerState::~ServerState() {}
|
||||
@@ -21,4 +23,4 @@ void ServerState::BroadcastPacket(const protocol::PacketBase& a_Packet) {
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace
|
||||
} // namespace td
|
||||
|
||||
@@ -71,7 +71,7 @@ DebugWorldState::DebugWorldState(Display& a_Display) : DisplayState(a_Display) {
|
||||
// client
|
||||
game::WorldPtr clientWorld = GetWorld();
|
||||
auto clientFakeSocket = client::FakeSocket::Connect(serverFakeSocket);
|
||||
m_Client = std::make_unique<client::Client>(clientFakeSocket);
|
||||
m_Client = std::make_unique<client::Client>(clientFakeSocket, "Player0");
|
||||
|
||||
// render
|
||||
m_Renderer.AddRenderer<render::WorldRenderer>(m_Camera, *clientWorld);
|
||||
|
||||
Reference in New Issue
Block a user