tmultiplex.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "tmultiplex.h"
  2. #include <api/timer/timerclient.h>
  3. #include <assert.h>
  4. VirtualTimer::VirtualTimer(TimerClient *_client, intptr_t _id, api_dependent *depend) :
  5. client(_client), id(_id), dep(depend)
  6. {
  7. name = client->timerclient_getName();
  8. mclient = client->timerclient_getMasterClient();
  9. }
  10. MainTimerMultiplexer::MainTimerMultiplexer() {
  11. setClient(this);
  12. }
  13. MainTimerMultiplexer::~MainTimerMultiplexer() {
  14. /*
  15. foreach(timerclients)
  16. VirtualTimer *vt = timerclients.getfor();
  17. //DebugString("TIMER MULTIPLEXER WARNING: TimerClient %X (%s) was not deregistered\n", vt->client, vt->name.getValue());
  18. endfor;
  19. */
  20. // NOTE: if you get a crash here, someone probably had a timer event outstanding
  21. // or didn't call down in timerclient_timerCallback()
  22. // Also this is guaranteed to happen if one of your timerclient objects (ie: a wnd) was not deleted
  23. // eventho your DLL has been unloaded. the watched pointer will remain watched instead of unregistering
  24. // itself from its viewers. DependentViewerI (one of our direct ancestors) will try to dereference that pointer in
  25. // order to signal it that a viewer was detached, and it will crash.
  26. timerclients.deleteAll();
  27. }
  28. void MainTimerMultiplexer::add(TimerClient *client, intptr_t id, int ms) {
  29. remove(client, id);
  30. api_dependent *d = client->timerclient_getDependencyPtr();
  31. assert(d != NULL);
  32. VirtualTimer *t = new VirtualTimer(client, id, d);
  33. timerclients.addItem(t);
  34. viewer_addViewItem(d);
  35. if (t->mclient) {
  36. d = t->mclient->timerclient_getDependencyPtr();
  37. ASSERT(d != NULL);
  38. viewer_addViewItem(d);
  39. }
  40. addTimer(ms, static_cast<void *>(t));
  41. }
  42. void MainTimerMultiplexer::remove(TimerClient *client, intptr_t id) {
  43. while (masters.haveItem(client)) masters.removeItem(client);
  44. for (int i=0;i<timerclients.getNumItems();i++) {
  45. VirtualTimer *t = timerclients.enumItem(i);
  46. masters.removeItem(t->mclient);//BU store mclient on VirtualTimer now
  47. if (t->client == client && (t->id == id || id == -1)) {
  48. viewer_delViewItem(t->dep);
  49. timerclients.removeByPos(i);
  50. removeTimer(static_cast<void *>(t));
  51. delete t;
  52. i--;
  53. }
  54. }
  55. }
  56. int MainTimerMultiplexer::isValidTimerClientPtr(TimerClient *tc, api_dependent *dep) {
  57. // try {
  58. __try {
  59. api_dependent *d = tc->timerclient_getDependencyPtr();
  60. if (d != dep) return 0;
  61. //} catch (...) {
  62. } __except (1) {
  63. return 0;
  64. }
  65. return 1;
  66. }
  67. void MainTimerMultiplexer::onMultiplexedTimer(void *data, int skip, int mssincelasttimer) {
  68. assert(data != NULL);
  69. VirtualTimer *t = static_cast<VirtualTimer *>(data);
  70. if (!isValidTimerClientPtr(t->client, t->dep)) {
  71. //DebugString("TIMER MULTIPLEXER WARNING: TimerClient %X (%s) is no longer valid! (%d)\n", t->client, t->name.getValue(), t->id);
  72. remove(t->client, -1);
  73. t->client = 0;
  74. } else {
  75. TimerClient *mc = t->client->timerclient_getMasterClient();
  76. if (mc) masters.addItem(mc);
  77. t->client->timerclient_setSkipped(skip);
  78. t->client->timerclient_setTimerDelay(mssincelasttimer);
  79. t->client->timerclient_timerCallback(t->id);
  80. // -----------------------------------------------------------------------
  81. // WARNING
  82. //
  83. // below this line, you can no longer assume that t is pointing at valid
  84. // memory, because timerCallback can eventually call remove
  85. // -----------------------------------------------------------------------
  86. }
  87. }
  88. void MainTimerMultiplexer::onServerTimer() {
  89. TimerMultiplexer::onServerTimer();
  90. TimerClient *last = NULL;
  91. for (int i=0;i<masters.getNumItems();i++) {
  92. TimerClient *t = masters.enumItem(i);
  93. if (t == last) continue;
  94. t->timerclient_onMasterClientMultiplex();
  95. last = t;
  96. }
  97. masters.removeAll();
  98. }
  99. int MainTimerMultiplexer::haveClient(TimerClient *client) {
  100. for (int i=0;i<timerclients.getNumItems();i++)
  101. {
  102. VirtualTimer *vt = timerclients.enumItem(i);
  103. if (vt && vt->client == client)
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. int MainTimerMultiplexer::viewer_onItemDeleted(api_dependent *item) {
  109. for (int i=0;i<timerclients.getNumItems();i++) {
  110. VirtualTimer *vt = timerclients.enumItem(i);
  111. if (vt->dep == item) {
  112. remove(vt->client, -1);
  113. return 1;
  114. }
  115. }
  116. return 1;
  117. }