WSHandler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #pragma once
  23. #include "../../Container/List.h"
  24. #include "../../Container/HashMap.h"
  25. #include "../../Core/Mutex.h"
  26. #include "../../IO/VectorBuffer.h"
  27. #include "WSConnection.h"
  28. #include "WSPacket.h"
  29. namespace Urho3D {
  30. /// Basic Websocket connection handler used by WS client and server
  31. class WSHandler {
  32. public:
  33. /// Construct.
  34. WSHandler();
  35. /// Destruct.
  36. ~WSHandler();
  37. /// Get incoming packet mutex
  38. Mutex& GetIncomingMutex() { return incomingMutex_; }
  39. /// Add incoming packet to the buffer
  40. void AddIncomingPacket(const WSPacket& packet);
  41. /// Get first incoming packet
  42. WSPacket* GetIncomingPacket();
  43. /// Remove first incoming packet
  44. void RemoveIncomingPacket();
  45. /// Get total number of incoming packets
  46. int GetNumIncomingPackets() { return incomingPackets_.Size(); }
  47. /// Get outgoing packet mutex
  48. Mutex& GetOutgoingMutex() { return outgoingMutex_; }
  49. /// Add outgoing packet to the buffer
  50. void AddOutgoingPacket(const WSPacket& packet);
  51. /// Get first outgoing packet for specific WS connection
  52. WSPacket* GetOutgoingPacket(const WSConnection& ws);
  53. /// Remove first outgoing packet for specific WS connection
  54. void RemoveOutgoingPacket(const WSConnection& ws);
  55. /// Get total number of outgoing packets for specific WS connection
  56. int GetNumOutgoingPackets(const WSConnection& ws);
  57. /// Get all outgoing packets for specific WS connection
  58. List<WSPacket>* GetOutgoingPackets(const WSConnection& ws);
  59. /// Retur all connection buffered outgoing packets
  60. HashMap<WSConnection, List<WSPacket>>* GetAllOutgoingPackets() { return &outgoingPackets_; };
  61. /// Handle all libwebsocket logs
  62. static void OutputWSLog(int level, const char *line);
  63. protected:
  64. /// Incoming packet mutex
  65. Mutex incomingMutex_;
  66. /// Outgoing packet mutex
  67. Mutex outgoingMutex_;
  68. /// Incoming packet buffer
  69. List<WSPacket> incomingPackets_;
  70. /// Outgoing packet buffer
  71. HashMap<WSConnection, List<WSPacket>> outgoingPackets_;
  72. };
  73. }