📄 sv_client.c
字号:
// snaps command
val = Info_ValueForKey (cl->userinfo, "snaps");
if (strlen(val)) {
i = atoi(val);
if ( i < 1 ) {
i = 1;
} else if ( i > 30 ) {
i = 30;
}
cl->snapshotMsec = 1000/i;
} else {
cl->snapshotMsec = 50;
}
// TTimo
// maintain the IP information
// this is set in SV_DirectConnect (directly on the server, not transmitted), may be lost when client updates it's userinfo
// the banning code relies on this being consistently present
val = Info_ValueForKey (cl->userinfo, "ip");
if (!val[0])
{
//Com_DPrintf("Maintain IP in userinfo for '%s'\n", cl->name);
if ( !NET_IsLocalAddress(cl->netchan.remoteAddress) )
Info_SetValueForKey( cl->userinfo, "ip", NET_AdrToString( cl->netchan.remoteAddress ) );
else
// force the "ip" info key to "localhost" for local clients
Info_SetValueForKey( cl->userinfo, "ip", "localhost" );
}
}
/*
==================
SV_UpdateUserinfo_f
==================
*/
static void SV_UpdateUserinfo_f( client_t *cl ) {
Q_strncpyz( cl->userinfo, Cmd_Argv(1), sizeof(cl->userinfo) );
SV_UserinfoChanged( cl );
// call prog code to allow overrides
VM_Call( gvm, GAME_CLIENT_USERINFO_CHANGED, cl - svs.clients );
}
typedef struct {
char *name;
void (*func)( client_t *cl );
} ucmd_t;
static ucmd_t ucmds[] = {
{"userinfo", SV_UpdateUserinfo_f},
{"disconnect", SV_Disconnect_f},
{"cp", SV_VerifyPaks_f},
{"vdr", SV_ResetPureClient_f},
{"download", SV_BeginDownload_f},
{"nextdl", SV_NextDownload_f},
{"stopdl", SV_StopDownload_f},
{"donedl", SV_DoneDownload_f},
{NULL, NULL}
};
/*
==================
SV_ExecuteClientCommand
Also called by bot code
==================
*/
void SV_ExecuteClientCommand( client_t *cl, const char *s, qboolean clientOK ) {
ucmd_t *u;
qboolean bProcessed = qfalse;
Cmd_TokenizeString( s );
// see if it is a server level command
for (u=ucmds ; u->name ; u++) {
if (!strcmp (Cmd_Argv(0), u->name) ) {
u->func( cl );
bProcessed = qtrue;
break;
}
}
if (clientOK) {
// pass unknown strings to the game
if (!u->name && sv.state == SS_GAME) {
VM_Call( gvm, GAME_CLIENT_COMMAND, cl - svs.clients );
}
}
else if (!bProcessed)
Com_DPrintf( "client text ignored for %s: %s\n", cl->name, Cmd_Argv(0) );
}
/*
===============
SV_ClientCommand
===============
*/
static qboolean SV_ClientCommand( client_t *cl, msg_t *msg ) {
int seq;
const char *s;
qboolean clientOk = qtrue;
seq = MSG_ReadLong( msg );
s = MSG_ReadString( msg );
// see if we have already executed it
if ( cl->lastClientCommand >= seq ) {
return qtrue;
}
Com_DPrintf( "clientCommand: %s : %i : %s\n", cl->name, seq, s );
// drop the connection if we have somehow lost commands
if ( seq > cl->lastClientCommand + 1 ) {
Com_Printf( "Client %s lost %i clientCommands\n", cl->name,
seq - cl->lastClientCommand + 1 );
SV_DropClient( cl, "Lost reliable commands" );
return qfalse;
}
// malicious users may try using too many string commands
// to lag other players. If we decide that we want to stall
// the command, we will stop processing the rest of the packet,
// including the usercmd. This causes flooders to lag themselves
// but not other people
// We don't do this when the client hasn't been active yet since its
// normal to spam a lot of commands when downloading
if ( !com_cl_running->integer &&
cl->state >= CS_ACTIVE &&
sv_floodProtect->integer &&
svs.time < cl->nextReliableTime ) {
// ignore any other text messages from this client but let them keep playing
// TTimo - moved the ignored verbose to the actual processing in SV_ExecuteClientCommand, only printing if the core doesn't intercept
clientOk = qfalse;
}
// don't allow another command for one second
cl->nextReliableTime = svs.time + 1000;
SV_ExecuteClientCommand( cl, s, clientOk );
cl->lastClientCommand = seq;
Com_sprintf(cl->lastClientCommandString, sizeof(cl->lastClientCommandString), "%s", s);
return qtrue; // continue procesing
}
//==================================================================================
/*
==================
SV_ClientThink
Also called by bot code
==================
*/
void SV_ClientThink (client_t *cl, usercmd_t *cmd) {
cl->lastUsercmd = *cmd;
if ( cl->state != CS_ACTIVE ) {
return; // may have been kicked during the last usercmd
}
VM_Call( gvm, GAME_CLIENT_THINK, cl - svs.clients );
}
/*
==================
SV_UserMove
The message usually contains all the movement commands
that were in the last three packets, so that the information
in dropped packets can be recovered.
On very fast clients, there may be multiple usercmd packed into
each of the backup packets.
==================
*/
static void SV_UserMove( client_t *cl, msg_t *msg, qboolean delta ) {
int i, key;
int cmdCount;
usercmd_t nullcmd;
usercmd_t cmds[MAX_PACKET_USERCMDS];
usercmd_t *cmd, *oldcmd;
if ( delta ) {
cl->deltaMessage = cl->messageAcknowledge;
} else {
cl->deltaMessage = -1;
}
cmdCount = MSG_ReadByte( msg );
if ( cmdCount < 1 ) {
Com_Printf( "cmdCount < 1\n" );
return;
}
if ( cmdCount > MAX_PACKET_USERCMDS ) {
Com_Printf( "cmdCount > MAX_PACKET_USERCMDS\n" );
return;
}
// use the checksum feed in the key
key = sv.checksumFeed;
// also use the message acknowledge
key ^= cl->messageAcknowledge;
// also use the last acknowledged server command in the key
key ^= Com_HashKey(cl->reliableCommands[ cl->reliableAcknowledge & (MAX_RELIABLE_COMMANDS-1) ], 32);
Com_Memset( &nullcmd, 0, sizeof(nullcmd) );
oldcmd = &nullcmd;
for ( i = 0 ; i < cmdCount ; i++ ) {
cmd = &cmds[i];
MSG_ReadDeltaUsercmdKey( msg, key, oldcmd, cmd );
oldcmd = cmd;
}
// save time for ping calculation
cl->frames[ cl->messageAcknowledge & PACKET_MASK ].messageAcked = svs.time;
// TTimo
// catch the no-cp-yet situation before SV_ClientEnterWorld
// if CS_ACTIVE, then it's time to trigger a new gamestate emission
// if not, then we are getting remaining parasite usermove commands, which we should ignore
if (sv_pure->integer != 0 && cl->pureAuthentic == 0 && !cl->gotCP) {
if (cl->state == CS_ACTIVE)
{
// we didn't get a cp yet, don't assume anything and just send the gamestate all over again
Com_DPrintf( "%s: didn't get cp command, resending gamestate\n", cl->name, cl->state );
SV_SendClientGameState( cl );
}
return;
}
// if this is the first usercmd we have received
// this gamestate, put the client into the world
if ( cl->state == CS_PRIMED ) {
SV_ClientEnterWorld( cl, &cmds[0] );
// the moves can be processed normaly
}
// a bad cp command was sent, drop the client
if (sv_pure->integer != 0 && cl->pureAuthentic == 0) {
SV_DropClient( cl, "Cannot validate pure client!");
return;
}
if ( cl->state != CS_ACTIVE ) {
cl->deltaMessage = -1;
return;
}
// usually, the first couple commands will be duplicates
// of ones we have previously received, but the servertimes
// in the commands will cause them to be immediately discarded
for ( i = 0 ; i < cmdCount ; i++ ) {
// if this is a cmd from before a map_restart ignore it
if ( cmds[i].serverTime > cmds[cmdCount-1].serverTime ) {
continue;
}
// extremely lagged or cmd from before a map_restart
//if ( cmds[i].serverTime > svs.time + 3000 ) {
// continue;
//}
// don't execute if this is an old cmd which is already executed
// these old cmds are included when cl_packetdup > 0
if ( cmds[i].serverTime <= cl->lastUsercmd.serverTime ) {
continue;
}
SV_ClientThink (cl, &cmds[ i ]);
}
}
/*
===========================================================================
USER CMD EXECUTION
===========================================================================
*/
/*
===================
SV_ExecuteClientMessage
Parse a client packet
===================
*/
void SV_ExecuteClientMessage( client_t *cl, msg_t *msg ) {
int c;
int serverId;
MSG_Bitstream(msg);
serverId = MSG_ReadLong( msg );
cl->messageAcknowledge = MSG_ReadLong( msg );
if (cl->messageAcknowledge < 0) {
// usually only hackers create messages like this
// it is more annoying for them to let them hanging
#ifndef NDEBUG
SV_DropClient( cl, "DEBUG: illegible client message" );
#endif
return;
}
cl->reliableAcknowledge = MSG_ReadLong( msg );
// NOTE: when the client message is fux0red the acknowledgement numbers
// can be out of range, this could cause the server to send thousands of server
// commands which the server thinks are not yet acknowledged in SV_UpdateServerCommandsToClient
if (cl->reliableAcknowledge < cl->reliableSequence - MAX_RELIABLE_COMMANDS) {
// usually only hackers create messages like this
// it is more annoying for them to let them hanging
#ifndef NDEBUG
SV_DropClient( cl, "DEBUG: illegible client message" );
#endif
cl->reliableAcknowledge = cl->reliableSequence;
return;
}
// if this is a usercmd from a previous gamestate,
// ignore it or retransmit the current gamestate
//
// if the client was downloading, let it stay at whatever serverId and
// gamestate it was at. This allows it to keep downloading even when
// the gamestate changes. After the download is finished, we'll
// notice and send it a new game state
//
// https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=536
// don't drop as long as previous command was a nextdl, after a dl is done, downloadName is set back to ""
// but we still need to read the next message to move to next download or send gamestate
// I don't like this hack though, it must have been working fine at some point, suspecting the fix is somewhere else
if ( serverId != sv.serverId && !*cl->downloadName && !strstr(cl->lastClientCommandString, "nextdl") ) {
if ( serverId >= sv.restartedServerId && serverId < sv.serverId ) { // TTimo - use a comparison here to catch multiple map_restart
// they just haven't caught the map_restart yet
Com_DPrintf("%s : ignoring pre map_restart / outdated client message\n", cl->name);
return;
}
// if we can tell that the client has dropped the last
// gamestate we sent them, resend it
if ( cl->messageAcknowledge > cl->gamestateMessageNum ) {
Com_DPrintf( "%s : dropped gamestate, resending\n", cl->name );
SV_SendClientGameState( cl );
}
return;
}
// read optional clientCommand strings
do {
c = MSG_ReadByte( msg );
if ( c == clc_EOF ) {
break;
}
if ( c != clc_clientCommand ) {
break;
}
if ( !SV_ClientCommand( cl, msg ) ) {
return; // we couldn't execute it because of the flood protection
}
if (cl->state == CS_ZOMBIE) {
return; // disconnect command
}
} while ( 1 );
// read the usercmd_t
if ( c == clc_move ) {
SV_UserMove( cl, msg, qtrue );
} else if ( c == clc_moveNoDelta ) {
SV_UserMove( cl, msg, qfalse );
} else if ( c != clc_EOF ) {
Com_Printf( "WARNING: bad command byte for client %i\n", cl - svs.clients );
}
// if ( msg->readcount != msg->cursize ) {
// Com_Printf( "WARNING: Junk at end of packet for client %i\n", cl - svs.clients );
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -