OERRCTRL.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. // Filename : OERRCTRL.H
  21. // Descrition : Error control
  22. #include <OERRCTRL.h>
  23. #include <ODPLAY.h>
  24. #include <OIMMPLAY.h>
  25. #include <CRC.h>
  26. #include <ALL.h>
  27. #define DEBUG_LOG_LOCAL 1
  28. #include <OLOG.h>
  29. // ---------- define constant -----------//
  30. // 0 don't display log, 1 display exception, 2 display more detail, 3 display all
  31. #define DEBUG_LOG_LEVEL 1
  32. const int SHORT_TIME_OUT = 100;
  33. const int TIME_OUT = 2000; // 2 sec
  34. const int CONNECT_LOST_TIME = 20000; // 20 sec
  35. static String debugStr;
  36. void ErrorControl::init(MultiPlayerType *mp, char ecPlayerId )
  37. {
  38. // ---------- initialize dp_id array ---------- //
  39. memset(dp_id, 0, sizeof(dp_id) );
  40. connecting_player_count = 0;
  41. self_ec_player_id = ecPlayerId;
  42. mp_ptr = mp;
  43. // ---------- initialize head and tail of queues ---------- //
  44. send_head = send_tail = 0;
  45. recv_head = recv_tail = 0;
  46. }
  47. void ErrorControl::deinit()
  48. {
  49. }
  50. void ErrorControl::set_dp_id(char ecPlayerId, long unsigned int dpPlayerId )
  51. {
  52. if( ecPlayerId != self_ec_player_id )
  53. {
  54. err_when( ecPlayerId < 1 || ecPlayerId > MAX_PLAYER );
  55. dp_id[ecPlayerId-1] = dpPlayerId;
  56. wait_to_receive[ecPlayerId-1] = 0;
  57. memset(recv_flag[ecPlayerId-1], 0, MAX_QUEUE );
  58. // next_send[ecPlayerId-1] = 0;
  59. // next_ack_send[ecPlayerId-1] = 0;
  60. // retrans_state[ecPlayerId-1] = 0;
  61. // ------- update connecting_player_count --------//
  62. connecting_player_count = 0;
  63. for(int p = 0; p < MAX_PLAYER; ++p)
  64. if( dp_id[p] )
  65. connecting_player_count++;
  66. }
  67. }
  68. // return ec_player_id, 0 for not found (can't found own dpPlayerId)
  69. char ErrorControl::get_ec_player_id( long unsigned int dpPlayerId )
  70. {
  71. if( dpPlayerId == BROADCAST_PID || dpPlayerId == 0)
  72. return 0;
  73. for( char ecPlayerId = 1; ecPlayerId <= MAX_PLAYER; ++ecPlayerId )
  74. {
  75. if( dpPlayerId == dp_id[ecPlayerId-1] )
  76. return ecPlayerId;
  77. }
  78. return 0;
  79. }
  80. // return 1 on success, -1 if queue is_full, 0 for other failure
  81. int ErrorControl::send(char ecPlayerId, void *dataPtr, long unsigned int dataLen)
  82. {
  83. if( connecting_player_count == 0)
  84. return 1;
  85. if( send_queue_space() < MAX_QUEUE/2)
  86. {
  87. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 1)
  88. DEBUG_LOG("ec_remote.send() fail, buffer half full");
  89. #endif
  90. return -1;
  91. }
  92. int frameId = en_send_queue();
  93. if( frameId < 0)
  94. {
  95. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 1)
  96. DEBUG_LOG("ec_remote.send() fail, buffer full");
  97. #endif
  98. return frameId;
  99. }
  100. else
  101. {
  102. // -------- add buffer to queue --------- //
  103. err_when( frameId >= MAX_QUEUE );
  104. VLenQueue &sq = send_queue[frameId];
  105. sq.clear();
  106. char *ecMsg = sq.reserve( sizeof(EcMsgHeader) + dataLen + CRC_LEN );
  107. ((EcMsgHeader *)ecMsg)->init( FIRST_SEND, self_ec_player_id, frameId );
  108. memcpy( ecMsg + sizeof(EcMsgHeader), dataPtr, dataLen );
  109. *((CRC_TYPE *) (ecMsg + sizeof(EcMsgHeader) + dataLen) ) = crc8((unsigned char *)ecMsg, sizeof(EcMsgHeader) + dataLen);
  110. // ------- clear all ack flags of that frame --------//
  111. PID_TYPE toDPid = BROADCAST_PID;
  112. clear_ack( frameId );
  113. if( ecPlayerId != 0)
  114. {
  115. toDPid = dp_id[ecPlayerId];
  116. err_when(toDPid == BROADCAST_PID);
  117. for(char p = 1; p <= MAX_PLAYER; ++p)
  118. if( p != ecPlayerId )
  119. set_ack(p, frameId);
  120. }
  121. // ------- try to send the data for the first time ------- //
  122. if( mp_ptr->send(toDPid, sq.queue_buf, sq.length()) )
  123. {
  124. // mark send time
  125. mark_send_time(frameId, TIME_OUT );
  126. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 3)
  127. debugStr = "ec_remote.send() successful, frame ";
  128. debugStr += frameId;
  129. DEBUG_LOG(debugStr);
  130. #endif
  131. // mark the func_id of the message RE_SEND
  132. ((EcMsgHeader *)ecMsg)->func_id = RE_SEND;
  133. // recalculate CRC
  134. *((CRC_TYPE *) (ecMsg + sizeof(EcMsgHeader) + dataLen) ) = crc8((unsigned char *)ecMsg, sizeof(EcMsgHeader) + dataLen);
  135. }
  136. else
  137. {
  138. // mark send time, mark a shorter time
  139. mark_send_time(frameId, SHORT_TIME_OUT);
  140. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 2)
  141. debugStr = "ec_remote.send() fail, frame ";
  142. debugStr += frameId;
  143. DEBUG_LOG(debugStr);
  144. #endif
  145. // still return true to sender, as it will be re-send later
  146. }
  147. }
  148. mp_ptr->after_send();
  149. return 1;
  150. }
  151. char *ErrorControl::receive(char *sendEcPlayerId, long unsigned int *dataLen)
  152. {
  153. // ----- draw the head of recv_queue ----- //
  154. if( is_recv_empty() )
  155. {
  156. return NULL;
  157. }
  158. else
  159. {
  160. char *dataPtr = receive_queue[recv_head].queue_buf;
  161. DWORD len = receive_queue[recv_head].length();
  162. err_when( len < sizeof(EcMsgHeader) + CRC_LEN);
  163. if( sendEcPlayerId )
  164. *sendEcPlayerId = ((EcMsgHeader *)dataPtr)->sender_id;
  165. if( dataLen )
  166. *dataLen = len - sizeof(EcMsgHeader) - CRC_LEN;
  167. return dataPtr + sizeof(EcMsgHeader);
  168. }
  169. }
  170. int ErrorControl::is_player_valid(char ecPlayerId)
  171. {
  172. return dp_id[ecPlayerId-1] != 0 || ecPlayerId == self_ec_player_id;
  173. }
  174. void ErrorControl::set_player_lost(char ecPlayerId)
  175. {
  176. dp_id[ecPlayerId-1] = 0;
  177. clear_acked_frame(); // some send_queue message may be waiting this player's ack
  178. }
  179. void ErrorControl::yield()
  180. {
  181. // -------- receive any frame from dplay -----------//
  182. char *recvPtr;
  183. DWORD recvLen;
  184. PID_TYPE from, to;
  185. static int simError = 1;
  186. // check any player lost
  187. // ##### begin Gilbert 2/5 ########//
  188. int sysMsgCount;
  189. int p;
  190. // detect any player lost, detected previous mp_ptr->send
  191. for( p = 1; p <= MAX_PLAYER; ++p)
  192. {
  193. if( dp_id[p-1] && !mp_ptr->is_player_connecting(dp_id[p-1]) )
  194. {
  195. set_player_lost(p);
  196. connecting_player_count--;
  197. }
  198. }
  199. mp_ptr->before_receive();
  200. while( (recvPtr = mp_ptr->receive(&from, &to, &recvLen, &sysMsgCount)) != NULL
  201. || sysMsgCount != 0)
  202. {
  203. // -------- detect any player lost ---------//
  204. if( sysMsgCount )
  205. {
  206. for( p = 1; p <= MAX_PLAYER; ++p)
  207. {
  208. if( dp_id[p-1] && !mp_ptr->is_player_connecting(dp_id[p-1]) )
  209. {
  210. set_player_lost(p);
  211. connecting_player_count--;
  212. }
  213. }
  214. }
  215. if( !recvPtr )
  216. break; // only received system message from direct play
  217. // ##### end Gilbert 2/5 ########//
  218. // -------- receive the message ----------//
  219. #ifdef DEBUG
  220. // simulate crc error
  221. // if( ++simError >= 10)
  222. // simError = 0;
  223. #endif
  224. if( simError && !crc8((unsigned char *)recvPtr, recvLen))
  225. {
  226. // crc correct
  227. EcMsgHeader ecMsg = *(EcMsgHeader *)recvPtr;
  228. switch( ecMsg.func_id )
  229. {
  230. case FIRST_SEND:
  231. // accept except frameId is wait_to_receive -1 or recv_flag is set
  232. if( is_waiting_receive( ecMsg.sender_id, ecMsg.frame_id ) )
  233. {
  234. err_when( ecMsg.sender_id <= 0 || ecMsg.sender_id > MAX_PLAYER);
  235. #if ( defined(DEBUG) && DEBUG_LOG_LEVEL >= 3 )
  236. debugStr = "ec_remote : FIRST_SEND received, from:";
  237. debugStr += ecMsg.sender_id;
  238. debugStr += " frame:";
  239. debugStr += ecMsg.frame_id;
  240. debugStr += " accepted";
  241. DEBUG_LOG(debugStr);
  242. #endif
  243. if( !is_recv_full() )
  244. {
  245. if( dp_id[ecMsg.sender_id-1] )
  246. {
  247. // mark recv_flag
  248. set_recv_flag(ecMsg.sender_id, ecMsg.frame_id);
  249. // send ACK
  250. char replyMsg[sizeof(EcMsgHeader) + CRC_LEN];
  251. ((EcMsgHeader *)replyMsg)->init(ACKNOW, self_ec_player_id, ecMsg.frame_id);
  252. *((CRC_TYPE *)(replyMsg + sizeof(EcMsgHeader))) = crc8((unsigned char *)replyMsg, sizeof(EcMsgHeader));
  253. mp_ptr->send( dp_id[ecMsg.sender_id-1], replyMsg, sizeof(replyMsg) );
  254. if( ecMsg.frame_id == wait_to_receive[ecMsg.sender_id-1] )
  255. {
  256. // clear recv_flag, until it is zero
  257. char &scanFrame = wait_to_receive[ecMsg.sender_id-1];
  258. for( ; recv_flag[ecMsg.sender_id-1][scanFrame]; inc_frame_id(scanFrame) )
  259. clear_recv_flag( ecMsg.sender_id, prev_frame_id(scanFrame) );
  260. }
  261. }
  262. // append the queue to receive queue
  263. en_recv_queue(recvPtr, recvLen);
  264. }
  265. else
  266. {
  267. // drop the message if the receive queue is full
  268. #if ( defined(DEBUG) && DEBUG_LOG_LEVEL >= 2 )
  269. DEBUG_LOG("ec_remote : but receive_queue is full, discard message");
  270. #endif
  271. }
  272. }
  273. else
  274. {
  275. #if ( defined(DEBUG) && DEBUG_LOG_LEVEL >= 2 )
  276. debugStr = "ec_remote : FIRST_SEND received, from:";
  277. debugStr += ecMsg.sender_id;
  278. debugStr += " frame:";
  279. debugStr += ecMsg.frame_id;
  280. debugStr += " discarded";
  281. DEBUG_LOG(debugStr);
  282. #endif
  283. // some frame before are missing, wait resend
  284. // discard the frame, but reply, for the sender not to
  285. // send the frame again
  286. if( dp_id[ecMsg.sender_id-1] )
  287. {
  288. // send ACK
  289. char replyMsg[sizeof(EcMsgHeader) + CRC_LEN];
  290. ((EcMsgHeader *)replyMsg)->init(ACKNOW, self_ec_player_id, ecMsg.frame_id);
  291. *((CRC_TYPE *)(replyMsg + sizeof(EcMsgHeader))) = crc8((unsigned char *)replyMsg, sizeof(EcMsgHeader));
  292. mp_ptr->send( dp_id[ecMsg.sender_id-1], replyMsg, sizeof(replyMsg) );
  293. }
  294. }
  295. break;
  296. case RE_SEND:
  297. // accept except frameId is wait_to_receive -1 or recv_flag is set
  298. if( is_waiting_receive( ecMsg.sender_id, ecMsg.frame_id ) )
  299. {
  300. #if ( defined(DEBUG) && DEBUG_LOG_LEVEL >= 2 )
  301. debugStr = "ec_remote : RE_SEND received, from:";
  302. debugStr += ecMsg.sender_id;
  303. debugStr += " frame:";
  304. debugStr += ecMsg.frame_id;
  305. debugStr += " accepted";
  306. DEBUG_LOG(debugStr);
  307. #endif
  308. err_when( ecMsg.sender_id <= 0 || ecMsg.sender_id > MAX_PLAYER);
  309. if( !is_recv_full() )
  310. {
  311. if( dp_id[ecMsg.sender_id-1] )
  312. {
  313. // mark recv_flag
  314. set_recv_flag(ecMsg.sender_id, ecMsg.frame_id);
  315. // send ACK
  316. char replyMsg[sizeof(EcMsgHeader) + CRC_LEN];
  317. ((EcMsgHeader *)replyMsg)->init(ACKNOW, self_ec_player_id, ecMsg.frame_id);
  318. *((CRC_TYPE *)(replyMsg + sizeof(EcMsgHeader))) = crc8((unsigned char *)replyMsg, sizeof(EcMsgHeader));
  319. mp_ptr->send( dp_id[ecMsg.sender_id-1], replyMsg, sizeof(replyMsg) );
  320. if( ecMsg.frame_id == wait_to_receive[ecMsg.sender_id-1] )
  321. {
  322. // clear recv_flag, until it is zero
  323. char &scanFrame = wait_to_receive[ecMsg.sender_id-1];
  324. for( ; recv_flag[ecMsg.sender_id-1][scanFrame]; inc_frame_id(scanFrame) )
  325. clear_recv_flag( ecMsg.sender_id, prev_frame_id(scanFrame) );
  326. }
  327. }
  328. // append the queue to receive queue
  329. en_recv_queue(recvPtr, recvLen);
  330. }
  331. else
  332. {
  333. // drop the message if the receive queue is full
  334. #if ( defined(DEBUG) && DEBUG_LOG_LEVEL >= 2 )
  335. DEBUG_LOG("ec_remote : but receive_queue is full, discard message");
  336. #endif
  337. }
  338. }
  339. else
  340. {
  341. #if ( defined(DEBUG) && DEBUG_LOG_LEVEL >= 2 )
  342. debugStr = "ec_remote : RE_SEND received, from:";
  343. debugStr += ecMsg.sender_id;
  344. debugStr += " frame:";
  345. debugStr += ecMsg.frame_id;
  346. debugStr += " discarded";
  347. DEBUG_LOG(debugStr);
  348. #endif
  349. // re-dundant frame, discard, but still reply ACK,
  350. // for the sender not to send the frame again
  351. if( dp_id[ecMsg.sender_id-1] )
  352. {
  353. // send ACK
  354. char replyMsg[sizeof(EcMsgHeader) + CRC_LEN];
  355. ((EcMsgHeader *)replyMsg)->init(ACKNOW, self_ec_player_id, ecMsg.frame_id);
  356. *((CRC_TYPE *)(replyMsg + sizeof(EcMsgHeader))) = crc8((unsigned char *)replyMsg, sizeof(EcMsgHeader));
  357. mp_ptr->send( dp_id[ecMsg.sender_id-1], replyMsg, sizeof(replyMsg) );
  358. }
  359. }
  360. break;
  361. case ACKNOW:
  362. // mark the frame ack
  363. if( is_waiting_ack(ecMsg.sender_id, ecMsg.frame_id) )
  364. {
  365. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 3 )
  366. debugStr = "ec_remote : ACKNOW received";
  367. debugStr += ecMsg.sender_id;
  368. debugStr += " frame:";
  369. debugStr += ecMsg.frame_id;
  370. debugStr += " accepted";
  371. DEBUG_LOG(debugStr);
  372. #endif
  373. set_ack(ecMsg.sender_id, ecMsg.frame_id);
  374. clear_acked_frame();
  375. }
  376. else
  377. {
  378. // discard the frame
  379. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 2 )
  380. debugStr = "ec_remote : ACKNOW received";
  381. debugStr += ecMsg.sender_id;
  382. debugStr += " frame:";
  383. debugStr += ecMsg.frame_id;
  384. debugStr += " discarded";
  385. DEBUG_LOG(debugStr);
  386. #endif
  387. }
  388. break;
  389. case NEGACK:
  390. // re-send only the frameId
  391. if( is_waiting_ack(ecMsg.sender_id, ecMsg.frame_id) )
  392. {
  393. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 2 )
  394. debugStr = "ec_remote : NEGACK received";
  395. debugStr += ecMsg.sender_id;
  396. debugStr += " frame:";
  397. debugStr += ecMsg.frame_id;
  398. debugStr += " accepted";
  399. DEBUG_LOG(debugStr);
  400. #endif
  401. char *replyMsg = send_queue[ecMsg.frame_id].queue_buf;
  402. DWORD replyLen = send_queue[ecMsg.frame_id].length();
  403. mp_ptr->send( dp_id[ecMsg.sender_id-1], replyMsg, replyLen );
  404. err_when( replyLen <= sizeof(EcMsgHeader) );
  405. // don't mark re-send time
  406. // mark the func_id of the message RE_SEND
  407. ((EcMsgHeader *)replyMsg)->func_id = RE_SEND;
  408. // recalculate CRC
  409. *((CRC_TYPE *) (replyMsg + replyLen - CRC_LEN) ) = crc8((unsigned char *)replyMsg, replyLen - CRC_LEN);
  410. DEBUG_LOG("ec_remote : frame retransmitted");
  411. }
  412. else
  413. {
  414. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 2 )
  415. debugStr = "ec_remote : NEGACK received";
  416. debugStr += ecMsg.sender_id;
  417. debugStr += " frame:";
  418. debugStr += ecMsg.frame_id;
  419. debugStr += " discarded";
  420. DEBUG_LOG(debugStr);
  421. #endif
  422. }
  423. break;
  424. default:
  425. err_here();
  426. }
  427. }
  428. else
  429. {
  430. // crc incorrect
  431. if( recvLen > sizeof(EcMsgHeader) + CRC_LEN )
  432. {
  433. char senderId = get_ec_player_id(from);
  434. if( senderId)
  435. {
  436. // send NEGACK frame
  437. char replyMsg[sizeof(EcMsgHeader) + CRC_LEN];
  438. ((EcMsgHeader *)replyMsg)->init(NEGACK, self_ec_player_id, wait_to_receive[senderId-1]);
  439. *((CRC_TYPE *)(replyMsg + sizeof(EcMsgHeader))) = crc8((unsigned char *)replyMsg, sizeof(EcMsgHeader));
  440. mp_ptr->send( dp_id[senderId-1], replyMsg, sizeof(replyMsg) );
  441. }
  442. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 2)
  443. DEBUG_LOG("ec_remote : long packet corrupted" );
  444. #endif
  445. }
  446. else
  447. {
  448. // it is probably, ACKNOW/ NEGACK frame, discard it
  449. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 2)
  450. DEBUG_LOG("ec_remote : short packet corrupted" );
  451. #endif
  452. }
  453. }
  454. }
  455. // ------ retransmit any un-acked and time-out-ed frame -------//
  456. clear_acked_frame();
  457. re_transmit();
  458. mp_ptr->after_send(); // re_transmit() will call after_send
  459. }
  460. int ErrorControl::is_send_empty()
  461. {
  462. return ( send_head == send_tail );
  463. }
  464. int ErrorControl::is_send_full()
  465. {
  466. return ( send_tail + 1 == send_head || send_tail + 1 == send_head + MAX_QUEUE );
  467. }
  468. int ErrorControl::send_queue_space()
  469. {
  470. // the queue can hold at most MAX_QUEUE-1 item
  471. return send_tail >= send_head ? MAX_QUEUE-1 - (send_tail - send_head) : send_head - send_tail -1 ;
  472. }
  473. // return frameId
  474. int ErrorControl::en_send_queue()
  475. {
  476. if( is_send_full() )
  477. return -1;
  478. else
  479. {
  480. char f = send_tail;
  481. err_when( f < 0 || f >= MAX_QUEUE);
  482. inc_frame_id( send_tail );
  483. return f;
  484. }
  485. }
  486. // free queue Id
  487. void ErrorControl::de_send_queue()
  488. {
  489. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 3)
  490. debugStr = "ec_remote.de_send_queue(), frame:";
  491. debugStr += send_head;
  492. DEBUG_LOG(debugStr);
  493. #endif
  494. inc_frame_id(send_head);
  495. }
  496. int ErrorControl::is_recv_empty()
  497. {
  498. return (recv_head == recv_tail);
  499. }
  500. int ErrorControl::is_recv_full()
  501. {
  502. return (recv_tail+1 == recv_head || recv_tail+1 == recv_head + MAX_RECV_QUEUE );
  503. }
  504. int ErrorControl::recv_queue_space()
  505. {
  506. // the queue can hold at most MAX_RECV_QUEUE-1 item
  507. return recv_tail >= recv_head ? MAX_RECV_QUEUE-1 - (recv_tail - recv_head) : recv_head - recv_tail -1 ;
  508. }
  509. void ErrorControl::en_recv_queue(void *dataPtr, long unsigned int dataLen)
  510. {
  511. if( is_recv_full() )
  512. {
  513. err_here(); // receive queue is full
  514. }
  515. else
  516. {
  517. char f = recv_tail;
  518. err_when( recv_tail < 0 || recv_tail >= MAX_RECV_QUEUE);
  519. if( ++recv_tail >= MAX_RECV_QUEUE )
  520. recv_tail = 0;
  521. receive_queue[f].clear();
  522. memcpy( receive_queue[f].reserve(dataLen), dataPtr, dataLen);
  523. }
  524. }
  525. void ErrorControl::de_recv_queue()
  526. {
  527. if( ++recv_head >= MAX_RECV_QUEUE )
  528. recv_head = 0;
  529. }
  530. int ErrorControl::is_waiting_ack(char ecPlayerId, char frameId)
  531. {
  532. // true if frameId is between send_head (inclusive) and send_tail (non-inclusive)
  533. return is_between( send_head, frameId, send_tail );
  534. }
  535. void ErrorControl::set_ack(char ecPlayerId, char frameId)
  536. {
  537. ack_flag[frameId][ecPlayerId-1] = 1;
  538. }
  539. void ErrorControl::clear_ack(char frameId)
  540. {
  541. memset( ack_flag[frameId], 0, MAX_PLAYER );
  542. }
  543. void ErrorControl::mark_send_time(char frameId, long unsigned int duration)
  544. {
  545. send_time[frameId] = m.get_time();
  546. re_send_after[frameId] = duration;
  547. }
  548. // larger the promptFactor, earlier to re-send
  549. int ErrorControl::need_re_send(char frameId, int promptFactor)
  550. {
  551. return ((m.get_time() - send_time[frameId]) * promptFactor) >= re_send_after[frameId];
  552. // do not use m.get_time() >= re_send_after[frameId] + send_time[frameId]
  553. // assume m.get_time() may count again from zero
  554. }
  555. int ErrorControl::are_all_acked(char frameId)
  556. {
  557. for( char ecPlayerId = 1; ecPlayerId <= MAX_PLAYER; ++ecPlayerId )
  558. {
  559. if( dp_id[ecPlayerId-1] && !ack_flag[frameId][ecPlayerId-1] )
  560. return 0;
  561. }
  562. return 1;
  563. }
  564. void ErrorControl::clear_acked_frame()
  565. {
  566. err_when( send_head < 0 || send_head >= MAX_QUEUE );
  567. for( ; !is_send_empty() && are_all_acked(send_head); de_send_queue() );
  568. }
  569. int ErrorControl::is_waiting_receive(char ecPlayerId, char frameId)
  570. {
  571. err_when( frameId < 0 || frameId >= MAX_QUEUE );
  572. // the first waiting frame is wait_to_receive, but may receive MAX_QUEUE-2
  573. // e.g wait_to_receive is 0, then 1 to MAX_QUEUE/2-1 are also acceptable
  574. return is_between(wait_to_receive[ecPlayerId-1], frameId,
  575. (wait_to_receive[ecPlayerId-1] + MAX_QUEUE/2) % MAX_QUEUE)
  576. && !recv_flag[ecPlayerId-1][frameId];
  577. }
  578. void ErrorControl::set_recv_flag(char ecPlayerId, char frameId)
  579. {
  580. recv_flag[ecPlayerId-1][frameId] = 1;
  581. }
  582. void ErrorControl::clear_recv_flag(char ecPlayerId, char frameId)
  583. {
  584. recv_flag[ecPlayerId-1][frameId] = 0;
  585. }
  586. void ErrorControl::re_transmit(int promptFactor)
  587. {
  588. for( char f = send_head; f != send_tail; inc_frame_id(f) )
  589. {
  590. if( !are_all_acked(f) && need_re_send(f, promptFactor) )
  591. {
  592. // count no. of remote player to re_send
  593. int resendSuccess = 0;
  594. int resendFail = 0;
  595. char *ecMsg = send_queue[f].queue_buf;
  596. DWORD ecMsgLen = send_queue[f].length();
  597. for( char ecPlayerId = 1; ecPlayerId <= MAX_PLAYER; ++ecPlayerId )
  598. {
  599. // resend to specific remote player
  600. if( dp_id[ecPlayerId-1] && !ack_flag[f][ecPlayerId-1] )
  601. {
  602. #if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 2)
  603. debugStr = "ec.remote : time-out retransmit frame ";
  604. debugStr += f;
  605. debugStr += " to ";
  606. debugStr += ecPlayerId;
  607. DEBUG_LOG(debugStr);
  608. #endif
  609. if( mp_ptr->send(dp_id[ecPlayerId-1], ecMsg, ecMsgLen))
  610. resendSuccess++;
  611. else
  612. resendFail++;
  613. }
  614. if( resendSuccess > 0)
  615. {
  616. if( resendFail > 0)
  617. {
  618. // some resend fail, mark short resend time
  619. mark_send_time(f, SHORT_TIME_OUT);
  620. }
  621. else
  622. {
  623. // all resend success, mark longer resend time
  624. mark_send_time(f, TIME_OUT );
  625. }
  626. // mark the func_id of the message RE_SEND
  627. ((EcMsgHeader *)ecMsg)->func_id = RE_SEND;
  628. // recalculate CRC
  629. *((CRC_TYPE *) (ecMsg + ecMsgLen - CRC_LEN )) = crc8((unsigned char *)ecMsg, ecMsgLen - CRC_LEN);
  630. }
  631. else if( resendFail > 0)
  632. {
  633. // all fail, mark a shorter time
  634. // mark send time, mark a shorter time
  635. mark_send_time(f, SHORT_TIME_OUT);
  636. }
  637. }
  638. }
  639. }
  640. mp_ptr->after_send();
  641. }