You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
8.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_MESSAGEQUEUE_H_
  28. #define TALK_BASE_MESSAGEQUEUE_H_
  29. #include <algorithm>
  30. #include <cstring>
  31. #include <list>
  32. #include <queue>
  33. #include <vector>
  34. #include "talk/base/basictypes.h"
  35. #include "talk/base/constructormagic.h"
  36. #include "talk/base/criticalsection.h"
  37. #include "talk/base/messagehandler.h"
  38. #include "talk/base/scoped_ptr.h"
  39. #include "talk/base/scoped_ref_ptr.h"
  40. #include "talk/base/sigslot.h"
  41. #include "talk/base/socketserver.h"
  42. #include "talk/base/timeutils.h"
  43. namespace talk_base {
  44. struct Message;
  45. class MessageQueue;
  46. // MessageQueueManager does cleanup of of message queues
  47. class MessageQueueManager {
  48. public:
  49. static MessageQueueManager* Instance();
  50. void Add(MessageQueue *message_queue);
  51. void Remove(MessageQueue *message_queue);
  52. void Clear(MessageHandler *handler);
  53. private:
  54. MessageQueueManager();
  55. ~MessageQueueManager();
  56. static MessageQueueManager* instance_;
  57. // This list contains 'active' MessageQueues.
  58. std::vector<MessageQueue *> message_queues_;
  59. CriticalSection crit_;
  60. };
  61. // Derive from this for specialized data
  62. // App manages lifetime, except when messages are purged
  63. class MessageData {
  64. public:
  65. MessageData() {}
  66. virtual ~MessageData() {}
  67. };
  68. template <class T>
  69. class TypedMessageData : public MessageData {
  70. public:
  71. explicit TypedMessageData(const T& data) : data_(data) { }
  72. const T& data() const { return data_; }
  73. T& data() { return data_; }
  74. private:
  75. T data_;
  76. };
  77. // Like TypedMessageData, but for pointers that require a delete.
  78. template <class T>
  79. class ScopedMessageData : public MessageData {
  80. public:
  81. explicit ScopedMessageData(T* data) : data_(data) { }
  82. const scoped_ptr<T>& data() const { return data_; }
  83. scoped_ptr<T>& data() { return data_; }
  84. private:
  85. scoped_ptr<T> data_;
  86. };
  87. // Like ScopedMessageData, but for reference counted pointers.
  88. template <class T>
  89. class ScopedRefMessageData : public MessageData {
  90. public:
  91. explicit ScopedRefMessageData(T* data) : data_(data) { }
  92. const scoped_refptr<T>& data() const { return data_; }
  93. scoped_refptr<T>& data() { return data_; }
  94. private:
  95. scoped_refptr<T> data_;
  96. };
  97. template<class T>
  98. inline MessageData* WrapMessageData(const T& data) {
  99. return new TypedMessageData<T>(data);
  100. }
  101. template<class T>
  102. inline const T& UseMessageData(MessageData* data) {
  103. return static_cast< TypedMessageData<T>* >(data)->data();
  104. }
  105. template<class T>
  106. class DisposeData : public MessageData {
  107. public:
  108. explicit DisposeData(T* data) : data_(data) { }
  109. virtual ~DisposeData() { delete data_; }
  110. private:
  111. T* data_;
  112. };
  113. const uint32 MQID_ANY = static_cast<uint32>(-1);
  114. const uint32 MQID_DISPOSE = static_cast<uint32>(-2);
  115. // No destructor
  116. struct Message {
  117. Message() {
  118. memset(this, 0, sizeof(*this));
  119. }
  120. inline bool Match(MessageHandler* handler, uint32 id) const {
  121. return (handler == NULL || handler == phandler)
  122. && (id == MQID_ANY || id == message_id);
  123. }
  124. MessageHandler *phandler;
  125. uint32 message_id;
  126. MessageData *pdata;
  127. uint32 ts_sensitive;
  128. };
  129. typedef std::list<Message> MessageList;
  130. // DelayedMessage goes into a priority queue, sorted by trigger time. Messages
  131. // with the same trigger time are processed in num_ (FIFO) order.
  132. class DelayedMessage {
  133. public:
  134. DelayedMessage(int delay, uint32 trigger, uint32 num, const Message& msg)
  135. : cmsDelay_(delay), msTrigger_(trigger), num_(num), msg_(msg) { }
  136. bool operator< (const DelayedMessage& dmsg) const {
  137. return (dmsg.msTrigger_ < msTrigger_)
  138. || ((dmsg.msTrigger_ == msTrigger_) && (dmsg.num_ < num_));
  139. }
  140. int cmsDelay_; // for debugging
  141. uint32 msTrigger_;
  142. uint32 num_;
  143. Message msg_;
  144. };
  145. class MessageQueue {
  146. public:
  147. explicit MessageQueue(SocketServer* ss = NULL);
  148. virtual ~MessageQueue();
  149. SocketServer* socketserver() { return ss_; }
  150. void set_socketserver(SocketServer* ss);
  151. // Note: The behavior of MessageQueue has changed. When a MQ is stopped,
  152. // futher Posts and Sends will fail. However, any pending Sends and *ready*
  153. // Posts (as opposed to unexpired delayed Posts) will be delivered before
  154. // Get (or Peek) returns false. By guaranteeing delivery of those messages,
  155. // we eliminate the race condition when an MessageHandler and MessageQueue
  156. // may be destroyed independently of each other.
  157. virtual void Quit();
  158. virtual bool IsQuitting();
  159. virtual void Restart();
  160. // Get() will process I/O until:
  161. // 1) A message is available (returns true)
  162. // 2) cmsWait seconds have elapsed (returns false)
  163. // 3) Stop() is called (returns false)
  164. virtual bool Get(Message *pmsg, int cmsWait = kForever,
  165. bool process_io = true);
  166. virtual bool Peek(Message *pmsg, int cmsWait = 0);
  167. virtual void Post(MessageHandler *phandler, uint32 id = 0,
  168. MessageData *pdata = NULL, bool time_sensitive = false);
  169. virtual void PostDelayed(int cmsDelay, MessageHandler *phandler,
  170. uint32 id = 0, MessageData *pdata = NULL) {
  171. return DoDelayPost(cmsDelay, TimeAfter(cmsDelay), phandler, id, pdata);
  172. }
  173. virtual void PostAt(uint32 tstamp, MessageHandler *phandler,
  174. uint32 id = 0, MessageData *pdata = NULL) {
  175. return DoDelayPost(TimeUntil(tstamp), tstamp, phandler, id, pdata);
  176. }
  177. virtual void Clear(MessageHandler *phandler, uint32 id = MQID_ANY,
  178. MessageList* removed = NULL);
  179. virtual void Dispatch(Message *pmsg);
  180. virtual void ReceiveSends();
  181. // Amount of time until the next message can be retrieved
  182. virtual int GetDelay();
  183. bool empty() const { return size() == 0u; }
  184. size_t size() const {
  185. CritScope cs(&crit_); // msgq_.size() is not thread safe.
  186. return msgq_.size() + dmsgq_.size() + (fPeekKeep_ ? 1u : 0u);
  187. }
  188. // Internally posts a message which causes the doomed object to be deleted
  189. template<class T> void Dispose(T* doomed) {
  190. if (doomed) {
  191. Post(NULL, MQID_DISPOSE, new DisposeData<T>(doomed));
  192. }
  193. }
  194. // When this signal is sent out, any references to this queue should
  195. // no longer be used.
  196. sigslot::signal0<> SignalQueueDestroyed;
  197. protected:
  198. class PriorityQueue : public std::priority_queue<DelayedMessage> {
  199. public:
  200. container_type& container() { return c; }
  201. void reheap() { make_heap(c.begin(), c.end(), comp); }
  202. };
  203. void EnsureActive();
  204. void DoDelayPost(int cmsDelay, uint32 tstamp, MessageHandler *phandler,
  205. uint32 id, MessageData* pdata);
  206. // The SocketServer is not owned by MessageQueue.
  207. SocketServer* ss_;
  208. // If a server isn't supplied in the constructor, use this one.
  209. scoped_ptr<SocketServer> default_ss_;
  210. bool fStop_;
  211. bool fPeekKeep_;
  212. Message msgPeek_;
  213. // A message queue is active if it has ever had a message posted to it.
  214. // This also corresponds to being in MessageQueueManager's global list.
  215. bool active_;
  216. MessageList msgq_;
  217. PriorityQueue dmsgq_;
  218. uint32 dmsgq_next_num_;
  219. mutable CriticalSection crit_;
  220. private:
  221. DISALLOW_COPY_AND_ASSIGN(MessageQueue);
  222. };
  223. } // namespace talk_base
  224. #endif // TALK_BASE_MESSAGEQUEUE_H_