OREMOTE2.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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 : OREMOTE2.CPP
  21. //Description : Object Remote - part 2
  22. #include <windows.h>
  23. #include <windowsx.h>
  24. #include <mmsystem.h>
  25. #define DEBUG_LOG_LOCAL 1
  26. #include <ALL.h>
  27. #include <OVGA.h>
  28. #include <OSTR.h>
  29. #include <OFONT.h>
  30. #include <OSYS.h>
  31. #include <OWORLD.h>
  32. #include <OPOWER.h>
  33. #include <ONATION.h>
  34. #include <OREMOTE.h>
  35. #include <OERRCTRL.h>
  36. #include <OLOG.h>
  37. //---------- Define static functions ----------//
  38. // static int validate_queue(char* queueBuf, int queuedSize);
  39. //-------- Begin of function Remote::new_msg ---------//
  40. //
  41. // Allocate a RemoteMsg with the specified data size.
  42. //
  43. // <short> msgId - id. of the message.
  44. // <int> dataSize - the data size of the RemoteMsg needed (size of RemoteMsg::data_buf)
  45. //
  46. // Data structure:
  47. //
  48. // <short> size of RemoteMsg + <RemoteMsg> RemoteMsg body
  49. // ^ ^
  50. // | Allocate starts here | return variable here to the client function
  51. //
  52. RemoteMsg* Remote::new_msg(int msgId, int dataSize)
  53. {
  54. int msgSize = sizeof(DWORD) + dataSize;
  55. // <DWORD> is for the message id.
  56. char* shortPtr;
  57. //--- use common_msg_buf, if the requested one is not bigger than common_msg_buf ---//
  58. int reqSize = sizeof(short) + msgSize;
  59. // <short> for length
  60. if(reqSize <= COMMON_MSG_BUF_SIZE )
  61. {
  62. shortPtr = common_msg_buf;
  63. }
  64. else //---- allocate a new RemoteMsg if the requested one is bigger than common_msg_buf ----//
  65. {
  66. shortPtr = (char *)mem_add( reqSize );
  67. }
  68. //---------- return RemoteMsg now -----------//
  69. *(short *)shortPtr = msgSize;
  70. shortPtr += sizeof(short);
  71. RemoteMsg* remoteMsgPtr = (RemoteMsg*) shortPtr;
  72. remoteMsgPtr->id = (DWORD) msgId;
  73. return remoteMsgPtr;
  74. }
  75. //--------- End of function Remote::new_msg ---------//
  76. //-------- Begin of function Remote::free_msg ---------//
  77. //
  78. // Free a RemoteMsg previously allocated by remote_msg().
  79. //
  80. // Data structure:
  81. //
  82. // <short> size of RemoteMsg + <RemoteMsg> RemoteMsg body
  83. // ^ ^
  84. // | Allocate starts here | return variable here to the client function
  85. //
  86. void Remote::free_msg(RemoteMsg* remoteMsgPtr)
  87. {
  88. char* memPtr = (char *)remoteMsgPtr - sizeof(short);
  89. // include size of message, sizeof MPMSG_FREE_MSG
  90. if( memPtr != common_msg_buf ) // the real allocation block start 2 bytes before remoteMsgPtr
  91. mem_del( memPtr );
  92. }
  93. //--------- End of function Remote::free_msg ---------//
  94. //-------- Begin of function Remote::send_msg ---------//
  95. //
  96. // <RemoteMsg*> remoteMsgPtr - the pointer to RemoteMsg to be sent.
  97. // [DPID] receiverId - send to whom, 0 for everybody
  98. // (default : 0)
  99. //
  100. // <short> size of RemoteMsg + <RemoteMsg> RemoteMsg body
  101. // ^ ^
  102. // | Allocate starts here | return variable here to the client function
  103. //
  104. void Remote::send_msg(RemoteMsg* remoteMsgPtr, int receiverId)
  105. {
  106. if( handle_vga_lock )
  107. vga_front.temp_unlock();
  108. // structure : <short>, <RemoteMsg>
  109. char* memPtr = (char*)remoteMsgPtr - sizeof(short); // the preceeding <short> is the size of RemoteMsg
  110. int msgSize = *(short *)memPtr + sizeof(short);
  111. // mp_ptr->send( receiverId, memPtr, msgSize );
  112. ec_remote.send( ec_remote.get_ec_player_id(receiverId), memPtr, msgSize);
  113. packet_send_count++;
  114. if( handle_vga_lock )
  115. vga_front.temp_restore_lock();
  116. }
  117. //--------- End of function Remote::send_msg ---------//
  118. //-------- Begin of function Remote::send_free_msg ---------//
  119. //
  120. // <RemoteMsg*> remoteMsgPtr - the pointer to RemoteMsg to be sent.
  121. // [DPID] receiverId - send to whom, 0 for everybody
  122. // (default : 0)
  123. //
  124. // <short> size of RemoteMsg + <RemoteMsg> RemoteMsg body
  125. // ^ ^
  126. // | Allocate starts here | return variable here to the client function
  127. //
  128. void Remote::send_free_msg(RemoteMsg* remoteMsgPtr, int receiverId)
  129. {
  130. if( handle_vga_lock )
  131. vga_front.temp_unlock();
  132. char* memPtr = (char*)remoteMsgPtr - sizeof(short); // the preceeding <short> is the size of RemoteMsg
  133. int msgSize = *(short *)memPtr + sizeof(short);
  134. // mp_ptr->send( receiverId, memPtr, msgSize );
  135. ec_remote.send( ec_remote.get_ec_player_id(receiverId), memPtr, msgSize);
  136. packet_send_count++;
  137. if( memPtr != common_msg_buf ) // the real allocation block start 2 bytes before remoteMsgPtr
  138. mem_del( memPtr );
  139. if( handle_vga_lock )
  140. vga_front.temp_restore_lock();
  141. }
  142. //--------- End of function Remote::send_free_msg ---------//
  143. //-------- Begin of function Remote::new_send_queue_msg ---------//
  144. //
  145. // Add the message into the sending queue.
  146. //
  147. // <short> msgId - id. of the message.
  148. // <int> dataSize - the data size of the RemoteMsg needed (size of RemoteMsg::data_buf)
  149. //
  150. // Queue data structure:
  151. //
  152. // <1st msg size> + <1st msg body> + <2nd msg size> + ...
  153. //
  154. // return : <char*> the pointer RemoteMsg::data_buf of the allocated message
  155. //
  156. char* Remote::new_send_queue_msg(int msgId, int dataSize)
  157. {
  158. char *dataPtr = send_queue[0].reserve(sizeof(short) + sizeof(DWORD) + dataSize);
  159. // ----- put the size of message ------//
  160. *(short *)dataPtr = sizeof(DWORD) + dataSize;
  161. dataPtr += sizeof(short);
  162. // ----- put the message id --------//
  163. ((RemoteMsg *)dataPtr)->id = msgId;
  164. return ((RemoteMsg *)dataPtr)->data_buf;
  165. }
  166. //--------- End of function Remote::new_send_queue_msg ---------//
  167. //-------- Begin of function Remote::send_queue_now ---------//
  168. //
  169. // Send out all send_queued message.
  170. //
  171. // [DPID] receiverId - send to whom, 0 for everybody
  172. // (default: 0)
  173. //
  174. // return : <int> 1 - sent successfully, the message has been received
  175. // by the receivers.
  176. // 0 - not successful.
  177. //
  178. int Remote::send_queue_now(int receiverId)
  179. {
  180. if( send_queue[0].length() ==0 )
  181. return 1;
  182. if( !send_queue[0].validate_queue() )
  183. err.run( "Queue Corrupted, Remote::send_queue_now()" );
  184. if( handle_vga_lock )
  185. vga_front.temp_unlock();
  186. //----------------------------------------------//
  187. int sendFlag = 1;
  188. //if(!mp_ptr->send( receiverId, send_queue[0].queue_buf, send_queue[0].length()))
  189. if(ec_remote.send( ec_remote.get_ec_player_id(receiverId), send_queue[0].queue_buf, send_queue[0].length()) <= 0 )
  190. sendFlag = 0;
  191. packet_send_count++;
  192. //---------------------------------------------//
  193. if( handle_vga_lock )
  194. vga_front.temp_restore_lock();
  195. return sendFlag;
  196. }
  197. //--------- End of function Remote::send_queue_now ---------//
  198. //-------- Begin of function Remote::append_send_to_receive ---------//
  199. //
  200. // Append all send queues to the receiving queue.
  201. //
  202. void Remote::append_send_to_receive()
  203. {
  204. if( send_queue[0].length() ==0 )
  205. return;
  206. int n;
  207. for( n = 0; n < RECEIVE_QUEUE_BACKUP; ++n)
  208. {
  209. if( send_frame_count[0] == receive_frame_count[n])
  210. {
  211. receive_queue[n].append_queue(send_queue[0]);
  212. if( !receive_queue[n].validate_queue() )
  213. err.run( "Queue Corrupted, Remote::append_send_to_receive()-3" );
  214. break;
  215. }
  216. }
  217. err_when( n >= RECEIVE_QUEUE_BACKUP ); // not found
  218. }
  219. //--------- End of function Remote::append_send_to_receive ---------//
  220. //-------- Begin of function Remote::poll_msg ---------//
  221. int Remote::poll_msg()
  222. {
  223. if ( !is_enable() || poll_msg_flag <= 0)
  224. return 0;
  225. //--------------------------------------------//
  226. int receivedFlag=0;
  227. DWORD msgListSize;
  228. int loopCount=0;
  229. if( handle_vga_lock )
  230. vga_front.temp_unlock();
  231. ec_remote.yield();
  232. char *recvBuf;
  233. // DPID from, to;
  234. char from;
  235. DWORD recvLen;
  236. // while( (recvBuf = mp_ptr->receive(&from, &to, &recvLen)) != NULL)
  237. while( (recvBuf = ec_remote.receive(&from, (long unsigned int *)&recvLen)) != NULL)
  238. {
  239. err_when(++loopCount > 1000 );
  240. msgListSize = recvLen;
  241. //-------- received successfully ----------//
  242. /*
  243. if( !power.enable_flag ) // power.enable_flag determines whether the multiplayer game has started or not
  244. {
  245. int validateLen = receive_queue[0].length();
  246. memcpy( receive_queue[0].reserve(msgListSize), recvBuf, msgListSize );
  247. if( !receive_queue[0].validate_queue(validateLen) )
  248. err.run( "Queue Corrupted, Remote::poll_msg()-99" );
  249. receivedFlag=1;
  250. }
  251. else
  252. */
  253. err_when( msgListSize < sizeof(short) + sizeof(DWORD) );
  254. RemoteMsg *rMsg = (RemoteMsg *)(recvBuf + sizeof(short) );
  255. int queueToCopy = -1;
  256. //--- if message id !=MSG_QUEUE_HEADER, this packet is sent by send_free_msg(), not by send_queue_now() ---//
  257. if( rMsg->id != MSG_QUEUE_HEADER )
  258. {
  259. // DEBUG_LOG("begin MSG_xxxx received");
  260. // DEBUG_LOG(rMsg->id);
  261. // DEBUG_LOG(sys.frame_count);
  262. // DEBUG_LOG("end MSG_xxxx received");
  263. queueToCopy = 0;
  264. }
  265. else
  266. {
  267. // ------- find which receive queue to hold the message -----//
  268. DWORD senderFrameCount = *(DWORD *)rMsg->data_buf;
  269. for(int n = 0; n < RECEIVE_QUEUE_BACKUP; ++n )
  270. {
  271. if( senderFrameCount == receive_frame_count[n] )
  272. {
  273. queueToCopy = n;
  274. break;
  275. }
  276. }
  277. // DEBUG_LOG("begin MSG_QUEUE_HEADER received");
  278. // DEBUG_LOG(senderFrameCount);
  279. // DEBUG_LOG(sys.frame_count);
  280. // DEBUG_LOG(n);
  281. // DEBUG_LOG("end MSG_QUEUE_HEADER received");
  282. }
  283. if( queueToCopy >= 0 && queueToCopy < RECEIVE_QUEUE_BACKUP )
  284. {
  285. RemoteQueue &rq = receive_queue[queueToCopy];
  286. int validateLen = rq.length();
  287. memcpy( rq.reserve(msgListSize), recvBuf, msgListSize );
  288. if( !rq.validate_queue(validateLen) )
  289. err.run( "Queue Corrupted, Remote::poll_msg()-2" );
  290. }
  291. else
  292. {
  293. // discard the frame in non-debug mode
  294. DEBUG_LOG("message is discard" );
  295. if( rMsg->id != MSG_QUEUE_HEADER )
  296. {
  297. DEBUG_LOG(rMsg->id);
  298. }
  299. else
  300. {
  301. DWORD senderFrameCount = *(DWORD *)rMsg->data_buf;
  302. DEBUG_LOG("MSG_QUEUE_HEADER message");
  303. DEBUG_LOG(senderFrameCount);
  304. }
  305. }
  306. ec_remote.de_recv_queue();
  307. receivedFlag = 1;
  308. packet_receive_count++;
  309. }
  310. if( handle_vga_lock )
  311. vga_front.temp_restore_lock();
  312. return receivedFlag;
  313. }
  314. //--------- End of function Remote::poll_msg ---------//
  315. //-------- Begin of function Remote::process_receive_queue ---------//
  316. //
  317. // Process messages in the receive queue.
  318. //
  319. void Remote::process_receive_queue()
  320. {
  321. if( !process_queue_flag ) // avoid sys.yield()
  322. return;
  323. int processFlag;
  324. int loopCount=0;
  325. disable_poll_msg();
  326. RemoteQueue &rq = receive_queue[0];
  327. RemoteQueueTraverse rqt(rq);
  328. if( !rq.validate_queue() )
  329. err.run( "Queue corrupted, Remote::process_receive_queue()" );
  330. //---- if not started yet, process the message without handling the message sequence ----//
  331. if( !power.enable_flag )
  332. {
  333. for( rqt.traverse_set_start(); !rqt.traverse_finish(); rqt.traverse_next() )
  334. {
  335. err_when( ++loopCount > 1000 );
  336. RemoteMsg* remoteMsgPtr = rqt.get_remote_msg();
  337. remoteMsgPtr->process_msg();
  338. }
  339. }
  340. else //--------- process in the order of nation recno --------//
  341. {
  342. LOG_BEGIN;
  343. for( int nationRecno=1 ; nationRecno<=nation_array.size() ; ++nationRecno )
  344. {
  345. if( nation_array.is_deleted(nationRecno) || nation_array[nationRecno]->nation_type==NATION_AI )
  346. continue;
  347. nation_processing = nationRecno;
  348. processFlag=0;
  349. //-------- scan through the message queue --------//
  350. loopCount = 0;
  351. for( rqt.traverse_set_start(); !rqt.traverse_finish(); rqt.traverse_next() )
  352. {
  353. err_when( ++loopCount > 1000 );
  354. RemoteMsg* remoteMsgPtr = rqt.get_remote_msg();
  355. //--- check if this message indicates the start of a new message queue ---//
  356. if( remoteMsgPtr->id == MSG_QUEUE_HEADER )
  357. {
  358. short msgNation = *(short *)(&remoteMsgPtr->data_buf[sizeof(DWORD)]);
  359. //--- if this is the message queue of the nation we should process now ----//
  360. if(msgNation==nationRecno ) // struct of data_buf: <DWORD> frameCount + <short> nationRecno
  361. {
  362. processFlag = 1;
  363. }
  364. else //--- if this is a message queue that should be processed now ---//
  365. {
  366. if( processFlag ) //--- if we were previously processing the right queue, now the processing is complete by this point ---//
  367. break; // message on next nation is met, stop this nation
  368. }
  369. }
  370. else if( remoteMsgPtr->id == MSG_QUEUE_TRAILER )
  371. {
  372. short msgNation = *(short *)remoteMsgPtr->data_buf;
  373. //--- if this is the message queue of the nation we should process now ----//
  374. if(msgNation==nationRecno ) // struct of data_buf:<short> nationRecno
  375. break; // end of that nation
  376. }
  377. else
  378. {
  379. //--- if this is the message queue of the nation we should process now ----//
  380. if( processFlag )
  381. {
  382. #if defined(DEBUG) && defined(ENABLE_LOG)
  383. String logStr("begin process remote message id:");
  384. logStr += (long) remoteMsgPtr->id;
  385. logStr += " of nation ";
  386. logStr += nation_processing;
  387. LOG_MSG(logStr);
  388. #endif
  389. remoteMsgPtr->process_msg();
  390. LOG_MSG("end process remote message");
  391. LOG_MSG(m.get_random_seed());
  392. }
  393. }
  394. }
  395. nation_processing = 0;
  396. }
  397. LOG_END;
  398. }
  399. //---------- clear receive_queue[0] and shift from next queue ------//
  400. rq.clear();
  401. int n;
  402. for( n = 1; n < RECEIVE_QUEUE_BACKUP; ++n)
  403. {
  404. receive_queue[n-1].swap(receive_queue[n]);
  405. receive_frame_count[n-1] = receive_frame_count[n];
  406. }
  407. receive_frame_count[n-1]++;
  408. enable_poll_msg();
  409. }
  410. //--------- End of function Remote::process_receive_queue ---------//
  411. //-------- Begin of function Remote::process_specific_msg ---------//
  412. //
  413. // Pre-process a specific message type.
  414. //
  415. void Remote::process_specific_msg(DWORD msgId)
  416. {
  417. if( !process_queue_flag ) // avoid sys.yield()
  418. return;
  419. int loopCount=0;
  420. disable_poll_msg();
  421. RemoteQueue &rq = receive_queue[0];
  422. RemoteQueueTraverse rqt(rq);
  423. if( !rq.validate_queue() )
  424. err.run( "Queue corrupted, Remote::process_specific_msg()" );
  425. for( rqt.traverse_set_start(); !rqt.traverse_finish(); rqt.traverse_next() )
  426. {
  427. err_when( ++loopCount > 1000 );
  428. RemoteMsg* remoteMsgPtr = rqt.get_remote_msg();
  429. if( remoteMsgPtr->id == msgId )
  430. {
  431. remoteMsgPtr->process_msg();
  432. remoteMsgPtr->id = 0;
  433. }
  434. }
  435. enable_poll_msg();
  436. }
  437. //--------- End of function Remote::process_specific_msg ---------//
  438. /*
  439. //-------- Begin of function Remote::validate_queue ---------//
  440. //
  441. // Pre-process a specific message type.
  442. //
  443. static int validate_queue(char* queueBuf, int queuedSize)
  444. {
  445. char* queuePtr = queueBuf;
  446. RemoteMsg* remoteMsgPtr;
  447. int msgSize, processedSize=0;
  448. int loopCount=0;
  449. while( processedSize < queuedSize )
  450. {
  451. msgSize = *((short*)queuePtr);
  452. remoteMsgPtr = (RemoteMsg*) (queuePtr + sizeof(short));
  453. if( remoteMsgPtr->id )
  454. {
  455. if( remoteMsgPtr->id<FIRST_REMOTE_MSG_ID || remoteMsgPtr->id>LAST_REMOTE_MSG_ID )
  456. return 0;
  457. }
  458. queuePtr += sizeof(short) + msgSize;
  459. processedSize += sizeof(short) + msgSize;
  460. err_when( loopCount++ > 10000 ); // deadloop
  461. }
  462. return 1;
  463. }
  464. //--------- End of function Remote::validate_queue ---------//
  465. */
  466. //-------- Begin of function Remote::copy_send_to_backup ---------//
  467. //
  468. // Copy the whole send queue to the backup queue.
  469. //
  470. void Remote::copy_send_to_backup()
  471. {
  472. // ------ shift send_queue ---------//
  473. send_queue[SEND_QUEUE_BACKUP-1].clear();
  474. for(int n = SEND_QUEUE_BACKUP-1; n > 1; --n)
  475. {
  476. send_queue[n].swap(send_queue[n-1]);
  477. send_frame_count[n] = send_frame_count[n-1];
  478. }
  479. // now send_queue[1] is empty.
  480. send_queue[1] = send_queue[0];
  481. send_frame_count[1] = send_frame_count[0];
  482. }
  483. //--------- End of function Remote::copy_send_to_backup ---------//
  484. //-------- Begin of function Remote::send_backup_now ---------//
  485. //
  486. // Send out the backup queue now.
  487. //
  488. // [DPID] receiverId - send to whom, 0 for everybody
  489. // (default: 0)
  490. //
  491. // <int> requestFrameCount - need to send the backup buffer of the requested frame count
  492. //
  493. // return : <int> 1 - sent successfully, the message has been received
  494. // by the receivers.
  495. // 0 - not successful.
  496. //
  497. int Remote::send_backup_now(int receiverId, DWORD requestFrameCount)
  498. {
  499. //------ determine which backup buffer to send -----//
  500. font_san.disp( ZOOM_X2-100, 4, "", ZOOM_X2);
  501. for( int n = 1; n < SEND_QUEUE_BACKUP; ++n)
  502. {
  503. if( requestFrameCount == send_frame_count[n] )
  504. {
  505. //---------- if nothing to send -------------//
  506. int retFlag = 1;
  507. if( send_queue[n].length() > 0 )
  508. {
  509. if( handle_vga_lock )
  510. vga_front.temp_unlock();
  511. // retFlag = mp_ptr->send(receiverId, send_queue[n].queue_buf, send_queue[n].length());
  512. retFlag = ec_remote.send(ec_remote.get_ec_player_id(receiverId), send_queue[n].queue_buf, send_queue[n].length()) > 0;
  513. packet_send_count++;
  514. if( handle_vga_lock )
  515. vga_front.temp_restore_lock();
  516. }
  517. return retFlag;
  518. }
  519. }
  520. if( requestFrameCount < send_frame_count[SEND_QUEUE_BACKUP-1])
  521. err.run( "requestFrameCount:%d < backup2_frame_count:%d", requestFrameCount, send_frame_count[SEND_QUEUE_BACKUP-1] );
  522. return 0;
  523. }
  524. //--------- End of function Remote::send_backup_now ---------//
  525. //-------- Begin of function Remote::init_send_queue ---------//
  526. //
  527. // Initialize the header of the sending queue.
  528. //
  529. // <int> frameCount - frame count of this queue
  530. // <short> nationCount - nation recno of the sender
  531. //
  532. void Remote::init_send_queue(DWORD frameCount, short nationRecno)
  533. {
  534. send_queue[0].clear();
  535. // put into the queue : <message length>, MSG_QUEUE_HEADER, <frameCount>, <nationRecno>
  536. int msgSize = sizeof(DWORD)*2 + sizeof(short);
  537. char *sendPtr = send_queue[0].reserve(sizeof(short) + msgSize);
  538. *(short *)sendPtr = msgSize;
  539. sendPtr += sizeof(short);
  540. *(DWORD *)sendPtr = MSG_QUEUE_HEADER;
  541. sendPtr += sizeof(DWORD);
  542. *(DWORD *)sendPtr = send_frame_count[0] = next_send_frame(nationRecno, frameCount + process_frame_delay);
  543. sendPtr += sizeof(DWORD);
  544. *(short *)sendPtr = nationRecno;
  545. sendPtr += sizeof(short);
  546. }
  547. //--------- End of function Remote::init_send_queue ----------//
  548. //-------- Begin of function Remote::init_receive_queue ---------//
  549. //
  550. // Initialize the header of the receiving queue,
  551. // call this only when power is just enabled
  552. //
  553. // <int> frameCount - frame count of this queue
  554. // <short> nationCount - nation recno of the receiveer
  555. //
  556. void Remote::init_receive_queue(DWORD frameCount)
  557. {
  558. int n;
  559. for(n = 0; n < RECEIVE_QUEUE_BACKUP; ++n)
  560. {
  561. receive_queue[n].clear();
  562. receive_frame_count[n] = n+frameCount;
  563. }
  564. for(n = 0; n < process_frame_delay; ++n)
  565. {
  566. // nations are not created yet, put MSG_QUEUE_HEADER/MSG_NEXT_FRAME and MSG_QUEUE_TRAILER
  567. // even though the nation will not exist
  568. for(short nationRecno = 1; nationRecno <= MAX_NATION; ++nationRecno)
  569. {
  570. //if( nation_array.is_deleted(nationRecno) ||
  571. // nation_array[nationRecno]->nation_type==NATION_AI )
  572. // continue;
  573. char *receivePtr;
  574. int msgSize;
  575. // put into the queue : <message length>, MSG_QUEUE_HEADER, <frameCount>, <nationRecno>
  576. msgSize = sizeof(DWORD)*2 + sizeof(short);
  577. receivePtr = receive_queue[n].reserve(sizeof(short) + msgSize);
  578. *(short *)receivePtr = msgSize;
  579. receivePtr += sizeof(short);
  580. *(DWORD *)receivePtr = MSG_QUEUE_HEADER;
  581. receivePtr += sizeof(DWORD);
  582. *(DWORD *)receivePtr = frameCount + n;
  583. receivePtr += sizeof(DWORD);
  584. *(short *)receivePtr = nationRecno;
  585. receivePtr += sizeof(short);
  586. // put into the queue : <message length>, MSG_NEXT_FRAME, <nationRecno>
  587. msgSize = sizeof(DWORD) + sizeof(short);
  588. receivePtr = receive_queue[n].reserve(sizeof(short) + msgSize);
  589. *(short *)receivePtr = msgSize;
  590. receivePtr += sizeof(short);
  591. *(DWORD *)receivePtr = MSG_NEXT_FRAME;
  592. receivePtr += sizeof(DWORD);
  593. *(short *)receivePtr = nationRecno;
  594. receivePtr += sizeof(short);
  595. // put into the queue : <message length>, MSG_QUEUE_TRAILER, <nationRecno>
  596. msgSize = sizeof(DWORD) + sizeof(short);
  597. receivePtr = receive_queue[n].reserve(sizeof(short) + msgSize);
  598. *(short *)receivePtr = msgSize;
  599. receivePtr += sizeof(short);
  600. *(DWORD *)receivePtr = MSG_QUEUE_TRAILER;
  601. receivePtr += sizeof(DWORD);
  602. *(short *)receivePtr = nationRecno;
  603. receivePtr += sizeof(short);
  604. }
  605. }
  606. }
  607. //--------- End of function Remote::init_receive_queue ----------//
  608. //--------- Begin of function Remote::init_start_mp ----------//
  609. void Remote::init_start_mp()
  610. {
  611. int n;
  612. for( n = 0; n < SEND_QUEUE_BACKUP; ++n)
  613. {
  614. send_queue[n].clear();
  615. send_frame_count[n] = 0;
  616. }
  617. for( n = 0; n < RECEIVE_QUEUE_BACKUP; ++n)
  618. {
  619. receive_queue[n].clear();
  620. receive_frame_count[n] = 0;
  621. }
  622. }
  623. //--------- End of function Remote::init_start_mp ----------//
  624. //--------- Begin of function Remote::enable_process_queue --------//
  625. void Remote::enable_process_queue()
  626. {
  627. process_queue_flag = 1;
  628. }
  629. //--------- End of function Remote::enable_process_queue --------//
  630. //--------- Begin of function Remote::disable_process_queue --------//
  631. void Remote::disable_process_queue()
  632. {
  633. process_queue_flag = 0;
  634. }
  635. //--------- End of function Remote::disable_process_queue --------//
  636. //-------- Begin of function Remote::enable_poll_msg ---------//
  637. void Remote::enable_poll_msg()
  638. {
  639. poll_msg_flag = 1;
  640. }
  641. //-------- End of function Remote::enable_poll_msg ---------//
  642. //-------- Begin of function Remote::disable_poll_msg ---------//
  643. void Remote::disable_poll_msg()
  644. {
  645. poll_msg_flag = 0;
  646. }
  647. //-------- End of function Remote::disable_poll_msg ---------//