Skip to content

Commit

Permalink
Multiplayer (part 8)
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Dec 11, 2023
1 parent e6d435c commit 88a0bd2
Show file tree
Hide file tree
Showing 25 changed files with 1,244 additions and 52 deletions.
6 changes: 6 additions & 0 deletions Sources/Jazz2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@
<ClInclude Include="Jazz2\Multiplayer\PacketTypes.h" />
<ClInclude Include="Jazz2\Multiplayer\Peer.h" />
<ClInclude Include="Jazz2\Multiplayer\Reason.h" />
<ClInclude Include="Jazz2\Multiplayer\ServerDiscovery.h" />
<ClInclude Include="Jazz2\PitType.h" />
<ClInclude Include="Jazz2\PlayerActions.h" />
<ClInclude Include="Jazz2\PlayerType.h" />
Expand Down Expand Up @@ -388,6 +389,7 @@
<ClInclude Include="Jazz2\UI\Menu\BeginSection.h" />
<ClInclude Include="Jazz2\UI\Menu\ControlsOptionsSection.h" />
<ClInclude Include="Jazz2\UI\Menu\ControlsSection.h" />
<ClInclude Include="Jazz2\UI\Menu\CreateServerOptionsSection.h" />
<ClInclude Include="Jazz2\UI\Menu\CustomLevelSelectSection.h" />
<ClInclude Include="Jazz2\UI\Menu\EpisodeSelectSection.h" />
<ClInclude Include="Jazz2\UI\Menu\GameplayEnhancementsSection.h" />
Expand All @@ -408,6 +410,7 @@
<ClInclude Include="Jazz2\UI\Menu\RemapControlsSection.h" />
<ClInclude Include="Jazz2\UI\Menu\RescaleModeSection.h" />
<ClInclude Include="Jazz2\UI\Menu\ScrollableMenuSection.h" />
<ClInclude Include="Jazz2\UI\Menu\ServerSelectSection.h" />
<ClInclude Include="Jazz2\UI\Menu\SimpleMessageSection.h" />
<ClInclude Include="Jazz2\UI\Menu\SoundsOptionsSection.h" />
<ClInclude Include="Jazz2\UI\Menu\StartGameOptionsSection.h" />
Expand Down Expand Up @@ -730,6 +733,7 @@
<ClCompile Include="Jazz2\Multiplayer\ConnectionResult.cpp" />
<ClCompile Include="Jazz2\Multiplayer\MultiLevelHandler.cpp" />
<ClCompile Include="Jazz2\Multiplayer\NetworkManager.cpp" />
<ClCompile Include="Jazz2\Multiplayer\ServerDiscovery.cpp" />
<ClCompile Include="Jazz2\PreferencesCache.cpp" />
<ClCompile Include="Jazz2\Resources.cpp" />
<ClCompile Include="Jazz2\Scripting\JJ2PlusDefinitions.cpp" />
Expand All @@ -753,6 +757,7 @@
<ClCompile Include="Jazz2\UI\Menu\AboutSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\BeginSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\ControlsOptionsSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\CreateServerOptionsSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\CustomLevelSelectSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\EpisodeSelectSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\GameplayEnhancementsSection.cpp" />
Expand All @@ -769,6 +774,7 @@
<ClCompile Include="Jazz2\UI\Menu\RefreshCacheSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\RemapControlsSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\RescaleModeSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\ServerSelectSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\SimpleMessageSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\SoundsOptionsSection.cpp" />
<ClCompile Include="Jazz2\UI\Menu\StartGameOptionsSection.cpp" />
Expand Down
18 changes: 18 additions & 0 deletions Sources/Jazz2.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,15 @@
<ClInclude Include="$(ExtensionLibraryPath)\Containers\StringUtils.h">
<Filter>Header Files\Shared\Containers</Filter>
</ClInclude>
<ClInclude Include="Jazz2\Multiplayer\ServerDiscovery.h">
<Filter>Header Files\Jazz2\Multiplayer</Filter>
</ClInclude>
<ClInclude Include="Jazz2\UI\Menu\ServerSelectSection.h">
<Filter>Header Files\Jazz2\UI\Menu</Filter>
</ClInclude>
<ClInclude Include="Jazz2\UI\Menu\CreateServerOptionsSection.h">
<Filter>Header Files\Jazz2\UI\Menu</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Main.cpp">
Expand Down Expand Up @@ -2375,6 +2384,15 @@
<ClCompile Include="$(ExtensionLibraryPath)\Containers\StringUtils.cpp">
<Filter>Source Files\Shared\Containers</Filter>
</ClCompile>
<ClCompile Include="Jazz2\Multiplayer\ServerDiscovery.cpp">
<Filter>Source Files\Jazz2\Multiplayer</Filter>
</ClCompile>
<ClCompile Include="Jazz2\UI\Menu\ServerSelectSection.cpp">
<Filter>Source Files\Jazz2\UI\Menu</Filter>
</ClCompile>
<ClCompile Include="Jazz2\UI\Menu\CreateServerOptionsSection.cpp">
<Filter>Source Files\Jazz2\UI\Menu</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resources.rc">
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/IRootController.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Jazz2

#if defined(WITH_MULTIPLAYER)
virtual bool ConnectToServer(const StringView& address, std::uint16_t port) = 0;
virtual bool CreateServer(std::uint16_t port) = 0;
virtual bool CreateServer(LevelInitialization&& levelInit, std::uint16_t port) = 0;
#endif

virtual Flags GetFlags() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Jazz2/Multiplayer/MultiLevelHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ namespace Jazz2::Multiplayer
std::uint64_t prevState = (it->second.PressedKeys & 0xffffffffu);
it->second.PressedKeys = packet.ReadVariableUint32() | (prevState << 32);

LOGD("Player %i pressed 0x%08x, last state was 0x%08x", playerIndex, it->second.PressedKeys & 0xffffffffu, prevState);
//LOGD("Player %i pressed 0x%08x, last state was 0x%08x", playerIndex, it->second.PressedKeys & 0xffffffffu, prevState);
return true;
}
}
Expand Down
42 changes: 31 additions & 11 deletions Sources/Jazz2/Multiplayer/NetworkManager.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "NetworkManager.h"
#define ENET_IMPLEMENTATION
#include "NetworkManager.h"

#if defined(WITH_MULTIPLAYER)

#include "INetworkHandler.h"
#include "../../nCine/Base/Timer.h"

/*
// <mmeapi.h> included by "enet.h" still uses `far` macro
#define far
Expand All @@ -17,31 +19,32 @@
// Undefine it again after include
#undef far
*/

#include <Containers/String.h>
#include <Threading/Interlocked.h>

using namespace Death;

namespace Jazz2::Multiplayer
{
std::int32_t NetworkManager::_initializeCount = 0;

NetworkManager::NetworkManager()
: _initialized(false), _host(nullptr), _state(NetworkState::None), _handler(nullptr)
: _host(nullptr), _state(NetworkState::None), _handler(nullptr)
{
int error = enet_initialize();
RETURN_ASSERT_MSG(error == 0, "Initialization failed with error %i", error);
_initialized = (error == 0);
InitializeBackend();
}

NetworkManager::~NetworkManager()
{
Dispose();

if (_initialized) {
enet_deinitialize();
}
ReleaseBackend();
}

bool NetworkManager::CreateClient(INetworkHandler* handler, const StringView& address, std::uint16_t port, std::uint32_t clientData)
{
if (!_initialized || _host != nullptr) {
if (_host != nullptr) {
return false;
}

Expand Down Expand Up @@ -72,7 +75,7 @@ namespace Jazz2::Multiplayer

bool NetworkManager::CreateServer(INetworkHandler* handler, std::uint16_t port)
{
if (!_initialized || _host != nullptr) {
if (_host != nullptr) {
return false;
}

Expand All @@ -83,6 +86,8 @@ namespace Jazz2::Multiplayer
_host = enet_host_create(&addr, MaxPeerCount, (std::size_t)NetworkChannel::Count, 0, 0);
RETURNF_ASSERT_MSG(_host != nullptr, "Failed to create a server");

_discovery = std::make_unique<ServerDiscovery>(handler, port);

_handler = handler;
_state = NetworkState::Listening;
_thread.Run(NetworkManager::OnServerThread, this);
Expand Down Expand Up @@ -172,6 +177,21 @@ namespace Jazz2::Multiplayer
enet_peer_disconnect_now(peer._enet, (std::uint32_t)reason);
}

void NetworkManager::InitializeBackend()
{
if (Interlocked::Increment(&_initializeCount) == 1) {
std::int32_t error = enet_initialize();
RETURN_ASSERT_MSG(error == 0, "enet_initialize() failed with error %i", error);
}
}

void NetworkManager::ReleaseBackend()
{
if (Interlocked::Decrement(&_initializeCount) == 0) {
enet_deinitialize();
}
}

void NetworkManager::OnClientThread(void* param)
{
NetworkManager* _this = static_cast<NetworkManager*>(param);
Expand Down
10 changes: 9 additions & 1 deletion Sources/Jazz2/Multiplayer/NetworkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Peer.h"
#include "Reason.h"
#include "ServerDiscovery.h"
#include "../../Common.h"
#include "../../nCine/Threading/Thread.h"
#include "../../nCine/Threading/ThreadSync.h"
Expand Down Expand Up @@ -37,6 +38,8 @@ namespace Jazz2::Multiplayer

class NetworkManager
{
friend class ServerDiscovery;

public:
NetworkManager();
~NetworkManager();
Expand All @@ -58,13 +61,18 @@ namespace Jazz2::Multiplayer
static constexpr std::size_t MaxPeerCount = 64;
static constexpr std::uint32_t ProcessingIntervalMs = 4;

bool _initialized;
_ENetHost* _host;
Thread _thread;
NetworkState _state;
SmallVector<_ENetPeer*, 1> _peers;
INetworkHandler* _handler;
Mutex _lock;
std::unique_ptr<ServerDiscovery> _discovery;

static std::int32_t _initializeCount;

static void InitializeBackend();
static void ReleaseBackend();

static void OnClientThread(void* param);
static void OnServerThread(void* param);
Expand Down
2 changes: 2 additions & 0 deletions Sources/Jazz2/Multiplayer/PacketTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Jazz2::Multiplayer
{
Null,
Ping,
Reserved,

Auth,
LevelReady,
Expand All @@ -28,6 +29,7 @@ namespace Jazz2::Multiplayer
{
Null,
Pong,
Reserved,

LoadLevel,
PlaySfx,
Expand Down
Loading

0 comments on commit 88a0bd2

Please sign in to comment.