* Add network primitives by Freddie Witherden <EvilGuru>
* Modify them slighly to use get/set functions for the package direction (instead of a global accessable to all files which forward declare it) * Add a NETfloat to the set of primitives * Modify NETstring to use strnlen1 instead of strnlen which is a GNU extension to the C library (strnlen1 has been copied from gettext 0.16.1 which is GPLv2+) NB: rerun your buildsystems NB2: Xcode requires an update git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@2358 4a71c877-e1ca-e34f-864e-861f7616d084master
parent
4213087057
commit
74e64738fa
|
@ -11,9 +11,9 @@ CLEANFILES = resource_parser.tab.h strres_parser.tab.h
|
|||
|
||||
noinst_LIBRARIES = libframework.a
|
||||
noinst_HEADERS = configfile.h cursors.h cursors16.h debug.h fractions.h frame.h \
|
||||
frameint.h frameresource.h input.h listmacs.h resly.h \
|
||||
frameint.h frameresource.h input.h listmacs.h resly.h strnlen1.h \
|
||||
strres.h strresly.h treap.h treapint.h trig.h types.h utf8.h
|
||||
|
||||
libframework_a_SOURCES = SDL_framerate.c configfile.c debug.c exceptionhandler.c \
|
||||
frame.c frameresource.c input.c resource_lexer.lex.c resource_parser.tab.c \
|
||||
strres.c strres_lexer.lex.c strres_parser.tab.c treap.c trig.c utf8.c
|
||||
strnlen1.c strres.c strres_lexer.lex.c strres_parser.tab.c treap.c trig.c utf8.c
|
||||
|
|
|
@ -9,6 +9,7 @@ SRC=configfile.c \
|
|||
input.c \
|
||||
resource_parser.tab.c \
|
||||
resource_lexer.lex.c \
|
||||
strnlen1.c \
|
||||
strres.c \
|
||||
strres_parser.tab.c \
|
||||
strres_lexer.lex.c \
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
This file is part of Warzone 2100.
|
||||
Find the length of STRING + 1, but scan at most MAXLEN bytes.
|
||||
Copyright (C) 2005-2006 Free Software Foundation, Inc.
|
||||
|
||||
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
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Warzone 2100 is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Warzone 2100; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA.
|
||||
|
||||
$Revision$
|
||||
$Id$
|
||||
$HeadURL$
|
||||
*/
|
||||
|
||||
/* Specification. */
|
||||
#include "strnlen1.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/* Find the length of STRING + 1, but scan at most MAXLEN bytes.
|
||||
If no '\0' terminator is found in that many characters, return MAXLEN. */
|
||||
/* This is the same as strnlen (string, maxlen - 1) + 1. */
|
||||
size_t strnlen1(const char* string, size_t maxlen)
|
||||
{
|
||||
// Find the first NUL char
|
||||
const char* end = memchr (string, '\0', maxlen);
|
||||
|
||||
if (end != NULL)
|
||||
return end - string + 1;
|
||||
else
|
||||
return maxlen;
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
This file is part of Warzone 2100.
|
||||
Find the length of STRING + 1, but scan at most MAXLEN bytes.
|
||||
Copyright (C) 2005-2006 Free Software Foundation, Inc.
|
||||
|
||||
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
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Warzone 2100 is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Warzone 2100; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, USA.
|
||||
|
||||
$Revision$
|
||||
$Id$
|
||||
$HeadURL$
|
||||
*/
|
||||
|
||||
#ifndef __INCLUDED_LIB_FRAMEWORK_STRNLEN1_H__
|
||||
#define __INCLUDED_LIB_FRAMEWORK_STRNLEN1_H__
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* Find the length of STRING + 1, but scan at most MAXLEN bytes.
|
||||
If no '\0' terminator is found in that many characters, return MAXLEN. */
|
||||
/* This is the same as strnlen (string, maxlen - 1) + 1. */
|
||||
extern size_t strnlen1 (const char* string, size_t maxlen);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif // __INCLUDED_LIB_FRAMEWORK_STRNLEN1_H__
|
|
@ -65,6 +65,12 @@ static void NETallowJoining(void);
|
|||
extern BOOL MultiPlayerJoin(UDWORD dpid); /* from src/multijoin.c ! */
|
||||
extern BOOL MultiPlayerLeave(UDWORD dpid); /* from src/multijoin.c ! */
|
||||
|
||||
/*
|
||||
* Network globals, these are part of the new network API
|
||||
*/
|
||||
NETMSG NetMsg;
|
||||
static PACKETDIR NetDir;
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
||||
// Types
|
||||
|
||||
|
@ -221,14 +227,14 @@ static BOOL NET_recvMessage(NETBUFSOCKET* bs, NETMSG* pMsg)
|
|||
return TRUE;
|
||||
|
||||
error:
|
||||
if (bs->buffer_start != 0)
|
||||
if (bs->buffer_start != 0)
|
||||
{
|
||||
static char* tmp_buffer = NULL;
|
||||
char* buffer_start = bs->buffer + bs->buffer_start;
|
||||
char* tmp;
|
||||
|
||||
// Create tmp buffer if necessary
|
||||
if (tmp_buffer == NULL)
|
||||
if (tmp_buffer == NULL)
|
||||
{
|
||||
tmp_buffer = (char*)malloc(NET_BUFFER_SIZE);
|
||||
}
|
||||
|
@ -271,9 +277,9 @@ static unsigned int NET_CreatePlayer(const char* name, unsigned int flags)
|
|||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = 1; i < MAX_CONNECTED_PLAYERS; ++i)
|
||||
for (i = 1; i < MAX_CONNECTED_PLAYERS; ++i)
|
||||
{
|
||||
if (players[i].allocated == FALSE)
|
||||
if (players[i].allocated == FALSE)
|
||||
{
|
||||
players[i].allocated = TRUE;
|
||||
strcpy(players[i].name, name);
|
||||
|
@ -859,7 +865,7 @@ BOOL NETrecv(NETMSG * pMsg)
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
if (is_server)
|
||||
if (is_server)
|
||||
{
|
||||
NETallowJoining();
|
||||
}
|
||||
|
@ -868,7 +874,7 @@ BOOL NETrecv(NETMSG * pMsg)
|
|||
receive_message:
|
||||
received = FALSE;
|
||||
|
||||
if (is_server)
|
||||
if (is_server)
|
||||
{
|
||||
if (connected_bsocket[current] == NULL)
|
||||
{
|
||||
|
@ -877,29 +883,29 @@ receive_message:
|
|||
|
||||
received = NET_recvMessage(connected_bsocket[current], pMsg);
|
||||
|
||||
if (received == FALSE)
|
||||
if (received == FALSE)
|
||||
{
|
||||
unsigned int i = current + 1;
|
||||
|
||||
if (socket_set == NULL
|
||||
|| SDLNet_CheckSockets(socket_set, NET_READ_TIMEOUT) <= 0)
|
||||
|| SDLNet_CheckSockets(socket_set, NET_READ_TIMEOUT) <= 0)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
for (;;)
|
||||
for (;;)
|
||||
{
|
||||
if (connected_bsocket[i]->socket == NULL)
|
||||
if (connected_bsocket[i]->socket == NULL)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
else if (NET_fillBuffer(connected_bsocket[i], socket_set))
|
||||
else if (NET_fillBuffer(connected_bsocket[i], socket_set))
|
||||
{
|
||||
// we received some data, add to buffer
|
||||
received = NET_recvMessage(connected_bsocket[i], pMsg);
|
||||
current = i;
|
||||
break;
|
||||
}
|
||||
else if (connected_bsocket[i]->socket == NULL)
|
||||
}
|
||||
else if (connected_bsocket[i]->socket == NULL)
|
||||
{
|
||||
// check if we droped any players in the check above
|
||||
unsigned int* message_dpid = (unsigned int*)(message.body);
|
||||
|
@ -916,12 +922,12 @@ receive_message:
|
|||
MultiPlayerLeave(i);
|
||||
}
|
||||
|
||||
if (++i == MAX_CONNECTED_PLAYERS)
|
||||
if (++i == MAX_CONNECTED_PLAYERS)
|
||||
{
|
||||
i = 0;
|
||||
}
|
||||
|
||||
if (i == current+1)
|
||||
if (i == current+1)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
@ -929,17 +935,17 @@ receive_message:
|
|||
}
|
||||
} else {
|
||||
// we are a client
|
||||
if (bsocket == NULL)
|
||||
if (bsocket == NULL)
|
||||
{
|
||||
return FALSE;
|
||||
} else {
|
||||
received = NET_recvMessage(bsocket, pMsg);
|
||||
|
||||
if (received == FALSE)
|
||||
if (received == FALSE)
|
||||
{
|
||||
if ( socket_set != NULL
|
||||
&& SDLNet_CheckSockets(socket_set, NET_READ_TIMEOUT) > 0
|
||||
&& NET_fillBuffer(bsocket, socket_set))
|
||||
&& NET_fillBuffer(bsocket, socket_set))
|
||||
{
|
||||
received = NET_recvMessage(bsocket, pMsg);
|
||||
}
|
||||
|
@ -947,40 +953,40 @@ receive_message:
|
|||
}
|
||||
}
|
||||
|
||||
if (received == FALSE)
|
||||
if (received == FALSE)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
size = pMsg->size + sizeof(pMsg->size) + sizeof(pMsg->type)
|
||||
+ sizeof(pMsg->destination);
|
||||
if (is_server == FALSE)
|
||||
if (is_server == FALSE)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
else if (pMsg->destination == NET_ALL_PLAYERS)
|
||||
}
|
||||
else if (pMsg->destination == NET_ALL_PLAYERS)
|
||||
{
|
||||
unsigned int j;
|
||||
|
||||
// we are the host, and have received a broadcast packet; distribute it
|
||||
for (j = 0; j < MAX_CONNECTED_PLAYERS; ++j)
|
||||
for (j = 0; j < MAX_CONNECTED_PLAYERS; ++j)
|
||||
{
|
||||
if ( j != current
|
||||
&& connected_bsocket[j] != NULL
|
||||
&& connected_bsocket[j]->socket != NULL)
|
||||
&& connected_bsocket[j]->socket != NULL)
|
||||
{
|
||||
SDLNet_TCP_Send(connected_bsocket[j]->socket,
|
||||
pMsg, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (pMsg->destination != NetPlay.dpidPlayer)
|
||||
}
|
||||
else if (pMsg->destination != NetPlay.dpidPlayer)
|
||||
{
|
||||
// message was not meant for us; send it further
|
||||
if ( pMsg->destination < MAX_CONNECTED_PLAYERS
|
||||
&& connected_bsocket[pMsg->destination] != NULL
|
||||
&& connected_bsocket[pMsg->destination]->socket != NULL)
|
||||
&& connected_bsocket[pMsg->destination]->socket != NULL)
|
||||
{
|
||||
debug(LOG_NET, "Reflecting message type %hhu to UDWORD %hhu", pMsg->type, pMsg->destination);
|
||||
SDLNet_TCP_Send(connected_bsocket[pMsg->destination]->socket,
|
||||
|
@ -1179,7 +1185,7 @@ void NETregisterServer(int state)
|
|||
// ////////////////////////////////////////////////////////////////////////
|
||||
// Host a game with a given name and player name. & 4 user game flags
|
||||
|
||||
static void NETallowJoining(void)
|
||||
static void NETallowJoining(void)
|
||||
{
|
||||
unsigned int i;
|
||||
UDWORD numgames = SDL_SwapBE32(1); // always 1 on normal server
|
||||
|
@ -1189,12 +1195,12 @@ static void NETallowJoining(void)
|
|||
|
||||
NETregisterServer(1);
|
||||
|
||||
if (tmp_socket_set == NULL)
|
||||
if (tmp_socket_set == NULL)
|
||||
{
|
||||
// initialize server socket set
|
||||
// FIXME: why is this not done in NETinit()?? - Per
|
||||
tmp_socket_set = SDLNet_AllocSocketSet(MAX_TMP_SOCKETS+1);
|
||||
if (tmp_socket_set == NULL)
|
||||
if (tmp_socket_set == NULL)
|
||||
{
|
||||
debug(LOG_ERROR, "NETallowJoining: Cannot create socket set: %s", SDLNet_GetError());
|
||||
return;
|
||||
|
@ -1204,11 +1210,11 @@ static void NETallowJoining(void)
|
|||
|
||||
if (SDLNet_CheckSockets(tmp_socket_set, NET_READ_TIMEOUT) > 0)
|
||||
{
|
||||
if (SDLNet_SocketReady(tcp_socket))
|
||||
if (SDLNet_SocketReady(tcp_socket))
|
||||
{
|
||||
for (i = 0; i < MAX_TMP_SOCKETS; ++i)
|
||||
for (i = 0; i < MAX_TMP_SOCKETS; ++i)
|
||||
{
|
||||
if (tmp_socket[i] == NULL)
|
||||
if (tmp_socket[i] == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
@ -1217,7 +1223,7 @@ static void NETallowJoining(void)
|
|||
SDLNet_TCP_AddSocket(tmp_socket_set, tmp_socket[i]);
|
||||
if (SDLNet_CheckSockets(tmp_socket_set, 1000) > 0
|
||||
&& SDLNet_SocketReady(tmp_socket)
|
||||
&& SDLNet_TCP_Recv(tmp_socket[i], buffer, 5))
|
||||
&& SDLNet_TCP_Recv(tmp_socket[i], buffer, 5))
|
||||
{
|
||||
if(strcmp(buffer, "list")==0)
|
||||
{
|
||||
|
@ -1233,10 +1239,10 @@ static void NETallowJoining(void)
|
|||
return;
|
||||
}
|
||||
}
|
||||
for(i = 0; i < MAX_TMP_SOCKETS; ++i)
|
||||
for(i = 0; i < MAX_TMP_SOCKETS; ++i)
|
||||
{
|
||||
if ( tmp_socket[i] != NULL
|
||||
&& SDLNet_SocketReady(tmp_socket[i]) > 0)
|
||||
&& SDLNet_SocketReady(tmp_socket[i]) > 0)
|
||||
{
|
||||
int size = SDLNet_TCP_Recv(tmp_socket[i], &message, sizeof(NETMSG));
|
||||
|
||||
|
@ -1247,7 +1253,7 @@ static void NETallowJoining(void)
|
|||
SDLNet_TCP_Close(tmp_socket[i]);
|
||||
tmp_socket[i] = NULL;
|
||||
}
|
||||
else if (message.type == MSG_JOIN)
|
||||
else if (message.type == MSG_JOIN)
|
||||
{
|
||||
int j;
|
||||
|
||||
|
@ -1272,10 +1278,10 @@ static void NETallowJoining(void)
|
|||
message.size = 1;
|
||||
|
||||
// Send info about players to newcomer.
|
||||
for (j = 0; j < MAX_CONNECTED_PLAYERS; ++j)
|
||||
for (j = 0; j < MAX_CONNECTED_PLAYERS; ++j)
|
||||
{
|
||||
if ( players[j].allocated
|
||||
&& dpid != players[j].id)
|
||||
&& dpid != players[j].id)
|
||||
{
|
||||
message.body[0] = players[j].id;
|
||||
NETsend(&message, dpid, TRUE);
|
||||
|
@ -1286,7 +1292,7 @@ static void NETallowJoining(void)
|
|||
message.body[0] = dpid;
|
||||
NETbcast(&message, TRUE);
|
||||
|
||||
for (j = 0; j < MAX_CONNECTED_PLAYERS; ++j)
|
||||
for (j = 0; j < MAX_CONNECTED_PLAYERS; ++j)
|
||||
{
|
||||
NETBroadcastPlayerInfo(j);
|
||||
}
|
||||
|
@ -1560,7 +1566,7 @@ BOOL NETjoinGame(UDWORD gameNumber, const char* playername)
|
|||
{
|
||||
NETrecv(&message);
|
||||
|
||||
if (message.type == MSG_ACCEPTED)
|
||||
if (message.type == MSG_ACCEPTED)
|
||||
{
|
||||
NetPlay.dpidPlayer = message.body[0];
|
||||
debug(LOG_NET, "NETjoinGame: I'm player %u", NetPlay.dpidPlayer);
|
||||
|
@ -1585,6 +1591,15 @@ BOOL NETjoinGame(UDWORD gameNumber, const char* playername)
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
void NETsetPacketDir(PACKETDIR dir)
|
||||
{
|
||||
NetDir = dir;
|
||||
}
|
||||
|
||||
PACKETDIR NETgetPacketDir()
|
||||
{
|
||||
return NetDir;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Set the masterserver name
|
||||
|
|
|
@ -26,6 +26,8 @@
|
|||
#ifndef _netplay_h
|
||||
#define _netplay_h
|
||||
|
||||
#include "nettypes.h"
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
||||
// Include this file in your game to add multiplayer facilities.
|
||||
|
||||
|
@ -67,6 +69,7 @@ typedef struct {
|
|||
uint8_t type; // type of packet
|
||||
uint8_t destination; // player to send to, or NET_ALL_PLAYERS
|
||||
char body[MaxMsgSize];
|
||||
BOOL status; // If the packet compiled or not (this is _not_ sent!)
|
||||
} NETMSG;
|
||||
|
||||
#define AUDIOMSG 255 // an audio packet (special message);
|
||||
|
@ -111,6 +114,8 @@ typedef struct {
|
|||
|
||||
extern NETPLAY NetPlay;
|
||||
|
||||
extern NETMSG NetMsg;
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
||||
// functions available to you.
|
||||
extern BOOL NETinit(BOOL bFirstCall); //init(guid can be NULL)
|
||||
|
@ -150,6 +155,9 @@ extern BOOL NETgetGlobalPlayerData(UDWORD dpid, void *pData, SDWORD *pSize);
|
|||
extern BOOL NETsetLocalPlayerData(UDWORD dpid, void *pData, SDWORD size);
|
||||
extern BOOL NETsetGlobalPlayerData(UDWORD dpid, void *pData, SDWORD size);
|
||||
|
||||
extern void NETsetPacketDir(PACKETDIR dir);
|
||||
extern PACKETDIR NETgetPacketDir(void);
|
||||
|
||||
#include "netlog.h"
|
||||
|
||||
extern void NETsetMasterserverName(const char* hostname);
|
||||
|
|
|
@ -16,3 +16,356 @@
|
|||
along with Warzone 2100; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
/*
|
||||
* Nettypes.c
|
||||
*
|
||||
* Contains the 'new' Network API functiosn for sending and receiving both
|
||||
* low-level primitives and higher-level types.
|
||||
*/
|
||||
|
||||
#include "lib/framework/wzglobal.h"
|
||||
#include "lib/framework/strnlen1.h"
|
||||
#include <string.h>
|
||||
|
||||
#ifdef WZ_OS_WIN
|
||||
# include <winsock.h>
|
||||
#else
|
||||
# include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#include "../framework/frame.h"
|
||||
#include "netplay.h"
|
||||
#include "nettypes.h"
|
||||
|
||||
/*
|
||||
* Begin & End functions
|
||||
*/
|
||||
|
||||
/*
|
||||
* Initialises the packet and sets the type of the packet to type
|
||||
* FIXME: Use an enum for the packet type.
|
||||
*/
|
||||
void NETbegin(uint8_t type, PACKETDIR dir)
|
||||
{
|
||||
NETsetPacketDir(dir);
|
||||
NetMsg.type = type;
|
||||
NetMsg.size = 0;
|
||||
NetMsg.status = TRUE;
|
||||
|
||||
// If encoding zero the message body and size
|
||||
if (dir == PACKET_ENCODE)
|
||||
{
|
||||
memset(&NetMsg.body, '\0', MaxMsgSize);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Sends the packet, sending it to the player specified. If player is equal to
|
||||
* NET_ALL_PLAYERS then the packet is, as expected, sent to everyone.
|
||||
*/
|
||||
BOOL NETend(uint8_t player)
|
||||
{
|
||||
assert(NETgetPacketDir() != PACKET_INVALID);
|
||||
|
||||
// If the packet is not being sent or failed to compile
|
||||
if (NETgetPacketDir() != PACKET_ENCODE || !NetMsg.status)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// We have sent the packet, so make it invalid (to prevent re-sending)
|
||||
NETsetPacketDir(PACKET_INVALID);
|
||||
|
||||
// Send the packet, updating the send functions is on my todo list!
|
||||
if (player == NET_ALL_PLAYERS)
|
||||
{
|
||||
return NETbcast(&NetMsg, TRUE);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NETsend(&NetMsg, player, TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Primitive functions (ints and strings). Due to the lack of C++ and the fact
|
||||
* that I hate macros this is a lot longer than it should be.
|
||||
*/
|
||||
|
||||
BOOL NETint8_t(int8_t *ip)
|
||||
{
|
||||
int8_t *store = (int8_t *) &NetMsg.body[NetMsg.size];
|
||||
|
||||
// Make sure there is enough data/space left in the packet
|
||||
if (sizeof(int8_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
||||
{
|
||||
return NetMsg.status = FALSE;
|
||||
}
|
||||
|
||||
// 8-bit (1 byte) integers need no endian-swapping!
|
||||
if (NETgetPacketDir() == PACKET_ENCODE)
|
||||
{
|
||||
*store = *ip;
|
||||
}
|
||||
else if (NETgetPacketDir() == PACKET_DECODE)
|
||||
{
|
||||
*ip = *store;
|
||||
}
|
||||
|
||||
// Increment the size of the message
|
||||
NetMsg.size += sizeof(int8_t);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL NETuint8_t(uint8_t *ip)
|
||||
{
|
||||
uint8_t *store = (uint8_t *) &NetMsg.body[NetMsg.size];
|
||||
|
||||
// Make sure there is enough data/space left in the packet
|
||||
if (sizeof(uint8_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
||||
{
|
||||
return NetMsg.status = FALSE;
|
||||
}
|
||||
|
||||
// 8-bit (1 byte) integers need no endian-swapping!
|
||||
if (NETgetPacketDir() == PACKET_ENCODE)
|
||||
{
|
||||
*store = *ip;
|
||||
}
|
||||
else if (NETgetPacketDir() == PACKET_DECODE)
|
||||
{
|
||||
*ip = *store;
|
||||
}
|
||||
|
||||
// Increment the size of the message
|
||||
NetMsg.size += sizeof(uint8_t);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL NETint16_t(int16_t *ip)
|
||||
{
|
||||
int16_t *store = (int16_t *) &NetMsg.body[NetMsg.size];
|
||||
|
||||
// Make sure there is enough data/space left in the packet
|
||||
if (sizeof(int16_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
||||
{
|
||||
return NetMsg.status = FALSE;
|
||||
}
|
||||
|
||||
// Convert the integer into the network byte order (big endian)
|
||||
if (NETgetPacketDir() == PACKET_ENCODE)
|
||||
{
|
||||
*store = htons(*ip);
|
||||
}
|
||||
else if (NETgetPacketDir() == PACKET_DECODE)
|
||||
{
|
||||
*ip = ntohs(*store);
|
||||
}
|
||||
|
||||
// Increment the size of the message
|
||||
NetMsg.size += sizeof(int16_t);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL NETuint16_t(uint16_t *ip)
|
||||
{
|
||||
uint16_t *store = (uint16_t *) &NetMsg.body[NetMsg.size];
|
||||
|
||||
// Make sure there is enough data/space left in the packet
|
||||
if (sizeof(uint16_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
||||
{
|
||||
return NetMsg.status = FALSE;
|
||||
}
|
||||
|
||||
// Convert the integer into the network byte order (big endian)
|
||||
if (NETgetPacketDir() == PACKET_ENCODE)
|
||||
{
|
||||
*store = htons(*ip);
|
||||
}
|
||||
else if (NETgetPacketDir() == PACKET_DECODE)
|
||||
{
|
||||
*ip = ntohs(*store);
|
||||
}
|
||||
|
||||
// Increment the size of the message
|
||||
NetMsg.size += sizeof(uint16_t);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL NETint32_t(int32_t *ip)
|
||||
{
|
||||
int32_t *store = (int32_t *) &NetMsg.body[NetMsg.size];
|
||||
|
||||
// Make sure there is enough data/space left in the packet
|
||||
if (sizeof(int32_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
||||
{
|
||||
return NetMsg.status = FALSE;
|
||||
}
|
||||
|
||||
// Convert the integer into the network byte order (big endian)
|
||||
if (NETgetPacketDir() == PACKET_ENCODE)
|
||||
{
|
||||
*store = htonl(*ip);
|
||||
}
|
||||
else if (NETgetPacketDir() == PACKET_DECODE)
|
||||
{
|
||||
*ip = ntohl(*store);
|
||||
}
|
||||
|
||||
// Increment the size of the message
|
||||
NetMsg.size += sizeof(int32_t);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL NETuint32_t(uint32_t *ip)
|
||||
{
|
||||
uint32_t *store = (uint32_t *) &NetMsg.body[NetMsg.size];
|
||||
|
||||
// Make sure there is enough data/space left in the packet
|
||||
if (sizeof(uint32_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
||||
{
|
||||
return NetMsg.status = FALSE;
|
||||
}
|
||||
|
||||
// Convert the integer into the network byte order (big endian)
|
||||
if (NETgetPacketDir() == PACKET_ENCODE)
|
||||
{
|
||||
*store = htonl(*ip);
|
||||
}
|
||||
else if (NETgetPacketDir() == PACKET_DECODE)
|
||||
{
|
||||
*ip = ntohl(*store);
|
||||
}
|
||||
|
||||
// Increment the size of the message
|
||||
NetMsg.size += sizeof(uint32_t);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL NETfloat(float* fp)
|
||||
{
|
||||
/*
|
||||
* NB: Not portable.
|
||||
* This routine only works on machines with IEEE754 floating point numbers.
|
||||
*/
|
||||
|
||||
#if !defined(__STDC_IEC_559__) \
|
||||
&& !defined(__m68k__) && !defined(__sparc__) && !defined(__i386__) \
|
||||
&& !defined(__mips__) && !defined(__ns32k__) && !defined(__alpha__) \
|
||||
&& !defined(__arm__) && !defined(__ppc__) && !defined(__ia64__) \
|
||||
&& !defined(__arm26__) && !defined(__sparc64__) && !defined(__amd64__)
|
||||
# error "this platform hasn't been confirmed to support IEEE754 floating point numbers"
|
||||
#endif
|
||||
|
||||
// IEEE754 floating point numbers can be treated the same as 32bit integers
|
||||
// with regards to endian conversion
|
||||
STATIC_ASSERT(sizeof(float) == sizeof(int32_t));
|
||||
|
||||
return NETint32_t((int32_t*) fp);
|
||||
}
|
||||
|
||||
BOOL NETbool(BOOL *bp)
|
||||
{
|
||||
// Bools are converted to uint8_ts
|
||||
uint8_t tmp = (uint8_t) *bp;
|
||||
NETuint8_t(&tmp);
|
||||
|
||||
// If we are decoding and managed to extract the value set it
|
||||
if (NETgetPacketDir() == PACKET_DECODE && NetMsg.status)
|
||||
{
|
||||
*bp = (BOOL) tmp;
|
||||
}
|
||||
|
||||
return NetMsg.status;
|
||||
}
|
||||
|
||||
/*
|
||||
* NETnull should be used to either add 4 bytes of padding to a message, or to
|
||||
* discard 4 bytes of data from a message.
|
||||
*/
|
||||
BOOL NETnull ()
|
||||
{
|
||||
uint32_t zero = 0;
|
||||
return NETuint32_t(&zero);
|
||||
}
|
||||
|
||||
BOOL NETstring(char *str, uint16_t maxlen)
|
||||
{
|
||||
/*
|
||||
* Strings sent over the network are prefixed with their length, sent as an
|
||||
* unsigned 16-bit integer.
|
||||
*/
|
||||
|
||||
// Work out the length of the string if we are encoding
|
||||
uint16_t len = (NETgetPacketDir() == PACKET_ENCODE) ? strnlen1(str, maxlen) : 0;
|
||||
char *store;
|
||||
|
||||
// Add/fetch the length from the packet
|
||||
NETuint16_t(&len);
|
||||
|
||||
// Map store to the message buffer
|
||||
store = (char *) &NetMsg.body[NetMsg.size];
|
||||
|
||||
// Make sure there is enough data/space left in the packet
|
||||
if (len + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
||||
{
|
||||
return NetMsg.status = FALSE;
|
||||
}
|
||||
|
||||
if (NETgetPacketDir() == PACKET_ENCODE)
|
||||
{
|
||||
memcpy(store, str, len);
|
||||
}
|
||||
else if (NETgetPacketDir() == PACKET_DECODE)
|
||||
{
|
||||
memcpy(str, store, len);
|
||||
}
|
||||
|
||||
// Increment the size of the message
|
||||
NetMsg.size += sizeof(len) + len;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void NETcoder(PACKETDIR dir)
|
||||
{
|
||||
char str[100];
|
||||
char *original = "THIS IS A TEST STRING";
|
||||
BOOL b = TRUE;
|
||||
uint32_t u32 = 32;
|
||||
uint16_t u16 = 16;
|
||||
uint8_t u8 = 8;
|
||||
int32_t i32 = -32;
|
||||
int16_t i16 = -16;
|
||||
int8_t i8 = -8;
|
||||
|
||||
strcpy(str, original);
|
||||
NETbegin(0, dir);
|
||||
NETbool(&b); assert(b == TRUE);
|
||||
NETuint32_t(&u32); assert(u32 == 32);
|
||||
NETuint16_t(&u16); assert(u16 == 16);
|
||||
NETuint8_t(&u8); assert(u8 == 8);
|
||||
NETint32_t(&i32); assert(i32 == -32);
|
||||
NETint16_t(&i16); assert(i16 == -16);
|
||||
NETint8_t(&i8); assert(i8 == -8);
|
||||
NETstring(str, 99); assert(strncmp(str, original, 99) == 0);
|
||||
NETend(0);
|
||||
}
|
||||
|
||||
void NETtest()
|
||||
{
|
||||
NETMSG cmp;
|
||||
|
||||
memset(&cmp, 0, sizeof(cmp));
|
||||
memset(&NetMsg, 0, sizeof(NetMsg));
|
||||
NETcoder(PACKET_ENCODE);
|
||||
memcpy(&cmp, &NetMsg, sizeof(cmp));
|
||||
NETcoder(PACKET_DECODE);
|
||||
ASSERT(memcmp(&cmp, &NetMsg, sizeof(cmp)) == 0, "nettypes unit test failed");
|
||||
}
|
||||
|
|
|
@ -16,9 +16,44 @@
|
|||
along with Warzone 2100; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
/*
|
||||
* Nettypes.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __INCLUDE_LIB_NETPLAY_NETTYPES_H__
|
||||
#define __INCLUDE_LIB_NETPLAY_NETTYPES_H__
|
||||
|
||||
#include "netplay.h"
|
||||
|
||||
#endif //__INCLUDE_LIB_NETPLAY_NETTYPES_H__
|
||||
typedef enum packetDirectionEnum
|
||||
{
|
||||
PACKET_ENCODE,
|
||||
PACKET_DECODE,
|
||||
PACKET_INVALID
|
||||
} PACKETDIR;
|
||||
|
||||
void NETbegin(uint8_t type, PACKETDIR dir);
|
||||
BOOL NETend(uint8_t player);
|
||||
BOOL NETint8_t(int8_t *ip);
|
||||
BOOL NETuint8_t(uint8_t *ip);
|
||||
BOOL NETint16_t(int16_t *ip);
|
||||
BOOL NETuint16_t(uint16_t *ip);
|
||||
BOOL NETint32_t(int32_t *ip);
|
||||
BOOL NETuint32_t(uint32_t *ip);
|
||||
BOOL NETbool(BOOL *bp);
|
||||
BOOL NETnull(void);
|
||||
BOOL NETstring(char *str, uint16_t maxlen);
|
||||
|
||||
#define NETenum(enumPtr) \
|
||||
{ \
|
||||
int32_t _val = (NETgetPacketDir() == PACKET_ENCODE) ? *(enumPtr) : 0; \
|
||||
\
|
||||
NETint32_t(&_val); \
|
||||
\
|
||||
*(enumPtr) = _val; \
|
||||
}
|
||||
|
||||
void NETtest();
|
||||
|
||||
#endif
|
||||
|
|
|
@ -36,24 +36,34 @@
|
|||
<Add option="-Wall" />
|
||||
<Add option="-Werror-implicit-function-declaration" />
|
||||
<Add option="-std=gnu99" />
|
||||
<Add option='-DVERSION=\\"TRUNK\\"' />
|
||||
<Add option='-DLOCALEDIR=\\"\\"' />
|
||||
<Add option='-DPACKAGE=\\"warzone2100\\"' />
|
||||
<Add option='-DVERSION=\"TRUNK-r2026\"' />
|
||||
<Add option='-DLOCALEDIR=\"\"' />
|
||||
<Add option='-DPACKAGE=\"warzone2100\"' />
|
||||
<Add option="-DWIN32" />
|
||||
<Add option="-DYY_STATIC" />
|
||||
<Add directory="." />
|
||||
<Add directory="C:\devpkg\include" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add library="mingw32" />
|
||||
<Add library="SDLmain" />
|
||||
<Add library="physfs" />
|
||||
<Add library="png" />
|
||||
<Add library="m" />
|
||||
<Add library="z" />
|
||||
<Add library="SDL" />
|
||||
<Add library="pthread" />
|
||||
<Add library="gdi32" />
|
||||
<Add library="SDL_net" />
|
||||
<Add library="GL" />
|
||||
<Add library="GLU" />
|
||||
<Add library="openal" />
|
||||
<Add library="opengl32" />
|
||||
<Add library="glu32" />
|
||||
<Add library="openal32" />
|
||||
<Add library="vorbisfile" />
|
||||
<Add library="vorbis" />
|
||||
<Add library="ogg" />
|
||||
<Add library="dbghelp" />
|
||||
<Add library="winmm" />
|
||||
<Add library="wsock32" />
|
||||
<Add library="glc32" />
|
||||
<Add directory="C:\devpkg\lib" />
|
||||
</Linker>
|
||||
<Unit filename="lib/framework/SDL_framerate.c">
|
||||
<Option compilerVar="CC" />
|
||||
|
@ -106,6 +116,10 @@
|
|||
<Option weight="10" />
|
||||
<Option compiler="gcc" use="1" buildCommand="bison -y -d -olib/framework/resource_parser.tab.c $file\n$compiler $options $includes -c lib/framework/resource_parser.tab.c -o $object" />
|
||||
</Unit>
|
||||
<Unit filename="lib/framework/strnlen1.c">
|
||||
<Option compilerVar="CC" />
|
||||
</Unit>
|
||||
<Unit filename="lib/framework/strnlen1.h" />
|
||||
<Unit filename="lib/framework/strres.c">
|
||||
<Option compilerVar="CC" />
|
||||
</Unit>
|
||||
|
@ -866,6 +880,9 @@
|
|||
<Option compilerVar="CC" />
|
||||
</Unit>
|
||||
<Unit filename="src/wrappers.h" />
|
||||
<Unit filename="win32\warzone2100.rc">
|
||||
<Option compilerVar="WINDRES" />
|
||||
</Unit>
|
||||
<Extensions>
|
||||
<code_completion />
|
||||
</Extensions>
|
||||
|
|
|
@ -239,6 +239,10 @@
|
|||
RelativePath="..\lib\framework\SDL_framerate.c"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\lib\framework\strnlen1.c"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\lib\framework\strres.c"
|
||||
>
|
||||
|
@ -1280,6 +1284,10 @@
|
|||
RelativePath="..\lib\framework\frameresource.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\lib\framework\strnlen1.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\lib\framework\utf8.h"
|
||||
>
|
||||
|
|
Loading…
Reference in New Issue