diff --git a/include/ClientLib/ClientSession.hpp b/include/ClientLib/ClientSession.hpp index b52c5ad2..958332a9 100644 --- a/include/ClientLib/ClientSession.hpp +++ b/include/ClientLib/ClientSession.hpp @@ -39,7 +39,7 @@ namespace bw inline bool IsConnected() const; - void HandleIncomingPacket(Nz::NetPacket& packet); + void HandleIncomingPacket(Nz::ByteArray& packet); inline void QuerySessionInfo(std::function callback) const; diff --git a/include/ClientLib/ClientSession.inl b/include/ClientLib/ClientSession.inl index 18ee9e7d..2314ca4b 100644 --- a/include/ClientLib/ClientSession.inl +++ b/include/ClientLib/ClientSession.inl @@ -4,7 +4,6 @@ #include #include -#include namespace bw { @@ -48,8 +47,9 @@ namespace bw if (!IsConnected()) return; - Nz::NetPacket data; - m_commandStore.SerializePacket(data, packet); + Nz::ByteArray data; + Nz::ByteStream byteStream(&data, Nz::OpenMode::Write); + m_commandStore.SerializePacket(byteStream, packet); const auto& command = m_commandStore.GetOutgoingCommand(); m_bridge->SendPacket(command.channelId, command.flags, std::move(data)); diff --git a/include/ClientLib/LocalSessionBridge.hpp b/include/ClientLib/LocalSessionBridge.hpp index 9ba23139..0042b14b 100644 --- a/include/ClientLib/LocalSessionBridge.hpp +++ b/include/ClientLib/LocalSessionBridge.hpp @@ -22,13 +22,13 @@ namespace bw void Disconnect() override; - void HandleIncomingPacket(Nz::NetPacket& packet) override; + void HandleIncomingPacket(Nz::ByteArray& packet) override; inline bool IsServer() const; bool IsLocal() const override; void QueryInfo(std::function callback) const override; - void SendPacket(Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::NetPacket&& packet) override; + void SendPacket(Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::ByteArray&& packet) override; private: std::size_t m_peerId; diff --git a/include/ClientLib/LocalSessionManager.hpp b/include/ClientLib/LocalSessionManager.hpp index 7b5403a6..52d67767 100644 --- a/include/ClientLib/LocalSessionManager.hpp +++ b/include/ClientLib/LocalSessionManager.hpp @@ -38,14 +38,14 @@ namespace bw private: void DisconnectPeer(std::size_t peerId); - void SendPacket(std::size_t peerId, Nz::NetPacket&& packet, bool isServer); + void SendPacket(std::size_t peerId, Nz::ByteArray&& packet, bool isServer); struct Peer { std::shared_ptr clientBridge; std::shared_ptr serverBridge; - std::vector clientPackets; - std::vector serverPackets; + std::vector clientPackets; + std::vector serverPackets; MatchClientSession* session; bool disconnectionRequested = false; }; diff --git a/include/ClientLib/NetworkReactorManager.hpp b/include/ClientLib/NetworkReactorManager.hpp index ffed2483..82993cee 100644 --- a/include/ClientLib/NetworkReactorManager.hpp +++ b/include/ClientLib/NetworkReactorManager.hpp @@ -41,7 +41,7 @@ namespace bw private: void HandlePeerConnection(bool outgoing, std::size_t peerId, Nz::UInt32 data); void HandlePeerDisconnection(std::size_t peerId, Nz::UInt32 data); - void HandlePeerPacket(std::size_t peerId, Nz::NetPacket& packet); + void HandlePeerPacket(std::size_t peerId, Nz::ByteArray& packet); std::vector> m_reactors; std::vector> m_connections; diff --git a/include/CoreLib/CommandStore.hpp b/include/CoreLib/CommandStore.hpp index 1903f736..07170118 100644 --- a/include/CoreLib/CommandStore.hpp +++ b/include/CoreLib/CommandStore.hpp @@ -7,8 +7,8 @@ #ifndef BURGWAR_CORELIB_COMMANDSTORE_HPP #define BURGWAR_CORELIB_COMMANDSTORE_HPP +#include #include -#include #include #include #include @@ -33,11 +33,11 @@ namespace bw template const OutgoingCommand& GetOutgoingCommand() const; template - void SerializePacket(Nz::NetPacket& packet, const T& data) const; + void SerializePacket(Nz::ByteStream& packet, const T& data) const; - bool UnserializePacket(PeerRef peer, Nz::NetPacket& packet) const; + bool UnserializePacket(PeerRef peer, Nz::ByteStream& packet) const; - using UnserializeFunction = std::function; + using UnserializeFunction = std::function; struct IncomingCommand { @@ -59,7 +59,7 @@ namespace bw template void RegisterOutgoingCommand(const char* name, Nz::ENetPacketFlags flags, Nz::UInt8 channelId); private: - using HandleFunction = std::function; + using HandleFunction = std::function; std::vector m_incomingCommands; std::vector m_outgoingCommands; diff --git a/include/CoreLib/CommandStore.inl b/include/CoreLib/CommandStore.inl index 78eff45f..335f752a 100644 --- a/include/CoreLib/CommandStore.inl +++ b/include/CoreLib/CommandStore.inl @@ -52,7 +52,7 @@ namespace bw IncomingCommand& newCommand = m_incomingCommands[packetId]; newCommand.enabled = true; - newCommand.unserialize = [this, cb = std::forward(callback)](PeerRef peer, Nz::NetPacket& packet) + newCommand.unserialize = [this, cb = std::forward(callback)](PeerRef peer, Nz::ByteStream& packet) { T data; try @@ -91,7 +91,7 @@ namespace bw template template - void CommandStore::SerializePacket(Nz::NetPacket& packet, const T& data) const + void CommandStore::SerializePacket(Nz::ByteStream& packet, const T& data) const { packet << static_cast(T::Type); @@ -106,7 +106,7 @@ namespace bw } template - bool CommandStore::UnserializePacket(PeerRef peer, Nz::NetPacket& packet) const + bool CommandStore::UnserializePacket(PeerRef peer, Nz::ByteStream& packet) const { Nz::UInt8 opcode; try diff --git a/include/CoreLib/MatchClientSession.hpp b/include/CoreLib/MatchClientSession.hpp index 72a3dfef..52a50a54 100644 --- a/include/CoreLib/MatchClientSession.hpp +++ b/include/CoreLib/MatchClientSession.hpp @@ -50,7 +50,7 @@ namespace bw inline MatchClientVisibility& GetVisibility(); inline const MatchClientVisibility& GetVisibility() const; - void HandleIncomingPacket(Nz::NetPacket& packet); + void HandleIncomingPacket(Nz::ByteArray& packet); void OnTick(Nz::Time elapsedTime); diff --git a/include/CoreLib/MatchClientSession.inl b/include/CoreLib/MatchClientSession.inl index fe92e59d..ade76ead 100644 --- a/include/CoreLib/MatchClientSession.inl +++ b/include/CoreLib/MatchClientSession.inl @@ -45,8 +45,10 @@ namespace bw template void MatchClientSession::SendPacket(const T& packet) { - Nz::NetPacket data; - m_commandStore.SerializePacket(data, packet); + Nz::ByteArray data; + Nz::ByteStream stream(&data, Nz::OpenMode::Write); + m_commandStore.SerializePacket(stream, packet); + stream.FlushBits(); const auto& command = m_commandStore.GetOutgoingCommand(); m_bridge->SendPacket(command.channelId, command.flags, std::move(data)); diff --git a/include/CoreLib/NetworkReactor.hpp b/include/CoreLib/NetworkReactor.hpp index 5bdbb458..f7dc86ca 100644 --- a/include/CoreLib/NetworkReactor.hpp +++ b/include/CoreLib/NetworkReactor.hpp @@ -47,7 +47,7 @@ namespace bw void QueryInfo(std::size_t peerId, PeerInfoCallback callback); - void SendData(std::size_t peerId, Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::NetPacket&& packet); + void SendData(std::size_t peerId, Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::ByteArray&& packet); NetworkReactor& operator=(const NetworkReactor&) = delete; NetworkReactor& operator=(NetworkReactor&&) = delete; @@ -96,7 +96,7 @@ namespace bw struct PacketEvent { - Nz::NetPacket packet; + Nz::ByteArray packet; }; struct PeerInfoResponse @@ -121,7 +121,7 @@ namespace bw { Nz::ENetPacketFlags flags; Nz::UInt8 channelId; - Nz::NetPacket packet; + Nz::ByteArray packet; }; struct QueryPeerInfo diff --git a/include/CoreLib/NetworkSessionBridge.hpp b/include/CoreLib/NetworkSessionBridge.hpp index a955bfa0..de40bb47 100644 --- a/include/CoreLib/NetworkSessionBridge.hpp +++ b/include/CoreLib/NetworkSessionBridge.hpp @@ -28,7 +28,7 @@ namespace bw void QueryInfo(std::function callback) const override; - void SendPacket(Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::NetPacket&& packet) override; + void SendPacket(Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::ByteArray&& packet) override; private: std::size_t m_peerId; diff --git a/include/CoreLib/NetworkSessionManager.hpp b/include/CoreLib/NetworkSessionManager.hpp index 0d3194af..9714bbb1 100644 --- a/include/CoreLib/NetworkSessionManager.hpp +++ b/include/CoreLib/NetworkSessionManager.hpp @@ -29,7 +29,7 @@ namespace bw private: void HandlePeerConnection(bool outgoing, std::size_t peerId, Nz::UInt32 data); void HandlePeerDisconnection(std::size_t peerId, Nz::UInt32 data); - void HandlePeerPacket(std::size_t peerId, Nz::NetPacket&& packet); + void HandlePeerPacket(std::size_t peerId, Nz::ByteArray&& packet); std::vector m_peerIdToSession; NetworkReactor m_reactor; diff --git a/include/CoreLib/Protocol/Packets.hpp b/include/CoreLib/Protocol/Packets.hpp index 943facf9..2b15faf1 100644 --- a/include/CoreLib/Protocol/Packets.hpp +++ b/include/CoreLib/Protocol/Packets.hpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include diff --git a/include/CoreLib/SessionBridge.hpp b/include/CoreLib/SessionBridge.hpp index 6f20f8ac..6aad2558 100644 --- a/include/CoreLib/SessionBridge.hpp +++ b/include/CoreLib/SessionBridge.hpp @@ -33,15 +33,15 @@ namespace bw virtual void HandleConnection(Nz::UInt32 data); virtual void HandleDisconnection(Nz::UInt32 data); - virtual void HandleIncomingPacket(Nz::NetPacket& packet); + virtual void HandleIncomingPacket(Nz::ByteArray& packet); virtual void QueryInfo(std::function callback) const = 0; - virtual void SendPacket(Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::NetPacket&& data) = 0; + virtual void SendPacket(Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::ByteArray&& data) = 0; NazaraSignal(OnConnected, Nz::UInt32 /*data*/); NazaraSignal(OnDisconnected, Nz::UInt32 /*data*/); - NazaraSignal(OnIncomingPacket, Nz::NetPacket& /*packet*/); + NazaraSignal(OnIncomingPacket, Nz::ByteArray& /*packet*/); struct SessionInfo { diff --git a/src/ClientLib/ClientCommandStore.cpp b/src/ClientLib/ClientCommandStore.cpp index 0682ef87..aea8fda6 100644 --- a/src/ClientLib/ClientCommandStore.cpp +++ b/src/ClientLib/ClientCommandStore.cpp @@ -53,16 +53,16 @@ namespace bw IncomingCommand(ScriptPacket); // Outgoing commands - OutgoingCommand(Auth, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(DownloadClientFileRequest, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(NetworkStrings, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(PlayerChat, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(PlayerConsoleCommand, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(PlayerSelectWeapon, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(PlayersInput, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(Ready, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(ScriptPacket, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(UpdatePlayerName, Nz::ENetPacketFlag_Reliable, 1); + OutgoingCommand(Auth, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(DownloadClientFileRequest, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(NetworkStrings, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(PlayerChat, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(PlayerConsoleCommand, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(PlayerSelectWeapon, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(PlayersInput, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(Ready, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(ScriptPacket, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(UpdatePlayerName, Nz::ENetPacketFlag::Reliable, 1); #undef IncomingCommand #undef OutgoingCommand diff --git a/src/ClientLib/ClientMatch.cpp b/src/ClientLib/ClientMatch.cpp index ca27bca3..eac83925 100644 --- a/src/ClientLib/ClientMatch.cpp +++ b/src/ClientLib/ClientMatch.cpp @@ -511,80 +511,73 @@ namespace bw if (m_debug) { - Nz::NetPacket debugPacket; - while (m_debug->socket.ReceivePacket(&debugPacket, nullptr)) + Nz::ByteArray debugPacket(0xFFFF); + std::size_t received; + while (m_debug->socket.Receive(debugPacket.GetBuffer(), debugPacket.GetSize(), nullptr, &received) && received > 0) { - switch (debugPacket.GetNetCode()) + debugPacket.Resize(received); + Nz::ByteStream stream(&debugPacket, Nz::OpenMode::Read); + + Nz::UInt32 entityCount; + stream >> entityCount; + + for (Nz::UInt32 i = 0; i < entityCount; ++i) { - case 1: //< StatePacket - { - Nz::UInt32 entityCount; - debugPacket >> entityCount; + CompressedUnsigned layerId; + CompressedUnsigned entityId; + stream >> layerId; + stream >> entityId; - for (Nz::UInt32 i = 0; i < entityCount; ++i) - { - CompressedUnsigned layerId; - CompressedUnsigned entityId; - debugPacket >> layerId; - debugPacket >> entityId; + bool isPhysical; + Nz::Vector2f linearVelocity; + Nz::RadianAnglef angularVelocity; + Nz::Vector2f position; + Nz::RadianAnglef rotation; - bool isPhysical; - Nz::Vector2f linearVelocity; - Nz::RadianAnglef angularVelocity; - Nz::Vector2f position; - Nz::RadianAnglef rotation; + stream >> isPhysical; - debugPacket >> isPhysical; + if (isPhysical) + stream >> linearVelocity >> angularVelocity; - if (isPhysical) - debugPacket >> linearVelocity >> angularVelocity; + stream >> position >> rotation; - debugPacket >> position >> rotation; + auto& layer = m_layers[layerId]; + if (layer->IsEnabled()) + { + if (auto entityOpt = layer->GetEntityByServerId(entityId)) + { + ClientLayerEntity& entity = entityOpt.value(); + ClientLayerEntity* ghostEntity = entity.GetGhost(); + /*if (isPhysical) + ghostEntity->UpdateState(position, rotation, linearVelocity, angularVelocity); + else*/ + ghostEntity->UpdateState(position, rotation); + + ghostEntity->SyncVisuals(); + } + } - auto& layer = m_layers[layerId]; - if (layer->IsEnabled()) + /*if (auto it = m_serverEntityIdToClient.find(entityId); it != m_serverEntityIdToClient.end()) + { + ServerEntity& serverEntity = it.value(); + if (serverEntity.serverGhost) + { + if (isPhysical && serverEntity.serverGhost->HasComponent()) { - if (auto entityOpt = layer->GetEntityByServerId(entityId)) - { - ClientLayerEntity& entity = entityOpt.value(); - ClientLayerEntity* ghostEntity = entity.GetGhost(); - /*if (isPhysical) - ghostEntity->UpdateState(position, rotation, linearVelocity, angularVelocity); - else*/ - ghostEntity->UpdateState(position, rotation); - - ghostEntity->SyncVisuals(); - } + auto& ghostPhysics = serverEntity.serverGhost->GetComponent(); + ghostPhysics.SetPosition(position); + ghostPhysics.SetRotation(rotation); + ghostPhysics.SetAngularVelocity(angularVelocity); + ghostPhysics.SetVelocity(linearVelocity); } - - /*if (auto it = m_serverEntityIdToClient.find(entityId); it != m_serverEntityIdToClient.end()) + else { - ServerEntity& serverEntity = it.value(); - if (serverEntity.serverGhost) - { - if (isPhysical && serverEntity.serverGhost->HasComponent()) - { - auto& ghostPhysics = serverEntity.serverGhost->GetComponent(); - ghostPhysics.SetPosition(position); - ghostPhysics.SetRotation(rotation); - ghostPhysics.SetAngularVelocity(angularVelocity); - ghostPhysics.SetVelocity(linearVelocity); - } - else - { - auto& ghostNode = serverEntity.serverGhost->GetComponent(); - ghostNode.SetPosition(position); - ghostNode.SetRotation(rotation); - } - } - }*/ + auto& ghostNode = serverEntity.serverGhost->GetComponent(); + ghostNode.SetPosition(position); + ghostNode.SetRotation(rotation); + } } - - break; - } - - default: - break; + }*/ } } } diff --git a/src/ClientLib/ClientSession.cpp b/src/ClientLib/ClientSession.cpp index fbe570d3..e97e9d63 100644 --- a/src/ClientLib/ClientSession.cpp +++ b/src/ClientLib/ClientSession.cpp @@ -28,7 +28,7 @@ namespace bw OnSessionDisconnected(); }); - m_onIncomingPacketSlot.Connect(m_bridge->OnIncomingPacket, [this](Nz::NetPacket& packet) + m_onIncomingPacketSlot.Connect(m_bridge->OnIncomingPacket, [this](Nz::ByteArray& packet) { HandleIncomingPacket(packet); }); @@ -65,9 +65,10 @@ namespace bw } } - void ClientSession::HandleIncomingPacket(Nz::NetPacket& packet) + void ClientSession::HandleIncomingPacket(Nz::ByteArray& packet) { - m_commandStore.UnserializePacket(this, packet); + Nz::ByteStream stream(&packet, Nz::OpenMode::Read); + m_commandStore.UnserializePacket(this, stream); } void ClientSession::OnSessionConnected() diff --git a/src/ClientLib/LocalSessionBridge.cpp b/src/ClientLib/LocalSessionBridge.cpp index 7ac82868..b5c02a8c 100644 --- a/src/ClientLib/LocalSessionBridge.cpp +++ b/src/ClientLib/LocalSessionBridge.cpp @@ -40,12 +40,12 @@ namespace bw return true; } - void LocalSessionBridge::HandleIncomingPacket(Nz::NetPacket& packet) + void LocalSessionBridge::HandleIncomingPacket(Nz::ByteArray& packet) { BurgAppComponent& app = m_sessionManager.GetOwner()->GetMatch().GetApp(); m_lastReceiveTime = app.GetAppTime(); - m_sessionInfo.totalByteReceived += packet.GetDataSize(); + m_sessionInfo.totalByteReceived += packet.GetSize(); m_sessionInfo.totalPacketReceived++; SessionBridge::HandleIncomingPacket(packet); @@ -60,11 +60,11 @@ namespace bw callback(m_sessionInfo); } - void LocalSessionBridge::SendPacket(Nz::UInt8 /*channelId*/, Nz::ENetPacketFlags /*flags*/, Nz::NetPacket&& packet) + void LocalSessionBridge::SendPacket(Nz::UInt8 /*channelId*/, Nz::ENetPacketFlags /*flags*/, Nz::ByteArray&& packet) { assert(IsConnected()); - m_sessionInfo.totalByteSent += packet.GetDataSize(); + m_sessionInfo.totalByteSent += packet.GetSize(); m_sessionInfo.totalPacketSent++; m_sessionManager.SendPacket(m_peerId, std::move(packet), m_isServer); diff --git a/src/ClientLib/LocalSessionManager.cpp b/src/ClientLib/LocalSessionManager.cpp index 73afe1cc..199fd6a0 100644 --- a/src/ClientLib/LocalSessionManager.cpp +++ b/src/ClientLib/LocalSessionManager.cpp @@ -72,14 +72,12 @@ namespace bw peer.disconnectionRequested = true; } - void LocalSessionManager::SendPacket(std::size_t peerId, Nz::NetPacket&& packet, bool isServer) + void LocalSessionManager::SendPacket(std::size_t peerId, Nz::ByteArray&& packet, bool isServer) { assert(peerId < m_peers.size() && m_peers[peerId]); Peer& peer = m_peers[peerId].value(); // Reset cursor position - packet.GetStream()->SetCursorPos(Nz::NetPacket::HeaderSize); - if (isServer) peer.clientPackets.emplace_back(std::move(packet)); else diff --git a/src/ClientLib/NetworkReactorManager.cpp b/src/ClientLib/NetworkReactorManager.cpp index 4b1b6842..8b1b630d 100644 --- a/src/ClientLib/NetworkReactorManager.cpp +++ b/src/ClientLib/NetworkReactorManager.cpp @@ -52,7 +52,7 @@ namespace bw { reactorPtr->Poll([&](bool outgoing, std::size_t clientId, Nz::UInt32 data) { HandlePeerConnection(outgoing, clientId, data); }, [&](std::size_t clientId, Nz::UInt32 data) { HandlePeerDisconnection(clientId, data); }, - [&](std::size_t clientId, Nz::NetPacket&& packet) { HandlePeerPacket(clientId, packet); }); + [&](std::size_t clientId, Nz::ByteArray&& packet) { HandlePeerPacket(clientId, packet); }); } } @@ -67,7 +67,7 @@ namespace bw m_connections[peerId].reset(); } - void NetworkReactorManager::HandlePeerPacket(std::size_t peerId, Nz::NetPacket& packet) + void NetworkReactorManager::HandlePeerPacket(std::size_t peerId, Nz::ByteArray& packet) { m_connections[peerId]->HandleIncomingPacket(packet); } diff --git a/src/CoreLib/Match.cpp b/src/CoreLib/Match.cpp index cd802a20..a2e409b5 100644 --- a/src/CoreLib/Match.cpp +++ b/src/CoreLib/Match.cpp @@ -664,12 +664,13 @@ namespace bw m_debug->lastBroadcastTime = m_app.GetAppTime(); // Send all entities state - Nz::NetPacket debugPacket(1); + Nz::ByteArray debugPacket; + Nz::ByteStream stream(&debugPacket); - std::size_t offset = debugPacket.GetStream()->GetCursorPos(); + std::size_t offset = stream.GetStream()->GetCursorPos(); Nz::UInt32 entityCount = 0; - debugPacket << entityCount; + stream << entityCount; for (LayerIndex i = 0; i < m_terrain->GetLayerCount(); ++i) { @@ -685,13 +686,13 @@ namespace bw CompressedUnsigned layerId(i); CompressedUnsigned entityId(entitySync->GetNetworkId()); - debugPacket << layerId; - debugPacket << entityId; + stream << layerId; + stream << entityId; Nz::RigidBody2DComponent* entityPhys = entity.try_get(); bool isPhysical = (entityPhys != nullptr); - debugPacket << isPhysical; + stream << isPhysical; Nz::Vector2f entityPosition; Nz::RadianAnglef entityRotation; @@ -701,7 +702,7 @@ namespace bw entityPosition = entityPhys->GetPosition(); entityRotation = entityPhys->GetRotation(); - debugPacket << entityPhys->GetVelocity() << entityPhys->GetAngularVelocity(); + stream << entityPhys->GetVelocity() << entityPhys->GetAngularVelocity(); } else { @@ -709,19 +710,19 @@ namespace bw entityRotation = AngleFromQuaternion(entityNode->GetGlobalRotation()); } - debugPacket << entityPosition << entityRotation; + stream << entityPosition << entityRotation; }); } - debugPacket.GetStream()->SetCursorPos(offset); - debugPacket << entityCount; + stream.GetStream()->SetCursorPos(offset); + stream << entityCount; Nz::IpAddress localAddress = Nz::IpAddress::LoopbackIpV4; for (std::size_t i = 0; i < 4; ++i) { localAddress.SetPort(static_cast(42000 + i)); - if (!m_debug->socket.SendPacket(localAddress, debugPacket)) + if (!m_debug->socket.Send(localAddress, debugPacket.GetConstBuffer(), debugPacket.GetSize(), nullptr)) bwLog(GetLogger(), LogLevel::Error, "Failed to send debug packet: {0}", Nz::ErrorToString(m_debug->socket.GetLastError())); } } diff --git a/src/CoreLib/MatchClientSession.cpp b/src/CoreLib/MatchClientSession.cpp index bf689b78..b2cfdb63 100644 --- a/src/CoreLib/MatchClientSession.cpp +++ b/src/CoreLib/MatchClientSession.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace @@ -33,7 +34,7 @@ namespace bw m_peerInfoUpdateCounter(Nz::Time::Zero()) { m_visibility = std::make_unique(match, *this); - m_bridge->OnIncomingPacket.Connect([this](Nz::NetPacket& packet) + m_bridge->OnIncomingPacket.Connect([this](Nz::ByteArray& packet) { HandleIncomingPacket(packet); }); @@ -52,9 +53,10 @@ namespace bw m_bridge->Disconnect(); } - void MatchClientSession::HandleIncomingPacket(Nz::NetPacket& packet) + void MatchClientSession::HandleIncomingPacket(Nz::ByteArray& packet) { - m_commandStore.UnserializePacket(*this, packet); + Nz::ByteStream stream(&packet, Nz::OpenMode::Read); + m_commandStore.UnserializePacket(*this, stream); } void MatchClientSession::OnTick(Nz::Time /*elapsedTime*/) diff --git a/src/CoreLib/NetworkReactor.cpp b/src/CoreLib/NetworkReactor.cpp index 490f1004..04787e19 100644 --- a/src/CoreLib/NetworkReactor.cpp +++ b/src/CoreLib/NetworkReactor.cpp @@ -97,7 +97,7 @@ namespace bw m_outgoingQueue.enqueue(std::move(outgoingRequest)); } - void NetworkReactor::SendData(std::size_t peerId, Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::NetPacket&& packet) + void NetworkReactor::SendData(std::size_t peerId, Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::ByteArray&& packet) { assert(peerId >= m_firstId); diff --git a/src/CoreLib/NetworkSessionBridge.cpp b/src/CoreLib/NetworkSessionBridge.cpp index f2ffc49a..caba7169 100644 --- a/src/CoreLib/NetworkSessionBridge.cpp +++ b/src/CoreLib/NetworkSessionBridge.cpp @@ -36,9 +36,8 @@ namespace bw }); } - void NetworkSessionBridge::SendPacket(Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::NetPacket && packet) + void NetworkSessionBridge::SendPacket(Nz::UInt8 channelId, Nz::ENetPacketFlags flags, Nz::ByteArray && packet) { - packet.FlushBits(); m_reactor.SendData(m_peerId, channelId, flags, std::move(packet)); } } diff --git a/src/CoreLib/NetworkSessionManager.cpp b/src/CoreLib/NetworkSessionManager.cpp index 494caa3e..d48e8e72 100644 --- a/src/CoreLib/NetworkSessionManager.cpp +++ b/src/CoreLib/NetworkSessionManager.cpp @@ -23,7 +23,7 @@ namespace bw { m_reactor.Poll([&](bool outgoing, std::size_t peerId, Nz::UInt32 data) { HandlePeerConnection(outgoing, peerId, data); }, [&](std::size_t peerId, Nz::UInt32 data) { HandlePeerDisconnection(peerId, data); }, - [&](std::size_t peerId, Nz::NetPacket&& packet) { HandlePeerPacket(peerId, std::move(packet)); }); + [&](std::size_t peerId, Nz::ByteArray&& packet) { HandlePeerPacket(peerId, std::move(packet)); }); } void NetworkSessionManager::HandlePeerConnection(bool /*outgoing*/, std::size_t peerId, Nz::UInt32 /*data*/) @@ -51,7 +51,7 @@ namespace bw session = nullptr; } - void NetworkSessionManager::HandlePeerPacket(std::size_t peerId, Nz::NetPacket&& packet) + void NetworkSessionManager::HandlePeerPacket(std::size_t peerId, Nz::ByteArray&& packet) { MatchClientSession* session = m_peerIdToSession[peerId]; assert(session); diff --git a/src/CoreLib/PlayerCommandStore.cpp b/src/CoreLib/PlayerCommandStore.cpp index cb577d2f..39190451 100644 --- a/src/CoreLib/PlayerCommandStore.cpp +++ b/src/CoreLib/PlayerCommandStore.cpp @@ -29,39 +29,39 @@ namespace bw IncomingCommand(UpdatePlayerName); // Outgoing commands - OutgoingCommand(AuthFailure, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(AuthSuccess, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(ChatMessage, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(ClientAssetList, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(ClientScriptList, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(ConsoleAnswer, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(ControlEntity, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(CreateEntities, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(DeleteEntities, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(DisableLayer, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(DownloadClientFileFragment, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(DownloadClientFileResponse, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(EnableLayer, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(EntitiesAnimation, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(EntitiesDeath, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(EntitiesInputs, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(EntitiesScale, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(EntityPhysics, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(EntityWeapon, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(HealthUpdate, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(InputTimingCorrection, Nz::ENetPacketFlag_Unsequenced, 0); - OutgoingCommand(MapReset, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(MatchData, Nz::ENetPacketFlag_Reliable, 0); + OutgoingCommand(AuthFailure, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(AuthSuccess, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(ChatMessage, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(ClientAssetList, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(ClientScriptList, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(ConsoleAnswer, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(ControlEntity, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(CreateEntities, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(DeleteEntities, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(DisableLayer, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(DownloadClientFileFragment, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(DownloadClientFileResponse, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(EnableLayer, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(EntitiesAnimation, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(EntitiesDeath, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(EntitiesInputs, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(EntitiesScale, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(EntityPhysics, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(EntityWeapon, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(HealthUpdate, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(InputTimingCorrection, Nz::ENetPacketFlag::Unsequenced, 0); + OutgoingCommand(MapReset, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(MatchData, Nz::ENetPacketFlag::Reliable, 0); OutgoingCommand(MatchState, 0, 1); - OutgoingCommand(NetworkStrings, Nz::ENetPacketFlag_Reliable, 0); - OutgoingCommand(PlayerControlEntity, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(PlayerJoined, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(PlayerLayer, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(PlayerLeaving, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(PlayerNameUpdate, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(PlayerPingUpdate, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(PlayerWeapons, Nz::ENetPacketFlag_Reliable, 1); - OutgoingCommand(ScriptPacket, Nz::ENetPacketFlag_Reliable, 1); + OutgoingCommand(NetworkStrings, Nz::ENetPacketFlag::Reliable, 0); + OutgoingCommand(PlayerControlEntity, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(PlayerJoined, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(PlayerLayer, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(PlayerLeaving, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(PlayerNameUpdate, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(PlayerPingUpdate, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(PlayerWeapons, Nz::ENetPacketFlag::Reliable, 1); + OutgoingCommand(ScriptPacket, Nz::ENetPacketFlag::Reliable, 1); #undef IncomingCommand #undef OutgoingCommand diff --git a/src/CoreLib/SessionBridge.cpp b/src/CoreLib/SessionBridge.cpp index d191e7cf..5db78da1 100644 --- a/src/CoreLib/SessionBridge.cpp +++ b/src/CoreLib/SessionBridge.cpp @@ -22,7 +22,7 @@ namespace bw OnDisconnected(data); } - void SessionBridge::HandleIncomingPacket(Nz::NetPacket& packet) + void SessionBridge::HandleIncomingPacket(Nz::ByteArray& packet) { assert(m_isConnected);