add client

This commit is contained in:
2025-08-08 13:24:50 +02:00
parent b09c7f9efd
commit ac3e949323
14 changed files with 306 additions and 33 deletions

30
include/client/Client.h Normal file
View 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

View 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

View 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

View 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

View 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

View File

@@ -19,15 +19,10 @@ class Server {
void Update();
void UpdateState(const std::shared_ptr<IServerState>& a_State) {
m_State = a_State;
m_State->SetServer(this);
}
void UpdateState(const std::shared_ptr<IServerState>& a_State);
private:
void Update(float a_Delta) {
m_State->Update(a_Delta);
}
void Update(float a_Delta);
friend class IServerState;
};

View File

@@ -1,23 +1,34 @@
#pragma once
#include <client/socket/FakeSocket.h>
#include <optional>
#include <server/IServerSocket.h>
namespace td {
namespace server {
class FakeSocket : public IServerSocket {
private:
std::vector<std::optional<std::shared_ptr<client::FakeSocket>>> m_Clients;
public:
FakeSocket() {}
~FakeSocket() {}
utils::Signal<PeerID, const protocol::PacketBase&> OnSendToFakePeer;
void ConnectFakePeer(PeerID a_Peer);
PeerID ConnectFakePeer(const std::shared_ptr<client::FakeSocket>& a_Socket);
void DisconnectFakePeer(PeerID a_Peer);
void ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet);
protected:
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