CONNECT.CPP.BAK 35 KB

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