Skip to content

Commit

Permalink
More cleaning in the net code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolfering committed Oct 15, 2024
1 parent 189faf6 commit 001d5b1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 65 deletions.
71 changes: 19 additions & 52 deletions Source_Files/Network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ static short localPlayerIndex;
static short localPlayerIdentifier;
static std::string gameSessionIdentifier;
static NetTopologyPtr topology;
static StarGameProtocol sStarGameProtocol;
static NetworkGameProtocol* sCurrentGameProtocol = NULL;
static StarGameProtocol sCurrentGameProtocol;

static byte *deferred_script_data = NULL;
static size_t deferred_script_length = 0;
Expand Down Expand Up @@ -1215,9 +1214,7 @@ bool NetEnter(bool use_remote_hub)
if (!added_exit_procedure) atexit(NetExit);
added_exit_procedure= true;
}

sCurrentGameProtocol = static_cast<NetworkGameProtocol*>(&sStarGameProtocol);


topology = (NetTopologyPtr)malloc(sizeof(NetTopology));
assert(topology);
memset(topology, 0, sizeof(NetTopology));
Expand All @@ -1227,7 +1224,7 @@ bool NetEnter(bool use_remote_hub)
ddpSocket = SDL_SwapBE16(GAME_PORT);
auto error = NetDDPOpenSocket(&ddpSocket, NetDDPPacketHandler);
if (!error) {
sCurrentGameProtocol->Enter(&netState);
sCurrentGameProtocol.Enter(&netState);
netState = netDown;
handlerState = netDown;
}
Expand Down Expand Up @@ -1398,7 +1395,7 @@ void NetExit(
bool
NetSync()
{
return sCurrentGameProtocol->Sync(topology, dynamic_world->tick_count, localPlayerIndex, local_is_server());
return sCurrentGameProtocol.Sync(topology, dynamic_world->tick_count, localPlayerIndex, local_is_server());
}


Expand All @@ -1411,7 +1408,7 @@ NetUnSync()
NetRemoteHubSendCommand(RemoteHubCommand::kEndGame_Command, dynamic_world->tick_count);
}

return sCurrentGameProtocol->UnSync(true, dynamic_world->tick_count);
return sCurrentGameProtocol.UnSync(true, dynamic_world->tick_count);
}

std::weak_ptr<Pinger>
Expand Down Expand Up @@ -1814,7 +1811,7 @@ void NetSetupTopologyFromStarts(const player_start_data* inStartArray, short inS
void
NetDDPPacketHandler(DDPPacketBufferPtr packet)
{
sCurrentGameProtocol->PacketHandler(packet);
sCurrentGameProtocol.PacketHandler(packet);
}


Expand Down Expand Up @@ -2328,24 +2325,16 @@ byte *NetReceiveGameData(bool do_physics)
return map_buffer;
}

void NetSetInitialParameters(
short updates_per_packet,
short update_latency)
{
// initial_updates_per_packet= updates_per_packet;
// initial_update_latency= update_latency;
}

int32
NetGetNetTime(void)
{
return sCurrentGameProtocol->GetNetTime();
return sCurrentGameProtocol.GetNetTime();
}

bool
NetCheckWorldUpdate()
{
return sCurrentGameProtocol->CheckWorldUpdate();
return sCurrentGameProtocol.CheckWorldUpdate();
}

extern const NetworkStats& hub_stats(int player_index);
Expand All @@ -2356,7 +2345,7 @@ void NetProcessMessagesInGame() {
connection_to_server->dispatchIncomingMessages();
} else {
// update stats
if (sCurrentGameProtocol == static_cast<NetworkGameProtocol*>(&sStarGameProtocol) && last_network_stats_send + network_stats_send_period < machine_tick_count())
if (last_network_stats_send + network_stats_send_period < machine_tick_count())
{
std::vector<NetworkStats> stats(topology->player_count);
for (int playerIndex = 0; playerIndex < topology->player_count; ++playerIndex)
Expand Down Expand Up @@ -2787,56 +2776,34 @@ bool NetAllowOverlayMap() {

extern int32 spoke_latency();

int32 NetGetLatency() {
if (sCurrentGameProtocol == static_cast<NetworkGameProtocol*>(&sStarGameProtocol) && connection_to_server) {
return spoke_latency();
} else {
return NetworkStats::invalid;
}
int32 NetGetLatency()
{
return local_is_server() ? NetworkStats::invalid : spoke_latency();
}

const NetworkStats& NetGetStats(int player_index)
{
if (sCurrentGameProtocol == static_cast<NetworkGameProtocol*>(&sStarGameProtocol))
if (local_is_server())
{
if (connection_to_server)
{
if (player_index < sNetworkStats.size())
{
return sNetworkStats[player_index];
}
else
{
return sInvalidStats;
}
}
else
{
return hub_stats(player_index);
}
}
else
{
return sInvalidStats;
return hub_stats(player_index);
}

return player_index < sNetworkStats.size() ? sNetworkStats[player_index] : sInvalidStats;
}

int32 NetGetUnconfirmedActionFlagsCount()
{
assert (sCurrentGameProtocol);
return sCurrentGameProtocol->GetUnconfirmedActionFlagsCount();
return sCurrentGameProtocol.GetUnconfirmedActionFlagsCount();
}

uint32 NetGetUnconfirmedActionFlag(int32 offset)
{
assert (sCurrentGameProtocol);
return sCurrentGameProtocol->PeekUnconfirmedActionFlag(offset);
return sCurrentGameProtocol.PeekUnconfirmedActionFlag(offset);
}

void NetUpdateUnconfirmedActionFlags()
{
assert (sCurrentGameProtocol);
return sCurrentGameProtocol->UpdateUnconfirmedActionFlags();
return sCurrentGameProtocol.UpdateUnconfirmedActionFlags();
}

#endif // !defined(DISABLE_NETWORKING)
Expand Down
5 changes: 2 additions & 3 deletions Source_Files/Network/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ typedef struct game_info
uint32 parent_checksum;

// network parameters
int16 initial_updates_per_packet;
int16 initial_update_latency;
int16 initial_updates_per_packet; //obsolete
int16 initial_update_latency; //obsolete
} game_info;

#define MAX_NET_PLAYER_NAME_LENGTH 32
Expand Down Expand Up @@ -244,7 +244,6 @@ struct player_start_data;
// Gatherer may call this once after all players are gathered but before NetStart()
void NetSetupTopologyFromStarts(const player_start_data* inStartArray, short inStartCount);

void NetSetInitialParameters(short updates_per_packet, short update_latency);
void NetSetDefaultInflater(CommunicationsChannel* channel);
bool NetSync(void);
bool NetUnSync(void);
Expand Down
13 changes: 3 additions & 10 deletions Source_Files/Network/network_dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,6 @@ int network_join(void)
{
write_preferences ();

game_info* myGameInfo= (game_info *)NetGetGameData();
NetSetInitialParameters(myGameInfo->initial_updates_per_packet, myGameInfo->initial_update_latency);
if (gMetaserverClient && gMetaserverClient->isConnected())
{
gMetaserverClient->setMode(1, NetSessionIdentifier());
Expand Down Expand Up @@ -1354,14 +1352,9 @@ bool SetupNetgameDialog::SetupNetworkGameByRunning (
game_information->parent_checksum = read_wad_file_checksum(get_map_file());
game_information->difficulty_level = active_network_preferences->difficulty_level;

int updates_per_packet = 1;
int update_latency = 0;
vassert(updates_per_packet > 0 && update_latency >= 0 && updates_per_packet < 16,
csprintf(temporary, "You idiot! updates_per_packet = %d, update_latency = %d", updates_per_packet, update_latency));
game_information->initial_updates_per_packet = updates_per_packet;
game_information->initial_update_latency = update_latency;
NetSetInitialParameters(updates_per_packet, update_latency);

game_information->initial_updates_per_packet = 1;
game_information->initial_update_latency = 0;

game_information->initial_random_seed = resuming_game ? dynamic_world->random_seed : (uint16) machine_tick_count();

#if mac
Expand Down

0 comments on commit 001d5b1

Please sign in to comment.