WSClient.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/Object.h"
  25. #include "../../Core/Mutex.h"
  26. #include "../../IO/VectorBuffer.h"
  27. #include "WSHandler.h"
  28. namespace Urho3D {
  29. class Network;
  30. class WorkItem;
  31. /// Client state
  32. enum WSClientState {
  33. WCS_CONNECTING,
  34. WCS_CONNECTED,
  35. WCS_CONNECTION_FAILED,
  36. WCS_DISCONNECTED,
  37. };
  38. /// Websocket client handler
  39. class WSClient: public WSHandler, public Object {
  40. URHO3D_OBJECT(WSClient, Object);
  41. public:
  42. /// Construct.
  43. WSClient(Context* context);
  44. /// Destruct.
  45. ~WSClient();
  46. /// Connect to Websockets server
  47. int Connect(const String& address, unsigned short port);
  48. /// Run the update loop to send/fetch all the buffered messages
  49. void Update(float timestep);
  50. /// Disconnect from the server
  51. void Disconnect();
  52. /// Set the client connection state
  53. void SetState(WSClientState state);
  54. /// Set the Websocket connection information for this client
  55. void SetWSConnection(lws *ws);
  56. /// Get Websocket connection information for this client
  57. lws* GetWSConnection() { return ws_; };
  58. /// Get server address
  59. const String& GetAddress() const { return address_; };
  60. /// Get server port
  61. unsigned short GetPort() { return port_; };
  62. /// Get server protocol used for connection
  63. const String& GetServerProtocol() const { return serverProtocol_; };
  64. /// Send out client messages
  65. void SendOutMessages(lws *ws);
  66. private:
  67. /// Handle work item finished event
  68. void HandleWorkItemFinished(StringHash eventType, VariantMap& eventData);
  69. /// Work item which runs the WS service loop
  70. SharedPtr<WorkItem> serviceWorkItem_;
  71. /// Websocket connection information
  72. struct lws *ws_;
  73. /// Current state of the client
  74. WSClientState currentState_;
  75. /// Next state of the client that will be handled in the next update loop
  76. WSClientState nextState_;
  77. /// Server address
  78. String address_;
  79. /// server port
  80. unsigned short port_;
  81. /// Server protocol used for connection
  82. String serverProtocol_;
  83. };
  84. }