Skip to content

Commit

Permalink
net: postpone onPlayerConnect callback until client confirms they hav…
Browse files Browse the repository at this point in the history
…e received their finalization packet.
  • Loading branch information
zpl-zak committed Jan 14, 2024
1 parent 79d6f88 commit 3eed5d1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions code/framework/src/integrations/client/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "instance.h"

#include <networking/messages/client_connection_finalized.h>
#include <networking/messages/client_initialise_player.h>
#include <networking/messages/client_handshake.h>
#include <networking/messages/client_kick.h>

Expand Down Expand Up @@ -227,6 +228,10 @@ namespace Framework::Integrations::Client {
if (_onConnectionFinalized) {
_onConnectionFinalized(newPlayer, msg->GetServerTickRate());
}

// Notify server we are ready to obtain player data
Framework::Networking::Messages::ClientInitPlayer initPlayer {};
net->Send(initPlayer, SLNet::UNASSIGNED_RAKNET_GUID);
});
net->RegisterMessage<ClientKick>(GameMessages::GAME_CONNECTION_KICKED, [](SLNet::RakNetGUID guid, ClientKick *msg) {
std::string reason = "Unknown.";
Expand Down
10 changes: 7 additions & 3 deletions code/framework/src/integrations/server/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "world/server.h"

#include "networking/messages/client_connection_finalized.h"
#include "networking/messages/client_initialise_player.h"
#include "networking/messages/client_handshake.h"
#include "networking/messages/client_kick.h"
#include "networking/messages/messages.h"
Expand Down Expand Up @@ -285,9 +286,6 @@ namespace Framework::Integrations::Server {

Logging::GetLogger(FRAMEWORK_INNER_SERVER)->info("Player {} guid {} entity id {}", msg->GetPlayerName(), guid.g, newPlayer.id());

if (_onPlayerConnectCallback)
_onPlayerConnectCallback(newPlayer, guid.g);

// Send the connection finalized packet
Framework::Networking::Messages::ClientConnectionFinalized answer;
answer.FromParameters(_opts.tickInterval, newPlayer.id());
Expand All @@ -310,6 +308,12 @@ namespace Framework::Integrations::Server {
net->GetPeer()->CloseConnection(guid, true);
});

net->RegisterMessage<ClientInitPlayer>(Framework::Networking::Messages::GameMessages::GAME_INIT_PLAYER, [this, net](SLNet::RakNetGUID guid, ClientInitPlayer *stub) {
auto e = _worldEngine->GetEntityByGUID(guid.g);
if (_onPlayerConnectCallback && e.is_valid() && e.is_alive())
_onPlayerConnectCallback(e, guid.g);
});

Framework::World::Modules::Base::SetupServerReceivers(net, _worldEngine.get());

Logging::GetLogger(FRAMEWORK_INNER_SERVER)->info("Game sync networking messages registered");
Expand Down
32 changes: 32 additions & 0 deletions code/framework/src/networking/messages/client_initialise_player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* MafiaHub OSS license
* Copyright (c) 2021-2023, MafiaHub. All rights reserved.
*
* This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework.
* See LICENSE file in the source repository for information regarding licensing.
*/

#pragma once

#include "messages.h"

#include <BitStream.h>

#include <flecs/flecs.h>

namespace Framework::Networking::Messages {
class ClientInitPlayer final: public IMessage {
public:
uint8_t GetMessageID() const override {
return GAME_INIT_PLAYER;
}

void Serialize(SLNet::BitStream *bs, bool write) override {

}

bool Valid() const override {
return true;
}
};
} // namespace Framework::Networking::Messages
1 change: 1 addition & 0 deletions code/framework/src/networking/messages/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Framework::Networking::Messages {
GAME_CONNECTION_HANDSHAKE = INTERNAL_NEXT_MESSAGE_ID,
GAME_CONNECTION_FINALIZED,
GAME_CONNECTION_KICKED,
GAME_INIT_PLAYER,

// Game sync entity streamer messages
GAME_SYNC_ENTITY_SPAWN,
Expand Down

0 comments on commit 3eed5d1

Please sign in to comment.