2007-11-04 03:34:51 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// Name: l_net.c
|
|
|
|
// Function: -
|
|
|
|
// Programmer: MrElusive
|
|
|
|
// Last update: -
|
|
|
|
// Tab size: 3
|
|
|
|
// Notes:
|
|
|
|
//====================================================================
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "l_net.h"
|
|
|
|
#include "l_net_wins.h"
|
|
|
|
|
|
|
|
#define GetMemory malloc
|
|
|
|
#define FreeMemory free
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define qtrue 1
|
|
|
|
#define qfalse 0
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2012-03-17 20:01:54 +00:00
|
|
|
void WinPrint( char *str, ... ){
|
2007-11-04 03:34:51 +00:00
|
|
|
va_list argptr;
|
2012-03-17 20:01:54 +00:00
|
|
|
char text[4096];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
va_start( argptr,str );
|
|
|
|
vsprintf( text, str, argptr );
|
|
|
|
va_end( argptr );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2013-05-03 06:57:29 +00:00
|
|
|
printf("%s", text );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
#else
|
2012-03-17 20:01:54 +00:00
|
|
|
void WinPrint( char *str, ... ){
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void Net_SetAddressPort( address_t *address, int port ){
|
2007-11-04 03:34:51 +00:00
|
|
|
sockaddr_t addr;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
WINS_StringToAddr( address->ip, &addr );
|
|
|
|
WINS_SetSocketPort( &addr, port );
|
|
|
|
strcpy( address->ip, WINS_AddrToString( &addr ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function Net_SetAddressPort
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int Net_AddressCompare( address_t *addr1, address_t *addr2 ){
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef _WIN32
|
2012-03-17 20:01:54 +00:00
|
|
|
return stricmp( addr1->ip, addr2->ip );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
2013-05-03 06:57:29 +00:00
|
|
|
#if __linux__ || __APPLE__
|
2012-03-17 20:01:54 +00:00
|
|
|
return strcasecmp( addr1->ip, addr2->ip );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
} //end of the function Net_AddressCompare
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void Net_SocketToAddress( socket_t *sock, address_t *address ){
|
|
|
|
strcpy( address->ip, WINS_AddrToString( &sock->addr ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function Net_SocketToAddress
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int Net_Send( socket_t *sock, netmessage_t *msg ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int size;
|
|
|
|
|
|
|
|
size = msg->size;
|
|
|
|
msg->size = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
NMSG_WriteLong( msg, size - 4 );
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->size = size;
|
|
|
|
//WinPrint("Net_Send: message of size %d\n", sendmsg.size);
|
2012-03-17 20:01:54 +00:00
|
|
|
return WINS_Write( sock->socket, msg->data, msg->size, NULL );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function Net_SendSocketReliable
|
|
|
|
//===========================================================================
|
|
|
|
// returns the number of bytes recieved
|
|
|
|
// -1 on error
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int Net_Receive( socket_t *sock, netmessage_t *msg ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int curread;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( sock->remaining > 0 ) {
|
|
|
|
curread = WINS_Read( sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL );
|
|
|
|
if ( curread == -1 ) {
|
|
|
|
WinPrint( "Net_Receive: read error\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
|
|
|
sock->remaining -= curread;
|
|
|
|
sock->msg.size += curread;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( sock->remaining <= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
sock->remaining = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( msg, &sock->msg, sizeof( netmessage_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
sock->msg.size = 0;
|
|
|
|
return msg->size - 4;
|
|
|
|
} //end if
|
|
|
|
return 0;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
sock->msg.size = WINS_Read( sock->socket, sock->msg.data, 4, NULL );
|
|
|
|
if ( sock->msg.size == 0 ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( sock->msg.size == -1 ) {
|
|
|
|
WinPrint( "Net_Receive: size header read error\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
//WinPrint("Net_Receive: message size header %d\n", msg->size);
|
2007-11-04 03:34:51 +00:00
|
|
|
sock->msg.read = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
sock->remaining = NMSG_ReadLong( &sock->msg );
|
|
|
|
if ( sock->remaining == 0 ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( sock->remaining < 0 || sock->remaining > MAX_NETMESSAGE ) {
|
|
|
|
WinPrint( "Net_Receive: invalid message size %d\n", sock->remaining );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
//try to read the message
|
|
|
|
curread = WINS_Read( sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL );
|
|
|
|
if ( curread == -1 ) {
|
|
|
|
WinPrint( "Net_Receive: read error\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
|
|
|
sock->remaining -= curread;
|
|
|
|
sock->msg.size += curread;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( sock->remaining <= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
sock->remaining = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( msg, &sock->msg, sizeof( netmessage_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
sock->msg.size = 0;
|
|
|
|
return msg->size - 4;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
//the message has not been completely read yet
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef _DEBUG
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "++timo TODO: debug the Net_Receive on big size messages\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
} //end of the function Net_Receive
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
socket_t *Net_AllocSocket( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
socket_t *sock;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
sock = (socket_t *) GetMemory( sizeof( socket_t ) );
|
|
|
|
memset( sock, 0, sizeof( socket_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
return sock;
|
|
|
|
} //end of the function Net_AllocSocket
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void Net_FreeSocket( socket_t *sock ){
|
|
|
|
FreeMemory( sock );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function Net_FreeSocket
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
socket_t *Net_Connect( address_t *address, int port ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int newsock;
|
|
|
|
socket_t *sock;
|
|
|
|
sockaddr_t sendaddr;
|
|
|
|
|
|
|
|
// see if we can resolve the host name
|
2012-03-17 20:01:54 +00:00
|
|
|
WINS_StringToAddr( address->ip, &sendaddr );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
newsock = WINS_OpenReliableSocket( port );
|
|
|
|
if ( newsock == -1 ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
sock = Net_AllocSocket();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( sock == NULL ) {
|
|
|
|
WINS_CloseSocket( newsock );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
} //end if
|
|
|
|
sock->socket = newsock;
|
|
|
|
|
|
|
|
//connect to the host
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( WINS_Connect( newsock, &sendaddr ) == -1 ) {
|
|
|
|
Net_FreeSocket( sock );
|
|
|
|
WINS_CloseSocket( newsock );
|
|
|
|
WinPrint( "Net_Connect: error connecting\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
} //end if
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( &sock->addr, &sendaddr, sizeof( sockaddr_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
//now we can send messages
|
|
|
|
//
|
|
|
|
return sock;
|
|
|
|
} //end of the function Net_Connect
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
socket_t *Net_ListenSocket( int port ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int newsock;
|
|
|
|
socket_t *sock;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
newsock = WINS_OpenReliableSocket( port );
|
|
|
|
if ( newsock == -1 ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( WINS_Listen( newsock ) == -1 ) {
|
|
|
|
WINS_CloseSocket( newsock );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
} //end if
|
|
|
|
sock = Net_AllocSocket();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( sock == NULL ) {
|
|
|
|
WINS_CloseSocket( newsock );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
} //end if
|
|
|
|
sock->socket = newsock;
|
2012-03-17 20:01:54 +00:00
|
|
|
WINS_GetSocketAddr( newsock, &sock->addr );
|
|
|
|
WinPrint( "listen socket opened at %s\n", WINS_AddrToString( &sock->addr ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
//
|
|
|
|
return sock;
|
|
|
|
} //end of the function Net_ListenSocket
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
socket_t *Net_Accept( socket_t *sock ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int newsocket;
|
|
|
|
sockaddr_t sendaddr;
|
|
|
|
socket_t *newsock;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
newsocket = WINS_Accept( sock->socket, &sendaddr );
|
|
|
|
if ( newsocket == -1 ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
newsock = Net_AllocSocket();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( newsock == NULL ) {
|
|
|
|
WINS_CloseSocket( newsocket );
|
2007-11-04 03:34:51 +00:00
|
|
|
return NULL;
|
|
|
|
} //end if
|
|
|
|
newsock->socket = newsocket;
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( &newsock->addr, &sendaddr, sizeof( sockaddr_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
//
|
|
|
|
return newsock;
|
|
|
|
} //end of the function Net_Accept
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void Net_Disconnect( socket_t *sock ){
|
|
|
|
WINS_CloseSocket( sock->socket );
|
|
|
|
Net_FreeSocket( sock );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function Net_Disconnect
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void Net_StringToAddress( char *string, address_t *address ){
|
|
|
|
strcpy( address->ip, string );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function Net_StringToAddress
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void Net_MyAddress( address_t *address ){
|
|
|
|
strcpy( address->ip, WINS_MyAddress() );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function Net_MyAddress
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int Net_Setup( void ){
|
|
|
|
if ( !WINS_Init() ) {
|
2008-04-14 20:15:34 +00:00
|
|
|
return qfalse;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2008-04-14 20:15:34 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
WinPrint( "my address is %s\n", WINS_MyAddress() );
|
2007-11-04 03:34:51 +00:00
|
|
|
//
|
|
|
|
return qtrue;
|
|
|
|
} //end of the function Net_Setup
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void Net_Shutdown( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
WINS_Shutdown();
|
|
|
|
} //end of the function Net_Shutdown
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void NMSG_Clear( netmessage_t *msg ){
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->size = 4;
|
|
|
|
} //end of the function NMSG_Clear
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void NMSG_WriteChar( netmessage_t *msg, int c ){
|
|
|
|
if ( c < -128 || c > 127 ) {
|
|
|
|
WinPrint( "NMSG_WriteChar: range error\n" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( msg->size >= MAX_NETMESSAGE ) {
|
|
|
|
WinPrint( "NMSG_WriteChar: overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
} //end if
|
|
|
|
msg->data[msg->size] = c;
|
|
|
|
msg->size++;
|
|
|
|
} //end of the function NMSG_WriteChar
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void NMSG_WriteByte( netmessage_t *msg, int c ){
|
|
|
|
if ( c < -128 || c > 127 ) {
|
|
|
|
WinPrint( "NMSG_WriteByte: range error\n" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( msg->size + 1 >= MAX_NETMESSAGE ) {
|
|
|
|
WinPrint( "NMSG_WriteByte: overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
} //end if
|
|
|
|
msg->data[msg->size] = c;
|
|
|
|
msg->size++;
|
|
|
|
} //end of the function NMSG_WriteByte
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void NMSG_WriteShort( netmessage_t *msg, int c ){
|
|
|
|
if ( c < ( (short)0x8000 ) || c > (short)0x7fff ) {
|
|
|
|
WinPrint( "NMSG_WriteShort: range error" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( msg->size + 2 >= MAX_NETMESSAGE ) {
|
|
|
|
WinPrint( "NMSG_WriteShort: overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
msg->data[msg->size] = c & 0xff;
|
|
|
|
msg->data[msg->size + 1] = c >> 8;
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->size += 2;
|
|
|
|
} //end of the function NMSG_WriteShort
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void NMSG_WriteLong( netmessage_t *msg, int c ){
|
|
|
|
if ( msg->size + 4 >= MAX_NETMESSAGE ) {
|
|
|
|
WinPrint( "NMSG_WriteLong: overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
msg->data[msg->size] = c & 0xff;
|
|
|
|
msg->data[msg->size + 1] = ( c >> 8 ) & 0xff;
|
|
|
|
msg->data[msg->size + 2] = ( c >> 16 ) & 0xff;
|
|
|
|
msg->data[msg->size + 3] = c >> 24;
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->size += 4;
|
|
|
|
} //end of the function NMSG_WriteLong
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void NMSG_WriteFloat( netmessage_t *msg, float c ){
|
|
|
|
if ( msg->size + 4 >= MAX_NETMESSAGE ) {
|
|
|
|
WinPrint( "NMSG_WriteLong: overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
msg->data[msg->size] = *( (int *)&c ) & 0xff;
|
|
|
|
msg->data[msg->size + 1] = ( *( (int *)&c ) >> 8 ) & 0xff;
|
|
|
|
msg->data[msg->size + 2] = ( *( (int *)&c ) >> 16 ) & 0xff;
|
|
|
|
msg->data[msg->size + 3] = *( (int *)&c ) >> 24;
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->size += 4;
|
|
|
|
} //end of the function NMSG_WriteFloat
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void NMSG_WriteString( netmessage_t *msg, char *string ){
|
|
|
|
if ( msg->size + strlen( string ) + 1 >= MAX_NETMESSAGE ) {
|
|
|
|
WinPrint( "NMSG_WriteString: overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( &msg->data[msg->size], string, strlen( string ) + 1 );
|
|
|
|
msg->size += strlen( string ) + 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function NMSG_WriteString
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void NMSG_ReadStart( netmessage_t *msg ){
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->readoverflow = qfalse;
|
|
|
|
msg->read = 4;
|
|
|
|
} //end of the function NMSG_ReadStart
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int NMSG_ReadChar( netmessage_t *msg ){
|
|
|
|
if ( msg->read + 1 > msg->size ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->readoverflow = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
WinPrint( "NMSG_ReadChar: read overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
|
|
|
} //end if
|
|
|
|
msg->read++;
|
2012-03-17 20:01:54 +00:00
|
|
|
return msg->data[msg->read - 1];
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function NMSG_ReadChar
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int NMSG_ReadByte( netmessage_t *msg ){
|
|
|
|
if ( msg->read + 1 > msg->size ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->readoverflow = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
WinPrint( "NMSG_ReadByte: read overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
|
|
|
} //end if
|
|
|
|
msg->read++;
|
2012-03-17 20:01:54 +00:00
|
|
|
return msg->data[msg->read - 1];
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function NMSG_ReadByte
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int NMSG_ReadShort( netmessage_t *msg ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int c;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( msg->read + 2 > msg->size ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->readoverflow = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
WinPrint( "NMSG_ReadShort: read overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
c = (short)( msg->data[msg->read] + ( msg->data[msg->read + 1] << 8 ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->read += 2;
|
|
|
|
return c;
|
|
|
|
} //end of the function NMSG_ReadShort
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int NMSG_ReadLong( netmessage_t *msg ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int c;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( msg->read + 4 > msg->size ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->readoverflow = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
WinPrint( "NMSG_ReadLong: read overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
|
|
|
} //end if
|
|
|
|
c = msg->data[msg->read]
|
2012-03-17 20:01:54 +00:00
|
|
|
+ ( msg->data[msg->read + 1] << 8 )
|
|
|
|
+ ( msg->data[msg->read + 2] << 16 )
|
|
|
|
+ ( msg->data[msg->read + 3] << 24 );
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->read += 4;
|
|
|
|
return c;
|
|
|
|
} //end of the function NMSG_ReadLong
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
float NMSG_ReadFloat( netmessage_t *msg ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int c;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( msg->read + 4 > msg->size ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->readoverflow = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
WinPrint( "NMSG_ReadLong: read overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
|
|
|
} //end if
|
|
|
|
c = msg->data[msg->read]
|
2012-03-17 20:01:54 +00:00
|
|
|
+ ( msg->data[msg->read + 1] << 8 )
|
|
|
|
+ ( msg->data[msg->read + 2] << 16 )
|
|
|
|
+ ( msg->data[msg->read + 3] << 24 );
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->read += 4;
|
|
|
|
return *(float *)&c;
|
|
|
|
} //end of the function NMSG_ReadFloat
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
char *NMSG_ReadString( netmessage_t *msg ){
|
|
|
|
static char string[2048];
|
2007-11-04 03:34:51 +00:00
|
|
|
int l, c;
|
2008-07-25 19:14:48 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
l = 0;
|
|
|
|
do
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( msg->read + 1 > msg->size ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
msg->readoverflow = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
WinPrint( "NMSG_ReadString: read overflow\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
string[l] = 0;
|
|
|
|
return string;
|
|
|
|
} //end if
|
|
|
|
c = msg->data[msg->read];
|
|
|
|
msg->read++;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c == 0 ) {
|
|
|
|
break;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
string[l] = c;
|
|
|
|
l++;
|
2012-03-17 20:01:54 +00:00
|
|
|
} while ( l < sizeof( string ) - 1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
string[l] = 0;
|
|
|
|
return string;
|
|
|
|
} //end of the function NMSG_ReadString
|