CONNECT.CPP 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /CounterStrike/CONNECT.CPP 1 3/03/97 10:24a Joe_bostic $ */
  19. /***************************************************************************
  20. ** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S **
  21. ***************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : CONNECT.CPP *
  26. * *
  27. * Programmer : Bill Randolph *
  28. * *
  29. * Start Date : December 20, 1994 *
  30. * *
  31. * Last Update : May 31, 1995 [BRR] *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * ConnectionClass::ConnectionClass -- class constructor *
  35. * ConnectionClass::~ConnectionClass -- class destructor *
  36. * ConnectionClass::Init -- Initializes connection queue to empty *
  37. * ConnectionClass::Send_Packet -- adds a packet to the send queue *
  38. * ConnectionClass::Receive_Packet -- adds packet to receive queue *
  39. * ConnectionClass::Get_Packet -- gets a packet from receive queue *
  40. * ConnectionClass::Service -- main polling routine; services packets *
  41. * ConnectionClass::Service_Send_Queue -- services the send queue *
  42. * ConnectionClass::Service_Receive_Queue -- services receive queue *
  43. * ConnectionClass::Time -- gets current time *
  44. * ConnectionClass::Command_Name -- returns name for a packet command *
  45. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  46. #include "function.h"
  47. #include <stdio.h>
  48. #include <mem.h>
  49. #include <sys\timeb.h>
  50. #include "connect.h"
  51. #include "WolDebug.h"
  52. /*
  53. ********************************* Globals ***********************************
  54. */
  55. static char *ConnectionClass::Commands[PACKET_COUNT] = {
  56. "ADATA",
  57. "NDATA",
  58. "ACK"
  59. };
  60. /***************************************************************************
  61. * ConnectionClass::ConnectionClass -- class constructor *
  62. * *
  63. * INPUT: *
  64. * numsend desired # of entries for the send queue *
  65. * numreceive desired # of entries for the receive queue *
  66. * maxlen max length of an application packet *
  67. * magicnum the packet "magic number" for this connection *
  68. * retry_delta the time to wait between sends *
  69. * max_retries the max # of retries allowed for a packet *
  70. * (-1 means retry forever, based on this parameter) *
  71. * timeout the max amount of time before we give up on a packet *
  72. * (-1 means retry forever, based on this parameter) *
  73. * extralen max size of app-specific extra bytes (optional) *
  74. * *
  75. * OUTPUT: *
  76. * none. *
  77. * *
  78. * WARNINGS: *
  79. * none. *
  80. * *
  81. * HISTORY: *
  82. * 12/20/1994 BR : Created. *
  83. *=========================================================================*/
  84. ConnectionClass::ConnectionClass (int numsend, int numreceive,
  85. int maxlen, unsigned short magicnum, unsigned long retry_delta,
  86. unsigned long max_retries, unsigned long timeout, int extralen)
  87. {
  88. /*------------------------------------------------------------------------
  89. Compute our maximum packet length
  90. ------------------------------------------------------------------------*/
  91. MaxPacketLen = maxlen + sizeof(CommHeaderType);
  92. /*------------------------------------------------------------------------
  93. Assign the magic number
  94. ------------------------------------------------------------------------*/
  95. MagicNum = magicnum;
  96. /*------------------------------------------------------------------------
  97. Initialize the retry time. This is the time that t2 - t1 must be greater
  98. than before a retry will occur.
  99. ------------------------------------------------------------------------*/
  100. RetryDelta = retry_delta;
  101. /*------------------------------------------------------------------------
  102. Set the maximum allowable retries.
  103. ------------------------------------------------------------------------*/
  104. MaxRetries = max_retries;
  105. /*------------------------------------------------------------------------
  106. Set the timeout for this connection.
  107. ------------------------------------------------------------------------*/
  108. Timeout = timeout;
  109. /*------------------------------------------------------------------------
  110. Allocate the packet staging buffer. This will be used to
  111. ------------------------------------------------------------------------*/
  112. PacketBuf = new char[ MaxPacketLen ];
  113. /*------------------------------------------------------------------------
  114. Allocate the packet Queue. This will store incoming packets (placed there
  115. by Receive_Packet), and outgoing packets (placed there by Send_Packet).
  116. It can optionally store "extra" bytes, which are stored along with each
  117. packet, but aren't transmitted as part of the packet. If 'extralen'
  118. is 0, the CommBufferClass ignores this parameter.
  119. ------------------------------------------------------------------------*/
  120. Queue = new CommBufferClass (numsend, numreceive, MaxPacketLen, extralen);
  121. } /* end of ConnectionClass */
  122. /***************************************************************************
  123. * ConnectionClass::~ConnectionClass -- class destructor *
  124. * *
  125. * INPUT: *
  126. * none. *
  127. * *
  128. * OUTPUT: *
  129. * none. *
  130. * *
  131. * WARNINGS: *
  132. * none. *
  133. * *
  134. * HISTORY: *
  135. * 12/20/1994 BR : Created. *
  136. *=========================================================================*/
  137. ConnectionClass::~ConnectionClass ()
  138. {
  139. /*------------------------------------------------------------------------
  140. Free memory.
  141. ------------------------------------------------------------------------*/
  142. delete [] PacketBuf;
  143. delete Queue;
  144. } /* end of ~ConnectionClass */
  145. /***************************************************************************
  146. * ConnectionClass::Init -- Initializes connection queue to empty *
  147. * *
  148. * INPUT: *
  149. * none. *
  150. * *
  151. * OUTPUT: *
  152. * none. *
  153. * *
  154. * WARNINGS: *
  155. * none. *
  156. * *
  157. * HISTORY: *
  158. * 12/20/1994 BR : Created. *
  159. *=========================================================================*/
  160. void ConnectionClass::Init (void)
  161. {
  162. NumRecNoAck = 0;
  163. NumRecAck = 0;
  164. NumSendNoAck = 0;
  165. NumSendAck = 0;
  166. LastSeqID = 0xffffffff;
  167. LastReadID = 0xffffffff;
  168. Queue->Init();
  169. } /* end of Init */
  170. /***************************************************************************
  171. * ConnectionClass::Send_Packet -- adds a packet to the send queue *
  172. * *
  173. * This routine prefixes the given buffer with a CommHeaderType and *
  174. * queues the resulting packet into the Send Queue. (It's actually the *
  175. * Service() routine that handles the hardware-dependent Send of the data).*
  176. * The packet's MagicNumber, Code, and PacketID are set here. *
  177. * *
  178. * INPUT: *
  179. * buf buffer to send *
  180. * buflen length of buffer *
  181. * ack_req 1 = ACK is required for this packet; 0 = isn't *
  182. * *
  183. * OUTPUT: *
  184. * 1 = packet was queue'd OK, 0 = wasn't *
  185. * *
  186. * WARNINGS: *
  187. * none. *
  188. * *
  189. * HISTORY: *
  190. * 12/20/1994 BR : Created. *
  191. *=========================================================================*/
  192. int ConnectionClass::Send_Packet (void * buf, int buflen, int ack_req)
  193. {
  194. /*------------------------------------------------------------------------
  195. Set the magic # for the packet
  196. ------------------------------------------------------------------------*/
  197. ((CommHeaderType *)PacketBuf)->MagicNumber = MagicNum;
  198. /*------------------------------------------------------------------------
  199. Set the packet Code: DATA_ACK if it requires an ACK, NOACK if it doesn't
  200. Set the packet ID to the appropriate counter value.
  201. ------------------------------------------------------------------------*/
  202. if (ack_req) {
  203. ((CommHeaderType *)PacketBuf)->Code = PACKET_DATA_ACK;
  204. ((CommHeaderType *)PacketBuf)->PacketID = NumSendAck;
  205. }
  206. else {
  207. ((CommHeaderType *)PacketBuf)->Code = PACKET_DATA_NOACK;
  208. ((CommHeaderType *)PacketBuf)->PacketID = NumSendNoAck;
  209. }
  210. /*------------------------------------------------------------------------
  211. Now build the packet
  212. ------------------------------------------------------------------------*/
  213. memcpy(PacketBuf + sizeof(CommHeaderType), buf, buflen);
  214. /*------------------------------------------------------------------------
  215. Add it to the queue; don't add any extra data with it.
  216. ------------------------------------------------------------------------*/
  217. if (Queue->Queue_Send(PacketBuf,buflen + sizeof(CommHeaderType), NULL, 0)) {
  218. if (ack_req) {
  219. NumSendAck++;
  220. }
  221. else {
  222. NumSendNoAck++;
  223. }
  224. return(1);
  225. }
  226. else {
  227. return(0);
  228. }
  229. } /* end of Send_Packet */
  230. /***************************************************************************
  231. * ConnectionClass::Receive_Packet -- adds packet to receive queue *
  232. * *
  233. * INPUT: *
  234. * buf buffer to process (already includes CommHeaderType) *
  235. * buflen length of buffer to process *
  236. * *
  237. * OUTPUT: *
  238. * 1 = packet was processed OK, 0 = error *
  239. * *
  240. * WARNINGS: *
  241. * none. *
  242. * *
  243. * HISTORY: *
  244. * 12/20/1994 BR : Created. *
  245. *=========================================================================*/
  246. int ConnectionClass::Receive_Packet (void * buf, int buflen)
  247. {
  248. CommHeaderType *packet; // ptr to packet header
  249. SendQueueType *send_entry; // ptr to send entry header
  250. ReceiveQueueType *rec_entry; // ptr to recv entry header
  251. CommHeaderType *entry_data; // ptr to queue entry data
  252. CommHeaderType ackpacket; // ACK packet to send
  253. int i;
  254. int save_packet = 1; // 0 = this is a resend
  255. int found;
  256. /*------------------------------------------------------------------------
  257. Check the magic #
  258. ------------------------------------------------------------------------*/
  259. packet = (CommHeaderType *)buf;
  260. if (packet->MagicNumber != MagicNum) {
  261. return(0);
  262. }
  263. /*------------------------------------------------------------------------
  264. Handle an incoming ACK
  265. ------------------------------------------------------------------------*/
  266. if (packet->Code == PACKET_ACK) {
  267. for (i = 0; i < Queue->Num_Send(); i++) {
  268. /*..................................................................
  269. Get queue entry ptr
  270. ..................................................................*/
  271. send_entry = Queue->Get_Send(i);
  272. /*..................................................................
  273. If ptr is valid, get ptr to its data
  274. ..................................................................*/
  275. if (send_entry != NULL) {
  276. entry_data = (CommHeaderType *)send_entry->Buffer;
  277. /*...............................................................
  278. If ACK is for this entry, mark it
  279. ...............................................................*/
  280. if (packet->PacketID==entry_data->PacketID &&
  281. entry_data->Code == PACKET_DATA_ACK) {
  282. send_entry->IsACK = 1;
  283. break;
  284. }
  285. }
  286. }
  287. return(1);
  288. }
  289. /*------------------------------------------------------------------------
  290. Handle an incoming PACKET_DATA_NOACK packet
  291. ------------------------------------------------------------------------*/
  292. else if (packet->Code == PACKET_DATA_NOACK) {
  293. /*.....................................................................
  294. If there's only one slot left, don't tie up the queue with this packet
  295. .....................................................................*/
  296. if (Queue->Max_Receive() - Queue->Num_Receive() <= 1) {
  297. return(0);
  298. }
  299. /*.....................................................................
  300. Error if we can't queue the packet
  301. .....................................................................*/
  302. if (!Queue->Queue_Receive (buf, buflen, NULL, 0)) {
  303. return(0);
  304. }
  305. NumRecNoAck++;
  306. return(1);
  307. }
  308. /*------------------------------------------------------------------------
  309. Handle an incoming PACKET_DATA_ACK packet
  310. ------------------------------------------------------------------------*/
  311. else if (packet->Code == PACKET_DATA_ACK) {
  312. /*.....................................................................
  313. If this is a packet requires an ACK, and it's ID is older than our
  314. "oldest" ID, we know it's a resend; send an ACK, but don't queue it
  315. .....................................................................*/
  316. if (packet->PacketID <= LastSeqID && LastSeqID != 0xffffffff) {
  317. save_packet = 0;
  318. }
  319. /*.....................................................................
  320. Otherwise, scan the queue for this entry; if it's found, it's a
  321. resend, so don't save it.
  322. .....................................................................*/
  323. else {
  324. save_packet = 1;
  325. for (i = 0; i < Queue->Num_Receive(); i++) {
  326. rec_entry = Queue->Get_Receive(i);
  327. if (rec_entry) {
  328. entry_data = (CommHeaderType *)rec_entry->Buffer;
  329. /*...........................................................
  330. Packet is found; it's a resend
  331. ...........................................................*/
  332. if (entry_data->Code == PACKET_DATA_ACK &&
  333. entry_data->PacketID == packet->PacketID) {
  334. save_packet = 0;
  335. break;
  336. }
  337. }
  338. }
  339. } /* end of scan for resend */
  340. /*.....................................................................
  341. Queue the packet & update our LastSeqID value.
  342. .....................................................................*/
  343. if (save_packet) {
  344. /*..................................................................
  345. If there's only one slot left, make sure we only put a packet in it
  346. if this packet will let us increment our LastSeqID; otherwise, we'll
  347. get stuck, forever unable to increment LastSeqID.
  348. ..................................................................*/
  349. if (Queue->Max_Receive() - Queue->Num_Receive() <= 1) {
  350. if (packet->PacketID != (LastSeqID + 1) ) {
  351. return(0);
  352. }
  353. }
  354. /*..................................................................
  355. If we can't queue the packet, return; don't send an ACK.
  356. ..................................................................*/
  357. if (!Queue->Queue_Receive (buf, buflen, NULL, 0)) {
  358. return(0);
  359. }
  360. NumRecAck++;
  361. /*..................................................................
  362. Update our LastSeqID value if we can. Anything less than LastSeqID
  363. we'll know is a resend.
  364. ..................................................................*/
  365. if (packet->PacketID == (LastSeqID + 1)) {
  366. LastSeqID = packet->PacketID;
  367. /*...............................................................
  368. Now that we have a new 'LastSeqID', search our Queue to see if
  369. the next ID is there; if so, keep checking for the next one;
  370. break only when the next one isn't found. This forces
  371. LastSeqID to be the largest possible value.
  372. ...............................................................*/
  373. do {
  374. found = 0;
  375. for (i = 0; i < Queue->Num_Receive(); i++) {
  376. rec_entry = Queue->Get_Receive(i);
  377. if (rec_entry) {
  378. entry_data = (CommHeaderType *)rec_entry->Buffer;
  379. /*......................................................
  380. Entry is found
  381. ......................................................*/
  382. if (entry_data->Code == PACKET_DATA_ACK &&
  383. entry_data->PacketID == (LastSeqID + 1)) {
  384. LastSeqID = entry_data->PacketID;
  385. found = 1;
  386. break;
  387. }
  388. }
  389. }
  390. } while (found);
  391. }
  392. } /* end of save packet */
  393. /*.....................................................................
  394. Send an ACK, regardless of whether this was a resend or not.
  395. .....................................................................*/
  396. ackpacket.MagicNumber = Magic_Num();
  397. ackpacket.Code = PACKET_ACK;
  398. ackpacket.PacketID = packet->PacketID;
  399. Send ((char *)&ackpacket, sizeof(CommHeaderType), NULL, 0);
  400. return(1);
  401. }
  402. return(0);
  403. } /* end of Receive_Packet */
  404. /***************************************************************************
  405. * ConnectionClass::Get_Packet -- gets a packet from receive queue *
  406. * *
  407. * INPUT: *
  408. * buf location to store buffer *
  409. * buflen filled in with length of 'buf' *
  410. * *
  411. * OUTPUT: *
  412. * 1 = packet was read, 0 = wasn't *
  413. * *
  414. * WARNINGS: *
  415. * none. *
  416. * *
  417. * HISTORY: *
  418. * 12/20/1994 BR : Created. *
  419. *=========================================================================*/
  420. int ConnectionClass::Get_Packet (void * buf, int *buflen)
  421. {
  422. ReceiveQueueType *rec_entry; // ptr to receive entry header
  423. int packetlen; // size of received packet
  424. CommHeaderType *entry_data;
  425. int i;
  426. /*------------------------------------------------------------------------
  427. Ensure that we read the packets in order. LastReadID is the ID of the
  428. last PACKET_DATA_ACK packet we read.
  429. ------------------------------------------------------------------------*/
  430. for (i = 0; i < Queue->Num_Receive(); i++) {
  431. rec_entry = Queue->Get_Receive(i);
  432. /*.....................................................................
  433. Only read this entry if it hasn't been yet
  434. .....................................................................*/
  435. if (rec_entry && rec_entry->IsRead==0) {
  436. entry_data = (CommHeaderType *)rec_entry->Buffer;
  437. /*..................................................................
  438. If this is a DATA_ACK packet, its ID must be one greater than
  439. the last one we read.
  440. ..................................................................*/
  441. if ( (entry_data->Code == PACKET_DATA_ACK) &&
  442. (entry_data->PacketID == (LastReadID + 1))) {
  443. LastReadID = entry_data->PacketID;
  444. rec_entry->IsRead = 1;
  445. packetlen = rec_entry->BufLen - sizeof(CommHeaderType);
  446. if (packetlen > 0) {
  447. memcpy(buf, rec_entry->Buffer + sizeof(CommHeaderType),
  448. packetlen);
  449. }
  450. (*buflen) = packetlen;
  451. return(1);
  452. }
  453. /*..................................................................
  454. If this is a DATA_NOACK packet, who cares what the ID is?
  455. ..................................................................*/
  456. else if (entry_data->Code == PACKET_DATA_NOACK) {
  457. rec_entry->IsRead = 1;
  458. packetlen = rec_entry->BufLen - sizeof(CommHeaderType);
  459. if (packetlen > 0) {
  460. memcpy(buf, rec_entry->Buffer + sizeof(CommHeaderType),
  461. packetlen);
  462. }
  463. (*buflen) = packetlen;
  464. return(1);
  465. }
  466. }
  467. }
  468. return(0);
  469. } /* end of Get_Packet */
  470. /***************************************************************************
  471. * ConnectionClass::Service -- main polling routine; services packets *
  472. * *
  473. * INPUT: *
  474. * none. *
  475. * *
  476. * OUTPUT: *
  477. * 1 = OK, 0 = error (connection is broken!) *
  478. * *
  479. * WARNINGS: *
  480. * none. *
  481. * *
  482. * HISTORY: *
  483. * 12/20/1994 BR : Created. *
  484. *=========================================================================*/
  485. int ConnectionClass::Service (void)
  486. {
  487. /*------------------------------------------------------------------------
  488. Service the Send Queue: This [re]sends packets in the Send Queue which
  489. haven't been ACK'd yet, and if their retry timeout has expired, and
  490. updates the FirstTime, LastTime & SendCount values in the Queue entry.
  491. Entries that have been ACK'd should be removed.
  492. Service the Receive Queue: This sends ACKs for packets that haven't
  493. been ACK'd yet. Entries that the app has read, and have been ACK'd,
  494. should be removed.
  495. ------------------------------------------------------------------------*/
  496. if ( Service_Send_Queue() && Service_Receive_Queue() ) {
  497. return(1);
  498. }
  499. else {
  500. return(0);
  501. }
  502. } /* end of Service */
  503. /***************************************************************************
  504. * ConnectionClass::Service_Send_Queue -- services the send queue *
  505. * *
  506. * INPUT: *
  507. * none. *
  508. * *
  509. * OUTPUT: *
  510. * 1 = OK, 0 = error *
  511. * *
  512. * WARNINGS: *
  513. * none. *
  514. * *
  515. * HISTORY: *
  516. * 12/20/1994 BR : Created. *
  517. *=========================================================================*/
  518. int ConnectionClass::Service_Send_Queue (void)
  519. {
  520. int i;
  521. int num_entries;
  522. SendQueueType *send_entry; // ptr to send queue entry
  523. CommHeaderType *packet_hdr; // packet header
  524. unsigned long curtime; // current time
  525. int bad_conn = 0;
  526. /*------------------------------------------------------------------------
  527. Remove any ACK'd packets from the queue
  528. ------------------------------------------------------------------------*/
  529. for (i = 0; i < Queue->Num_Send(); i++) {
  530. /*.....................................................................
  531. Get this queue entry
  532. .....................................................................*/
  533. send_entry = Queue->Get_Send(i);
  534. /*.....................................................................
  535. If ACK has been received, unqueue it
  536. .....................................................................*/
  537. if (send_entry->IsACK) {
  538. /*..................................................................
  539. Update this queue's response time
  540. ..................................................................*/
  541. packet_hdr = (CommHeaderType *)send_entry->Buffer;
  542. if (packet_hdr->Code == PACKET_DATA_ACK) {
  543. Queue->Add_Delay(Time() - send_entry->FirstTime);
  544. }
  545. /*..................................................................
  546. Unqueue the packet
  547. ..................................................................*/
  548. Queue->UnQueue_Send(NULL,NULL,i,NULL,NULL);
  549. i--;
  550. }
  551. }
  552. /*------------------------------------------------------------------------
  553. Loop through all entries in the Send queue. [Re]Send any entries that
  554. need it.
  555. ------------------------------------------------------------------------*/
  556. num_entries = Queue->Num_Send();
  557. for (i = 0; i < num_entries; i++) {
  558. send_entry = Queue->Get_Send(i);
  559. if (send_entry->IsACK) {
  560. continue;
  561. }
  562. /*.....................................................................
  563. Only send the message if time has elapsed. (The message's Time
  564. fields are init'd to 0 when a message is queue'd or unqueue'd, so the
  565. first time through, the delta time will appear large.)
  566. .....................................................................*/
  567. curtime = Time();
  568. if (curtime - send_entry->LastTime > RetryDelta) {
  569. /*..................................................................
  570. Send the message
  571. ..................................................................*/
  572. Send (send_entry->Buffer, send_entry->BufLen, send_entry->ExtraBuffer,
  573. send_entry->ExtraLen);
  574. /*..................................................................
  575. Fill in Time fields
  576. ..................................................................*/
  577. send_entry->LastTime = curtime;
  578. if (send_entry->SendCount==0) {
  579. send_entry->FirstTime = curtime;
  580. /*...............................................................
  581. If this is the 1st time we're sending this packet, and it doesn't
  582. require an ACK, mark it as ACK'd; then, the next time through,
  583. it will just be removed from the queue.
  584. ...............................................................*/
  585. packet_hdr = (CommHeaderType *)send_entry->Buffer;
  586. if (packet_hdr->Code == PACKET_DATA_NOACK) {
  587. send_entry->IsACK = 1;
  588. }
  589. }
  590. /*..................................................................
  591. Update SendCount
  592. ..................................................................*/
  593. send_entry->SendCount++;
  594. /*..................................................................
  595. Perform error detection, based on either MaxRetries or Timeout
  596. ..................................................................*/
  597. if (MaxRetries != -1 && send_entry->SendCount > MaxRetries) {
  598. bad_conn = 1;
  599. }
  600. if (Timeout != -1 &&
  601. (send_entry->LastTime - send_entry->FirstTime) > Timeout) {
  602. bad_conn = 1;
  603. }
  604. }
  605. }
  606. /*------------------------------------------------------------------------
  607. If the connection is going bad, return an error
  608. ------------------------------------------------------------------------*/
  609. if (bad_conn) {
  610. return(0);
  611. }
  612. else {
  613. return(1);
  614. }
  615. } /* end of Service_Send_Queue */
  616. /***************************************************************************
  617. * ConnectionClass::Service_Receive_Queue -- services receive queue *
  618. * *
  619. * INPUT: *
  620. * none. *
  621. * *
  622. * OUTPUT: *
  623. * 1 = OK, 0 = error *
  624. * *
  625. * WARNINGS: *
  626. * none. *
  627. * *
  628. * HISTORY: *
  629. * 12/20/1994 BR : Created. *
  630. *=========================================================================*/
  631. int ConnectionClass::Service_Receive_Queue (void)
  632. {
  633. ReceiveQueueType *rec_entry; // ptr to receive entry header
  634. CommHeaderType *packet_hdr; // packet header
  635. int i;
  636. /*------------------------------------------------------------------------
  637. Remove all dead packets.
  638. PACKET_DATA_NOACK: if it's been read, throw it away.
  639. PACKET_DATA_ACK: if it's been read, and its ID is older than LastSeqID,
  640. throw it away.
  641. ------------------------------------------------------------------------*/
  642. for (i = 0; i < Queue->Num_Receive(); i++) {
  643. rec_entry = Queue->Get_Receive(i);
  644. if (rec_entry->IsRead) {
  645. packet_hdr = (CommHeaderType *)(rec_entry->Buffer);
  646. if (packet_hdr->Code == PACKET_DATA_NOACK) {
  647. Queue->UnQueue_Receive(NULL,NULL,i,NULL,NULL);
  648. i--;
  649. }
  650. else if (packet_hdr->PacketID < LastSeqID) {
  651. Queue->UnQueue_Receive(NULL,NULL,i,NULL,NULL);
  652. i--;
  653. }
  654. }
  655. }
  656. return(1);
  657. } /* end of Service_Receive_Queue */
  658. /***************************************************************************
  659. * ConnectionClass::Time -- gets current time *
  660. * *
  661. * INPUT: *
  662. * *
  663. * OUTPUT: *
  664. * none. *
  665. * *
  666. * WARNINGS: *
  667. * none. *
  668. * *
  669. * HISTORY: *
  670. * 12/20/1994 BR : Created. *
  671. *=========================================================================*/
  672. unsigned long ConnectionClass::Time (void)
  673. {
  674. static struct timeb mytime; // DOS time
  675. unsigned long msec;
  676. #ifdef WWLIB32_H
  677. /*------------------------------------------------------------------------
  678. If the Westwood timer system has been activated, use TickCount's value
  679. ------------------------------------------------------------------------*/
  680. if (TimerSystemOn) {
  681. return(TickCount); // Westwood Library time
  682. }
  683. /*------------------------------------------------------------------------
  684. Otherwise, use the DOS timer
  685. ------------------------------------------------------------------------*/
  686. else {
  687. ftime(&mytime);
  688. msec = (unsigned long)mytime.time * 1000L + (unsigned long)mytime.millitm;
  689. return((msec / 100) * 6);
  690. }
  691. #else
  692. /*------------------------------------------------------------------------
  693. If the Westwood library isn't being used, use the DOS timer.
  694. ------------------------------------------------------------------------*/
  695. ftime(&mytime);
  696. msec = (unsigned long)mytime.time * 1000L + (unsigned long)mytime.millitm;
  697. return((msec / 100) * 6);
  698. #endif
  699. } /* end of Time */
  700. /***************************************************************************
  701. * ConnectionClass::Command_Name -- returns name for given packet command *
  702. * *
  703. * INPUT: *
  704. * command packet Command value to get name for *
  705. * *
  706. * OUTPUT: *
  707. * ptr to command name, NULL if invalid *
  708. * *
  709. * WARNINGS: *
  710. * none. *
  711. * *
  712. * HISTORY: *
  713. * 05/31/1995 BRR : Created. *
  714. *=========================================================================*/
  715. char *ConnectionClass::Command_Name(int command)
  716. {
  717. if (command >= 0 && command < PACKET_COUNT) {
  718. return(Commands[command]);
  719. }
  720. else {
  721. return(NULL);
  722. }
  723. } /* end of Command_Name */
  724. /************************** end of connect.cpp *****************************/