- netcode cleanup.

This moves all parts in the Duke/Redneck modules that access ENet into net.cpp/network.cpp and changes global ENet structs to use C++ notation instead of typedefs so that anonymous definitions for them can be added where needed.
These changes are done to avoid including enet.h globally which is a major issue because this file does not work without including windows.h, making it extremely dirty.
This commit is contained in:
Christoph Oelckers 2020-02-03 21:06:03 +01:00
parent 1cb182be2f
commit 4350a4dd31
11 changed files with 310 additions and 276 deletions

View file

@ -5685,12 +5685,7 @@ int GameInterface::app_main()
#ifndef NETCODE_DISABLE
if (g_networkMode == NET_SERVER || g_networkMode == NET_DEDICATED_SERVER)
{
ENetAddress address = { ENET_HOST_ANY, g_netPort };
g_netServer = enet_host_create(&address, MAXPLAYERS, CHAN_MAX, 0, 0);
if (g_netServer == NULL)
initprintf("An error occurred while trying to create an ENet server host.\n");
else initprintf("Multiplayer server initialized\n");
Net_InitNetwork();
}
#endif
numplayers = 1;

View file

@ -48,7 +48,7 @@ BEGIN_DUKE_NS
ENetHost *g_netServer = NULL;
ENetHost *g_netClient = NULL;
ENetPeer *g_netClientPeer = NULL;
enet_uint16 g_netPort = 23513;
uint16_t g_netPort = 23513;
int32_t g_netDisconnect = 0;
char g_netPassword[32];
int32_t g_networkMode = NET_CLIENT;
@ -5240,6 +5240,141 @@ void Net_SendRTS(int ridiculeNum)
}
}
void Net_InitNetwork()
{
ENetAddress address = { ENET_HOST_ANY, g_netPort };
g_netServer = enet_host_create(&address, MAXPLAYERS, CHAN_MAX, 0, 0);
if (g_netServer == NULL)
initprintf("An error occurred while trying to create an ENet server host.\n");
else initprintf("Multiplayer server initialized\n");
}
void Net_PrintLag(FString &output)
{
// lag meter
if (g_netClientPeer)
{
output.AppendFormat("%d +- %d ms\n", (g_netClientPeer->lastRoundTripTime + g_netClientPeer->roundTripTime) / 2,
(g_netClientPeer->lastRoundTripTimeVariance + g_netClientPeer->roundTripTimeVariance) / 2);
}
}
int osdcmd_listplayers(osdcmdptr_t parm)
{
ENetPeer* currentPeer;
char ipaddr[32];
if (parm && parm->numparms != 0)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
initprintf("Connected clients:\n");
for (currentPeer = g_netServer->peers;
currentPeer < &g_netServer->peers[g_netServer->peerCount];
++currentPeer)
{
if (currentPeer->state != ENET_PEER_STATE_CONNECTED)
continue;
enet_address_get_host_ip(&currentPeer->address, ipaddr, sizeof(ipaddr));
initprintf("%s %s\n", ipaddr,
g_player[(intptr_t)currentPeer->data].user_name);
}
return OSDCMD_OK;
}
#if 0
static int osdcmd_kick(osdcmdptr_t parm)
{
ENetPeer* currentPeer;
uint32_t hexaddr;
if (parm->numparms != 1)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
for (currentPeer = g_netServer->peers;
currentPeer < &g_netServer->peers[g_netServer->peerCount];
++currentPeer)
{
if (currentPeer->state != ENET_PEER_STATE_CONNECTED)
continue;
sscanf(parm->parms[0], "%" SCNx32 "", &hexaddr);
if (currentPeer->address.host == hexaddr)
{
initprintf("Kicking %x (%s)\n", currentPeer->address.host,
g_player[(intptr_t)currentPeer->data].user_name);
enet_peer_disconnect(currentPeer, DISC_KICKED);
return OSDCMD_OK;
}
}
initprintf("Player %s not found!\n", parm->parms[0]);
osdcmd_listplayers(NULL);
return OSDCMD_OK;
}
static int osdcmd_kickban(osdcmdptr_t parm)
{
ENetPeer* currentPeer;
uint32_t hexaddr;
if (parm->numparms != 1)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
for (currentPeer = g_netServer->peers;
currentPeer < &g_netServer->peers[g_netServer->peerCount];
++currentPeer)
{
if (currentPeer->state != ENET_PEER_STATE_CONNECTED)
continue;
sscanf(parm->parms[0], "%" SCNx32 "", &hexaddr);
// TODO: implement banning logic
if (currentPeer->address.host == hexaddr)
{
char ipaddr[32];
enet_address_get_host_ip(&currentPeer->address, ipaddr, sizeof(ipaddr));
initprintf("Host %s is now banned.\n", ipaddr);
initprintf("Kicking %x (%s)\n", currentPeer->address.host,
g_player[(intptr_t)currentPeer->data].user_name);
enet_peer_disconnect(currentPeer, DISC_BANNED);
return OSDCMD_OK;
}
}
initprintf("Player %s not found!\n", parm->parms[0]);
osdcmd_listplayers(NULL);
return OSDCMD_OK;
}
#endif
#endif
//-------------------------------------------------------------------------------------------------

View file

@ -31,7 +31,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define EDUKE32_UNUSED
#endif
#include "enet.h"
struct ENetHost;
struct ENetPeer;
struct ENetAddress;
BEGIN_DUKE_NS
@ -44,7 +46,7 @@ extern ENetPeer *g_netClientPeer;
extern char g_netPassword[32];
extern int32_t g_netDisconnect;
extern int32_t g_netPlayersWaiting;
extern enet_uint16 g_netPort;
extern uint16_t g_netPort;
extern int32_t g_networkMode;
extern int32_t g_netIndex;
@ -298,6 +300,8 @@ void Net_WaitForInitialSnapshot();
void Net_SendTaunt(int ridiculeNum);
void Net_SendRTS(int ridiculeNum);
void Net_InitNetwork();
void Net_PrintLag(FString& output);
#else

View file

@ -543,120 +543,8 @@ static int osdcmd_password(osdcmdptr_t parm)
return OSDCMD_OK;
}
static int osdcmd_listplayers(osdcmdptr_t parm)
{
ENetPeer *currentPeer;
char ipaddr[32];
int osdcmd_listplayers(osdcmdptr_t parm);
if (parm && parm->numparms != 0)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
initprintf("Connected clients:\n");
for (currentPeer = g_netServer -> peers;
currentPeer < & g_netServer -> peers [g_netServer -> peerCount];
++ currentPeer)
{
if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
continue;
enet_address_get_host_ip(&currentPeer->address, ipaddr, sizeof(ipaddr));
initprintf("%s %s\n", ipaddr,
g_player[(intptr_t)currentPeer->data].user_name);
}
return OSDCMD_OK;
}
#if 0
static int osdcmd_kick(osdcmdptr_t parm)
{
ENetPeer *currentPeer;
uint32_t hexaddr;
if (parm->numparms != 1)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
for (currentPeer = g_netServer -> peers;
currentPeer < & g_netServer -> peers [g_netServer -> peerCount];
++ currentPeer)
{
if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
continue;
sscanf(parm->parms[0],"%" SCNx32 "", &hexaddr);
if (currentPeer->address.host == hexaddr)
{
initprintf("Kicking %x (%s)\n", currentPeer->address.host,
g_player[(intptr_t)currentPeer->data].user_name);
enet_peer_disconnect(currentPeer, DISC_KICKED);
return OSDCMD_OK;
}
}
initprintf("Player %s not found!\n", parm->parms[0]);
osdcmd_listplayers(NULL);
return OSDCMD_OK;
}
static int osdcmd_kickban(osdcmdptr_t parm)
{
ENetPeer *currentPeer;
uint32_t hexaddr;
if (parm->numparms != 1)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
for (currentPeer = g_netServer -> peers;
currentPeer < & g_netServer -> peers [g_netServer -> peerCount];
++ currentPeer)
{
if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
continue;
sscanf(parm->parms[0],"%" SCNx32 "", &hexaddr);
// TODO: implement banning logic
if (currentPeer->address.host == hexaddr)
{
char ipaddr[32];
enet_address_get_host_ip(&currentPeer->address, ipaddr, sizeof(ipaddr));
initprintf("Host %s is now banned.\n", ipaddr);
initprintf("Kicking %x (%s)\n", currentPeer->address.host,
g_player[(intptr_t)currentPeer->data].user_name);
enet_peer_disconnect(currentPeer, DISC_BANNED);
return OSDCMD_OK;
}
}
initprintf("Player %s not found!\n", parm->parms[0]);
osdcmd_listplayers(NULL);
return OSDCMD_OK;
}
#endif
static int osdcmd_printtimes(osdcmdptr_t UNUSED(parm))

View file

@ -619,12 +619,9 @@ FString GameInterface::statFPS(void)
output.AppendFormat("G_MoveWorld(): %.3f ms\n", g_moveWorldTime);
}
// lag meter
if (g_netClientPeer)
{
output.AppendFormat("%d +- %d ms\n", (g_netClientPeer->lastRoundTripTime + g_netClientPeer->roundTripTime)/2,
(g_netClientPeer->lastRoundTripTimeVariance + g_netClientPeer->roundTripTimeVariance)/2);
}
#ifndef NETCODE_DISABLE
Net_PrintLag(output);
#endif
}
if (cumulativeFrameDelay >= 1000.0)

View file

@ -7153,15 +7153,7 @@ int GameInterface::app_main()
initprintf("CON debugging activated (level %d).\n",g_scriptDebug);
#ifndef NETCODE_DISABLE
if (g_networkMode == NET_SERVER/* || g_networkMode == NET_DEDICATED_SERVER*/)
{
ENetAddress address = { ENET_HOST_ANY, g_netPort };
g_netServer = enet_host_create(&address, MAXPLAYERS, CHAN_MAX, 0, 0);
if (g_netServer == NULL)
initprintf("An error occurred while trying to create an ENet server host.\n");
else initprintf("Multiplayer server initialized\n");
}
Net_InitNetwork();
#endif
numplayers = 1;
g_mostConcurrentPlayers = ud.multimode; // Lunatic needs this (player[] bound)

View file

@ -3682,6 +3682,145 @@ void Net_SendRTS(int ridiculeNum)
}
}
void Net_InitNetwork()
{
if (g_networkMode == NET_SERVER/* || g_networkMode == NET_DEDICATED_SERVER*/)
{
ENetAddress address = { ENET_HOST_ANY, g_netPort };
g_netServer = enet_host_create(&address, MAXPLAYERS, CHAN_MAX, 0, 0);
if (g_netServer == NULL)
initprintf("An error occurred while trying to create an ENet server host.\n");
else initprintf("Multiplayer server initialized\n");
}
}
void Net_PrintLag(FString& output)
{
// lag meter
if (g_netClientPeer)
{
output.AppendFormat("%d +- %d ms\n", (g_netClientPeer->lastRoundTripTime + g_netClientPeer->roundTripTime) / 2,
(g_netClientPeer->lastRoundTripTimeVariance + g_netClientPeer->roundTripTimeVariance) / 2);
}
}
int osdcmd_listplayers(osdcmdptr_t parm)
{
ENetPeer* currentPeer;
char ipaddr[32];
if (parm && parm->numparms != 0)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
initprintf("Connected clients:\n");
for (currentPeer = g_netServer->peers;
currentPeer < &g_netServer->peers[g_netServer->peerCount];
++currentPeer)
{
if (currentPeer->state != ENET_PEER_STATE_CONNECTED)
continue;
enet_address_get_host_ip(&currentPeer->address, ipaddr, sizeof(ipaddr));
initprintf("%s %s\n", ipaddr,
g_player[(intptr_t)currentPeer->data].user_name);
}
return OSDCMD_OK;
}
#if 0
static int osdcmd_kick(osdcmdptr_t parm)
{
ENetPeer* currentPeer;
uint32_t hexaddr;
if (parm->numparms != 1)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
for (currentPeer = g_netServer->peers;
currentPeer < &g_netServer->peers[g_netServer->peerCount];
++currentPeer)
{
if (currentPeer->state != ENET_PEER_STATE_CONNECTED)
continue;
sscanf(parm->parms[0], "%" SCNx32 "", &hexaddr);
if (currentPeer->address.host == hexaddr)
{
initprintf("Kicking %x (%s)\n", currentPeer->address.host,
g_player[(intptr_t)currentPeer->data].user_name);
enet_peer_disconnect(currentPeer, DISC_KICKED);
return OSDCMD_OK;
}
}
initprintf("Player %s not found!\n", parm->parms[0]);
osdcmd_listplayers(NULL);
return OSDCMD_OK;
}
static int osdcmd_kickban(osdcmdptr_t parm)
{
ENetPeer* currentPeer;
uint32_t hexaddr;
if (parm->numparms != 1)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
for (currentPeer = g_netServer->peers;
currentPeer < &g_netServer->peers[g_netServer->peerCount];
++currentPeer)
{
if (currentPeer->state != ENET_PEER_STATE_CONNECTED)
continue;
sscanf(parm->parms[0], "%" SCNx32 "", &hexaddr);
// TODO: implement banning logic
if (currentPeer->address.host == hexaddr)
{
char ipaddr[32];
enet_address_get_host_ip(&currentPeer->address, ipaddr, sizeof(ipaddr));
initprintf("Host %s is now banned.\n", ipaddr);
initprintf("Kicking %x (%s)\n", currentPeer->address.host,
g_player[(intptr_t)currentPeer->data].user_name);
enet_peer_disconnect(currentPeer, DISC_BANNED);
return OSDCMD_OK;
}
}
initprintf("Player %s not found!\n", parm->parms[0]);
osdcmd_listplayers(NULL);
return OSDCMD_OK;
}
#endif
#endif // !defined NETCODE_DISABLE
END_RR_NS

View file

@ -23,13 +23,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef netplay_h_
#define netplay_h_
#ifdef _WIN32
// include this before enet does
# define NEED_WINSOCK2_H
# include "windows_inc.h"
#endif
#include "enet.h"
struct ENetHost;
struct ENetPeer;
struct ENetEvent;
struct ENetPacket;
BEGIN_RR_NS
@ -42,7 +39,7 @@ extern ENetPeer *g_netClientPeer;
extern char g_netPassword[32];
extern int32_t g_netDisconnect;
extern int32_t g_netPlayersWaiting;
extern enet_uint16 g_netPort;
extern uint16_t g_netPort;
#ifndef NETCODE_DISABLE
extern int32_t g_networkMode;
#else
@ -308,6 +305,8 @@ void faketimerhandler(void);
void Net_SendTaunt(int ridiculeNum);
void Net_SendRTS(int ridiculeNum);
void Net_InitNetwork();
void Net_PrintLag(FString& output);
#else

View file

@ -403,120 +403,7 @@ static int osdcmd_password(osdcmdptr_t parm)
return OSDCMD_OK;
}
static int osdcmd_listplayers(osdcmdptr_t parm)
{
ENetPeer *currentPeer;
char ipaddr[32];
if (parm && parm->numparms != 0)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
initprintf("Connected clients:\n");
for (currentPeer = g_netServer -> peers;
currentPeer < & g_netServer -> peers [g_netServer -> peerCount];
++ currentPeer)
{
if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
continue;
enet_address_get_host_ip(&currentPeer->address, ipaddr, sizeof(ipaddr));
initprintf("%s %s\n", ipaddr,
g_player[(intptr_t)currentPeer->data].user_name);
}
return OSDCMD_OK;
}
#if 0
static int osdcmd_kick(osdcmdptr_t parm)
{
ENetPeer *currentPeer;
uint32_t hexaddr;
if (parm->numparms != 1)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
for (currentPeer = g_netServer -> peers;
currentPeer < & g_netServer -> peers [g_netServer -> peerCount];
++ currentPeer)
{
if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
continue;
sscanf(parm->parms[0],"%" SCNx32 "", &hexaddr);
if (currentPeer->address.host == hexaddr)
{
initprintf("Kicking %x (%s)\n", currentPeer->address.host,
g_player[(intptr_t)currentPeer->data].user_name);
enet_peer_disconnect(currentPeer, DISC_KICKED);
return OSDCMD_OK;
}
}
initprintf("Player %s not found!\n", parm->parms[0]);
osdcmd_listplayers(NULL);
return OSDCMD_OK;
}
static int osdcmd_kickban(osdcmdptr_t parm)
{
ENetPeer *currentPeer;
uint32_t hexaddr;
if (parm->numparms != 1)
return OSDCMD_SHOWHELP;
if (!g_netServer)
{
initprintf("You are not the server.\n");
return OSDCMD_OK;
}
for (currentPeer = g_netServer -> peers;
currentPeer < & g_netServer -> peers [g_netServer -> peerCount];
++ currentPeer)
{
if (currentPeer -> state != ENET_PEER_STATE_CONNECTED)
continue;
sscanf(parm->parms[0],"%" SCNx32 "", &hexaddr);
// TODO: implement banning logic
if (currentPeer->address.host == hexaddr)
{
char ipaddr[32];
enet_address_get_host_ip(&currentPeer->address, ipaddr, sizeof(ipaddr));
initprintf("Host %s is now banned.\n", ipaddr);
initprintf("Kicking %x (%s)\n", currentPeer->address.host,
g_player[(intptr_t)currentPeer->data].user_name);
enet_peer_disconnect(currentPeer, DISC_BANNED);
return OSDCMD_OK;
}
}
initprintf("Player %s not found!\n", parm->parms[0]);
osdcmd_listplayers(NULL);
return OSDCMD_OK;
}
#endif
int osdcmd_listplayers(osdcmdptr_t parm);
#endif
static int osdcmd_printtimes(osdcmdptr_t UNUSED(parm))

View file

@ -618,11 +618,9 @@ FString GameInterface::statFPS()
}
// lag meter
if (g_netClientPeer)
{
output.AppendFormat("%d +- %d ms\n", (g_netClientPeer->lastRoundTripTime + g_netClientPeer->roundTripTime)/2,
(g_netClientPeer->lastRoundTripTimeVariance + g_netClientPeer->roundTripTimeVariance) / 2);
}
#ifndef NETCODE_DISABLE
Net_PrintLag(output);
#endif
}
if (cumulativeFrameDelay >= 1000.0)

View file

@ -487,11 +487,11 @@ extern "C" {
* but not for enet_host_create. Once a server responds to a broadcast, the
* address is updated from ENET_HOST_BROADCAST to the server's actual IP address.
*/
typedef struct _ENetAddress {
struct ENetAddress {
struct in6_addr host;
enet_uint16 port;
enet_uint16 sin6_scope_id;
} ENetAddress;
};
#define in6_equal(in6_addr_a, in6_addr_b) (memcmp(&in6_addr_a, &in6_addr_b, sizeof(struct in6_addr)) == 0)
@ -530,14 +530,14 @@ extern "C" {
* ENET_PACKET_FLAG_SENT - whether the packet has been sent from all queues it has been entered into
* @sa ENetPacketFlag
*/
typedef struct _ENetPacket {
struct ENetPacket {
size_t referenceCount; /**< internal use only */
enet_uint32 flags; /**< bitwise-or of ENetPacketFlag constants */
enet_uint8 * data; /**< allocated data for packet */
size_t dataLength; /**< length of data */
ENetPacketFreeCallback freeCallback; /**< function to be called when the packet is no longer in use */
void * userData; /**< application private data, may be freely modified */
} ENetPacket;
};
typedef struct _ENetAcknowledgement {
ENetListNode acknowledgementList;
@ -629,9 +629,9 @@ extern "C" {
*
* No fields should be modified unless otherwise specified.
*/
typedef struct _ENetPeer {
struct ENetPeer {
ENetListNode dispatchList;
struct _ENetHost *host;
struct ENetHost *host;
enet_uint16 outgoingPeerID;
enet_uint16 incomingPeerID;
enet_uint32 connectID;
@ -694,7 +694,7 @@ extern "C" {
enet_uint32 unsequencedWindow[ENET_PEER_UNSEQUENCED_WINDOW_SIZE / 32];
enet_uint32 eventData;
size_t totalWaitingData;
} ENetPeer;
};
/** An ENet packet compressor for compressing UDP packets before socket sends or receives. */
typedef struct _ENetCompressor {
@ -715,7 +715,7 @@ extern "C" {
typedef enet_uint32 (ENET_CALLBACK * ENetChecksumCallback)(const ENetBuffer *buffers, size_t bufferCount);
/** Callback for intercepting received raw UDP packets. Should return 1 to intercept, 0 to ignore, or -1 to propagate an error. */
typedef int (ENET_CALLBACK * ENetInterceptCallback)(struct _ENetHost *host, void *event);
typedef int (ENET_CALLBACK * ENetInterceptCallback)(struct ENetHost *host, void *event);
/** An ENet host for communicating with peers.
*
@ -732,7 +732,7 @@ extern "C" {
* @sa enet_host_bandwidth_limit()
* @sa enet_host_bandwidth_throttle()
*/
typedef struct _ENetHost {
struct ENetHost {
ENetSocket socket;
ENetAddress address; /**< Internet address of the host */
enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */
@ -769,7 +769,7 @@ extern "C" {
size_t duplicatePeers; /**< optional number of allowed peers from duplicate IPs, defaults to ENET_PROTOCOL_MAXIMUM_PEER_ID */
size_t maximumPacketSize; /**< the maximum allowable packet size that may be sent or received on a peer */
size_t maximumWaitingData; /**< the maximum aggregate amount of buffer space a peer may use waiting for packets to be delivered */
} ENetHost;
};
/**
* An ENet event type, as specified in @ref ENetEvent.
@ -811,13 +811,13 @@ extern "C" {
*
* @sa enet_host_service
*/
typedef struct _ENetEvent {
struct ENetEvent {
ENetEventType type; /**< type of the event */
ENetPeer * peer; /**< peer that generated a connect, disconnect or receive event */
enet_uint8 channelID; /**< channel on the peer that generated the event, if appropriate */
enet_uint32 data; /**< data associated with the event, if appropriate */
ENetPacket * packet; /**< packet associated with the event, if appropriate */
} ENetEvent;
};
// =======================================================================//
// !