OREMOTEQ.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 : OREMOTEQ.CPP
  21. // Description : remote message queue
  22. #include <ALL.h>
  23. #include <OREMOTEQ.h>
  24. #include <OREMOTE.h>
  25. // structure of queue_buf in RemoteQueue :
  26. // <1st message length (short), not including this 2 bytes> <1st message content>
  27. // <2nd message length> <2nd message content> ...
  28. // ------- begin of function RemoteQueue::RemoteQueue -------//
  29. RemoteQueue::RemoteQueue() : VLenQueue()
  30. {
  31. }
  32. RemoteQueue::RemoteQueue(int s) : VLenQueue(s)
  33. {
  34. }
  35. RemoteQueue::RemoteQueue(RemoteQueue &q) : VLenQueue(q)
  36. {
  37. }
  38. // ------- end of function RemoteQueue::RemoteQueue -------//
  39. // ------- begin of function RemoteQueue::~RemoteQueue -------//
  40. RemoteQueue::~RemoteQueue()
  41. {
  42. }
  43. // ------- end of function RemoteQueue::~RemoteQueue -------//
  44. /*
  45. //
  46. // try not to use RemoteQueue::traverse_set_start,
  47. // RemoteQueue::traverse_finish and RemoteQueue::traverse_next
  48. // use RemoteQueueTraverse::traverse_set_start instead
  49. // see RemoteQueue::validate to see how to use RemoteQueueTraverse
  50. //
  51. // ------- begin of function RemoteQueue::traverse_set_start -------//
  52. void RemoteQueue::traverse_set_start(int start)
  53. {
  54. queue_ptr = queue_buf + start;
  55. }
  56. // ------- end of function RemoteQueue::traverse_set_start -------//
  57. // ------- begin of function RemoteQueue::traverse_finish -------//
  58. int RemoteQueue::traverse_finish()
  59. {
  60. err_when( (unsigned)queue_ptr < (unsigned)queue_buf );
  61. return queue_ptr - queue_buf >= queued_size;
  62. }
  63. // ------- end of function RemoteQueue::traverse_finish -------//
  64. // ------- begin of function RemoteQueue::traverse_next -------//
  65. // queue_ptr points to the message length
  66. void RemoteQueue::traverse_next()
  67. {
  68. err_when( queue_ptr - queue_buf >= queued_size );
  69. err_when( *(unsigned short *)queue_ptr >= 0x8000 );
  70. queue_ptr += *(unsigned short *)queue_ptr + sizeof(unsigned short); // skip the message length as well
  71. }
  72. // ------- end of function RemoteQueue::traverse_next -------//
  73. // ------- begin of function RemoteQueue::get_remote_msg -------//
  74. RemoteMsg *RemoteQueue::get_remote_msg(short *msgLen)
  75. {
  76. if( traverse_finish() )
  77. {
  78. if(msgLen)
  79. msgLen = 0;
  80. return NULL;
  81. }
  82. else
  83. {
  84. char *ptr1 = queue_ptr + sizeof(short);
  85. if(msgLen)
  86. msgLen = (short *)queue_ptr;
  87. return (RemoteMsg *)ptr1;
  88. }
  89. }
  90. // ------- end of function RemoteQueue::get_remote_msg -------//
  91. */
  92. // ------- begin of function RemoteQueue::validate_queue -------//
  93. int RemoteQueue::validate_queue(int start)
  94. {
  95. int loopCount = 0;
  96. RemoteQueueTraverse rqt(*this, start);
  97. for( rqt.traverse_set_start(start); !rqt.traverse_finish(); rqt.traverse_next() )
  98. {
  99. RemoteMsg *remoteMsgPtr = rqt.get_remote_msg();
  100. if( remoteMsgPtr->id )
  101. {
  102. if( remoteMsgPtr->id<FIRST_REMOTE_MSG_ID || remoteMsgPtr->id>LAST_REMOTE_MSG_ID )
  103. return 0;
  104. }
  105. err_when( ++loopCount > 200 );
  106. }
  107. return 1;
  108. }
  109. // ------- end of function RemoteQueue::validate_queue -------//
  110. // ------- begin of function RemoteQueueTraverse::RemoteQueueTraverse -------//
  111. RemoteQueueTraverse::RemoteQueueTraverse( RemoteQueue &rq, int start ) :
  112. remote_queue(rq), offset(start)
  113. {
  114. }
  115. // ------- end of function RemoteQueueTraverse::RemoteQueueTraverse -------//
  116. // ------- begin of function RemoteQueueTraverse::traverse_set_start -------//
  117. void RemoteQueueTraverse::traverse_set_start(int start)
  118. {
  119. offset = start;
  120. }
  121. // ------- end of function RemoteQueueTraverse::traverse_set_start -------//
  122. // ------- begin of function RemoteQueueTraverse::traverse_finish -------//
  123. int RemoteQueueTraverse::traverse_finish()
  124. {
  125. return offset >= remote_queue.queued_size;
  126. }
  127. // ------- end of function RemoteQueueTraverse::traverse_finish -------//
  128. // ------- begin of function RemoteQueueTraverse::traverse_next -------//
  129. void RemoteQueueTraverse::traverse_next()
  130. {
  131. err_when( traverse_finish() );
  132. unsigned short msgLen = *(unsigned short *)(remote_queue.queue_buf + offset);
  133. err_when( msgLen >= 0x8000 );
  134. offset += msgLen + sizeof(unsigned short);
  135. }
  136. // ------- end of function RemoteQueueTraverse::traverse_next -------//
  137. // ------- begin of function RemoteQueueTraverse::get_remote_msg -------//
  138. RemoteMsg *RemoteQueueTraverse::get_remote_msg(short *msgLen)
  139. {
  140. char *retPtr = remote_queue.queue_buf + offset;
  141. if( msgLen )
  142. {
  143. *msgLen = *(unsigned short *)retPtr;
  144. }
  145. retPtr += sizeof(unsigned short);
  146. return (RemoteMsg *)retPtr;
  147. }
  148. // ------- end of function RemoteQueueTraverse::get_remote_msg -------//