Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netplay: assorted minor fixes #3696

Merged
merged 9 commits into from
Mar 26, 2024
7 changes: 4 additions & 3 deletions lib/netplay/netlog.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2020 Warzone 2100 Project
Copyright (C) 2005-2024 Warzone 2100 Project

Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -20,9 +20,10 @@
#ifndef _netlog_h
#define _netlog_h

#include "lib/framework/frame.h"
#include "lib/framework/types.h"
#include "lib/framework/wzglobal.h"

#include "netplay.h"
#include <stdint.h>

bool NETstartLogging();
bool NETstopLogging();
Expand Down
576 changes: 3 additions & 573 deletions lib/netplay/netplay.cpp

Large diffs are not rendered by default.

73 changes: 6 additions & 67 deletions lib/netplay/netplay.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2020 Warzone 2100 Project
Copyright (C) 2005-2024 Warzone 2100 Project

Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -29,10 +29,15 @@
#include "lib/framework/crc.h"
#include "src/factionid.h"
#include "nettypes.h"
#include "wzfile.h"
#include "netlog.h"
#include "sync_debug.h"

#include <physfs.h>
#include <vector>
#include <functional>
#include <memory>

// Lobby Connection errors

enum LOBBY_ERROR_TYPES
Expand Down Expand Up @@ -217,40 +222,6 @@ struct SYNC_COUNTER
uint16_t rejected;
};

void physfs_file_safe_close(PHYSFS_file* f);

struct WZFile
{
public:
// WZFile() : handle_(nullptr, physfs_file_safe_close), size(0), pos(0) { hash.setZero(); }
WZFile(PHYSFS_file *handle, const std::string &filename, Sha256 hash, uint32_t size = 0) : handle_(handle, physfs_file_safe_close), filename(filename), hash(hash), size(size), pos(0) {}

~WZFile();

// Prevent copies
WZFile(const WZFile&) = delete;
void operator=(const WZFile&) = delete;

// Allow move semantics
WZFile(WZFile&& other) = default;
WZFile& operator=(WZFile&& other) = default;

public:
bool closeFile();
inline PHYSFS_file* handle() const
{
return handle_.get();
}

private:
std::unique_ptr<PHYSFS_file, void(*)(PHYSFS_file*)> handle_;
public:
std::string filename;
Sha256 hash;
uint32_t size = 0;
uint32_t pos = 0; // Current position, the range [0; currPos[ has been sent or received already.
};

enum class AIDifficulty : int8_t
{
SUPEREASY,
Expand Down Expand Up @@ -462,8 +433,6 @@ bool NEThostGame(const char *SessionName, const char *PlayerName, bool spectator
bool NETchangePlayerName(UDWORD player, char *newName);// change a players name.
void NETfixDuplicatePlayerNames(); // Change a player's name automatically, if there are duplicates.

#include "netlog.h"

void NETsetMasterserverName(const char *hostname);
const char *NETgetMasterserverName();
void NETsetMasterserverPort(unsigned int port);
Expand All @@ -475,7 +444,6 @@ bool NETgetJoinPreferenceIPv6();
void NETsetDefaultMPHostFreeChatPreference(bool enabled);
bool NETgetDefaultMPHostFreeChatPreference();

bool NETsetupTCPIP(const char *machine);
void NETsetGamePassword(const char *password);
void NETBroadcastPlayerInfo(uint32_t index);
void NETBroadcastTwoPlayerInfo(uint32_t index1, uint32_t index2);
Expand All @@ -501,7 +469,6 @@ bool NETGameIsLocked();
void NETGameLocked(bool flag);
void NETresetGamePassword();
bool NETregisterServer(int state);
bool NETprocessQueuedServerUpdates();
void NETsetPlayerConnectionStatus(CONNECTION_STATUS status, unsigned player); ///< Cumulative, except that CONNECTIONSTATUS_NORMAL resets.
bool NETcheckPlayerConnectionStatus(CONNECTION_STATUS status, unsigned player); ///< True iff connection status icon hasn't expired for this player. CONNECTIONSTATUS_NORMAL means any status, NET_ALL_PLAYERS means all players.

Expand All @@ -511,34 +478,6 @@ bool NETsetAsyncJoinApprovalResult(const std::string& uniqueJoinID, bool approve

const char *messageTypeToString(unsigned messageType);

/// Sync debugging. Only prints anything, if different players would print different things.
#define syncDebug(...) do { _syncDebug(__FUNCTION__, __VA_ARGS__); } while(0)
#ifdef WZ_CC_MINGW
void _syncDebug(const char *function, const char *str, ...) WZ_DECL_FORMAT(__MINGW_PRINTF_FORMAT, 2, 3);
#else
void _syncDebug(const char *function, const char *str, ...) WZ_DECL_FORMAT(printf, 2, 3);
#endif

/// Faster than syncDebug. Make sure that str is a format string that takes ints only.
void _syncDebugIntList(const char *function, const char *str, int *ints, size_t numInts);
#define syncDebugBacktrace() do { _syncDebugBacktrace(__FUNCTION__); } while(0)
void _syncDebugBacktrace(const char *function); ///< Adds a backtrace to syncDebug, if the platform supports it. Can be a bit slow, don't call way too often, unless desperate.
uint32_t syncDebugGetCrc(); ///< syncDebug() calls between uint32_t crc = syncDebugGetCrc(); and syncDebugSetCrc(crc); appear in synch debug logs, but without triggering a desynch if different.
void syncDebugSetCrc(uint32_t crc); ///< syncDebug() calls between uint32_t crc = syncDebugGetCrc(); and syncDebugSetCrc(crc); appear in synch debug logs, but without triggering a desynch if different.

typedef uint16_t GameCrcType; // Truncate CRC of game state to 16 bits, to save a bit of bandwidth.
void resetSyncDebug(); ///< Resets the syncDebug, so syncDebug from a previous game doesn't cause a spurious desynch dump.
GameCrcType nextDebugSync(); ///< Returns a CRC corresponding to all syncDebug() calls since the last nextDebugSync() or resetSyncDebug() call.
bool checkDebugSync(uint32_t checkGameTime, GameCrcType checkCrc); ///< Dumps all syncDebug() calls from that gameTime, if the CRC doesn't match.


// Set whether verbose debug mode - outputting the current player's sync log for every single game tick - is enabled until a specific gameTime value
// WARNING: This may significantly impact performance *and* will fill up your drive with a lot of logs data!
// It is only intended to be used for debugging issues such as: replays desyncing when gameplay does not, etc. (And don't let the game run too long / set untilGameTime too high!)
void NET_setDebuggingModeVerboseOutputAllSyncLogs(uint32_t untilGameTime = 0);
void debugVerboseLogSyncIfNeeded();


/**
* This structure provides read-only access to a player, and can be used to identify players uniquely.
*
Expand Down
27 changes: 2 additions & 25 deletions lib/netplay/netsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1527,30 +1527,7 @@ Socket *socketOpenAny(const SocketAddress *addr, unsigned timeout)
return ret;
}

size_t socketArrayOpen(Socket **sockets, size_t maxSockets, const SocketAddress *addr, unsigned timeout)
{
size_t i = 0;
while (i < maxSockets && addr != nullptr)
{
if (addr->ai_family == AF_INET || addr->ai_family == AF_INET6)
{
sockets[i] = socketOpen(addr, timeout);
i += sockets[i] != nullptr;
}

addr = addr->ai_next;
}
std::fill(sockets + i, sockets + maxSockets, (Socket *)nullptr);
return i;
}

void socketArrayClose(Socket **sockets, size_t maxSockets)
{
std::for_each(sockets, sockets + maxSockets, socketClose); // Close any open sockets.
std::fill(sockets, sockets + maxSockets, (Socket *)nullptr); // Set the pointers to NULL.
}

WZ_DECL_NONNULL(1) bool socketHasIPv4(Socket *sock)
WZ_DECL_NONNULL(1) bool socketHasIPv4(const Socket *sock)
{
if (sock->fd[SOCK_IPV4_LISTEN] != INVALID_SOCKET)
{
Expand All @@ -1573,7 +1550,7 @@ WZ_DECL_NONNULL(1) bool socketHasIPv4(Socket *sock)
}
}

WZ_DECL_NONNULL(1) bool socketHasIPv6(Socket *sock)
WZ_DECL_NONNULL(1) bool socketHasIPv6(const Socket *sock)
{
return sock->fd[SOCK_IPV6_LISTEN] != INVALID_SOCKET;
}
Expand Down
8 changes: 3 additions & 5 deletions lib/netplay/netsocket.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2020 Warzone 2100 Project
Copyright (C) 2005-2024 Warzone 2100 Project

Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -100,10 +100,8 @@ Socket *socketListen(unsigned int port); ///< Cre
WZ_DECL_NONNULL(1) Socket *socketAccept(Socket *sock); ///< Accepts an incoming Socket connection from a listening Socket.
WZ_DECL_NONNULL(1) void socketClose(Socket *sock); ///< Destroys the Socket.
Socket *socketOpenAny(const SocketAddress *addr, unsigned timeout); ///< Opens a Socket, using the first address that works in addr.
size_t socketArrayOpen(Socket **sockets, size_t maxSockets, const SocketAddress *addr, unsigned timeout); ///< Opens up to maxSockets Sockets, of the types listed in addr.
void socketArrayClose(Socket **sockets, size_t maxSockets); ///< Closes all Sockets in the array.
WZ_DECL_NONNULL(1) bool socketHasIPv4(Socket *sock);
WZ_DECL_NONNULL(1) bool socketHasIPv6(Socket *sock);
WZ_DECL_NONNULL(1) bool socketHasIPv4(const Socket *sock);
WZ_DECL_NONNULL(1) bool socketHasIPv6(const Socket *sock);

WZ_DECL_NONNULL(1) char const *getSocketTextAddress(Socket const *sock); ///< Gets a string with the socket address.
std::vector<unsigned char> ipv4_AddressString_To_NetBinary(const std::string& ipv4Address);
Expand Down
Loading
Loading