WSServer.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "../../Core/Mutex.h"
  25. #include "../../Core/Object.h"
  26. #include "WSConnection.h"
  27. #include "WSHandler.h"
  28. namespace Urho3D {
  29. class Network;
  30. class WorkItem;
  31. /// Server state
  32. enum WSServerState {
  33. WSS_STOPPED,
  34. WSS_RUNNING
  35. };
  36. /// Websocket server handler
  37. class WSServer: public WSHandler, public Object {
  38. URHO3D_OBJECT(WSServer, Object);
  39. public:
  40. /// Construct.
  41. explicit WSServer(Context* context);
  42. /// Destruct.
  43. ~WSServer();
  44. /// Start whe Websockets server
  45. int StartServer(unsigned short port, unsigned int maxConnections);
  46. /// Stop the Websockets server
  47. void StopServer();
  48. /// Run the update loop to send/fetch all the buffered messages
  49. void Update(float timestep);
  50. /// New client connection to the server established, add it to the queue
  51. void AddPendingConnection(lws* ws);
  52. /// Client connection lost, add it to the queue
  53. void AddClosedConnection(lws* ws);
  54. /// Mark client for disconnection
  55. void MarkForDisconnect(const WSConnection& ws);
  56. /// Check if connection should be closed
  57. bool IsMarkedForDisconnect(lws* ws);
  58. /// Remove close connection
  59. void RemoveDisconnected(lws* ws);
  60. /// Set the server state
  61. void SetState(WSServerState state);
  62. /// Get maximum number of connections allowed
  63. unsigned short GetMaxConnections() { return maxConnections_; };
  64. private:
  65. /// Handle work item finished event
  66. void HandleWorkItemFinished(StringHash eventType, VariantMap& eventData);
  67. /// Work item which runs the WS service loop
  68. SharedPtr<WorkItem> serviceWorkItem_;
  69. /// Pending connections queue that should be processed inside the engine
  70. List<WSConnection> pendingConnections_;
  71. /// Connections that should be closed in the next service loop
  72. List<WSConnection> pendingDisconnects_;
  73. /// Close connections queue that should be cleared up from the engine
  74. List<WSConnection> closedConnections;
  75. /// Current state of the server
  76. WSServerState currentState_;
  77. /// Next state of the server that will be handled in the next update loop
  78. WSServerState nextState_;
  79. /// Max allowed connections to the server
  80. unsigned int maxConnections_;
  81. };
  82. }