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.

140 lines
4.9 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_TRANSPORT_IOSTREAM_BASE_HPP
  28. #define WEBSOCKETPP_TRANSPORT_IOSTREAM_BASE_HPP
  29. #include <string>
  30. #include <vector>
  31. #include <websocketpp/common/connection_hdl.hpp>
  32. #include <websocketpp/common/cpp11.hpp>
  33. #include <websocketpp/common/functional.hpp>
  34. #include <websocketpp/common/system_error.hpp>
  35. #include <websocketpp/transport/base/connection.hpp>
  36. namespace websocketpp
  37. {
  38. namespace transport
  39. {
  40. /// Transport policy that uses STL iostream for I/O and does not support timers
  41. namespace iostream
  42. {
  43. /// The type and signature of the callback used by iostream transport to write
  44. typedef lib::function<lib::error_code(connection_hdl, const char *, size_t)> write_handler;
  45. /// The type and signature of the callback used by iostream transport to perform
  46. /// vectored writes.
  47. /**
  48. * If a vectored write handler is not set the standard write handler will be
  49. * called multiple times.
  50. */
  51. typedef lib::function<lib::error_code(connection_hdl, const std::vector<transport::buffer> & bufs)> vector_write_handler;
  52. /// The type and signature of the callback used by iostream transport to signal
  53. /// a transport shutdown.
  54. typedef lib::function<lib::error_code(connection_hdl)> shutdown_handler;
  55. /// iostream transport errors
  56. namespace error
  57. {
  58. enum value
  59. {
  60. /// Catch-all error for transport policy errors that don't fit in other
  61. /// categories
  62. general = 1,
  63. /// async_read_at_least call requested more bytes than buffer can store
  64. invalid_num_bytes,
  65. /// async_read called while another async_read was in progress
  66. double_read,
  67. /// An operation that requires an output stream was attempted before
  68. /// setting one.
  69. output_stream_required,
  70. /// stream error
  71. bad_stream
  72. };
  73. /// iostream transport error category
  74. class category : public lib::error_category
  75. {
  76. public:
  77. category() {}
  78. const char * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_
  79. {
  80. return "websocketpp.transport.iostream";
  81. }
  82. std::string message(int value) const
  83. {
  84. switch (value)
  85. {
  86. case general : return "Generic iostream transport policy error";
  87. case invalid_num_bytes : return "async_read_at_least call requested more bytes than buffer can store";
  88. case double_read : return "Async read already in progress";
  89. case output_stream_required: return "An output stream to be set before async_write can be used";
  90. case bad_stream : return "A stream operation returned ios::bad";
  91. default : return "Unknown";
  92. }
  93. }
  94. };
  95. /// Get a reference to a static copy of the iostream transport error category
  96. inline const lib::error_category & get_category()
  97. {
  98. static category instance;
  99. return instance;
  100. }
  101. /// Get an error code with the given value and the iostream transport category
  102. inline lib::error_code make_error_code(error::value e)
  103. {
  104. return lib::error_code(static_cast<int>(e), get_category());
  105. }
  106. } // namespace error
  107. } // namespace iostream
  108. } // namespace transport
  109. } // namespace websocketpp
  110. _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
  111. template <> struct is_error_code_enum<websocketpp::transport::iostream::error::value>
  112. {
  113. static const bool value = true;
  114. };
  115. _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
  116. #endif // WEBSOCKETPP_TRANSPORT_IOSTREAM_BASE_HPP