NetworkQueue.lua.txt 716 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. -- Socket消息队列
  2. local NetworkQueue = {}
  3. local queue = require("Base/Utils.lua"):CreateQueue()
  4. local map = {}
  5. function NetworkQueue:PushUidOnSend(uid)
  6. queue:Enqueue(uid)
  7. end
  8. function NetworkQueue:PushMsgOnRecv(uid, data)
  9. map[uid] = data
  10. end
  11. function NetworkQueue:TryPopMsg()
  12. local uid = queue:Peek()
  13. if (uid == nil) then
  14. return nil
  15. end
  16. local ret = map[uid]
  17. if ret == nil then
  18. print("NetworkQueue uid is not inside the queue. Msg may not received yet.")
  19. return nil
  20. end
  21. queue:Dequeue()
  22. map[uid] = nil
  23. return ret
  24. end
  25. function NetworkQueue:Reset()
  26. queue = require("Base/Utils.lua"):CreateQueue()
  27. map = {}
  28. end
  29. return NetworkQueue