d_clisrv.h 12.3 KB
Newer Older
Alam Ed Arias committed
1 2 3
// SONIC ROBO BLAST 2
//-----------------------------------------------------------------------------
// Copyright (C) 1998-2000 by DooM Legacy Team.
James R. committed
4
// Copyright (C) 1999-2020 by Sonic Team Junior.
Alam Ed Arias committed
5 6 7 8 9 10 11 12 13 14 15 16
//
// This program is free software distributed under the
// terms of the GNU General Public License, version 2.
// See the 'LICENSE' file for more details.
//-----------------------------------------------------------------------------
/// \file  d_clisrv.h
/// \brief high level networking stuff

#ifndef __D_CLISRV__
#define __D_CLISRV__

#include "d_ticcmd.h"
17
#include "d_net.h"
Alam Ed Arias committed
18
#include "d_netcmd.h"
19
#include "d_net.h"
Alam Ed Arias committed
20 21
#include "tables.h"
#include "d_player.h"
22
#include "mserv.h"
Alam Ed Arias committed
23

James R. committed
24
/*
25 26 27
The 'packet version' is used to distinguish packet formats.
This version is independent of VERSION and SUBVERSION. Different
applications may follow different packet versions.
James R. committed
28
*/
LJ Sonic committed
29
#define PACKETVERSION 3
James R. committed
30

Alam Ed Arias committed
31 32 33 34 35 36 37
// Network play related stuff.
// There is a data struct that stores network
//  communication related stuff, and another
//  one that defines the actual packets to
//  be transmitted.

// Networking and tick handling related.
38
#define BACKUPTICS 1024
39
#define CLIENTBACKUPTICS 32
Alam Ed Arias committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#define MAXTEXTCMD 256
//
// Packet structure
//
typedef enum
{
	PT_NOTHING,       // To send a nop through the network. ^_~
	PT_SERVERCFG,     // Server config used in start game
	                  // (must stay 1 for backwards compatibility).
	                  // This is a positive response to a CLIENTJOIN request.
	PT_CLIENTCMD,     // Ticcmd of the client.
	PT_CLIENTMIS,     // Same as above with but saying resend from.
	PT_CLIENT2CMD,    // 2 cmds in the packet for splitscreen.
	PT_CLIENT2MIS,    // Same as above with but saying resend from
	PT_NODEKEEPALIVE, // Same but without ticcmd and consistancy
	PT_NODEKEEPALIVEMIS,
	PT_SERVERTICS,    // All cmds for the tic.
	PT_SERVERREFUSE,  // Server refuses joiner (reason inside).
	PT_SERVERSHUTDOWN,
	PT_CLIENTQUIT,    // Client closes the connection.

	PT_ASKINFO,       // Anyone can ask info of the server.
	PT_SERVERINFO,    // Send game & server info (gamespy).
	PT_PLAYERINFO,    // Send information for players in game (gamespy).
	PT_REQUESTFILE,   // Client requests a file transfer
	PT_ASKINFOVIAMS,  // Packet from the MS requesting info be sent to new client.
	                  // If this ID changes, update masterserver definition.

LJ Sonic committed
68
	PT_WILLRESENDGAMESTATE, // Hey Client, I am about to resend you the gamestate!
LJ Sonic committed
69 70
	PT_CANRECEIVEGAMESTATE, // Okay Server, I'm ready to receive it, you can go ahead.
	PT_RECEIVEDGAMESTATE,   // Thank you Server, I am ready to play again!
LJ Sonic committed
71

LJ Sonic committed
72 73 74 75
	PT_SENDINGLUAFILE, // Server telling a client Lua needs to open a file
	PT_ASKLUAFILE,     // Client telling the server they don't have the file
	PT_HASLUAFILE,     // Client telling the server they have the file

Alam Ed Arias committed
76 77 78
	// Add non-PT_CANFAIL packet types here to avoid breaking MS compatibility.

	PT_CANFAIL,       // This is kind of a priority. Anything bigger than CANFAIL
79
	                  // allows HSendPacket(*, true, *, *) to return false.
Alam Ed Arias committed
80 81 82
	                  // In addition, this packet can't occupy all the available slots.

	PT_FILEFRAGMENT = PT_CANFAIL, // A part of a file.
LJ Sonic committed
83 84
	PT_FILEACK,
	PT_FILERECEIVED,
Alam Ed Arias committed
85 86 87 88 89

	PT_TEXTCMD,       // Extra text commands from the client.
	PT_TEXTCMD2,      // Splitscreen text commands.
	PT_CLIENTJOIN,    // Client wants to join; used in start game.
	PT_NODETIMEOUT,   // Packet sent to self if the connection times out.
90 91 92

	PT_LOGIN,         // Login attempt from the client.

Alam Ed Arias committed
93 94 95 96
	PT_PING,          // Packet sent to tell clients the other client's latency to server.
	NUMPACKETTYPE
} packettype_t;

97 98 99 100
#ifdef PACKETDROP
void Command_Drop(void);
void Command_Droprate(void);
#endif
LJ Sonic committed
101
#ifdef _DEBUG
102 103
void Command_Numnodes(void);
#endif
104

Alam Ed Arias committed
105 106 107 108
#if defined(_MSC_VER)
#pragma pack(1)
#endif

109
// Client to server packet
Alam Ed Arias committed
110 111 112 113 114 115 116 117
typedef struct
{
	UINT8 client_tic;
	UINT8 resendfrom;
	INT16 consistancy;
	ticcmd_t cmd;
} ATTRPACK clientcmd_pak;

118
// Splitscreen packet
Alam Ed Arias committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
// WARNING: must have the same format of clientcmd_pak, for more easy use
typedef struct
{
	UINT8 client_tic;
	UINT8 resendfrom;
	INT16 consistancy;
	ticcmd_t cmd, cmd2;
} ATTRPACK client2cmd_pak;

#ifdef _MSC_VER
#pragma warning(disable :  4200)
#endif

// Server to client packet
// this packet is too large
typedef struct
{
136
	tic_t starttic;
Alam Ed Arias committed
137 138
	UINT8 numtics;
	UINT8 numslots; // "Slots filled": Highest player number in use plus one.
139
	ticcmd_t cmds[45]; // Normally [BACKUPTIC][MAXPLAYERS] but too large
Alam Ed Arias committed
140 141
} ATTRPACK servertics_pak;

Alam Ed Arias committed
142 143
typedef struct
{
144 145
	UINT8 version; // Different versions don't work
	UINT8 subversion; // Contains build version
Alam Ed Arias committed
146

147
	// Server launch stuffs
Alam Ed Arias committed
148 149 150 151 152 153 154 155 156 157
	UINT8 serverplayer;
	UINT8 totalslotnum; // "Slots": highest player number in use plus one.

	tic_t gametic;
	UINT8 clientnode;
	UINT8 gamestate;

	UINT8 gametype;
	UINT8 modifiedgame;

158
	char server_context[8]; // Unique context id, generated at server startup.
Alam Ed Arias committed
159 160
} ATTRPACK serverconfig_pak;

LJ Sonic committed
161 162
typedef struct
{
Alam Ed Arias committed
163
	UINT8 fileid;
LJ Sonic committed
164
	UINT32 filesize;
165
	UINT8 iteration;
Alam Ed Arias committed
166 167
	UINT32 position;
	UINT16 size;
168
	UINT8 data[0]; // Size is variable using hardware_MAXPACKETLENGTH
Alam Ed Arias committed
169 170
} ATTRPACK filetx_pak;

LJ Sonic committed
171 172 173 174 175 176 177 178 179
typedef struct
{
	UINT32 start;
	UINT32 acks;
} ATTRPACK fileacksegment_t;

typedef struct
{
	UINT8 fileid;
180
	UINT8 iteration;
LJ Sonic committed
181 182 183 184
	UINT8 numsegments;
	fileacksegment_t segments[0];
} ATTRPACK fileack_pak;

Alam Ed Arias committed
185 186 187 188
#ifdef _MSC_VER
#pragma warning(default : 4200)
#endif

James R. committed
189 190
#define MAXAPPLICATION 16

Alam Ed Arias committed
191 192
typedef struct
{
James R. committed
193 194 195
	UINT8 _255;/* see serverinfo_pak */
	UINT8 packetversion;
	char application[MAXAPPLICATION];
196 197
	UINT8 version; // Different versions don't work
	UINT8 subversion; // Contains build version
Alam Ed Arias committed
198 199
	UINT8 localplayers;
	UINT8 mode;
200
	char names[MAXSPLITSCREENPLAYERS][MAXPLAYERNAME];
Alam Ed Arias committed
201 202 203
} ATTRPACK clientconfig_pak;

#define MAXSERVERNAME 32
204
#define MAXFILENEEDED 915
205
// This packet is too large
Alam Ed Arias committed
206 207
typedef struct
{
James R. committed
208 209 210 211 212 213 214 215
	/*
	In the old packet, 'version' is the first field. Now that field is set
	to 255 always, so older versions won't be confused with the new
	versions or vice-versa.
	*/
	UINT8 _255;
	UINT8 packetversion;
	char  application[MAXAPPLICATION];
Alam Ed Arias committed
216 217 218 219
	UINT8 version;
	UINT8 subversion;
	UINT8 numberofplayer;
	UINT8 maxplayer;
LJ Sonic committed
220
	UINT8 refusereason; // 0: joinable, 1: joins disabled, 2: full
221
	char gametypename[24];
Alam Ed Arias committed
222 223 224 225 226 227 228 229 230 231 232 233
	UINT8 modifiedgame;
	UINT8 cheatsenabled;
	UINT8 isdedicated;
	UINT8 fileneedednum;
	tic_t time;
	tic_t leveltime;
	char servername[MAXSERVERNAME];
	char mapname[8];
	char maptitle[33];
	unsigned char mapmd5[16];
	UINT8 actnum;
	UINT8 iszone;
234
	UINT8 fileneeded[MAXFILENEEDED]; // is filled with writexxx (byteptr.h)
Alam Ed Arias committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
} ATTRPACK serverinfo_pak;

typedef struct
{
	char reason[255];
} ATTRPACK serverrefuse_pak;

typedef struct
{
	UINT8 version;
	tic_t time; // used for ping evaluation
} ATTRPACK askinfo_pak;

typedef struct
{
	char clientaddr[22];
	tic_t time; // used for ping evaluation
} ATTRPACK msaskinfo_pak;
Alam Ed Arias committed
253 254 255 256

// Shorter player information for external use.
typedef struct
{
257
	UINT8 num;
Alam Ed Arias committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271
	char name[MAXPLAYERNAME+1];
	UINT8 address[4]; // sending another string would run us up against MAXPACKETLENGTH
	UINT8 team;
	UINT8 skin;
	UINT8 data; // Color is first four bits, hasflag, isit and issuper have one bit each, the last is unused.
	UINT32 score;
	UINT16 timeinserver; // In seconds.
} ATTRPACK plrinfo;

// Shortest player information for join during intermission.
typedef struct
{
	char name[MAXPLAYERNAME+1];
	UINT8 skin;
272
	UINT16 color;
Alam Ed Arias committed
273 274 275 276 277 278
	UINT32 pflags;
	UINT32 score;
	UINT8 ctfteam;
} ATTRPACK plrconfig;

//
279
// Network packet data
Alam Ed Arias committed
280 281 282 283
//
typedef struct
{
	UINT32 checksum;
284 285
	UINT8 ack; // If not zero the node asks for acknowledgement, the receiver must resend the ack
	UINT8 ackreturn; // The return of the ack number
Alam Ed Arias committed
286 287

	UINT8 packettype;
288
	UINT8 reserved; // Padding
Alam Ed Arias committed
289 290
	union
	{
291 292 293 294 295 296
		clientcmd_pak clientpak;            //         144 bytes
		client2cmd_pak client2pak;          //         200 bytes
		servertics_pak serverpak;           //      132495 bytes (more around 360, no?)
		serverconfig_pak servercfg;         //         773 bytes
		UINT8 textcmd[MAXTEXTCMD+1];        //       66049 bytes (wut??? 64k??? More like 257 bytes...)
		filetx_pak filetxpak;               //         139 bytes
LJ Sonic committed
297 298
		fileack_pak fileack;
		UINT8 filereceived;
299
		clientconfig_pak clientcfg;         //         136 bytes
James R. committed
300
		UINT8 md5sum[16];
301 302 303 304
		serverinfo_pak serverinfo;          //        1024 bytes
		serverrefuse_pak serverrefuse;      //       65025 bytes (somehow I feel like those values are garbage...)
		askinfo_pak askinfo;                //          61 bytes
		msaskinfo_pak msaskinfo;            //          22 bytes
SteelT committed
305 306 307
		plrinfo playerinfo[MAXPLAYERS];     //         576 bytes(?)
		plrconfig playerconfig[MAXPLAYERS]; // (up to) 528 bytes(?)
		UINT32 pingtable[MAXPLAYERS+1];     //          68 bytes
308
	} u; // This is needed to pack diff packet types data together
Alam Ed Arias committed
309 310 311 312 313 314
} ATTRPACK doomdata_t;

#if defined(_MSC_VER)
#pragma pack()
#endif

315
#define MAXSERVERLIST (MAXNETNODES-1)
Alam Ed Arias committed
316 317 318 319 320 321 322 323 324 325
typedef struct
{
	SINT8 node;
	serverinfo_pak info;
} serverelem_t;

extern serverelem_t serverlist[MAXSERVERLIST];
extern UINT32 serverlistcount;
extern INT32 mapchangepending;

326
// Points inside doomcom
Alam Ed Arias committed
327 328
extern doomdata_t *netbuffer;

329
extern consvar_t cv_showjoinaddress;
Alam Ed Arias committed
330 331
extern consvar_t cv_playbackspeed;

332 333 334
#define BASEPACKETSIZE      offsetof(doomdata_t, u)
#define FILETXHEADER        offsetof(filetx_pak, data)
#define BASESERVERTICSSIZE  offsetof(doomdata_t, u.serverpak.cmds[0])
Alam Ed Arias committed
335 336 337 338 339 340 341 342 343

#define KICK_MSG_GO_AWAY     1
#define KICK_MSG_CON_FAIL    2
#define KICK_MSG_PLAYER_QUIT 3
#define KICK_MSG_TIMEOUT     4
#define KICK_MSG_BANNED      5
#define KICK_MSG_PING_HIGH   6
#define KICK_MSG_CUSTOM_KICK 7
#define KICK_MSG_CUSTOM_BAN  8
344
#define KICK_MSG_KEEP_BODY   0x80
Alam Ed Arias committed
345

Prisima the Fox committed
346 347 348 349 350
typedef enum
{
	KR_KICK          = 1, //Kicked by server
	KR_PINGLIMIT     = 2, //Broke Ping Limit
	KR_SYNCH         = 3, //Synch Failure
Alam Ed Arias committed
351
	KR_TIMEOUT       = 4, //Connection Timeout
Prisima the Fox committed
352 353
	KR_BAN           = 5, //Banned by server
	KR_LEAVE         = 6, //Quit the game
Alam Ed Arias committed
354

Prisima the Fox committed
355 356
} kickreason_t;

357 358 359 360
/* the max number of name changes in some time period */
#define MAXNAMECHANGES (5)
#define NAMECHANGERATE (60*TICRATE)

Alam Ed Arias committed
361
extern boolean server;
362
extern boolean serverrunning;
363
#define client (!server)
364
extern boolean dedicated; // For dedicated server
Alam Ed Arias committed
365 366 367 368 369 370
extern UINT16 software_MAXPACKETLENGTH;
extern boolean acceptnewnode;
extern SINT8 servernode;

void Command_Ping_f(void);
extern tic_t connectiontimeout;
371
extern tic_t jointimeout;
Alam Ed Arias committed
372 373 374
extern UINT16 pingmeasurecount;
extern UINT32 realpingtable[MAXPLAYERS];
extern UINT32 playerpingtable[MAXPLAYERS];
SteelT committed
375
extern tic_t servermaxping;
Alam Ed Arias committed
376

SteelT committed
377
extern consvar_t cv_netticbuffer, cv_allownewplayer, cv_joinnextround, cv_maxplayers, cv_joindelay, cv_rejointimeout;
378 379
extern consvar_t cv_resynchattempts, cv_blamecfail;
extern consvar_t cv_maxsend, cv_noticedownload, cv_downloadspeed;
Alam Ed Arias committed
380

381
// Used in d_net, the only dependence
382
tic_t ExpandTics(INT32 low, INT32 node);
Alam Ed Arias committed
383 384
void D_ClientServerInit(void);

385
// Initialise the other field
Alam Ed Arias committed
386 387 388
void RegisterNetXCmd(netxcmd_t id, void (*cmd_f)(UINT8 **p, INT32 playernum));
void SendNetXCmd(netxcmd_t id, const void *param, size_t nparam);
void SendNetXCmd2(netxcmd_t id, const void *param, size_t nparam); // splitsreen player
389
void SendKick(UINT8 playernum, UINT8 msg);
Alam Ed Arias committed
390 391 392 393 394 395 396 397 398 399 400 401

// Create any new ticcmds and broadcast to other players.
void NetUpdate(void);

void SV_StartSinglePlayerServer(void);
boolean SV_SpawnServer(void);
void SV_StopServer(void);
void SV_ResetServer(void);
void CL_AddSplitscreenPlayer(void);
void CL_RemoveSplitscreenPlayer(void);
void CL_Reset(void);
void CL_ClearPlayer(INT32 playernum);
402
void CL_QueryServerList(msg_server_t *list);
Alam Ed Arias committed
403
void CL_UpdateServerList(boolean internetsearch, INT32 room);
404
// Is there a game running
Alam Ed Arias committed
405 406 407 408 409 410
boolean Playing(void);

// Broadcasts special packets to other players
//  to notify of game exit
void D_QuitNetGame(void);

411
//? How many ticks to run?
Alam Ed Arias committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
void TryRunTics(tic_t realtic);

// extra data for lmps
// these functions scare me. they contain magic.
/*boolean AddLmpExtradata(UINT8 **demo_p, INT32 playernum);
void ReadLmpExtraData(UINT8 **demo_pointer, INT32 playernum);*/

#ifndef NONET
// translate a playername in a player number return -1 if not found and
// print a error message in the console
SINT8 nametonum(const char *name);
#endif

extern char motd[254], server_context[8];
extern UINT8 playernode[MAXPLAYERS];

INT32 D_NumPlayers(void);
void D_ResetTiccmds(void);

tic_t GetLag(INT32 node);
UINT8 GetFreeXCmdSize(void);

434 435
void D_MD5PasswordPass(const UINT8 *buffer, size_t len, const char *salt, void *dest);

LJ Sonic committed
436
extern UINT8 hu_redownloadinggamestate;
437

James R. committed
438
extern UINT8 adminpassmd5[16];
439
extern boolean adminpasswordset;
Alam Ed Arias committed
440
#endif