sv_send.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // sv_main.c -- server main program
  16. #include "server.h"
  17. /*
  18. =============================================================================
  19. Com_Printf redirection
  20. =============================================================================
  21. */
  22. char sv_outputbuf[SV_OUTPUTBUF_LENGTH];
  23. void SV_FlushRedirect (int sv_redirected, char *outputbuf)
  24. {
  25. if (sv_redirected == RD_PACKET)
  26. {
  27. Netchan_OutOfBandPrint (NS_SERVER, net_from, "print\n%s", outputbuf);
  28. }
  29. else if (sv_redirected == RD_CLIENT)
  30. {
  31. MSG_WriteByte (&sv_client->netchan.message, svc_print);
  32. MSG_WriteByte (&sv_client->netchan.message, PRINT_HIGH);
  33. MSG_WriteString (&sv_client->netchan.message, outputbuf);
  34. }
  35. }
  36. /*
  37. =============================================================================
  38. EVENT MESSAGES
  39. =============================================================================
  40. */
  41. /*
  42. =================
  43. SV_ClientPrintf
  44. Sends text across to be displayed if the level passes
  45. =================
  46. */
  47. void SV_ClientPrintf (client_t *cl, int level, char *fmt, ...)
  48. {
  49. va_list argptr;
  50. char string[1024];
  51. if (level < cl->messagelevel)
  52. return;
  53. va_start (argptr,fmt);
  54. vsprintf (string, fmt,argptr);
  55. va_end (argptr);
  56. MSG_WriteByte (&cl->netchan.message, svc_print);
  57. MSG_WriteByte (&cl->netchan.message, level);
  58. MSG_WriteString (&cl->netchan.message, string);
  59. }
  60. /*
  61. =================
  62. SV_BroadcastPrintf
  63. Sends text to all active clients
  64. =================
  65. */
  66. void SV_BroadcastPrintf (int level, char *fmt, ...)
  67. {
  68. va_list argptr;
  69. char string[2048];
  70. client_t *cl;
  71. int i;
  72. va_start (argptr,fmt);
  73. vsprintf (string, fmt,argptr);
  74. va_end (argptr);
  75. // echo to console
  76. if (dedicated->value)
  77. {
  78. char copy[1024];
  79. int i;
  80. // mask off high bits
  81. for (i=0 ; i<1023 && string[i] ; i++)
  82. copy[i] = string[i]&127;
  83. copy[i] = 0;
  84. Com_Printf ("%s", copy);
  85. }
  86. for (i=0, cl = svs.clients ; i<maxclients->value; i++, cl++)
  87. {
  88. if (level < cl->messagelevel)
  89. continue;
  90. if (cl->state != cs_spawned)
  91. continue;
  92. MSG_WriteByte (&cl->netchan.message, svc_print);
  93. MSG_WriteByte (&cl->netchan.message, level);
  94. MSG_WriteString (&cl->netchan.message, string);
  95. }
  96. }
  97. /*
  98. =================
  99. SV_BroadcastCommand
  100. Sends text to all active clients
  101. =================
  102. */
  103. void SV_BroadcastCommand (char *fmt, ...)
  104. {
  105. va_list argptr;
  106. char string[1024];
  107. if (!sv.state)
  108. return;
  109. va_start (argptr,fmt);
  110. vsprintf (string, fmt,argptr);
  111. va_end (argptr);
  112. MSG_WriteByte (&sv.multicast, svc_stufftext);
  113. MSG_WriteString (&sv.multicast, string);
  114. SV_Multicast (NULL, MULTICAST_ALL_R);
  115. }
  116. /*
  117. =================
  118. SV_Multicast
  119. Sends the contents of sv.multicast to a subset of the clients,
  120. then clears sv.multicast.
  121. MULTICAST_ALL same as broadcast (origin can be NULL)
  122. MULTICAST_PVS send to clients potentially visible from org
  123. MULTICAST_PHS send to clients potentially hearable from org
  124. =================
  125. */
  126. void SV_Multicast (vec3_t origin, multicast_t to)
  127. {
  128. client_t *client;
  129. byte *mask;
  130. int leafnum, cluster;
  131. int j;
  132. qboolean reliable;
  133. int area1, area2;
  134. reliable = false;
  135. if (to != MULTICAST_ALL_R && to != MULTICAST_ALL)
  136. {
  137. leafnum = CM_PointLeafnum (origin);
  138. area1 = CM_LeafArea (leafnum);
  139. }
  140. else
  141. {
  142. leafnum = 0; // just to avoid compiler warnings
  143. area1 = 0;
  144. }
  145. // if doing a serverrecord, store everything
  146. if (svs.demofile)
  147. SZ_Write (&svs.demo_multicast, sv.multicast.data, sv.multicast.cursize);
  148. switch (to)
  149. {
  150. case MULTICAST_ALL_R:
  151. reliable = true; // intentional fallthrough
  152. case MULTICAST_ALL:
  153. leafnum = 0;
  154. mask = NULL;
  155. break;
  156. case MULTICAST_PHS_R:
  157. reliable = true; // intentional fallthrough
  158. case MULTICAST_PHS:
  159. leafnum = CM_PointLeafnum (origin);
  160. cluster = CM_LeafCluster (leafnum);
  161. mask = CM_ClusterPHS (cluster);
  162. break;
  163. case MULTICAST_PVS_R:
  164. reliable = true; // intentional fallthrough
  165. case MULTICAST_PVS:
  166. leafnum = CM_PointLeafnum (origin);
  167. cluster = CM_LeafCluster (leafnum);
  168. mask = CM_ClusterPVS (cluster);
  169. break;
  170. default:
  171. mask = NULL;
  172. Com_Error (ERR_FATAL, "SV_Multicast: bad to:%i", to);
  173. }
  174. // send the data to all relevent clients
  175. for (j = 0, client = svs.clients; j < maxclients->value; j++, client++)
  176. {
  177. if (client->state == cs_free || client->state == cs_zombie)
  178. continue;
  179. if (client->state != cs_spawned && !reliable)
  180. continue;
  181. if (mask)
  182. {
  183. leafnum = CM_PointLeafnum (client->edict->s.origin);
  184. cluster = CM_LeafCluster (leafnum);
  185. area2 = CM_LeafArea (leafnum);
  186. if (!CM_AreasConnected (area1, area2))
  187. continue;
  188. if ( mask && (!(mask[cluster>>3] & (1<<(cluster&7)) ) ) )
  189. continue;
  190. }
  191. if (reliable)
  192. SZ_Write (&client->netchan.message, sv.multicast.data, sv.multicast.cursize);
  193. else
  194. SZ_Write (&client->datagram, sv.multicast.data, sv.multicast.cursize);
  195. }
  196. SZ_Clear (&sv.multicast);
  197. }
  198. /*
  199. ==================
  200. SV_StartSound
  201. Each entity can have eight independant sound sources, like voice,
  202. weapon, feet, etc.
  203. If cahnnel & 8, the sound will be sent to everyone, not just
  204. things in the PHS.
  205. FIXME: if entity isn't in PHS, they must be forced to be sent or
  206. have the origin explicitly sent.
  207. Channel 0 is an auto-allocate channel, the others override anything
  208. already running on that entity/channel pair.
  209. An attenuation of 0 will play full volume everywhere in the level.
  210. Larger attenuations will drop off. (max 4 attenuation)
  211. Timeofs can range from 0.0 to 0.1 to cause sounds to be started
  212. later in the frame than they normally would.
  213. If origin is NULL, the origin is determined from the entity origin
  214. or the midpoint of the entity box for bmodels.
  215. ==================
  216. */
  217. void SV_StartSound (vec3_t origin, edict_t *entity, int channel,
  218. int soundindex, float volume,
  219. float attenuation, float timeofs)
  220. {
  221. int sendchan;
  222. int flags;
  223. int i;
  224. int ent;
  225. vec3_t origin_v;
  226. qboolean use_phs;
  227. if (volume < 0 || volume > 1.0)
  228. Com_Error (ERR_FATAL, "SV_StartSound: volume = %f", volume);
  229. if (attenuation < 0 || attenuation > 4)
  230. Com_Error (ERR_FATAL, "SV_StartSound: attenuation = %f", attenuation);
  231. // if (channel < 0 || channel > 15)
  232. // Com_Error (ERR_FATAL, "SV_StartSound: channel = %i", channel);
  233. if (timeofs < 0 || timeofs > 0.255)
  234. Com_Error (ERR_FATAL, "SV_StartSound: timeofs = %f", timeofs);
  235. ent = NUM_FOR_EDICT(entity);
  236. if (channel & 8) // no PHS flag
  237. {
  238. use_phs = false;
  239. channel &= 7;
  240. }
  241. else
  242. use_phs = true;
  243. sendchan = (ent<<3) | (channel&7);
  244. flags = 0;
  245. if (volume != DEFAULT_SOUND_PACKET_VOLUME)
  246. flags |= SND_VOLUME;
  247. if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
  248. flags |= SND_ATTENUATION;
  249. // the client doesn't know that bmodels have weird origins
  250. // the origin can also be explicitly set
  251. if ( (entity->svflags & SVF_NOCLIENT)
  252. || (entity->solid == SOLID_BSP)
  253. || origin )
  254. flags |= SND_POS;
  255. // always send the entity number for channel overrides
  256. flags |= SND_ENT;
  257. if (timeofs)
  258. flags |= SND_OFFSET;
  259. // use the entity origin unless it is a bmodel or explicitly specified
  260. if (!origin)
  261. {
  262. origin = origin_v;
  263. if (entity->solid == SOLID_BSP)
  264. {
  265. for (i=0 ; i<3 ; i++)
  266. origin_v[i] = entity->s.origin[i]+0.5*(entity->mins[i]+entity->maxs[i]);
  267. }
  268. else
  269. {
  270. VectorCopy (entity->s.origin, origin_v);
  271. }
  272. }
  273. MSG_WriteByte (&sv.multicast, svc_sound);
  274. MSG_WriteByte (&sv.multicast, flags);
  275. MSG_WriteByte (&sv.multicast, soundindex);
  276. if (flags & SND_VOLUME)
  277. MSG_WriteByte (&sv.multicast, volume*255);
  278. if (flags & SND_ATTENUATION)
  279. MSG_WriteByte (&sv.multicast, attenuation*64);
  280. if (flags & SND_OFFSET)
  281. MSG_WriteByte (&sv.multicast, timeofs*1000);
  282. if (flags & SND_ENT)
  283. MSG_WriteShort (&sv.multicast, sendchan);
  284. if (flags & SND_POS)
  285. MSG_WritePos (&sv.multicast, origin);
  286. // if the sound doesn't attenuate,send it to everyone
  287. // (global radio chatter, voiceovers, etc)
  288. if (attenuation == ATTN_NONE)
  289. use_phs = false;
  290. if (channel & CHAN_RELIABLE)
  291. {
  292. if (use_phs)
  293. SV_Multicast (origin, MULTICAST_PHS_R);
  294. else
  295. SV_Multicast (origin, MULTICAST_ALL_R);
  296. }
  297. else
  298. {
  299. if (use_phs)
  300. SV_Multicast (origin, MULTICAST_PHS);
  301. else
  302. SV_Multicast (origin, MULTICAST_ALL);
  303. }
  304. }
  305. /*
  306. ===============================================================================
  307. FRAME UPDATES
  308. ===============================================================================
  309. */
  310. /*
  311. =======================
  312. SV_SendClientDatagram
  313. =======================
  314. */
  315. qboolean SV_SendClientDatagram (client_t *client)
  316. {
  317. byte msg_buf[MAX_MSGLEN];
  318. sizebuf_t msg;
  319. SV_BuildClientFrame (client);
  320. SZ_Init (&msg, msg_buf, sizeof(msg_buf));
  321. msg.allowoverflow = true;
  322. // send over all the relevant entity_state_t
  323. // and the player_state_t
  324. SV_WriteFrameToClient (client, &msg);
  325. // copy the accumulated multicast datagram
  326. // for this client out to the message
  327. // it is necessary for this to be after the WriteEntities
  328. // so that entity references will be current
  329. if (client->datagram.overflowed)
  330. Com_Printf ("WARNING: datagram overflowed for %s\n", client->name);
  331. else
  332. SZ_Write (&msg, client->datagram.data, client->datagram.cursize);
  333. SZ_Clear (&client->datagram);
  334. if (msg.overflowed)
  335. { // must have room left for the packet header
  336. Com_Printf ("WARNING: msg overflowed for %s\n", client->name);
  337. SZ_Clear (&msg);
  338. }
  339. // send the datagram
  340. Netchan_Transmit (&client->netchan, msg.cursize, msg.data);
  341. // record the size for rate estimation
  342. client->message_size[sv.framenum % RATE_MESSAGES] = msg.cursize;
  343. return true;
  344. }
  345. /*
  346. ==================
  347. SV_DemoCompleted
  348. ==================
  349. */
  350. void SV_DemoCompleted (void)
  351. {
  352. if (sv.demofile)
  353. {
  354. fclose (sv.demofile);
  355. sv.demofile = NULL;
  356. }
  357. SV_Nextserver ();
  358. }
  359. /*
  360. =======================
  361. SV_RateDrop
  362. Returns true if the client is over its current
  363. bandwidth estimation and should not be sent another packet
  364. =======================
  365. */
  366. qboolean SV_RateDrop (client_t *c)
  367. {
  368. int total;
  369. int i;
  370. // never drop over the loopback
  371. if (c->netchan.remote_address.type == NA_LOOPBACK)
  372. return false;
  373. total = 0;
  374. for (i = 0 ; i < RATE_MESSAGES ; i++)
  375. {
  376. total += c->message_size[i];
  377. }
  378. if (total > c->rate)
  379. {
  380. c->surpressCount++;
  381. c->message_size[sv.framenum % RATE_MESSAGES] = 0;
  382. return true;
  383. }
  384. return false;
  385. }
  386. /*
  387. =======================
  388. SV_SendClientMessages
  389. =======================
  390. */
  391. void SV_SendClientMessages (void)
  392. {
  393. int i;
  394. client_t *c;
  395. int msglen;
  396. byte msgbuf[MAX_MSGLEN];
  397. int r;
  398. msglen = 0;
  399. // read the next demo message if needed
  400. if (sv.state == ss_demo && sv.demofile)
  401. {
  402. if (sv_paused->value)
  403. msglen = 0;
  404. else
  405. {
  406. // get the next message
  407. r = fread (&msglen, 4, 1, sv.demofile);
  408. if (r != 1)
  409. {
  410. SV_DemoCompleted ();
  411. return;
  412. }
  413. msglen = LittleLong (msglen);
  414. if (msglen == -1)
  415. {
  416. SV_DemoCompleted ();
  417. return;
  418. }
  419. if (msglen > MAX_MSGLEN)
  420. Com_Error (ERR_DROP, "SV_SendClientMessages: msglen > MAX_MSGLEN");
  421. r = fread (msgbuf, msglen, 1, sv.demofile);
  422. if (r != 1)
  423. {
  424. SV_DemoCompleted ();
  425. return;
  426. }
  427. }
  428. }
  429. // send a message to each connected client
  430. for (i=0, c = svs.clients ; i<maxclients->value; i++, c++)
  431. {
  432. if (!c->state)
  433. continue;
  434. // if the reliable message overflowed,
  435. // drop the client
  436. if (c->netchan.message.overflowed)
  437. {
  438. SZ_Clear (&c->netchan.message);
  439. SZ_Clear (&c->datagram);
  440. SV_BroadcastPrintf (PRINT_HIGH, "%s overflowed\n", c->name);
  441. SV_DropClient (c);
  442. }
  443. if (sv.state == ss_cinematic
  444. || sv.state == ss_demo
  445. || sv.state == ss_pic
  446. )
  447. Netchan_Transmit (&c->netchan, msglen, msgbuf);
  448. else if (c->state == cs_spawned)
  449. {
  450. // don't overrun bandwidth
  451. if (SV_RateDrop (c))
  452. continue;
  453. SV_SendClientDatagram (c);
  454. }
  455. else
  456. {
  457. // just update reliable if needed
  458. if (c->netchan.message.cursize || curtime - c->netchan.last_sent > 1000 )
  459. Netchan_Transmit (&c->netchan, 0, NULL);
  460. }
  461. }
  462. }