base.hpp 4.8 KB

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