WSHandler.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #ifdef URHO3D_WEBSOCKETS
  23. #include "../../IO/Log.h"
  24. #include "WSHandler.h"
  25. #include "WSPacket.h"
  26. #ifndef __EMSCRIPTEN__
  27. #include <libwebsockets.h>
  28. #endif
  29. using namespace Urho3D;
  30. WSHandler::WSHandler()
  31. {
  32. }
  33. WSHandler::~WSHandler()
  34. {
  35. }
  36. void WSHandler::AddIncomingPacket(const WSPacket& packet)
  37. {
  38. Urho3D::MutexLock lock(GetIncomingMutex());
  39. incomingPackets_.Push(packet);
  40. }
  41. WSPacket* WSHandler::GetIncomingPacket()
  42. {
  43. return &incomingPackets_.Front();
  44. }
  45. void WSHandler::RemoveIncomingPacket()
  46. {
  47. if (GetNumIncomingPackets() > 0) {
  48. Urho3D::MutexLock lock(GetIncomingMutex());
  49. incomingPackets_.Front().second_.Clear();
  50. incomingPackets_.PopFront();
  51. }
  52. }
  53. void WSHandler::AddOutgoingPacket(const WSPacket& packet)
  54. {
  55. Urho3D::MutexLock lock(GetOutgoingMutex());
  56. outgoingPackets_[packet.first_].Push(packet);
  57. }
  58. WSPacket* WSHandler::GetOutgoingPacket(const WSConnection& ws)
  59. {
  60. if (outgoingPackets_.Contains(ws)) {
  61. return &outgoingPackets_[ws].Front();
  62. }
  63. return nullptr;
  64. }
  65. void WSHandler::RemoveOutgoingPacket(const WSConnection& ws)
  66. {
  67. if (GetNumOutgoingPackets(ws) > 0) {
  68. Urho3D::MutexLock lock(GetOutgoingMutex());
  69. outgoingPackets_[ws].Front().second_.Clear();
  70. outgoingPackets_[ws].PopFront();
  71. }
  72. }
  73. int WSHandler::GetNumOutgoingPackets(const WSConnection& ws)
  74. {
  75. if (outgoingPackets_.Contains(ws)) {
  76. return outgoingPackets_[ws].Size();
  77. }
  78. return 0;
  79. }
  80. List<WSPacket>* WSHandler::GetOutgoingPackets(const WSConnection& ws)
  81. {
  82. if (outgoingPackets_.Contains(ws)) {
  83. return &outgoingPackets_[ws];
  84. }
  85. return nullptr;
  86. }
  87. void WSHandler::OutputWSLog(int level, const char *line)
  88. {
  89. #ifndef __EMSCRIPTEN__
  90. switch(level) {
  91. case LLL_NOTICE:
  92. URHO3D_LOGINFOF("WSClient: %s", line);
  93. break;
  94. case LLL_WARN:
  95. URHO3D_LOGWARNINGF("WSClient: %s", line);
  96. break;
  97. case LLL_ERR:
  98. URHO3D_LOGERRORF("WSClient: %s", line);
  99. break;
  100. case LLL_USER:
  101. URHO3D_LOGINFOF("WSClient: %s", line);
  102. break;
  103. }
  104. #endif
  105. }
  106. #endif