Compare commits
5 Commits
6e9c747b2d
...
7abb31c2e1
| Author | SHA1 | Date | |
|---|---|---|---|
| 7abb31c2e1 | |||
| 8bc2f26578 | |||
| 9fadb86031 | |||
| 9f8dc0630d | |||
| c192c73d34 |
25
include/blitz/common/NonCopyable.h
Normal file
25
include/blitz/common/NonCopyable.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file NonCopyable.h
|
||||||
|
* \brief File containing the blitz::NonCopyable class
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \class NonCopyable
|
||||||
|
* \brief Class used to make a class non copyable
|
||||||
|
* \note Inherit from this class privately to make a class non copyable
|
||||||
|
*/
|
||||||
|
class NonCopyable {
|
||||||
|
public:
|
||||||
|
NonCopyable(const NonCopyable&) = delete;
|
||||||
|
NonCopyable& operator=(const NonCopyable&) = delete;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
NonCopyable() {}
|
||||||
|
~NonCopyable() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace blitz
|
||||||
38
include/blitz/network/EnetClient.h
Normal file
38
include/blitz/network/EnetClient.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Nazara/Network/ENetHost.hpp>
|
||||||
|
#include <blitz/common/NonCopyable.h>
|
||||||
|
#include <blitz/network/EnetConnexion.h>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace network {
|
||||||
|
|
||||||
|
class EnetClient : private NonCopyable {
|
||||||
|
public:
|
||||||
|
EnetClient(const Nz::IpAddress& address);
|
||||||
|
~EnetClient();
|
||||||
|
|
||||||
|
void Disconnect();
|
||||||
|
|
||||||
|
NazaraSignal(OnConnect);
|
||||||
|
NazaraSignal(OnDisconnect);
|
||||||
|
NazaraSignal(OnDisconnectTimeout);
|
||||||
|
|
||||||
|
const EnetConnexion& GetConnexion() const {
|
||||||
|
return m_Connexion;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
EnetConnexion m_Connexion;
|
||||||
|
Nz::ENetHost m_Host;
|
||||||
|
Nz::ENetPeer* m_Peer;
|
||||||
|
std::thread m_Thread;
|
||||||
|
bool m_Running;
|
||||||
|
|
||||||
|
void Update();
|
||||||
|
void WorkerThread();
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace network
|
||||||
|
} // namespace blitz
|
||||||
50
include/blitz/network/EnetConnexion.h
Normal file
50
include/blitz/network/EnetConnexion.h
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Nazara/Network/ENetPeer.hpp>
|
||||||
|
#include <blitz/protocol/PacketSerializer.h>
|
||||||
|
#include <blitz/protocol/Packets.h>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace network {
|
||||||
|
|
||||||
|
class EnetClient;
|
||||||
|
class EnetServer;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define DeclarePacket(Name, NFlag) \
|
||||||
|
void Send##Name(const blitz::protocol::data::Name& a_##Name) const { \
|
||||||
|
m_Peer->Send(0, NFlag, protocol::PacketSerializer::Serialize(protocol::packets::Name(a_##Name))); \
|
||||||
|
} \
|
||||||
|
NazaraSignal(On##Name, const blitz::protocol::data::Name&)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class EnetConnexion {
|
||||||
|
public:
|
||||||
|
EnetConnexion(Nz::ENetPeer* a_Peer = nullptr);
|
||||||
|
|
||||||
|
bool IsConnected() const {
|
||||||
|
if (!m_Peer)
|
||||||
|
return false;
|
||||||
|
return m_Peer->IsConnected();
|
||||||
|
}
|
||||||
|
|
||||||
|
DeclareAllPacket()
|
||||||
|
|
||||||
|
private:
|
||||||
|
Nz::ENetPeer* m_Peer;
|
||||||
|
|
||||||
|
void Recieve(Nz::ByteArray&);
|
||||||
|
void SetPeer(Nz::ENetPeer* a_Peer);
|
||||||
|
|
||||||
|
friend class EnetClient;
|
||||||
|
friend class EnetServer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#undef DeclarePacket
|
||||||
|
|
||||||
|
} // namespace network
|
||||||
|
} // namespace blitz
|
||||||
43
include/blitz/network/EnetServer.h
Normal file
43
include/blitz/network/EnetServer.h
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Nazara/Core/ThreadExt.hpp>
|
||||||
|
#include <Nazara/Network/ENetHost.hpp>
|
||||||
|
#include <blitz/common/NonCopyable.h>
|
||||||
|
#include <blitz/network/EnetConnexion.h>
|
||||||
|
#include <blitz/protocol/Packets.h>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <map>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace network {
|
||||||
|
|
||||||
|
class EnetServer : private NonCopyable {
|
||||||
|
public:
|
||||||
|
EnetServer(std::uint16_t port);
|
||||||
|
~EnetServer();
|
||||||
|
|
||||||
|
void Destroy();
|
||||||
|
|
||||||
|
void BroadcastPacket(const protocol::Packet& a_Packet, Nz::ENetPacketFlags a_Flags);
|
||||||
|
|
||||||
|
EnetConnexion* GetConnexion(std::uint16_t a_PeerId);
|
||||||
|
|
||||||
|
NazaraSignal(OnClientConnect, EnetConnexion& /*a_Peer*/);
|
||||||
|
NazaraSignal(OnClientDisconnect, EnetConnexion& /*a_Peer*/);
|
||||||
|
NazaraSignal(OnClientDisconnectTimeout, EnetConnexion& /*a_Peer*/);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void Update();
|
||||||
|
void WorkerThread();
|
||||||
|
|
||||||
|
void RemoveConnexion(std::uint16_t a_PeerId);
|
||||||
|
|
||||||
|
Nz::ENetHost m_Host;
|
||||||
|
bool m_Running;
|
||||||
|
std::thread m_Thread;
|
||||||
|
std::map<std::uint16_t, EnetConnexion> m_Connexion;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace network
|
||||||
|
} // namespace blitz
|
||||||
19
include/blitz/protocol/PacketDeclare.h
Normal file
19
include/blitz/protocol/PacketDeclare.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define DeclareAllPacket() \
|
||||||
|
DeclarePacket(PlayerLogin, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(UpdateHealth, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(LoggingSuccess, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(PlayerDeath, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(PlayerJoin, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(PlayerLeave, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(PlayerStats, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(PlayerList, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(ServerConfig, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(ServerTps, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(UpdateGameState, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(KeepAlive, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(Disconnect, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(ChatMessage, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(PlayerPositionAndRotation, Nz::ENetPacketFlag::Reliable); \
|
||||||
|
DeclarePacket(PlayerShoot, Nz::ENetPacketFlag::Reliable);
|
||||||
@@ -1,10 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <blitz/protocol/Packets.h>
|
#include <blitz/protocol/Packets.h>
|
||||||
|
#include <blitz/protocol/PacketDeclare.h>
|
||||||
|
|
||||||
namespace blitz {
|
namespace blitz {
|
||||||
namespace protocol {
|
namespace protocol {
|
||||||
|
|
||||||
|
#define DeclarePacket(PacketName, ...) \
|
||||||
|
virtual void Visit(const packets::PacketName&) {}
|
||||||
|
|
||||||
class PacketVisitor {
|
class PacketVisitor {
|
||||||
protected:
|
protected:
|
||||||
PacketVisitor() {}
|
PacketVisitor() {}
|
||||||
@@ -13,23 +17,10 @@ class PacketVisitor {
|
|||||||
public:
|
public:
|
||||||
void Check(const Packet& packet);
|
void Check(const Packet& packet);
|
||||||
|
|
||||||
virtual void Visit(const packets::PlayerLogin&) {}
|
DeclareAllPacket()
|
||||||
virtual void Visit(const packets::UpdateHealth&) {}
|
|
||||||
virtual void Visit(const packets::LoggingSuccess&) {}
|
|
||||||
virtual void Visit(const packets::PlayerDeath&) {}
|
|
||||||
virtual void Visit(const packets::PlayerJoin&) {}
|
|
||||||
virtual void Visit(const packets::PlayerLeave&) {}
|
|
||||||
virtual void Visit(const packets::PlayerList&) {}
|
|
||||||
virtual void Visit(const packets::PlayerStats&) {}
|
|
||||||
virtual void Visit(const packets::ServerConfig&) {}
|
|
||||||
virtual void Visit(const packets::ServerTps&) {}
|
|
||||||
virtual void Visit(const packets::UpdateGameState&) {}
|
|
||||||
virtual void Visit(const packets::KeepAlive&) {}
|
|
||||||
virtual void Visit(const packets::Disconnect&) {}
|
|
||||||
virtual void Visit(const packets::ChatMessage&) {}
|
|
||||||
virtual void Visit(const packets::PlayerPositionAndRotation&) {}
|
|
||||||
virtual void Visit(const packets::PlayerShoot&) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#undef DeclarePacket
|
||||||
|
|
||||||
} // namespace protocol
|
} // namespace protocol
|
||||||
} // namespace blitz
|
} // namespace blitz
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <blitz/protocol/PacketData.h>
|
#include <blitz/protocol/PacketData.h>
|
||||||
|
#include <blitz/protocol/PacketDeclare.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace blitz {
|
namespace blitz {
|
||||||
namespace protocol {
|
namespace protocol {
|
||||||
@@ -87,33 +88,16 @@ class ConcretePacket : public Packet {
|
|||||||
// before including this file
|
// before including this file
|
||||||
// if you want to instantiate templates
|
// if you want to instantiate templates
|
||||||
#ifdef BLITZ_INSTANCIATE_PACKETS
|
#ifdef BLITZ_INSTANCIATE_PACKETS
|
||||||
#define DeclarePacket(Type) \
|
#define DeclarePacket(PacketName, ...) \
|
||||||
using Type = ConcretePacket<PacketType::Type, data::Type>; \
|
using PacketName = ConcretePacket<PacketType::PacketName, data::PacketName>; \
|
||||||
template class ConcretePacket<PacketType::Type, data::Type>
|
template class ConcretePacket<PacketType::PacketName, data::PacketName>
|
||||||
#else
|
#else
|
||||||
#define DeclarePacket(Type) using Type = ConcretePacket<PacketType::Type, data::Type>
|
#define DeclarePacket(PacketName, ...) using PacketName = ConcretePacket<PacketType::PacketName, data::PacketName>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
DeclareAllPacket()
|
||||||
|
|
||||||
|
#undef DeclarePacket
|
||||||
|
|
||||||
|
|
||||||
DeclarePacket(PlayerLogin);
|
|
||||||
DeclarePacket(UpdateHealth);
|
|
||||||
DeclarePacket(LoggingSuccess);
|
|
||||||
DeclarePacket(PlayerDeath);
|
|
||||||
DeclarePacket(PlayerJoin);
|
|
||||||
DeclarePacket(PlayerLeave);
|
|
||||||
DeclarePacket(PlayerStats);
|
|
||||||
DeclarePacket(PlayerList);
|
|
||||||
DeclarePacket(ServerConfig);
|
|
||||||
DeclarePacket(ServerTps);
|
|
||||||
DeclarePacket(UpdateGameState);
|
|
||||||
DeclarePacket(KeepAlive);
|
|
||||||
DeclarePacket(Disconnect);
|
|
||||||
DeclarePacket(ChatMessage);
|
|
||||||
DeclarePacket(PlayerPositionAndRotation);
|
|
||||||
DeclarePacket(PlayerShoot);
|
|
||||||
|
|
||||||
} // namespace packets
|
} // namespace packets
|
||||||
|
|
||||||
|
|||||||
69
src/blitz/network/EnetClient.cpp
Normal file
69
src/blitz/network/EnetClient.cpp
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#include <blitz/network/EnetClient.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace network {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
EnetClient::EnetClient(const Nz::IpAddress& address) : m_Running(true) {
|
||||||
|
m_Host.Create(Nz::IpAddress::LoopbackIpV4, 1);
|
||||||
|
m_Peer = m_Host.Connect(address);
|
||||||
|
m_Thread = std::thread(&EnetClient::WorkerThread, this);
|
||||||
|
m_Connexion.SetPeer(m_Peer);
|
||||||
|
}
|
||||||
|
|
||||||
|
EnetClient::~EnetClient() {
|
||||||
|
if (m_Peer->IsConnected())
|
||||||
|
Disconnect();
|
||||||
|
m_Host.Destroy();
|
||||||
|
m_Running = false;
|
||||||
|
m_Thread.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnetClient::Disconnect() {
|
||||||
|
m_Peer->DisconnectNow(0);
|
||||||
|
m_Connexion.SetPeer(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnetClient::WorkerThread() {
|
||||||
|
while (m_Running) {
|
||||||
|
Update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnetClient::Update() {
|
||||||
|
Nz::ENetEvent event;
|
||||||
|
int service = m_Host.Service(&event, 5);
|
||||||
|
if (service > 0) {
|
||||||
|
do {
|
||||||
|
switch (event.type) {
|
||||||
|
case Nz::ENetEventType::Disconnect:
|
||||||
|
OnDisconnect();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Nz::ENetEventType::DisconnectTimeout:
|
||||||
|
OnDisconnectTimeout();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Nz::ENetEventType::OutgoingConnect:
|
||||||
|
OnConnect();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Nz::ENetEventType::Receive:
|
||||||
|
m_Connexion.Recieve(event.packet.m_packet->data);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Nz::ENetEventType::None:
|
||||||
|
case Nz::ENetEventType::IncomingConnect:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (m_Host.CheckEvents(&event));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace network
|
||||||
|
} // namespace blitz
|
||||||
48
src/blitz/network/EnetConnexion.cpp
Normal file
48
src/blitz/network/EnetConnexion.cpp
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#include <blitz/network/EnetConnexion.h>
|
||||||
|
|
||||||
|
#include <blitz/protocol/PacketSerializer.h>
|
||||||
|
#include <blitz/protocol/PacketVisitor.h>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace network {
|
||||||
|
|
||||||
|
namespace packets = blitz::protocol::packets;
|
||||||
|
|
||||||
|
|
||||||
|
#define DeclarePacket(PacketName, ...) \
|
||||||
|
void Visit(const protocol::packets::PacketName& a_Packet) { \
|
||||||
|
m_Connexion.On##PacketName(a_Packet.m_Data); \
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class PacketDispatcher : public protocol::PacketVisitor {
|
||||||
|
public:
|
||||||
|
PacketDispatcher(EnetConnexion& a_Connexion) : m_Connexion(a_Connexion) {}
|
||||||
|
|
||||||
|
DeclareAllPacket();
|
||||||
|
|
||||||
|
private:
|
||||||
|
EnetConnexion& m_Connexion;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
EnetConnexion::EnetConnexion(Nz::ENetPeer* a_Peer) : m_Peer(a_Peer) {}
|
||||||
|
|
||||||
|
void EnetConnexion::SetPeer(Nz::ENetPeer* a_Peer) {
|
||||||
|
m_Peer = a_Peer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnetConnexion::Recieve(Nz::ByteArray& a_Data) {
|
||||||
|
auto packet = protocol::PacketSerializer::Deserialize(a_Data);
|
||||||
|
if (!packet)
|
||||||
|
return;
|
||||||
|
PacketDispatcher dispatcher(*this);
|
||||||
|
dispatcher.Check(*packet.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace network
|
||||||
|
} // namespace blitz
|
||||||
94
src/blitz/network/EnetServer.cpp
Normal file
94
src/blitz/network/EnetServer.cpp
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
#include <blitz/network/EnetServer.h>
|
||||||
|
|
||||||
|
#include <Nazara/Core/ByteStream.hpp>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
namespace network {
|
||||||
|
|
||||||
|
EnetServer::EnetServer(std::uint16_t port) : m_Running(true) {
|
||||||
|
m_Host.Create(Nz::NetProtocol::Any, port, 80);
|
||||||
|
m_Host.AllowsIncomingConnections(true);
|
||||||
|
m_Thread = std::thread(&EnetServer::WorkerThread, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnetServer::WorkerThread() {
|
||||||
|
while (m_Running) {
|
||||||
|
Update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EnetServer::~EnetServer() {
|
||||||
|
if (m_Running)
|
||||||
|
Destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnetServer::Update() {
|
||||||
|
Nz::ENetEvent event;
|
||||||
|
int service = m_Host.Service(&event, 5);
|
||||||
|
if (service > 0) {
|
||||||
|
do {
|
||||||
|
switch (event.type) {
|
||||||
|
case Nz::ENetEventType::Disconnect: {
|
||||||
|
EnetConnexion* connexion = GetConnexion(event.peer->GetPeerId());
|
||||||
|
if (!connexion)
|
||||||
|
break;
|
||||||
|
OnClientDisconnect(*connexion);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Nz::ENetEventType::DisconnectTimeout: {
|
||||||
|
EnetConnexion* connexion = GetConnexion(event.peer->GetPeerId());
|
||||||
|
if (!connexion)
|
||||||
|
break;
|
||||||
|
OnClientDisconnectTimeout(*connexion);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Nz::ENetEventType::IncomingConnect: {
|
||||||
|
Nz::ENetPeer* peer = event.peer;
|
||||||
|
m_Connexion.insert({peer->GetPeerId(), EnetConnexion(peer)});
|
||||||
|
OnClientConnect(*GetConnexion(peer->GetPeerId()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Nz::ENetEventType::Receive: {
|
||||||
|
EnetConnexion* connexion = GetConnexion(event.peer->GetPeerId());
|
||||||
|
if (!connexion)
|
||||||
|
break;
|
||||||
|
connexion->Recieve(event.packet.m_packet->data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Nz::ENetEventType::OutgoingConnect:
|
||||||
|
case Nz::ENetEventType::None:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (m_Host.CheckEvents(&event));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EnetConnexion* EnetServer::GetConnexion(std::uint16_t a_PeerId) {
|
||||||
|
auto it = m_Connexion.find(a_PeerId);
|
||||||
|
|
||||||
|
if (it == m_Connexion.end())
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
return &it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnetServer::BroadcastPacket(const protocol::Packet& a_Packet, Nz::ENetPacketFlags a_Flags) {
|
||||||
|
// m_Host.Broadcast(0, a_Flags, std::move(a_Data));
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnetServer::RemoveConnexion(std::uint16_t a_PeerId) {
|
||||||
|
m_Connexion.erase(a_PeerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnetServer::Destroy() {
|
||||||
|
m_Running = false;
|
||||||
|
m_Thread.join();
|
||||||
|
m_Host.Destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace network
|
||||||
|
} // namespace blitz
|
||||||
@@ -32,6 +32,9 @@ namespace PacketSerializer {
|
|||||||
void DeserializePacketData(ClassName::PacketDataType& a_Packet)
|
void DeserializePacketData(ClassName::PacketDataType& a_Packet)
|
||||||
|
|
||||||
|
|
||||||
|
#define DeclarePacket(PacketName, ...) \
|
||||||
|
VisitSerialize(packets::PacketName)
|
||||||
|
|
||||||
class Serializer : public PacketVisitor {
|
class Serializer : public PacketVisitor {
|
||||||
private:
|
private:
|
||||||
Nz::ByteStream& m_Buffer;
|
Nz::ByteStream& m_Buffer;
|
||||||
@@ -44,24 +47,13 @@ class Serializer : public PacketVisitor {
|
|||||||
Check(a_Packet);
|
Check(a_Packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
VisitSerialize(packets::PlayerLogin);
|
DeclareAllPacket()
|
||||||
VisitSerialize(packets::UpdateHealth);
|
|
||||||
VisitSerialize(packets::LoggingSuccess);
|
|
||||||
VisitSerialize(packets::PlayerDeath);
|
|
||||||
VisitSerialize(packets::PlayerJoin);
|
|
||||||
VisitSerialize(packets::PlayerLeave);
|
|
||||||
VisitSerialize(packets::PlayerList);
|
|
||||||
VisitSerialize(packets::PlayerStats);
|
|
||||||
VisitSerialize(packets::ServerConfig);
|
|
||||||
VisitSerialize(packets::ServerTps);
|
|
||||||
VisitSerialize(packets::UpdateGameState);
|
|
||||||
VisitSerialize(packets::KeepAlive);
|
|
||||||
VisitSerialize(packets::Disconnect);
|
|
||||||
VisitSerialize(packets::ChatMessage);
|
|
||||||
VisitSerialize(packets::PlayerPositionAndRotation);
|
|
||||||
VisitSerialize(packets::PlayerShoot);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#undef DeclarePacket
|
||||||
|
#define DeclarePacket(PacketName, ...) \
|
||||||
|
VisitDeserialize(packets::PacketName)
|
||||||
|
|
||||||
class Deserializer : public PacketVisitor {
|
class Deserializer : public PacketVisitor {
|
||||||
private:
|
private:
|
||||||
Nz::ByteStream& m_Buffer;
|
Nz::ByteStream& m_Buffer;
|
||||||
@@ -83,22 +75,7 @@ class Deserializer : public PacketVisitor {
|
|||||||
return m_Packet;
|
return m_Packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
VisitDeserialize(packets::PlayerLogin);
|
DeclareAllPacket()
|
||||||
VisitDeserialize(packets::UpdateHealth);
|
|
||||||
VisitDeserialize(packets::LoggingSuccess);
|
|
||||||
VisitDeserialize(packets::PlayerDeath);
|
|
||||||
VisitDeserialize(packets::PlayerJoin);
|
|
||||||
VisitDeserialize(packets::PlayerLeave);
|
|
||||||
VisitDeserialize(packets::PlayerList);
|
|
||||||
VisitDeserialize(packets::PlayerStats);
|
|
||||||
VisitDeserialize(packets::ServerConfig);
|
|
||||||
VisitDeserialize(packets::ServerTps);
|
|
||||||
VisitDeserialize(packets::UpdateGameState);
|
|
||||||
VisitDeserialize(packets::KeepAlive);
|
|
||||||
VisitDeserialize(packets::Disconnect);
|
|
||||||
VisitDeserialize(packets::ChatMessage);
|
|
||||||
VisitDeserialize(packets::PlayerPositionAndRotation);
|
|
||||||
VisitDeserialize(packets::PlayerShoot);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -252,9 +229,13 @@ void Deserializer::DeserializePacketData(data::Disconnect& a_Packet) {}
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Serializer::SerializePacketData(const data::ChatMessage& a_Packet) {}
|
void Serializer::SerializePacketData(const data::ChatMessage& a_Packet) {
|
||||||
|
m_Buffer << a_Packet.m_Text;
|
||||||
|
}
|
||||||
|
|
||||||
void Deserializer::DeserializePacketData(data::ChatMessage& a_Packet) {}
|
void Deserializer::DeserializePacketData(data::ChatMessage& a_Packet) {
|
||||||
|
m_Buffer >> a_Packet.m_Text;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
68
src/main.cpp
68
src/main.cpp
@@ -1,5 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "Network.h"
|
||||||
|
|
||||||
#include <Nazara/Core.hpp>
|
#include <Nazara/Core.hpp>
|
||||||
#include <Nazara/Graphics.hpp>
|
#include <Nazara/Graphics.hpp>
|
||||||
#include <Nazara/Renderer.hpp>
|
#include <Nazara/Renderer.hpp>
|
||||||
@@ -7,7 +9,8 @@
|
|||||||
#include <Nazara/Physics3D.hpp>
|
#include <Nazara/Physics3D.hpp>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
static Nz::Vector3f Get2DDirectionVectorFromRotation(float yaw) {
|
static Nz::Vector3f Get2DDirectionVectorFromRotation(float yaw)
|
||||||
|
{
|
||||||
return {
|
return {
|
||||||
-std::sin(yaw),
|
-std::sin(yaw),
|
||||||
0,
|
0,
|
||||||
@@ -136,6 +139,7 @@ static void CreateModel(Nz::EnttWorld &world)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f);
|
static Nz::EulerAnglesf camAngles(0.f, 0.f, 0.f);
|
||||||
|
static Nz::MillisecondClock updateClock;
|
||||||
|
|
||||||
static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Application<Nz::Graphics, Nz::Physics3D> &app)
|
static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Application<Nz::Graphics, Nz::Physics3D> &app)
|
||||||
{
|
{
|
||||||
@@ -151,7 +155,7 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Applicati
|
|||||||
entt::handle cameraEntity = world.CreateEntity();
|
entt::handle cameraEntity = world.CreateEntity();
|
||||||
|
|
||||||
auto &cameraNode = cameraEntity.emplace<Nz::NodeComponent>();
|
auto &cameraNode = cameraEntity.emplace<Nz::NodeComponent>();
|
||||||
cameraNode.SetPosition({0, 1, 0});
|
cameraNode.SetPosition({0, 2.5, 0});
|
||||||
|
|
||||||
auto &cameraComponent = cameraEntity.emplace<Nz::CameraComponent>(std::make_shared<Nz::RenderWindow>(windowSwapchain), Nz::ProjectionType::Perspective);
|
auto &cameraComponent = cameraEntity.emplace<Nz::CameraComponent>(std::make_shared<Nz::RenderWindow>(windowSwapchain), Nz::ProjectionType::Perspective);
|
||||||
cameraComponent.UpdateClearColor(Nz::Color(0.3f, 0.8f, 1.0f));
|
cameraComponent.UpdateClearColor(Nz::Color(0.3f, 0.8f, 1.0f));
|
||||||
@@ -176,32 +180,46 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Applicati
|
|||||||
camAngles.roll = 0.0f;
|
camAngles.roll = 0.0f;
|
||||||
|
|
||||||
cameraNode.SetRotation(camAngles);
|
cameraNode.SetRotation(camAngles);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
std::shared_ptr<Nz::BoxCollider3D> boxCollider = std::make_shared<Nz::BoxCollider3D>(Nz::Vector3f(1, 2, 1));
|
Nz::Vector3f playerSize{0.5, 2, 0.5};
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
std::shared_ptr<Nz::GraphicalMesh> boxMesh = Nz::GraphicalMesh::Build(Nz::Primitive::Box(playerSize));
|
||||||
|
|
||||||
|
std::shared_ptr<Nz::MaterialInstance> boxMaterial = Nz::MaterialInstance::Instantiate(Nz::MaterialType::Phong);
|
||||||
|
boxMaterial->SetValueProperty("BaseColor", Nz::Color::sRGBToLinear({1.0, 0.0, 0.0}));
|
||||||
|
|
||||||
|
std::shared_ptr<Nz::Model> sphereModel = std::make_shared<Nz::Model>(boxMesh);
|
||||||
|
sphereModel->SetMaterial(0, std::move(boxMaterial));
|
||||||
|
playerEntity.emplace<Nz::GraphicsComponent>(std::move(sphereModel));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Nz::BoxCollider3D> boxCollider = std::make_shared<Nz::BoxCollider3D>(playerSize);
|
||||||
|
|
||||||
static const int playerMass = 100000;
|
static const int playerMass = 100000;
|
||||||
|
|
||||||
Nz::RigidBody3D::DynamicSettings settings;
|
Nz::RigidBody3D::DynamicSettings settings;
|
||||||
settings.geom = boxCollider;
|
settings.geom = boxCollider;
|
||||||
settings.mass = playerMass;
|
settings.mass = playerMass;
|
||||||
auto &playerBody = playerEntity.emplace<Nz::RigidBody3DComponent>(settings);
|
|
||||||
|
|
||||||
app.AddUpdaterFunc([&](){
|
playerEntity.emplace<Nz::RigidBody3DComponent>(settings);
|
||||||
static Nz::MillisecondClock updateClock;
|
|
||||||
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::TickDuration(60)))
|
app.AddUpdaterFunc([playerEntity]()
|
||||||
|
{
|
||||||
|
if (std::optional<Nz::Time> deltaTime = updateClock.RestartIfOver(Nz::Time::Milliseconds(30)))
|
||||||
{
|
{
|
||||||
camAngles.roll = 0;
|
|
||||||
cameraNode.SetRotation(camAngles);
|
|
||||||
|
|
||||||
Nz::Vector3f front = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians());
|
Nz::Vector3f front = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians());
|
||||||
Nz::Vector3f left = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians() + 3.1415926535/2.0);
|
Nz::Vector3f left = Get2DDirectionVectorFromRotation(camAngles.yaw.ToRadians() + 3.1415926535/2.0);
|
||||||
|
|
||||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z))
|
auto& playerBody = playerEntity.get<Nz::RigidBody3DComponent>();
|
||||||
playerBody.AddForce(front * 10.f * playerMass, Nz::CoordSys::Local);
|
|
||||||
|
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Z)){
|
||||||
|
std::cout << front << "\n";
|
||||||
|
std::cout << "Pos : " << playerBody.GetPosition() << " \n";
|
||||||
|
playerBody.AddForce(front * 10.f * playerMass, Nz::CoordSys::Global);
|
||||||
|
}
|
||||||
|
|
||||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::S))
|
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::S))
|
||||||
playerBody.AddForce(-front * 10.f * playerMass, Nz::CoordSys::Local);
|
playerBody.AddForce(-front * 10.f * playerMass, Nz::CoordSys::Local);
|
||||||
@@ -212,14 +230,21 @@ static void CreateCamera(Nz::EnttWorld &world, Nz::Window &window, Nz::Applicati
|
|||||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D))
|
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D))
|
||||||
playerBody.AddForce(-left * 10.f * playerMass, Nz::CoordSys::Local);
|
playerBody.AddForce(-left * 10.f * playerMass, Nz::CoordSys::Local);
|
||||||
|
|
||||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space))
|
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Space)){
|
||||||
playerBody.AddForce(Nz::Vector3f::Up() * 15.f * playerMass, Nz::CoordSys::Local);
|
playerBody.AddForce(Nz::Vector3f::Up() * 15.f * playerMass, Nz::CoordSys::Global);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::R)) {
|
||||||
|
auto& playerNode = playerEntity.get<Nz::NodeComponent>();
|
||||||
|
playerNode.SetPosition({0, 10, 0});
|
||||||
|
playerBody.SetPosition({0, 10, 0});
|
||||||
|
}
|
||||||
|
|
||||||
|
//playerBody.SetRotation({});
|
||||||
|
} });
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int Video(int argc, char **argv) {
|
||||||
{
|
|
||||||
Nz::Application<Nz::Graphics, Nz::Physics3D> app(argc, argv);
|
Nz::Application<Nz::Graphics, Nz::Physics3D> app(argc, argv);
|
||||||
|
|
||||||
auto &windowing = app.AddComponent<Nz::WindowingAppComponent>();
|
auto &windowing = app.AddComponent<Nz::WindowingAppComponent>();
|
||||||
@@ -233,7 +258,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
auto &physSystem = world.AddSystem<Nz::Physics3DSystem>();
|
auto &physSystem = world.AddSystem<Nz::Physics3DSystem>();
|
||||||
physSystem.GetPhysWorld().SetMaxStepCount(1);
|
physSystem.GetPhysWorld().SetMaxStepCount(1);
|
||||||
physSystem.GetPhysWorld().SetStepSize(Nz::Time::TickDuration(50));
|
physSystem.GetPhysWorld().SetStepSize(Nz::Time::Milliseconds(20));
|
||||||
physSystem.GetPhysWorld().SetGravity(Nz::Vector3f::Down() * 9.81f);
|
physSystem.GetPhysWorld().SetGravity(Nz::Vector3f::Down() * 9.81f);
|
||||||
|
|
||||||
CreateCamera(world, window, app);
|
CreateCamera(world, window, app);
|
||||||
@@ -258,3 +283,8 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
return app.Run();
|
return app.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
TestNetwork();
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,13 +3,15 @@
|
|||||||
#include <blitz/protocol/PacketFactory.h>
|
#include <blitz/protocol/PacketFactory.h>
|
||||||
#include <blitz/utils/Test.h>
|
#include <blitz/utils/Test.h>
|
||||||
|
|
||||||
static int Test() {
|
namespace bp = blitz::protocol;
|
||||||
for (std::size_t i = 0; i < static_cast<std::size_t>(blitz::protocol::PacketType::PACKET_COUNT); i++) {
|
|
||||||
const auto& packet = blitz::protocol::PacketFactory::CreateReadOnlyPacket(blitz::protocol::PacketType(i));
|
|
||||||
|
|
||||||
Nz::ByteArray buffer = blitz::protocol::PacketSerializer::Serialize(*packet.get());
|
static int TestAllPackets() {
|
||||||
|
for (std::size_t i = 0; i < static_cast<std::size_t>(bp::PacketType::PACKET_COUNT); i++) {
|
||||||
|
const auto& packet = bp::PacketFactory::CreateReadOnlyPacket(bp::PacketType(i));
|
||||||
|
|
||||||
blitz::protocol::PacketPtr packet2 = blitz::protocol::PacketSerializer::Deserialize(buffer);
|
Nz::ByteArray buffer = bp::PacketSerializer::Serialize(*packet.get());
|
||||||
|
|
||||||
|
bp::PacketPtr packet2 = bp::PacketSerializer::Deserialize(buffer);
|
||||||
|
|
||||||
blitz_test_assert(packet2 != nullptr);
|
blitz_test_assert(packet2 != nullptr);
|
||||||
|
|
||||||
@@ -18,6 +20,12 @@ static int Test() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
static void Test() {
|
||||||
return Test();
|
bp::packets::ChatMessage packet({"caca"});
|
||||||
|
Nz::ByteArray buffer = bp::PacketSerializer::Serialize(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Test();
|
||||||
|
return TestAllPackets();
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,10 @@ add_requires("nazaraengine", { debug = is_mode("debug") })
|
|||||||
set_languages("c++20")
|
set_languages("c++20")
|
||||||
set_warnings("all")
|
set_warnings("all")
|
||||||
|
|
||||||
|
if is_mode("release") then
|
||||||
|
set_warnings("all", "error")
|
||||||
|
end
|
||||||
|
|
||||||
target("BlitzLib")
|
target("BlitzLib")
|
||||||
set_kind("static")
|
set_kind("static")
|
||||||
add_files("src/blitz/**.cpp")
|
add_files("src/blitz/**.cpp")
|
||||||
|
|||||||
Reference in New Issue
Block a user