add client
This commit is contained in:
30
include/client/Client.h
Normal file
30
include/client/Client.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <client/IClientSocket.h>
|
||||||
|
#include <client/IClientState.h>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace client {
|
||||||
|
|
||||||
|
class Client {
|
||||||
|
private:
|
||||||
|
std::shared_ptr<IClientSocket> m_Socket;
|
||||||
|
std::shared_ptr<IClientState> m_State;
|
||||||
|
std::chrono::time_point<std::chrono::system_clock> m_LastTime;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Client(const std::shared_ptr<IClientSocket>& a_Socket) : m_Socket(a_Socket), m_LastTime(std::chrono::system_clock::now()) {}
|
||||||
|
|
||||||
|
void Update();
|
||||||
|
|
||||||
|
void UpdateState(const std::shared_ptr<IClientState>& a_State);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void Update(float a_Delta);
|
||||||
|
|
||||||
|
friend class IClientState;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace client
|
||||||
|
} // namespace td
|
||||||
23
include/client/IClientSocket.h
Normal file
23
include/client/IClientSocket.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <td/Types.h>
|
||||||
|
#include <td/misc/Signal.h>
|
||||||
|
#include <td/protocol/packet/Packets.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace client {
|
||||||
|
|
||||||
|
class IClientSocket {
|
||||||
|
public:
|
||||||
|
utils::Signal<> OnConnect;
|
||||||
|
utils::Signal<> OnDisconnect;
|
||||||
|
utils::Signal<const protocol::PacketBase&> OnReceive;
|
||||||
|
|
||||||
|
virtual void Send(const protocol::PacketBase& a_Packet) = 0;
|
||||||
|
|
||||||
|
IClientSocket() {}
|
||||||
|
virtual ~IClientSocket() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace client
|
||||||
|
} // namespace td
|
||||||
33
include/client/IClientState.h
Normal file
33
include/client/IClientState.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <client/IClientSocket.h>
|
||||||
|
#include <td/misc/SlotGuard.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace client {
|
||||||
|
|
||||||
|
class Client;
|
||||||
|
|
||||||
|
class IClientState : private utils::SlotGuard {
|
||||||
|
public:
|
||||||
|
virtual void HandlePacket(const protocol::PacketBase& a_Packet) = 0;
|
||||||
|
virtual void Update(float a_Delta) = 0;
|
||||||
|
|
||||||
|
IClientState();
|
||||||
|
virtual ~IClientState();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||||
|
void SetNewState(const std::shared_ptr<IClientState>& a_NewState);
|
||||||
|
virtual void Init() {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Client* m_Client;
|
||||||
|
|
||||||
|
void SetClient(Client* a_Client);
|
||||||
|
|
||||||
|
friend class Client;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace server
|
||||||
|
} // namespace td
|
||||||
33
include/client/socket/FakeSocket.h
Normal file
33
include/client/socket/FakeSocket.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <client/IClientSocket.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
namespace server {
|
||||||
|
class FakeSocket;
|
||||||
|
} // namespace server
|
||||||
|
|
||||||
|
|
||||||
|
namespace client {
|
||||||
|
|
||||||
|
class FakeSocket : public IClientSocket {
|
||||||
|
private:
|
||||||
|
std::shared_ptr<server::FakeSocket> m_Server;
|
||||||
|
PeerID m_PeerId;
|
||||||
|
|
||||||
|
struct Private{ explicit Private() = default; };
|
||||||
|
|
||||||
|
public:
|
||||||
|
FakeSocket(Private) {}
|
||||||
|
~FakeSocket() {}
|
||||||
|
|
||||||
|
static std::shared_ptr<FakeSocket> Connect(const std::shared_ptr<server::FakeSocket>& a_Server);
|
||||||
|
|
||||||
|
void ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet);
|
||||||
|
|
||||||
|
virtual void Send(const protocol::PacketBase& a_Packet) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace client
|
||||||
|
} // namespace td
|
||||||
26
include/client/state/GameState.h
Normal file
26
include/client/state/GameState.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <client/IClientState.h>
|
||||||
|
#include <td/game/World.h>
|
||||||
|
#include <td/simulation/ServerSimulation.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace client {
|
||||||
|
|
||||||
|
class GameState : public IClientState {
|
||||||
|
private:
|
||||||
|
std::shared_ptr<game::World> m_World;
|
||||||
|
|
||||||
|
public:
|
||||||
|
GameState(const std::shared_ptr<game::World>& a_World);
|
||||||
|
~GameState() {}
|
||||||
|
|
||||||
|
virtual void HandlePacket(const protocol::PacketBase& a_Packet) override;
|
||||||
|
virtual void Update(float a_Delta) override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void Init() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace client
|
||||||
|
} // namespace td
|
||||||
@@ -19,15 +19,10 @@ class Server {
|
|||||||
|
|
||||||
void Update();
|
void Update();
|
||||||
|
|
||||||
void UpdateState(const std::shared_ptr<IServerState>& a_State) {
|
void UpdateState(const std::shared_ptr<IServerState>& a_State);
|
||||||
m_State = a_State;
|
|
||||||
m_State->SetServer(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Update(float a_Delta) {
|
void Update(float a_Delta);
|
||||||
m_State->Update(a_Delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
friend class IServerState;
|
friend class IServerState;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,23 +1,34 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <client/socket/FakeSocket.h>
|
||||||
|
#include <optional>
|
||||||
#include <server/IServerSocket.h>
|
#include <server/IServerSocket.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace server {
|
namespace server {
|
||||||
|
|
||||||
class FakeSocket : public IServerSocket {
|
class FakeSocket : public IServerSocket {
|
||||||
|
private:
|
||||||
|
std::vector<std::optional<std::shared_ptr<client::FakeSocket>>> m_Clients;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FakeSocket() {}
|
FakeSocket() {}
|
||||||
~FakeSocket() {}
|
~FakeSocket() {}
|
||||||
|
|
||||||
utils::Signal<PeerID, const protocol::PacketBase&> OnSendToFakePeer;
|
PeerID ConnectFakePeer(const std::shared_ptr<client::FakeSocket>& a_Socket);
|
||||||
|
|
||||||
void ConnectFakePeer(PeerID a_Peer);
|
|
||||||
void DisconnectFakePeer(PeerID a_Peer);
|
void DisconnectFakePeer(PeerID a_Peer);
|
||||||
void ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet);
|
void ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void SendPeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) override;
|
virtual void SendPeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* \return -1 if all previous ids are not free
|
||||||
|
*/
|
||||||
|
int GetNextFreeId();
|
||||||
|
|
||||||
|
friend class client::FakeSocket;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace server
|
} // namespace server
|
||||||
|
|||||||
24
src/client/Client.cpp
Normal file
24
src/client/Client.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#include <client/Client.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace client {
|
||||||
|
|
||||||
|
void Client::Update() {
|
||||||
|
auto timeElapsed = std::chrono::system_clock::now() - m_LastTime;
|
||||||
|
float timeSeconds = std::chrono::duration<float, std::chrono::seconds::period>(timeElapsed).count();
|
||||||
|
Update(timeSeconds);
|
||||||
|
m_LastTime = std::chrono::system_clock::now();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::UpdateState(const std::shared_ptr<IClientState>& a_State) {
|
||||||
|
m_State = a_State;
|
||||||
|
m_State->SetClient(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::Update(float a_Delta) {
|
||||||
|
assert(m_State);
|
||||||
|
m_State->Update(a_Delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace client
|
||||||
|
} // namespace td
|
||||||
27
src/client/IClientState.cpp
Normal file
27
src/client/IClientState.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include <client/IClientState.h>
|
||||||
|
#include <client/Client.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace client {
|
||||||
|
|
||||||
|
void IClientState::SetClient(Client* a_Client) {
|
||||||
|
assert(a_Client);
|
||||||
|
m_Client = a_Client;
|
||||||
|
Connect(m_Client->m_Socket->OnReceive, std::bind(&IClientState::HandlePacket, this, std::placeholders::_1));
|
||||||
|
Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
IClientState::IClientState() : m_Client(nullptr) {}
|
||||||
|
|
||||||
|
IClientState::~IClientState() {}
|
||||||
|
|
||||||
|
void IClientState::SendPacket(const protocol::PacketBase& a_Packet) {
|
||||||
|
m_Client->m_Socket->Send(a_Packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IClientState::SetNewState(const std::shared_ptr<IClientState>& a_NewState) {
|
||||||
|
m_Client->UpdateState(a_NewState);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace server
|
||||||
|
} // namespace td
|
||||||
19
src/client/socket/FakeSocket.cpp
Normal file
19
src/client/socket/FakeSocket.cpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include <client/socket/FakeSocket.h>
|
||||||
|
#include <server/socket/FakeSocket.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace client {
|
||||||
|
|
||||||
|
void FakeSocket::Send(const protocol::PacketBase& a_Packet) {
|
||||||
|
m_Server->OnReceivePeer(m_PeerId, a_Packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<FakeSocket> FakeSocket::Connect(const std::shared_ptr<server::FakeSocket>& a_Server) {
|
||||||
|
auto socket = std::make_shared<FakeSocket>(Private());
|
||||||
|
socket->m_Server = a_Server;
|
||||||
|
socket->m_PeerId = a_Server->ConnectFakePeer(socket);
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace client
|
||||||
|
} // namespace td
|
||||||
12
src/client/state/GameState.cpp
Normal file
12
src/client/state/GameState.cpp
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include <client/state/GameState.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace client {
|
||||||
|
|
||||||
|
GameState::GameState(const std::shared_ptr<game::World>& a_World) : m_World(a_World) {}
|
||||||
|
void GameState::HandlePacket(const protocol::PacketBase& a_Packet) {}
|
||||||
|
void GameState::Update(float a_Delta) {}
|
||||||
|
void GameState::Init() {}
|
||||||
|
|
||||||
|
} // namespace client
|
||||||
|
} // namespace td
|
||||||
45
src/main.cpp
45
src/main.cpp
@@ -19,6 +19,10 @@
|
|||||||
#include <server/socket/FakeSocket.h>
|
#include <server/socket/FakeSocket.h>
|
||||||
#include <server/state/GameState.h>
|
#include <server/state/GameState.h>
|
||||||
|
|
||||||
|
#include <client/Client.h>
|
||||||
|
#include <client/socket/FakeSocket.h>
|
||||||
|
#include <client/state/GameState.h>
|
||||||
|
|
||||||
class WorldApply : public td::protocol::PacketHandler {
|
class WorldApply : public td::protocol::PacketHandler {
|
||||||
private:
|
private:
|
||||||
td::game::World& m_World;
|
td::game::World& m_World;
|
||||||
@@ -102,8 +106,12 @@ int main(int argc, char** argv) {
|
|||||||
td::game::WorldPtr serverWorld = GetWorld();
|
td::game::WorldPtr serverWorld = GetWorld();
|
||||||
|
|
||||||
// server
|
// server
|
||||||
auto fakeSocket = std::make_shared<td::server::FakeSocket>();
|
auto serverFakeSocket = std::make_shared<td::server::FakeSocket>();
|
||||||
td::server::Server server(fakeSocket);
|
td::server::Server server(serverFakeSocket);
|
||||||
|
|
||||||
|
// client
|
||||||
|
auto clientFakeSocket = td::client::FakeSocket::Connect(serverFakeSocket);
|
||||||
|
td::client::Client client(clientFakeSocket);
|
||||||
|
|
||||||
// init GL context
|
// init GL context
|
||||||
td::Display display(1920, 1080, "Tower-Defense 2");
|
td::Display display(1920, 1080, "Tower-Defense 2");
|
||||||
@@ -125,32 +133,31 @@ int main(int argc, char** argv) {
|
|||||||
td::sim::ClientSimulation simulation(*clientWorld, td::STEP_TIME);
|
td::sim::ClientSimulation simulation(*clientWorld, td::STEP_TIME);
|
||||||
ClientHandler clientHandler(simulation);
|
ClientHandler clientHandler(simulation);
|
||||||
|
|
||||||
simulation.OnMissingLockSteps.Connect([&fakeSocket](const std::vector<td::StepTime>& a_MissingSteps){
|
|
||||||
fakeSocket->ReceiveFromFakePeer(0, td::protocol::packets::LockStepRequestPacket(a_MissingSteps));
|
|
||||||
});
|
|
||||||
|
|
||||||
// temporary tests
|
|
||||||
display.OnKeyDown.Connect([&fakeSocket](SDL_Keycode key) {
|
|
||||||
if (key == SDLK_A) {
|
|
||||||
fakeSocket->ReceiveFromFakePeer(0, td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
|
||||||
} else if (key == SDLK_Z) {
|
|
||||||
fakeSocket->ReceiveFromFakePeer(0, td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// make a fake player join
|
|
||||||
fakeSocket->ConnectFakePeer(0);
|
|
||||||
|
|
||||||
// packets from the server to the client
|
// packets from the server to the client
|
||||||
fakeSocket->OnSendToFakePeer.Connect([&clientHandler](td::PeerID a_Peer, const td::protocol::PacketBase& a_Packet) {
|
clientFakeSocket->OnReceive.Connect([&clientHandler](const td::protocol::PacketBase& a_Packet) {
|
||||||
a_Packet.Dispatch(clientHandler);
|
a_Packet.Dispatch(clientHandler);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
simulation.OnMissingLockSteps.Connect([&clientFakeSocket](const std::vector<td::StepTime>& a_MissingSteps) {
|
||||||
|
clientFakeSocket->Send(td::protocol::packets::LockStepRequestPacket(a_MissingSteps));
|
||||||
|
});
|
||||||
|
|
||||||
|
// temporary tests
|
||||||
|
display.OnKeyDown.Connect([&clientFakeSocket](SDL_Keycode key) {
|
||||||
|
if (key == SDLK_A) {
|
||||||
|
clientFakeSocket->Send(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
||||||
|
} else if (key == SDLK_Z) {
|
||||||
|
clientFakeSocket->Send(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
server.UpdateState(std::make_shared<td::server::GameState>(serverWorld));
|
server.UpdateState(std::make_shared<td::server::GameState>(serverWorld));
|
||||||
|
client.UpdateState(std::make_shared<td::client::GameState>(clientWorld));
|
||||||
|
|
||||||
while (!display.IsCloseRequested()) {
|
while (!display.IsCloseRequested()) {
|
||||||
display.PollEvents();
|
display.PollEvents();
|
||||||
server.Update();
|
server.Update();
|
||||||
|
client.Update();
|
||||||
float lerp = simulation.Update();
|
float lerp = simulation.Update();
|
||||||
renderer.Render(lerp);
|
renderer.Render(lerp);
|
||||||
display.Update();
|
display.Update();
|
||||||
|
|||||||
@@ -10,5 +10,15 @@ void Server::Update() {
|
|||||||
m_LastTime = std::chrono::system_clock::now();
|
m_LastTime = std::chrono::system_clock::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Server::UpdateState(const std::shared_ptr<IServerState>& a_State) {
|
||||||
|
m_State = a_State;
|
||||||
|
m_State->SetServer(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Server::Update(float a_Delta) {
|
||||||
|
assert(m_State);
|
||||||
|
m_State->Update(a_Delta);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace server
|
} // namespace server
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -4,20 +4,43 @@ namespace td {
|
|||||||
namespace server {
|
namespace server {
|
||||||
|
|
||||||
void FakeSocket::SendPeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) {
|
void FakeSocket::SendPeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) {
|
||||||
OnSendToFakePeer(a_Peer, a_Packet);
|
auto socket = m_Clients.at(a_Peer);
|
||||||
|
assert(socket.has_value());
|
||||||
|
socket.value()->OnReceive(a_Packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FakeSocket::ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) {
|
void FakeSocket::ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) {
|
||||||
OnReceivePeer(a_Peer, a_Packet);
|
OnReceivePeer(a_Peer, a_Packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FakeSocket::ConnectFakePeer(PeerID a_Peer) {
|
PeerID FakeSocket::ConnectFakePeer(const std::shared_ptr<client::FakeSocket>& a_Socket) {
|
||||||
OnConnectPeer(a_Peer);
|
int peerId = GetNextFreeId();
|
||||||
|
if (peerId == -1) {
|
||||||
|
peerId = m_Clients.size();
|
||||||
|
m_Clients.push_back(a_Socket);
|
||||||
|
} else {
|
||||||
|
m_Clients.emplace(m_Clients.begin() + peerId, a_Socket);
|
||||||
|
}
|
||||||
|
a_Socket->OnConnect();
|
||||||
|
OnConnectPeer(peerId);
|
||||||
|
return peerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FakeSocket::DisconnectFakePeer(PeerID a_Peer) {
|
void FakeSocket::DisconnectFakePeer(PeerID a_Peer) {
|
||||||
|
auto socket = m_Clients.at(a_Peer);
|
||||||
|
assert(socket.has_value());
|
||||||
|
socket.value()->OnDisconnect();
|
||||||
OnDisconnectPeer(a_Peer);
|
OnDisconnectPeer(a_Peer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int FakeSocket::GetNextFreeId() {
|
||||||
|
auto it = std::find_if(m_Clients.begin(), m_Clients.end(), [](const auto& a_Value) { return !a_Value.has_value(); });
|
||||||
|
|
||||||
|
if (it == m_Clients.end())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return std::distance(m_Clients.begin(), it);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace server
|
} // namespace server
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
Reference in New Issue
Block a user