spingle/source/net_defs.h

260 lines
7.9 KiB
C

/*
* net_defs.h -- functions and data private to the network layer
* net_sys.h and its dependencies must be included before net_defs.h.
*
* Copyright (C) 1996-1997 Id Software, Inc.
* Copyright (C) 2005-2012 O.Sezer <sezero@users.sourceforge.net>
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef spingle__net_defs_h
#define spingle__net_defs_h
struct qsockaddr
{
#if defined(HAVE_SA_LEN)
uint8_t qsa_len;
uint8_t qsa_family;
#else
int16_t qsa_family;
#endif /* BSD, sockaddr */
uint8_t qsa_data[14];
};
#define NET_HEADERSIZE (2 * sizeof(uint32_t))
#define NET_DATAGRAMSIZE (MAX_DATAGRAM + NET_HEADERSIZE)
// NetHeader flags
#define NETFLAG_LENGTH_MASK 0x0000ffff
#define NETFLAG_DATA 0x00010000
#define NETFLAG_ACK 0x00020000
#define NETFLAG_NAK 0x00040000
#define NETFLAG_EOM 0x00080000
#define NETFLAG_UNRELIABLE 0x00100000
#define NETFLAG_CTL 0x80000000
#if (NETFLAG_LENGTH_MASK & NET_MAXMESSAGE) != NET_MAXMESSAGE
#error "NET_MAXMESSAGE must fit within NETFLAG_LENGTH_MASK"
#endif
#define NET_PROTOCOL_VERSION 3
/**
This is the network info/connection protocol. It is used to find Quake
servers, get info about them, and connect to them. Once connected, the
Quake game protocol (documented elsewhere) is used.
General notes:
game_name is currently always "QUAKE", but is there so this same protocol
can be used for future games as well; can you say Quake2?
CCREQ_CONNECT
string game_name "QUAKE"
byte net_protocol_version NET_PROTOCOL_VERSION
CCREQ_SERVER_INFO
string game_name "QUAKE"
byte net_protocol_version NET_PROTOCOL_VERSION
CCREQ_PLAYER_INFO
byte player_number
CCREQ_RULE_INFO
string rule
CCREP_ACCEPT
long port
CCREP_REJECT
string reason
CCREP_SERVER_INFO
string server_address
string host_name
string level_name
byte current_players
byte max_players
byte protocol_version NET_PROTOCOL_VERSION
CCREP_PLAYER_INFO
byte player_number
string name
long colors
long frags
long connect_time
string address
CCREP_RULE_INFO
string rule
string value
note:
There are two address forms used above. The short form is just a
port number. The address that goes along with the port is defined as
"whatever address you receive this reponse from". This lets us use
the host OS to solve the problem of multiple host addresses (possibly
with no routing between them); the host will use the right address
when we reply to the inbound connection request. The long from is
a full address and port in a string. It is used for returning the
address of a server that is not running locally.
**/
#define CCREQ_CONNECT 0x01
#define CCREQ_SERVER_INFO 0x02
#define CCREQ_PLAYER_INFO 0x03
#define CCREQ_RULE_INFO 0x04
#define CCREP_ACCEPT 0x81
#define CCREP_REJECT 0x82
#define CCREP_SERVER_INFO 0x83
#define CCREP_PLAYER_INFO 0x84
#define CCREP_RULE_INFO 0x85
typedef struct qsocket_s
{
struct qsocket_s *next;
double connecttime;
double lastMessageTime;
double lastSendTime;
bool disconnected;
bool canSend;
bool sendNext;
int32_t driver;
int32_t landriver;
sys_socket_t socket;
void *driverdata;
uint32_t ackSequence;
uint32_t sendSequence;
uint32_t unreliableSendSequence;
int32_t sendMessageLength;
byte sendMessage [NET_MAXMESSAGE];
uint32_t receiveSequence;
uint32_t unreliableReceiveSequence;
int32_t receiveMessageLength;
byte receiveMessage [NET_MAXMESSAGE];
struct qsockaddr addr;
char address[NET_NAMELEN];
} qsocket_t;
extern qsocket_t *net_activeSockets;
extern qsocket_t *net_freeSockets;
extern int32_t net_numsockets;
typedef struct
{
const char *name;
bool initialized;
sys_socket_t controlSock;
sys_socket_t (*Init)(void);
void (*Shutdown)(void);
void (*Listen)(bool state);
sys_socket_t (*Open_Socket)(int32_t port);
int32_t (*Close_Socket)(sys_socket_t socketid);
int32_t (*Connect)(sys_socket_t socketid, struct qsockaddr *addr);
sys_socket_t (*CheckNewConnections)(void);
int32_t (*Read)(sys_socket_t socketid, byte *buf, int32_t len, struct qsockaddr *addr);
int32_t (*Write)(sys_socket_t socketid, byte *buf, int32_t len, struct qsockaddr *addr);
int32_t (*Broadcast)(sys_socket_t socketid, byte *buf, int32_t len);
const char * (*AddrToString)(struct qsockaddr *addr);
int32_t (*StringToAddr)(const char *string, struct qsockaddr *addr);
int32_t (*GetSocketAddr)(sys_socket_t socketid, struct qsockaddr *addr);
int32_t (*GetNameFromAddr)(struct qsockaddr *addr, char *name);
int32_t (*GetAddrFromName)(const char *name, struct qsockaddr *addr);
int32_t (*AddrCompare)(struct qsockaddr *addr1, struct qsockaddr *addr2);
int32_t (*GetSocketPort)(struct qsockaddr *addr);
int32_t (*SetSocketPort)(struct qsockaddr *addr, int32_t port);
} net_landriver_t;
#define MAX_NET_DRIVERS 8
extern net_landriver_t net_landrivers[];
extern const int32_t net_numlandrivers;
typedef struct
{
const char *name;
bool initialized;
int32_t (*Init)(void);
void (*Listen)(bool state);
void (*SearchForHosts)(bool xmit);
qsocket_t *(*Connect)(const char *host);
qsocket_t *(*CheckNewConnections)(void);
int32_t (*QGetMessage)(qsocket_t *sock);
int32_t (*QSendMessage)(qsocket_t *sock, sizebuf_t *data);
int32_t (*SendUnreliableMessage)(qsocket_t *sock, sizebuf_t *data);
bool (*CanSendMessage)(qsocket_t *sock);
bool (*CanSendUnreliableMessage)(qsocket_t *sock);
void (*Close)(qsocket_t *sock);
void (*Shutdown)(void);
} net_driver_t;
extern net_driver_t net_drivers[];
extern const int32_t net_numdrivers;
/* Loop driver must always be registered the first */
#define IS_LOOP_DRIVER(p) ((p) == 0)
extern int32_t net_driverlevel;
extern int32_t messagesSent;
extern int32_t messagesReceived;
extern int32_t unreliableMessagesSent;
extern int32_t unreliableMessagesReceived;
qsocket_t *NET_NewQSocket(void);
void NET_FreeQSocket(qsocket_t *);
double SetNetTime(void);
#define HOSTCACHESIZE 8
typedef struct
{
char name[16];
char map[16];
char cname[32];
int32_t users;
int32_t maxusers;
int32_t driver;
int32_t ldriver;
struct qsockaddr addr;
} hostcache_t;
extern int32_t hostCacheCount;
extern hostcache_t hostcache[HOSTCACHESIZE];
typedef struct PollProcedure_s
{
struct PollProcedure_s *next;
double nextTime;
void (*procedure)(void *arg);
void *arg;
} PollProcedure;
void SchedulePollProcedure(PollProcedure *pp, double timeOffset);
#endif