OERRCTRL.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // Description : Error control for dplay
  22. #ifndef __OERRCTRL_H
  23. #define __OERRCTRL_H
  24. #include <GAMEDEF.h>
  25. #include <MPTYPES.h>
  26. #include <OVQUEUE.h>
  27. struct EcMsgHeader
  28. {
  29. char func_id; // FIRST_SEND / RE_SEND / ACKNOW / NEGACK
  30. char sender_id; // 1 to MAX_NATION (ecPlayerId)
  31. char frame_id;
  32. void init( char funcId, char senderId, char frameId )
  33. {
  34. func_id = funcId;
  35. sender_id = senderId;
  36. frame_id = frameId;
  37. }
  38. };
  39. class MultiPlayerType;
  40. class ErrorControl
  41. {
  42. enum { FIRST_SEND, RE_SEND, ACKNOW, NEGACK };
  43. #ifdef AMPLUS
  44. // enum { MAX_PLAYER = MAX_NATION, MAX_QUEUE = 12, MAX_RECV_QUEUE = 48 };
  45. enum { MAX_PLAYER = MAX_NATION, MAX_QUEUE = 18, MAX_RECV_QUEUE = 72 };
  46. #else
  47. enum { MAX_PLAYER = MAX_NATION, MAX_QUEUE = 8, MAX_RECV_QUEUE = 32 };
  48. #endif
  49. // MAX_QUEUE/2 > Remote::MAX_PROCESS_FRAME_DELAY
  50. // MAX_RECV_QUEUE > MAX_QUEUE/2 * MAX_PLAYER
  51. private:
  52. MultiPlayerType *mp_ptr;
  53. int connecting_player_count; // no. of peers
  54. char self_ec_player_id;
  55. char send_head; // queue_head == queue_tail, empty
  56. char send_tail; // queue_tail == queue_head -1, full
  57. VLenQueue send_queue[MAX_QUEUE]; // include EcMsgHeader and 8-bit CRC
  58. char ack_flag[MAX_QUEUE][MAX_PLAYER];
  59. unsigned long send_time[MAX_QUEUE];
  60. unsigned long re_send_after[MAX_QUEUE];
  61. unsigned long dp_id[MAX_PLAYER]; // directPlay playerid, 0 if not valid
  62. char wait_to_receive[MAX_PLAYER];
  63. char recv_flag[MAX_PLAYER][MAX_QUEUE];
  64. // char next_send[MAX_PLAYER];
  65. // char next_ack_send[MAX_PLAYER];
  66. // char retrans_state[MAX_PLAYER];
  67. char recv_head;
  68. char recv_tail;
  69. VLenQueue receive_queue[MAX_RECV_QUEUE]; // include EcMsgHeader and 8-bit CRC
  70. private:
  71. static inline void inc_frame_id(char &frameId)
  72. { if(++frameId >= MAX_QUEUE) frameId = 0; }
  73. static inline char next_frame_id(char frameId)
  74. { return frameId >= MAX_QUEUE-1 ? 0 : frameId+1 ; }
  75. static inline char prev_frame_id(char frameId)
  76. { return frameId <= 0 ? MAX_QUEUE-1 : frameId-1 ; }
  77. static int is_between(int low, int t, int high)
  78. { return low <= high ? (low <= t && t < high) : (low <= t || t < high); }
  79. int is_send_empty();
  80. int is_send_full();
  81. int send_queue_space();
  82. int en_send_queue(); // return frameId
  83. void de_send_queue(); // free send_head
  84. int is_recv_empty();
  85. int is_recv_full();
  86. void en_recv_queue(void *dataPtr, unsigned long dataLen);
  87. int recv_queue_space();
  88. // functions on ack_flag
  89. int is_waiting_ack(char ecPlayerId, char frameId);
  90. void set_ack(char ecPlayerId, char frameId);
  91. void clear_ack(char frameId);
  92. int are_all_acked(char frameId);
  93. void clear_acked_frame();
  94. // functions on recv_flag
  95. int is_waiting_receive(char ecPlayerId, char frameId);
  96. void set_recv_flag(char ecPlayerId, char frameId);
  97. void clear_recv_flag(char ecPlayerId, char frameId);
  98. void mark_send_time(char frameId, unsigned long duration);
  99. int need_re_send(char frameId, int promptFactor);
  100. public:
  101. void init(MultiPlayerType *, char ecPlayerId );
  102. void deinit();
  103. void set_dp_id(char ecPlayerId, unsigned long dpPlayerId );
  104. char get_ec_player_id( unsigned long dpPlayerId );
  105. int send(char ecPlayerId, void *dataPtr, unsigned long dataLen);
  106. char *receive(char *sendEcPlayerId, unsigned long *dataLen);
  107. void de_recv_queue(); // get the content from recv_queue[recv_head] before de_recv_queue
  108. int is_player_valid(char ecPlayerId);
  109. void set_player_lost(char ecPlayerId);
  110. void yield();
  111. void re_transmit(int promptFactor=1);
  112. };
  113. extern ErrorControl ec_remote;
  114. #endif