WebSocket.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  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 "../Core/Object.h"
  24. #include "../Container/Str.h"
  25. #include "../Container/Vector.h"
  26. #include <memory>
  27. namespace asio
  28. {
  29. class io_service;
  30. }
  31. namespace Atomic
  32. {
  33. /// WebSocket connection state
  34. enum WebSocketState
  35. {
  36. WS_CONNECTING = 0, // Establishing connection
  37. WS_OPEN, // WebSocket is open
  38. WS_CLOSING, // WebSocket is being closed (and is probably still open)
  39. WS_CLOSED, // WebSocket is closed or was disconnected
  40. WS_INVALID, // Invalid state
  41. WS_FAIL_TO_CONNECT // WebSocket attempted to open, but the server refused
  42. };
  43. enum WebSocketMessageType
  44. {
  45. WSMT_CONTINUATION = 0x0,
  46. WSMT_TEXT = 0x1,
  47. WSMT_BINARY = 0x2,
  48. WSMT_RSV3 = 0x3,
  49. WSMT_RSV4 = 0x4,
  50. WSMT_RSV5 = 0x5,
  51. WSMT_RSV6 = 0x6,
  52. WSMT_RSV7 = 0x7,
  53. WSMT_CLOSE = 0x8,
  54. WSMT_PING = 0x9,
  55. WSMT_PONG = 0xA,
  56. WSMT_CONTROL_RSVB = 0xB,
  57. WSMT_CONTROL_RSVC = 0xC,
  58. WSMT_CONTROL_RSVD = 0xD,
  59. WSMT_CONTROL_RSVE = 0xE,
  60. WSMT_CONTROL_RSVF = 0xF
  61. };
  62. struct WebSocketInternalState;
  63. /// A WebSocket connection.
  64. class ATOMIC_API WebSocket : public Object
  65. {
  66. friend class Web;
  67. ATOMIC_OBJECT(WebSocket, Object)
  68. public:
  69. /// Construct with parameters.
  70. WebSocket(Context* context, const String& url);
  71. /// Destruct. Release the connection object.
  72. ~WebSocket();
  73. /// Return URL used in the request.
  74. const String& GetURL() const;
  75. /// Return error. Only non-empty in the error state.
  76. String GetError() const;
  77. /// Return connection state.
  78. WebSocketState GetState() const;
  79. /// Send a message.
  80. void Send(const String& message);
  81. /// Send a binary message.
  82. void SendBinary(const PODVector<unsigned char>& message);
  83. /// Disconnect the WebSocket.
  84. void Close();
  85. /// Attempt to reconnect the WebSocket.
  86. void OpenAgain();
  87. /// Return whether connection is in the open state.
  88. bool IsOpen() const { return GetState() == WS_OPEN; }
  89. private:
  90. void setup(asio::io_service *service);
  91. std::shared_ptr<WebSocketInternalState> is_;
  92. };
  93. }