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.

174 lines
5.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2014, 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_CLIENT_ENDPOINT_HPP
  28. #define WEBSOCKETPP_CLIENT_ENDPOINT_HPP
  29. #include <string>
  30. #include <websocketpp/common/system_error.hpp>
  31. #include <websocketpp/endpoint.hpp>
  32. #include <websocketpp/logger/levels.hpp>
  33. #include <websocketpp/uri.hpp>
  34. namespace websocketpp
  35. {
  36. /// Client endpoint role based on the given config
  37. /**
  38. *
  39. */
  40. template <typename config> class client : public endpoint<connection<config>, config>
  41. {
  42. public:
  43. /// Type of this endpoint
  44. typedef client<config> type;
  45. /// Type of the endpoint concurrency component
  46. typedef typename config::concurrency_type concurrency_type;
  47. /// Type of the endpoint transport component
  48. typedef typename config::transport_type transport_type;
  49. /// Type of the connections this server will create
  50. typedef connection<config> connection_type;
  51. /// Type of a shared pointer to the connections this server will create
  52. typedef typename connection_type::ptr connection_ptr;
  53. /// Type of the connection transport component
  54. typedef typename transport_type::transport_con_type transport_con_type;
  55. /// Type of a shared pointer to the connection transport component
  56. typedef typename transport_con_type::ptr transport_con_ptr;
  57. /// Type of the endpoint component of this server
  58. typedef endpoint<connection_type, config> endpoint_type;
  59. friend class connection<config>;
  60. explicit client() :
  61. endpoint_type(false)
  62. {
  63. endpoint_type::m_alog->write(log::alevel::devel, "client constructor");
  64. }
  65. /// Get a new connection
  66. /**
  67. * Creates and returns a pointer to a new connection to the given URI
  68. * suitable for passing to connect(connection_ptr). This method allows
  69. * applying connection specific settings before performing the opening
  70. * handshake.
  71. *
  72. * @param [in] location URI to open the connection to as a uri_ptr
  73. * @param [out] ec An status code indicating failure reasons, if any
  74. *
  75. * @return A connection_ptr to the new connection
  76. */
  77. connection_ptr get_connection(uri_ptr location, lib::error_code & ec)
  78. {
  79. if (location->get_secure() && ! transport_type::is_secure())
  80. {
  81. ec = error::make_error_code(error::endpoint_not_secure);
  82. return connection_ptr();
  83. }
  84. connection_ptr con = endpoint_type::create_connection();
  85. if (! con)
  86. {
  87. ec = error::make_error_code(error::con_creation_failed);
  88. return con;
  89. }
  90. con->set_uri(location);
  91. ec = lib::error_code();
  92. return con;
  93. }
  94. /// Get a new connection (string version)
  95. /**
  96. * Creates and returns a pointer to a new connection to the given URI
  97. * suitable for passing to connect(connection_ptr). This overload allows
  98. * default construction of the uri_ptr from a standard string.
  99. *
  100. * @param [in] u URI to open the connection to as a string
  101. * @param [out] ec An status code indicating failure reasons, if any
  102. *
  103. * @return A connection_ptr to the new connection
  104. */
  105. connection_ptr get_connection(const std::string & u, lib::error_code & ec)
  106. {
  107. uri_ptr location = lib::make_shared<uri>(u);
  108. if (! location->get_valid())
  109. {
  110. ec = error::make_error_code(error::invalid_uri);
  111. return connection_ptr();
  112. }
  113. return get_connection(location, ec);
  114. }
  115. /// Begin the connection process for the given connection
  116. /**
  117. * Initiates the opening connection handshake for connection con. Exact
  118. * behavior depends on the underlying transport policy.
  119. *
  120. * @param con The connection to connect
  121. *
  122. * @return The pointer to the connection originally passed in.
  123. */
  124. connection_ptr connect(connection_ptr con)
  125. {
  126. // Ask transport to perform a connection
  127. transport_type::async_connect(lib::static_pointer_cast<transport_con_type>(con), con->get_uri(), lib::bind(&type::handle_connect, this, con, lib::placeholders::_1));
  128. return con;
  129. }
  130. private:
  131. // handle_connect
  132. void handle_connect(connection_ptr con, const lib::error_code & ec)
  133. {
  134. if (ec)
  135. {
  136. con->terminate(ec);
  137. endpoint_type::m_elog->write(log::elevel::rerror, "handle_connect error: " + ec.message());
  138. }
  139. else
  140. {
  141. endpoint_type::m_alog->write(log::alevel::connect, "Successful connection");
  142. con->start();
  143. }
  144. }
  145. };
  146. } // namespace websocketpp
  147. #endif // WEBSOCKETPP_CLIENT_ENDPOINT_HPP