-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathN1NetcodeManager.cpp
More file actions
161 lines (131 loc) · 4.33 KB
/
Copy pathN1NetcodeManager.cpp
File metadata and controls
161 lines (131 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2025-2026 POWDER-RANGER. All Rights Reserved.
#include "Core/N1NetcodeManager.h"
#include "Core/N1NetworkClock.h"
#include "Core/N1PredictionBuffer.h"
#include "Pipeline/N1ReconciliationEngine.h"
#include "Pipeline/N1ClientPrediction.h"
#include "Pipeline/N1ServerAuthority.h"
#include "Pipeline/N1RollbackEngine.h"
#include "Pipeline/N1BlendInterpolator.h"
#include "UE6/N1UE6Compatibility.h"
#include "Engine/World.h"
// Static singleton storage
static TWeakObjectPtr<UN1NetcodeManager> GNetcodeManager;
UN1NetcodeManager::UN1NetcodeManager()
{
CurrentMode = EN1NetcodeMode::Standalone;
CurrentPhase = EN1NetcodePhase::Initializing;
}
void UN1NetcodeManager::Initialize(const FN1NetcodeConfig& InConfig, EN1NetcodeMode InMode)
{
Config = InConfig;
CurrentMode = InMode;
UE_LOG(LogN1Netcode, Log, TEXT("Initializing N1 Netcode Manager [Mode: %s]"),
*UEnum::GetValueAsString(InMode));
// Create subsystems
NetworkClock = NewObject<UN1NetworkClock>(this);
NetworkClock->Initialize(InMode == EN1NetcodeMode::Server || InMode == EN1NetcodeMode::ListenServer);
if (InMode == EN1NetcodeMode::Client || InMode == EN1NetcodeMode::ListenServer)
{
auto* Buffer = NewObject<UN1PredictionBuffer>(this);
int32 BufferFrames = FMath::CeilToInt((Config.MaxAcceptableLatencyMs / 1000.0f) * Config.ClientTickRate) + Config.MaxRollbackFrames;
Buffer->Initialize(BufferFrames, Config.ClientTickRate);
PredictionSystem = NewObject<UN1ClientPrediction>(this);
PredictionSystem->Initialize(Buffer, NetworkClock, Config.ClientTickRate);
RollbackEngine = NewObject<UN1RollbackEngine>(this);
RollbackEngine->Initialize(Config.MaxRollbackFrames);
BlendInterpolator = NewObject<UN1BlendInterpolator>(this);
BlendInterpolator->Initialize(Config.BlendFrames, EN1BlendCurve::SmoothStep);
ReconciliationEngine = NewObject<UN1ReconciliationEngine>(this);
ReconciliationEngine->Initialize(Config.RollbackThreshold, Config.MaxRollbackFrames);
}
if (InMode == EN1NetcodeMode::Server || InMode == EN1NetcodeMode::ListenServer)
{
ServerAuthority = NewObject<UN1ServerAuthority>(this);
ServerAuthority->Initialize(Config.ServerTickRate, Config.InputBufferMs);
}
// UE6 compatibility check
auto* UE6Compat = NewObject<UN1UE6Compatibility>(this);
UE6Compat->Initialize();
UE6Compat->LogCompatibilityStatus();
bInitialized = true;
SetPhase(EN1NetcodePhase::Active);
UE_LOG(LogN1Netcode, Log, TEXT("N1 Netcode Manager initialized successfully"));
}
void UN1NetcodeManager::Shutdown()
{
SetPhase(EN1NetcodePhase::ShuttingDown);
PredictionSystem = nullptr;
ReconciliationEngine = nullptr;
RollbackEngine = nullptr;
BlendInterpolator = nullptr;
ServerAuthority = nullptr;
NetworkClock = nullptr;
bInitialized = false;
GNetcodeManager.Reset();
UE_LOG(LogN1Netcode, Log, TEXT("N1 Netcode Manager shutdown complete"));
}
void UN1NetcodeManager::Tick(float DeltaTime)
{
if (!bInitialized) return;
// Update network clock
if (NetworkClock)
{
NetworkClock->Tick(DeltaTime);
}
// Tick client systems
if (PredictionSystem)
{
PredictionSystem->Tick(DeltaTime);
}
if (BlendInterpolator)
{
BlendInterpolator->Tick(DeltaTime);
}
// Server tick
if (ServerAuthority)
{
ServerAuthority->TickServer(DeltaTime);
}
// Periodic forced reconciliation
if (Config.ForcedReconciliationIntervalSec > 0)
{
PerformForcedReconciliation(DeltaTime);
}
}
UN1NetcodeManager* UN1NetcodeManager::GetN1Manager(UWorld* World)
{
if (GNetcodeManager.IsValid())
{
return GNetcodeManager.Get();
}
if (World)
{
UN1NetcodeManager* Manager = NewObject<UN1NetcodeManager>(World);
GNetcodeManager = Manager;
return Manager;
}
return nullptr;
}
void UN1NetcodeManager::SetPhase(EN1NetcodePhase NewPhase)
{
if (CurrentPhase != NewPhase)
{
EN1NetcodePhase OldPhase = CurrentPhase;
CurrentPhase = NewPhase;
OnPhaseChanged.Broadcast(NewPhase);
UE_LOG(LogN1Netcode, Verbose, TEXT("Phase transition: %s -> %s"),
*UEnum::GetValueAsString(OldPhase),
*UEnum::GetValueAsString(NewPhase));
}
}
void UN1NetcodeManager::PerformForcedReconciliation(float DeltaTime)
{
ForcedReconciliationTimer += DeltaTime;
if (ForcedReconciliationTimer >= Config.ForcedReconciliationIntervalSec)
{
ForcedReconciliationTimer = 0.0f;
UE_LOG(LogN1Netcode, Verbose, TEXT("Performing forced periodic reconciliation"));
// This would trigger a full-state resync in production
}
}