#pragma once #include "CNetTransceiver.h" #include "CComponent.h" #include "U_TypeDefs.h" #include "U_Networking.h" #include "CNetMessagesManager.h" #include #include class CNetServerInfo : public CNetMessage { public: void V_Write(CBufferWrapper& wrapper) override; void V_Read(CBufferWrapper& wrapper) override; void Process() override; double Rate = 33.0; DEFINE_NET_MESSAGE(); }; class CServer : public CComponent { public: class CClient { public: CClient(const CEndPoint& point); bool operator==(const CClient& other) const; void Update(); CEndPoint EndPoint; bool Authorized = false; std::wstring NickName; std::uint16_t NextPacketID = 0; CNetMessagesManager MessagesManager; CTimePoint LastPacket; }; void V_PostInit() override; void V_Update() override; bool IsClientExist(const CEndPoint& endpoint); CClient& GetClient(const CEndPoint& endpoint); void ProcessDatagram(boost::asio::ip::udp::endpoint point, std::vector& buffer, CTimePoint ptime); void HandleClientPacket(CClient& client, std::vector& buffer, CTimePoint ptime); void SendReliableMessageTo(CClient& client, std::shared_ptr msg); void SendUnreliableMessageTo(CClient& client, std::shared_ptr msg); void SendReliableMessageToAll(std::shared_ptr msg); void SendUnreliableMessageToAll(std::shared_ptr msg); void SendServerInfoTo(CClient& client); void SendServerInfoToAll(); clientid_t GetFreeID(); void SetRate(double timesPerSecond = 33.0); double GetRate() const; std::unique_ptr Transceiver; std::unordered_map Clients; DEFINE_SOL_USERTYPE(); DEFINE_COMPONENT(); private: double m_svRate = 33.0; std::unordered_map::iterator m_getClientIterator(const CEndPoint& endpoint); }; namespace std { template<> struct hash { size_t operator()(const CServer::CClient& p) const { size_t h1 = hash{}(p.EndPoint.address().to_string()); size_t h2 = hash{}(p.EndPoint.port()); return h1 ^ (h2 << 1); } }; }