You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2015, Peter Thorson. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the WebSocket++ Project nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. #ifndef WEBSOCKETPP_TRANSPORT_ASIO_SOCKET_BASE_HPP
  28. #define WEBSOCKETPP_TRANSPORT_ASIO_SOCKET_BASE_HPP
  29. #include <string>
  30. #include <websocketpp/common/asio.hpp>
  31. #include <websocketpp/common/connection_hdl.hpp>
  32. #include <websocketpp/common/cpp11.hpp>
  33. #include <websocketpp/common/functional.hpp>
  34. #include <websocketpp/common/memory.hpp>
  35. #include <websocketpp/common/system_error.hpp>
  36. // Interface that sockets/security policies must implement
  37. /*
  38. * Endpoint Interface
  39. *
  40. * bool is_secure() const;
  41. * @return Whether or not the endpoint creates secure connections
  42. *
  43. * lib::error_code init(socket_con_ptr scon);
  44. * Called by the transport after a new connection is created to initialize
  45. * the socket component of the connection.
  46. * @param scon Pointer to the socket component of the connection
  47. * @return Error code (empty on success)
  48. */
  49. // Connection
  50. // TODO
  51. // set_hostname(std::string hostname)
  52. // pre_init(init_handler);
  53. // post_init(init_handler);
  54. namespace websocketpp
  55. {
  56. namespace transport
  57. {
  58. namespace asio
  59. {
  60. namespace socket
  61. {
  62. typedef lib::function<void(const lib::asio::error_code &)> shutdown_handler;
  63. /**
  64. * The transport::asio::socket::* classes are a set of security/socket related
  65. * policies and support code for the ASIO transport types.
  66. */
  67. /// Errors related to asio transport sockets
  68. namespace error
  69. {
  70. enum value
  71. {
  72. /// Catch-all error for security policy errors that don't fit in other
  73. /// categories
  74. security = 1,
  75. /// Catch-all error for socket component errors that don't fit in other
  76. /// categories
  77. socket,
  78. /// A function was called in a state that it was illegal to do so.
  79. invalid_state,
  80. /// The application was prompted to provide a TLS context and it was
  81. /// empty or otherwise invalid
  82. invalid_tls_context,
  83. /// TLS Handshake Timeout
  84. tls_handshake_timeout,
  85. /// pass_through from underlying library
  86. pass_through,
  87. /// Required tls_init handler not present
  88. missing_tls_init_handler,
  89. /// TLS Handshake Failed
  90. tls_handshake_failed,
  91. /// Failed to set TLS SNI hostname
  92. tls_failed_sni_hostname
  93. };
  94. } // namespace error
  95. /// Error category related to asio transport socket policies
  96. class socket_category : public lib::error_category
  97. {
  98. public:
  99. const char * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_
  100. {
  101. return "websocketpp.transport.asio.socket";
  102. }
  103. std::string message(int value) const
  104. {
  105. switch (value)
  106. {
  107. case error::security : return "Security policy error";
  108. case error::socket : return "Socket component error";
  109. case error::invalid_state : return "Invalid state";
  110. case error::invalid_tls_context : return "Invalid or empty TLS context supplied";
  111. case error::tls_handshake_timeout : return "TLS handshake timed out";
  112. case error::pass_through : return "Pass through from socket policy";
  113. case error::missing_tls_init_handler: return "Required tls_init handler not present.";
  114. case error::tls_handshake_failed : return "TLS handshake failed";
  115. case error::tls_failed_sni_hostname : return "Failed to set TLS SNI hostname";
  116. default : return "Unknown";
  117. }
  118. }
  119. };
  120. inline const lib::error_category & get_socket_category()
  121. {
  122. static socket_category instance;
  123. return instance;
  124. }
  125. inline lib::error_code make_error_code(error::value e)
  126. {
  127. return lib::error_code(static_cast<int>(e), get_socket_category());
  128. }
  129. /// Type of asio transport socket policy initialization handlers
  130. typedef lib::function<void(const lib::error_code &)> init_handler;
  131. } // namespace socket
  132. } // namespace asio
  133. } // namespace transport
  134. } // namespace websocketpp
  135. #endif // WEBSOCKETPP_TRANSPORT_ASIO_SOCKET_BASE_HPP