/* ================== Host_ShutdownServer This only happens at the end of a game, not between levels ================== */ void Host_ShutdownServer(qboolean crash) { int i; int count; sizebuf_t buf; byte message[4]; double start; if (!sv.active) return; sv.active = false; // stop all client sounds immediately if (cls.state >= ca_connected) CL_Disconnect(); // flush any pending messages - like the score!!! start = Sys_DoubleTime(); do { count = 0; for (i = 0, host_client = svs.clients; i < svs.maxclients; i++, host_client++) { if (host_client->active && host_client->message.cursize) { if (NET_CanSendMessage(host_client->netconnection)) { NET_SendMessage(host_client->netconnection, &host_client->message); SZ_Clear(&host_client->message); } else { NET_GetMessage(host_client->netconnection); count++; } } } if ((Sys_DoubleTime() - start) > 3.0) break; } while (count); // make sure all the clients know we're disconnecting buf.data = message; buf.maxsize = 4; buf.cursize = 0; MSG_WriteByte(&buf, svc_disconnect); count = NET_SendToAll(&buf, 5); if (count) Con_Printf("%s: NET_SendToAll failed for %u clients\n", __func__, count); for (i = 0, host_client = svs.clients; i < svs.maxclients; i++, host_client++) if (host_client->active) SV_DropClient(crash); // // clear structures // memset(&sv, 0, sizeof(sv)); memset(svs.clients, 0, svs.maxclientslimit * sizeof(client_t)); }
/* =================== SV_ReadClientMessage Returns false if the client should be killed =================== */ qboolean SV_ReadClientMessage (void) { int ret; int cmd; char *s; do { nextmsg: ret = NET_GetMessage (host_client->netconnection); if (ret == -1) { Sys_Printf ("SV_ReadClientMessage: NET_GetMessage failed\n"); return false; } if (!ret) return true; MSG_BeginReading (); while (1) { if (!host_client->active) return false; // a command caused an error if (msg_badread) { Sys_Printf ("SV_ReadClientMessage: badread\n"); return false; } cmd = MSG_ReadChar (); switch (cmd) { case -1: goto nextmsg; // end of message default: Sys_Printf ("SV_ReadClientMessage: unknown command char\n"); return false; case clc_nop: // Sys_Printf ("clc_nop\n"); break; case clc_stringcmd: s = MSG_ReadString (); if (host_client->privileged) ret = 2; else ret = 0; if (Q_strncasecmp(s, "status", 6) == 0) ret = 1; else if (Q_strncasecmp(s, "god", 3) == 0) ret = 1; else if (Q_strncasecmp(s, "notarget", 8) == 0) ret = 1; else if (Q_strncasecmp(s, "fly", 3) == 0) ret = 1; else if (Q_strncasecmp(s, "name", 4) == 0) ret = 1; else if (Q_strncasecmp(s, "noclip", 6) == 0) ret = 1; else if (Q_strncasecmp(s, "say", 3) == 0) ret = 1; else if (Q_strncasecmp(s, "say_team", 8) == 0) ret = 1; else if (Q_strncasecmp(s, "tell", 4) == 0) ret = 1; else if (Q_strncasecmp(s, "color", 5) == 0) ret = 1; else if (Q_strncasecmp(s, "kill", 4) == 0) ret = 1; else if (Q_strncasecmp(s, "pause", 5) == 0) ret = 1; else if (Q_strncasecmp(s, "spawn", 5) == 0) ret = 1; else if (Q_strncasecmp(s, "begin", 5) == 0) ret = 1; else if (Q_strncasecmp(s, "prespawn", 8) == 0) ret = 1; else if (Q_strncasecmp(s, "kick", 4) == 0) ret = 1; else if (Q_strncasecmp(s, "ping", 4) == 0) ret = 1; else if (Q_strncasecmp(s, "give", 4) == 0) ret = 1; else if (Q_strncasecmp(s, "ban", 3) == 0) ret = 1; if (ret == 2) Cbuf_InsertText (s); else if (ret == 1) Cmd_ExecuteString (s, src_client); else Con_DPrintf("%s tried to %s\n", host_client->name, s); break; case clc_disconnect: // Sys_Printf ("SV_ReadClientMessage: client disconnected\n"); return false; case clc_move: SV_ReadClientMove (&host_client->cmd); break; } } } while (ret == 1); return true; }
int NET_SendToAll(sizebuf_t *data, int blocktime) { double start; int i; int count = 0; qboolean state1 [MAX_SCOREBOARD]; qboolean state2 [MAX_SCOREBOARD]; for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++) { if (!host_client->netconnection) continue; if (host_client->active) { if (host_client->netconnection->driver == 0) { NET_SendMessage(host_client->netconnection, data); state1[i] = true; state2[i] = true; continue; } count++; state1[i] = false; state2[i] = false; } else { state1[i] = true; state2[i] = true; } } start = Sys_FloatTime(); while (count) { count = 0; for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++) { if (! state1[i]) { if (NET_CanSendMessage (host_client->netconnection)) { state1[i] = true; NET_SendMessage(host_client->netconnection, data); } else { NET_GetMessage (host_client->netconnection); } count++; continue; } if (! state2[i]) { if (NET_CanSendMessage (host_client->netconnection)) { state2[i] = true; } else { NET_GetMessage (host_client->netconnection); } count++; continue; } } if ((Sys_FloatTime() - start) > blocktime) break; } return count; }
/* ==================== CL_GetMessage Handles recording and playback of demos, on top of NET_ code ==================== */ int CL_GetMessage (void) { int r, i; float f; if (cls.demoplayback) { // decide if it is time to grab the next message if (cls.signon == SIGNONS) // allways grab until fully connected { if (cls.timedemo) { if (host_framecount == cls.td_lastframe) return 0; // allready read this frame's message cls.td_lastframe = host_framecount; // if this is the second frame, grab the real td_starttime // so the bogus time on the first frame doesn't count if (host_framecount == cls.td_startframe + 1) cls.td_starttime = realtime; } else if ( /* cl.time > 0 && */ cl.time <= cl.mtime[0]) { return 0; // don't need another message yet } } // get the next message Sys_FileRead(cls.demofile, &net_message.cursize, 4); VectorCopy (cl.mviewangles[0], cl.mviewangles[1]); for (i=0 ; i<3 ; i++) { r = Sys_FileRead(cls.demofile, &f, 4) / 4; cl.mviewangles[0][i] = LittleFloat (f); } net_message.cursize = LittleLong (net_message.cursize); if (net_message.cursize > MAX_MSGLEN) Sys_Error ("Demo message (0x%08x) > MAX_MSGLEN (%d)", net_message.cursize, MAX_MSGLEN); r = Sys_FileRead(cls.demofile, net_message.data, net_message.cursize) / net_message.cursize; if (r != 1) { CL_StopPlayback (); return 0; } return 1; } while (1) { r = NET_GetMessage (cls.netcon); if (r != 1 && r != 2) return r; // discard nop keepalive message if (net_message.cursize == 1 && net_message.data[0] == svc_nop) Con_Printf ("<-- server to client keepalive\n"); else break; } if (cls.demorecording) CL_WriteDemoMessage (); return r; }
/* ==================== CL_GetMessage Handles recording and playback of demos, on top of NET_ code ==================== */ int CL_GetMessage (void) { int r, i; float f; if (cls.demoplayback) { // decide if it is time to grab the next message if (cls.signon == SIGNONS) // allways grab until fully connected { if (cls.timedemo) { if (host_framecount == cls.td_lastframe) return 0; // allready read this frame's message cls.td_lastframe = host_framecount; // if this is the second frame, grab the real td_starttime // so the bogus time on the first frame doesn't count if (host_framecount == cls.td_startframe + 1) cls.td_starttime = realtime; } else if ( /* cl.time > 0 && */ cl.time <= cl.mtime[0]) { return 0; // don't need another message yet } } // get the next message // if(intro_playing&&num_intro_msg>0&&num_intro_msg<21) // V_DarkFlash_f();//Fade into demo /* if(skip_start&&num_intro_msg>3) { while(num_intro_msg<1110) { fread (&net_message.cursize, 4, 1, cls.demofile); VectorCopy (cl.mviewangles[0], cl.mviewangles[1]); for (i=0 ; i<3 ; i++) { r = fread (&f, 4, 1, cls.demofile); cl.mviewangles[0][i] = LittleFloat (f); } net_message.cursize = LittleLong (net_message.cursize); num_intro_msg++; if (net_message.cursize > MAX_MSGLEN) Sys_Error ("Demo message > MAX_MSGLEN"); r = fread (net_message.data, net_message.cursize, 1, cls.demofile); if (r != 1) { CL_StopPlayback (); return 0; } if(num_intro_msg==174|| num_intro_msg==178|| num_intro_msg==428|| num_intro_msg==553|| num_intro_msg==1012) break; } if(num_intro_msg==1110) skip_start=false; } else {*/ fread (&net_message.cursize, 4, 1, cls.demofile); VectorCopy (cl.mviewangles[0], cl.mviewangles[1]); for (i=0 ; i<3 ; i++) { r = fread (&f, 4, 1, cls.demofile); cl.mviewangles[0][i] = LittleFloat (f); } net_message.cursize = LittleLong (net_message.cursize); num_intro_msg++; if (net_message.cursize > MAX_MSGLEN) Sys_Error ("Demo message > MAX_MSGLEN"); r = fread (net_message.data, net_message.cursize, 1, cls.demofile); if (r != 1) { CL_StopPlayback (); return 0; } // } // if (cls.demorecording) // CL_WriteDemoMessage (); return 1; } while (1) { r = NET_GetMessage (cls.netcon); if (r != 1 && r != 2) return r; // discard nop keepalive message if (net_message.cursize == 1 && net_message.data[0] == svc_nop) Con_Printf ("<-- server to client keepalive\n"); else break; } if (cls.demorecording) CL_WriteDemoMessage (); return r; }
int NET_SendToAll(sizebuf_t *data, double blocktime) { double start; int i; int count = 0; qboolean msg_init[MAX_SCOREBOARD]; /* data written */ qboolean msg_sent[MAX_SCOREBOARD]; /* send completed */ for (i = 0, host_client = svs.clients; i < svs.maxclients; i++, host_client++) { if (host_client->netconnection && host_client->active) { /* * Loopback driver guarantees delivery, skip checks */ if (IS_LOOP_DRIVER(host_client->netconnection->driver)) { NET_SendMessage(host_client->netconnection, data); msg_init[i] = true; msg_sent[i] = true; continue; } count++; msg_init[i] = false; msg_sent[i] = false; } else { msg_init[i] = true; msg_sent[i] = true; } } start = Sys_DoubleTime(); while (count) { count = 0; for (i = 0, host_client = svs.clients; i < svs.maxclients; i++, host_client++) { if (!msg_init[i]) { if (NET_CanSendMessage(host_client->netconnection)) { msg_init[i] = true; NET_SendMessage(host_client->netconnection, data); } else { NET_GetMessage(host_client->netconnection); } count++; continue; } if (!msg_sent[i]) { if (NET_CanSendMessage(host_client->netconnection)) { msg_sent[i] = true; } else { NET_GetMessage(host_client->netconnection); } count++; continue; } } if ((Sys_DoubleTime() - start) > blocktime) break; } return count; }
/* =================== SV_ReadClientMessage Returns false if the client should be killed =================== */ qboolean SV_ReadClientMessage (void) { int ret; int cmd; char *s; do { nextmsg: ret = NET_GetMessage (host_client->netconnection); if (ret == -1) { Sys_Printf ("SV_ReadClientMessage: NET_GetMessage failed\n"); return false; } if (!ret) return true; MSG_BeginReading (net_message); while (1) { if (!host_client->active) return false; // a command caused an error if (net_message->badread) { Sys_Printf ("SV_ReadClientMessage: badread\n"); return false; } cmd = MSG_ReadChar (net_message); switch (cmd) { case -1: goto nextmsg; // end of message default: Sys_Printf ("SV_ReadClientMessage: unknown command char\n"); return false; case clc_nop: // Sys_Printf ("SV_ReadClientMessage: clc_nop\n"); break; case clc_stringcmd: s = MSG_ReadString (net_message); ret = 0; if (nehahra) { if (strncasecmp(s, "max", 3) == 0) ret = 1; else if (strncasecmp(s, "monster", 7) == 0) ret = 1; else if (strncasecmp(s, "scrag", 5) == 0) ret = 1; else if (strncasecmp(s, "wraith", 6) == 0) ret = 1; else if (strncasecmp(s, "gimme", 5) == 0) ret = 1; } else { if (strncasecmp(s, "god", 3) == 0) ret = 1; else if (strncasecmp(s, "notarget", 8) == 0) ret = 1; else if (strncasecmp(s, "fly", 3) == 0) ret = 1; else if (strncasecmp(s, "noclip", 6) == 0) ret = 1; else if (strncasecmp(s, "give", 4) == 0) ret = 1; } if (strncasecmp(s, "status", 6) == 0) ret = 1; else if (strncasecmp(s, "freezeall", 9) == 0) ret = 1; else if (strncasecmp(s, "name", 4) == 0) ret = 1; else if (strncasecmp(s, "say", 3) == 0) ret = 1; else if (strncasecmp(s, "say_team", 8) == 0) ret = 1; else if (strncasecmp(s, "tell", 4) == 0) ret = 1; else if (strncasecmp(s, "color", 5) == 0) ret = 1; else if (strncasecmp(s, "kill", 4) == 0) ret = 1; else if (strncasecmp(s, "pause", 5) == 0) ret = 1; else if (strncasecmp(s, "spawn", 5) == 0) ret = 1; else if (strncasecmp(s, "begin", 5) == 0) ret = 1; else if (strncasecmp(s, "prespawn", 8) == 0) ret = 1; else if (strncasecmp(s, "kick", 4) == 0) ret = 1; else if (strncasecmp(s, "ping", 4) == 0) ret = 1; else if (strncasecmp(s, "ban", 3) == 0) ret = 1; else if (strncasecmp(s, "qcexec", 6) == 0) ret = 1; // qcexec command for qc testing if (ret == 1) Cmd_ExecuteString (s, src_client); else Con_DPrintf("%s tried to %s\n", host_client->name, s); break; case clc_disconnect: // Sys_Printf ("SV_ReadClientMessage: client disconnected\n"); return false; case clc_move: SV_ReadClientMove (&host_client->cmd); break; } } } while (ret == 1); return true; }
int NET_SendToAll (sizebuf_t *data, double blocktime) { double start; int i; int count = 0; qboolean msg_init[MAX_CLIENTS]; /* did we write the message to the client's connection */ qboolean msg_sent[MAX_CLIENTS]; /* did the msg arrive its destination (canSend state). */ for (i = 0, host_client = svs.clients; i < svs.maxclients; i++, host_client++) { if (host_client->netconnection && host_client->active) { count++; msg_init[i] = false; msg_sent[i] = false; } else { msg_init[i] = true; msg_sent[i] = true; } } start = Sys_DoubleTime(); while (count) { count = 0; for (i = 0, host_client = svs.clients; i < svs.maxclients; i++, host_client++) { if (! msg_init[i]) { if (NET_CanSendMessage (host_client->netconnection)) { msg_init[i] = true; NET_SendMessage(host_client->netconnection, data); } else { NET_GetMessage (host_client->netconnection); } count++; continue; } if (! msg_sent[i]) { if (NET_CanSendMessage (host_client->netconnection)) { msg_sent[i] = true; } else { NET_GetMessage (host_client->netconnection); } count++; continue; } } if ((Sys_DoubleTime() - start) > blocktime) break; } return count; }