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 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;
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user