home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / vmsnet / sources / games / 454 < prev    next >
Encoding:
Text File  |  1992-11-20  |  14.4 KB  |  502 lines

  1. Newsgroups: vmsnet.sources.games
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!rpi!usenet.coe.montana.edu!news.uoregon.edu!news.u.washington.edu!raven.alaska.edu!acad2.alaska.edu!asdmf
  3. From: asdmf@acad2.alaska.edu
  4. Subject: Vmsnetrek 42/47
  5. Message-ID: <1992Nov20.210123.1@acad2.alaska.edu>
  6. Lines: 490
  7. Sender: news@raven.alaska.edu (USENET News System)
  8. Nntp-Posting-Host: acad2.alaska.edu
  9. Organization: University of Alaska
  10. Date: Sat, 21 Nov 1992 01:01:23 GMT
  11.  
  12. -+-+-+-+-+-+-+-+ START OF PART 42 -+-+-+-+-+-+-+-+
  13. X`009`009UDPDIAG(("Connected to server's UDP port\n"));
  14. X`009`009commStatus = STAT_VERIFY_UDP;
  15. X`009`009if (udpWin)
  16. X`009`009    udprefresh(UDP_STATUS);
  17. X`009`009response.request = COMM_VERIFY;`009/* send verify request on UDP */
  18. X`009`009response.port = 0;
  19. X`009`009commSwitchTimeout = 25;`009/* wait 25 updates */
  20. Xsend:
  21. X`009`009sendServerPacket(&response);
  22. X`009    `125
  23. X`009`125
  24. X
  25. X`009break;
  26. X    case SWITCH_DENIED:
  27. X`009if (ntohs(packet->port)) `123
  28. X`009    UDPDIAG(("Switch to UDP failed (different version)\n"));
  29. X`009    warning("UDP protocol request failed (bad version)");
  30. X`009`125 else `123
  31. X`009    UDPDIAG(("Switch to UDP denied\n"));
  32. X`009    warning("UDP protocol request denied");
  33. X`009`125
  34. X`009commModeReq = commMode;
  35. X`009commStatus = STAT_CONNECTED;
  36. X`009commSwitchTimeout = 0;
  37. X`009if (udpWin)
  38. X`009    udprefresh(UDP_STATUS);
  39. X`009if (udpSock >= 0)
  40. X`009    closeUdpConn();
  41. X`009break;
  42. X    case SWITCH_VERIFY:
  43. X`009UDPDIAG(("Received UDP verification\n"));
  44. X`009break;
  45. X    default:
  46. X`009fprintf(stderr, "netrek: Got funny reply (%d) in UDP_REPLY packet\n",
  47. X`009`009packet->reply);
  48. X`009break;
  49. X    `125
  50. X`125
  51. X
  52. X#define MAX_PORT_RETRY`00910
  53. XopenUdpConn()
  54. X`123
  55. X    struct sockaddr_in addr;
  56. X    struct hostent *hp;
  57. X    int attempts;
  58. X
  59. X    if (udpSock >= 0) `123
  60. X`009fprintf(stderr, "netrek: tried to open udpSock twice\n");
  61. X`009return (0);`009/* pretend we succeeded (this could be bad) */
  62. X    `125
  63. X
  64. X    if ((udpSock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) `123
  65. X`009perror("netrek: unable to create DGRAM socket");
  66. X`009return (-1);
  67. X    `125
  68. X
  69. X    addr.sin_addr.s_addr = INADDR_ANY;
  70. X    addr.sin_family = AF_INET;
  71. X
  72. X    errno = 0;
  73. X    udpLocalPort = (getpid() & 32767) + (random() % 256);
  74. X    for (attempts = 0; attempts < MAX_PORT_RETRY; attempts++) `123
  75. X`009while (udpLocalPort < 2048) `123
  76. X`009    udpLocalPort = (udpLocalPort + 10687) & 32767;
  77. X`009`125
  78. X#ifdef GATEWAY
  79. X`009/* we need the gateway to know where to find us */
  80. X`009if (!strcmp(serverName, gw_mach)) `123
  81. X`009    UDPDIAG(("+ gateway test: binding to %d\n", gw_local_port));
  82. X`009    udpLocalPort = gw_local_port;
  83. X`009`125
  84. X#endif
  85. X`009addr.sin_port = htons(udpLocalPort);
  86. X`009if (bind(udpSock, &addr, sizeof(addr)) >= 0)
  87. X`009    break;
  88. X    `125
  89. X    if (attempts == MAX_PORT_RETRY) `123
  90. X`009perror("netrek: bind");
  91. X`009UDPDIAG(("Unable to find a local port to bind to\n"));
  92. X`009close(udpSock);
  93. X`009udpSock = -1;
  94. X`009return (-1);
  95. X    `125
  96. X
  97. X    UDPDIAG(("Local port is %d\n", udpLocalPort));
  98. X
  99. X    /* determine the address of the server */
  100. X    if (!serveraddr) `123
  101. X`009if ((addr.sin_addr.s_addr = inet_addr(serverName)) == -1) `123
  102. X`009    if ((hp = gethostbyname(serverName)) == NULL) `123
  103. X`009`009printf("Who is %s?\n", serverName);
  104. X`009`009exit(0);
  105. X`009    `125 else `123
  106. X`009`009addr.sin_addr.s_addr = *(long *) hp->h_addr;
  107. X`009    `125
  108. X`009`125
  109. X`009serveraddr = addr.sin_addr.s_addr;
  110. X`009UDPDIAG(("Found serveraddr == 0x%x\n", serveraddr));
  111. X    `125
  112. X    return (0);
  113. X`125
  114. X
  115. X#ifdef USE_PORTSWAP
  116. XconnUdpConn()
  117. X`123
  118. X    struct sockaddr_in addr;
  119. X
  120. X    addr.sin_addr.s_addr = serveraddr;
  121. X    addr.sin_family = AF_INET;
  122. X    addr.sin_port = htons(udpServerPort);
  123. X
  124. X    UDPDIAG(("Connecting to host 0x%x on port %d\n", serveraddr,udpServerPor
  125. Vt));
  126. X    if (connect(udpSock, &addr, sizeof(addr)) < 0) `123
  127. X`009fprintf(stderr, "Error %d: ");
  128. X`009perror("netrek: unable to connect UDP socket");
  129. X`009printUdpInfo();`009`009/* debug */
  130. X`009return (-1);
  131. X    `125
  132. X
  133. X    return (0);
  134. X`125
  135. X#endif
  136. X
  137. X#ifndef USE_PORTSWAP
  138. XrecvUdpConn()
  139. X`123
  140. X    fd_set readfds;
  141. X    struct timeval to;
  142. X    struct sockaddr_in from;
  143. X    struct sockaddr_in addr;
  144. X    int fromlen, res;
  145. X
  146. X    bzero(&from, sizeof(from));`009`009/* don't get garbage if really broken
  147. V */
  148. X
  149. X    /* we patiently wait until the server sends a packet to us */
  150. X    /* (note that we silently eat the first one) */
  151. X    UDPDIAG(("Issuing recvfrom() call\n"));
  152. X    printUdpInfo();
  153. X    fromlen = sizeof(from);
  154. X    FD_ZERO(&readfds);
  155. X    FD_SET(udpSock, &readfds);
  156. X    to.tv_sec = 6;`009`009/* wait 6 seconds, then abort */
  157. X    to.tv_usec = 0;
  158. X    if ((res = select(32, &readfds, 0, 0, &to)) <= 0) `123
  159. X`009if (!res) `123
  160. X`009    UDPDIAG(("timed out waiting for response\n"));
  161. X`009    warning("UDP connection request timed out");
  162. X`009    return (-1);
  163. X`009`125 else `123
  164. X`009    perror("select() before recvfrom()");
  165. X`009    return (-1);
  166. X`009`125
  167. X    `125
  168. X    if (recvfrom(udpSock, buf, BUFSIZ, 0, &from, &fromlen) < 0) `123
  169. X        perror("recvfrom");
  170. X`009UDPDIAG(("recvfrom failed, aborting UDP attempt"));
  171. X`009return (-1);
  172. X    `125
  173. X
  174. X    if (from.sin_addr.s_addr != serveraddr) `123
  175. X`009UDPDIAG(("Warning: from 0x%x, but server is 0x%x\n",
  176. X`009`009from.sin_addr.s_addr, serveraddr));
  177. X    `125
  178. X    if (from.sin_family != AF_INET) `123
  179. X`009UDPDIAG(("Warning: not AF_INET (%d)\n", from.sin_family));
  180. X    `125
  181. X    udpServerPort = ntohs(from.sin_port);
  182. X    UDPDIAG(("recvfrom() succeeded; will use server port %d\n", udpServerPor
  183. Vt));
  184. X#ifdef GATEWAY
  185. X    if (!strcmp(serverName, gw_mach)) `123
  186. X`009UDPDIAG(("+ actually, I'm going to use %d\n", gw_port));
  187. X`009udpServerPort = gw_port;
  188. X`009from.sin_port = htons(udpServerPort);
  189. X    `125
  190. X#endif
  191. X
  192. X    if (connect(udpSock, &from, sizeof(from)) < 0) `123
  193. X`009perror("netrek: unable to connect UDP socket after recvfrom()");
  194. X`009close(udpSock);
  195. X`009udpSock = -1;
  196. X`009return (-1);
  197. X    `125
  198. X
  199. X    return (0);
  200. X`125
  201. X#endif
  202. X
  203. XcloseUdpConn()
  204. X`123
  205. X    V_UDPDIAG(("Closing UDP socket\n"));
  206. X    if (udpSock < 0) `123
  207. X`009fprintf(stderr, "netrek: tried to close a closed UDP socket\n");
  208. X`009return (-1);
  209. X    `125
  210. X    shutdown(udpSock, 2);
  211. X    close(udpSock);
  212. X    udpSock = -1;
  213. X`125
  214. X
  215. X/* debugging info */
  216. XprintUdpInfo()
  217. X`123
  218. X    struct sockaddr_in addr;
  219. X    int len;
  220. X
  221. X    len = sizeof(addr);
  222. X    if (getsockname(udpSock, &addr, &len) < 0) `123
  223. X/*`009perror("printUdpInfo: getsockname");*/
  224. X`009return;
  225. X    `125
  226. X    UDPDIAG(("LOCAL: addr=0x%x, family=%d, port=%d\n", addr.sin_addr.s_addr,
  227. X`009addr.sin_family, ntohs(addr.sin_port)));
  228. X
  229. X    if (getpeername(udpSock, &addr, &len) < 0) `123
  230. X/*`009perror("printUdpInfo: getpeername");*/
  231. X`009return;
  232. X    `125
  233. X    UDPDIAG(("PEER : addr=0x%x, family=%d, port=%d\n", addr.sin_addr.s_addr,
  234. X`009addr.sin_family, ntohs(addr.sin_port)));
  235. X`125
  236. X
  237. X/* handles both SP_SEQUENCE and SP_SC_SEQUENCE */
  238. XhandleSequence(packet)
  239. Xstruct sequence_spacket *packet;
  240. X`123
  241. X    static int recent_count=0, recent_dropped=0;
  242. X    long newseq;
  243. X
  244. X    drop_flag = 0;
  245. X    if (chan != udpSock)
  246. X`009return;`009`009/* don't pay attention to TCP sequence #s */
  247. X
  248. X    udpTotal++;
  249. X    recent_count++;
  250. X
  251. X    /* update percent display every 256 updates (`12650 seconds usually) */
  252. X    if (!(udpTotal & 0xff))
  253. X`009if (udpWin) udprefresh(UDP_DROPPED);
  254. X
  255. X    newseq = (long) ntohs(packet->sequence);
  256. X/*    printf("read %d - ", newseq);*/
  257. X
  258. X    if (((unsigned short) sequence) > 65000 &&
  259. X`009((unsigned short) newseq) < 1000) `123
  260. X`009/* we rolled, set newseq = 65536+sequence and accept it */
  261. X`009sequence = ((sequence + 65536) & 0xffff0000) `124 newseq;
  262. X    `125 else `123
  263. X`009/* adjust newseq and do compare */
  264. X`009newseq `124= (sequence & 0xffff0000);
  265. X
  266. X`009if (!udpSequenceChk) `123`009/* put this here so that turning seq check
  267. V */
  268. X`009    sequence = newseq;`009/* on and off doesn't make us think we lost */
  269. X`009    return;`009`009/* a whole bunch of packets. */
  270. X`009`125
  271. X
  272. X`009if (newseq > sequence) `123
  273. X`009    /* accept */
  274. X`009    if (newseq != sequence+1) `123
  275. X`009`009udpDropped += (newseq-sequence)-1;
  276. X`009`009udpTotal += (newseq-sequence)-1;`009/* want TOTAL packets */
  277. X`009`009recent_dropped += (newseq-sequence)-1;
  278. X`009`009recent_count += (newseq-sequence)-1;
  279. X`009`009if (udpWin)
  280. X`009`009    udprefresh(UDP_DROPPED);
  281. X`009`009UDPDIAG(("sequence=%d, newseq=%d, we lost some\n",
  282. X`009`009    sequence, newseq));
  283. X`009    `125
  284. X`009    sequence = newseq;
  285. X`009`125 else `123
  286. X`009    /* reject */
  287. X`009    if (packet->type == SP_SC_SEQUENCE) `123
  288. X`009`009V_UDPDIAG(("(ignoring repeat %d)\n", newseq));
  289. X`009    `125 else `123
  290. X`009`009UDPDIAG(("sequence=%d, newseq=%d, ignoring transmission\n",
  291. X`009`009    sequence, newseq));
  292. X`009    `125
  293. X`009    drop_flag = 1;
  294. X`009`125
  295. X    `125
  296. X/*    printf("newseq %d, sequence %d\n", newseq, sequence);*/
  297. X
  298. X    if (recent_count > UDP_RECENT_INTR) `123
  299. X`009/* once a minute (at 5 upd/sec), report on how many were dropped */
  300. X`009/* during the last UDP_RECENT_INTR updates                       */
  301. X`009udpRecentDropped = recent_dropped;
  302. X`009recent_count = recent_dropped = 0;
  303. X`009if (udpWin)
  304. X`009    udprefresh(UDP_DROPPED);
  305. X    `125
  306. X`125
  307. X
  308. $ CALL UNPACK SOCKET.C;1 643885165
  309. $ create/nolog 'f'
  310. X/*
  311. X * stats.c
  312. X */
  313. X#include "copyright.h"
  314. X
  315. X#include <stdio.h>
  316. X#include "Wlib.h"
  317. X#include "defs.h"
  318. X#include "struct.h"
  319. X#include "data.h"
  320. X
  321. X#define`009MIN(a,b)`009(((a) < (b)) ? (a) : (b))
  322. X
  323. X#define`009BX_OFF()`009((textWidth + 1) * W_Textwidth + S_IBORDER)
  324. X#define`009BY_OFF(line)`009((line) * (W_Textheight + S_IBORDER) + S_IBORDER)
  325. X#define`009TX_OFF(len)`009((textWidth - len) * W_Textwidth + S_IBORDER)
  326. X#define`009TY_OFF(line)`009BY_OFF(line)
  327. X
  328. X#define STAT_WIDTH`009`009160
  329. X#define STAT_HEIGHT`009`009BY_OFF(NUM_SLIDERS)
  330. X#define STAT_BORDER`009`0092
  331. X#define S_IBORDER`009`0095
  332. X#define STAT_X`009`009`009422
  333. X#define STAT_Y`009`009`00913
  334. X
  335. X#define SL_WID`009`009`009\
  336. X`009(STAT_WIDTH - 2 * S_IBORDER - (textWidth + 1) * W_Textwidth)
  337. X#define SL_HEI`009`009`009(W_Textheight)
  338. X
  339. X#define NUM_ELS(a)`009`009(sizeof (a) / sizeof (*(a)))
  340. X#define NUM_SLIDERS`009`009NUM_ELS(sliders)
  341. X
  342. Xtypedef struct slider `123
  343. X`009char`009*label;
  344. X`009int`009min, max;
  345. X`009int`009low_red, high_red;
  346. X`009int`009label_length;
  347. X`009int`009diff;
  348. X`009int`009*var;
  349. X`009int`009lastVal;
  350. X`125 SLIDER;
  351. X
  352. Xtypedef struct record `123
  353. X`009int`009*data;
  354. X`009int`009last_value;
  355. X`125 RECORD;
  356. X
  357. Xstatic SLIDER`009sliders`091`093 = `123
  358. X`009`123 "Shields",`009`0090,`009100,`00920,`009100`009`125,
  359. X`009`123 "Damage",`009`0090,`009100,`0090,`0090`009`125,
  360. X`009`123 "Fuel",`009`0090,`00910000,`0092000,`00910000`009`125,
  361. X`009`123 "Warp",`009`0090,`0099,`0090,`0099`009`125,
  362. X`009`123 "Weapon Temp",`0090,`0091200,`0090,`009800`009`125,
  363. X`009`123 "Engine Temp",`0090,`0091200,`0090,`009800`009`125,
  364. X`125;
  365. X
  366. Xstatic int`009`009textWidth = 0;
  367. Xstatic int `009`009initialized = 0;
  368. X
  369. XinitStats()
  370. X`123
  371. X    int`009i;
  372. X    /*char`009*str;*/
  373. X
  374. X    if (initialized) return;
  375. X    initialized=1;
  376. X    sliders`0910`093.var = &(me->p_shield);
  377. X    sliders`0911`093.var = &(me->p_damage);
  378. X    sliders`0912`093.var = &(me->p_fuel);
  379. X    sliders`0913`093.var = &(me->p_speed);
  380. X    sliders`0914`093.var = &(me->p_wtemp);
  381. X    sliders`0915`093.var = &(me->p_etemp);
  382. X    for (i = 0; i < NUM_SLIDERS; i++) `123
  383. X`009sliders`091i`093.label_length = strlen(sliders`091i`093.label);
  384. X`009textWidth = MAX(textWidth, sliders`091i`093.label_length);
  385. X`009sliders`091i`093.diff = sliders`091i`093.max - sliders`091i`093.min;
  386. X`009sliders`091i`093.lastVal=0;
  387. X    `125
  388. X`125
  389. X
  390. XredrawStats()
  391. X`123
  392. X    int`009i;
  393. X
  394. X    W_ClearWindow(statwin);
  395. X    initStats();
  396. X    for (i=0; i<NUM_SLIDERS; i++) `123
  397. X`009sliders`091i`093.lastVal=0;
  398. X    `125
  399. X    for (i = 0; i < NUM_SLIDERS; i++) `123
  400. X`009W_WriteText(statwin, TX_OFF(sliders`091i`093.label_length), TY_OFF(i),
  401. X`009    textColor, sliders`091i`093.label, sliders`091i`093.label_length,
  402. X`009    W_RegularFont);
  403. X#ifdef VMS
  404. X`009boxf(0, BX_OFF() - 1, BY_OFF(i) - 1, SL_WID+2, SL_HEI+2, borderColor);
  405. X#else
  406. X`009box(0, BX_OFF() - 1, BY_OFF(i) - 1, SL_WID+2, SL_HEI+2, borderColor);
  407. X#endif
  408. X`009sliders`091i`093.lastVal=0;
  409. X    `125
  410. X`125
  411. X
  412. XupdateStats()
  413. X`123
  414. X    int`009i, value, diff, old_x, new_x;
  415. X    W_Color color;
  416. X    SLIDER`009*s;
  417. X
  418. X    initStats();
  419. X    for (i = 0; i < NUM_SLIDERS; i++) `123
  420. X`009s = &sliders`091i`093;
  421. X`009value= *(s->var);
  422. X`009if (value < s->min)
  423. X`009    value = s->min;
  424. X`009else if (value > s->max)
  425. X`009    value = s->max;
  426. X`009if (value == s->lastVal)
  427. X`009    continue;
  428. X`009diff = value - s->lastVal;
  429. X`009if (diff < 0) `123
  430. X`009    old_x = s->lastVal * SL_WID / s->diff;
  431. X`009    new_x = value * SL_WID / s->diff;
  432. X#ifdef VMS
  433. X`009    boxf(1, BX_OFF()+new_x, BY_OFF(i), old_x-new_x, SL_HEI, backColor);
  434. X#else
  435. X`009    box(1, BX_OFF()+new_x, BY_OFF(i), old_x-new_x, SL_HEI, backColor);
  436. X#endif
  437. X
  438. X`009    if (s->lastVal >= s->low_red && value < s->low_red)`032
  439. X#ifdef VMS
  440. X`009`009boxf(1, BX_OFF(), BY_OFF(i), new_x, SL_HEI, warningColor);
  441. X#else
  442. X`009`009box(1, BX_OFF(), BY_OFF(i), new_x, SL_HEI, warningColor);
  443. X#endif
  444. X`009    else if (s->lastVal > s->high_red && value <= s->high_red)`032
  445. X#ifdef VMS
  446. X`009`009boxf(1, BX_OFF(), BY_OFF(i), new_x, SL_HEI, myColor);
  447. X#else
  448. X`009`009box(1, BX_OFF(), BY_OFF(i), new_x, SL_HEI, myColor);
  449. X#endif
  450. X`009`125 else `123
  451. X`009    if (value < s->low_red)
  452. X`009`009color = warningColor;
  453. X`009    else if (value > s->high_red) `123
  454. X`009`009color = warningColor;
  455. X`009`009if (s->lastVal <= s->high_red)
  456. X`009`009    s->lastVal = 0;
  457. X`009    `125 else `123
  458. X`009`009color = myColor;
  459. X`009`009if (s->lastVal < s->low_red)
  460. X`009`009    s->lastVal = 0;
  461. X`009    `125
  462. X`009    old_x = s->lastVal * SL_WID / s->diff;
  463. X`009    new_x = value * SL_WID / s->diff;
  464. X#ifdef VMS
  465. X`009    boxf(1, BX_OFF() + old_x, BY_OFF(i), new_x - old_x, SL_HEI, color);
  466. X#else
  467. X`009    box(1, BX_OFF() + old_x, BY_OFF(i), new_x - old_x, SL_HEI, color);
  468. X#endif
  469. X`009`125
  470. X`009s->lastVal=value;
  471. X    `125
  472. X`125
  473. X
  474. X#ifdef VMS
  475. Xboxf(filled, x, y, wid, hei, color)
  476. X#else
  477. Xbox(filled, x, y, wid, hei, color)
  478. X#endif
  479. Xint filled, x, y, wid, hei;
  480. XW_Color color;
  481. X`123
  482. X    if (wid==0) return;
  483. X
  484. X    if (filled) `123
  485. X`009W_ClearArea(statwin, x, y, wid+1, hei+1, color);
  486. X`009return;
  487. X    `125
  488. X
  489. X    W_MakeLine(statwin,x,y,x+wid,y,color);
  490. X    W_MakeLine(statwin,x+wid,y,x+wid,y+hei,color);
  491. X    W_MakeLine(statwin,x+wid,y+hei,x,y+hei,color);
  492. X    W_MakeLine(statwin,x,y+hei,x,y,color);
  493. X`125
  494. X
  495. X
  496. Xcalibrate_stats()
  497. X`123
  498. X    register int i;
  499. X
  500. X    sliders`0910`093.max = me->p_ship.s_maxshield;
  501. +-+-+-+-+-+-+-+-  END  OF PART 42 +-+-+-+-+-+-+-+-
  502.