websocketpp.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <websocketpp/server.hpp>
  2. #include <websocketpp/config/asio_no_tls.hpp>
  3. namespace websocketpp
  4. {
  5. typedef lib::weak_ptr<void> connection_hdl;
  6. template <typename config>
  7. class endpoint : public config::socket_type
  8. {
  9. typedef lib::shared_ptr<lib::asio::steady_timer> timer_ptr;
  10. typedef typename connection_type::ptr connection_ptr;
  11. typedef typename connection_type::message_ptr message_ptr;
  12. typedef lib::function<void(connection_hdl)> open_handler;
  13. typedef lib::function<void(connection_hdl)> close_handler;
  14. typedef lib::function<void(connection_hdl)> http_handler;
  15. typedef lib::function<void(connection_hdl, message_ptr)>
  16. message_handler;
  17. /* websocketpp::log::alevel::none 禁⽌打印所有⽇志*/
  18. void set_access_channels(log::level channels); /*设置⽇志打印等级*/
  19. void clear_access_channels(log::level channels); /*清除指定等级的⽇志*/
  20. /*设置指定事件的回调函数*/
  21. void set_open_handler(open_handler h); /*websocket握⼿成功回调处理函数*/
  22. void set_close_handler(close_handler h); /*websocket连接关闭回调处理函数*/
  23. void set_message_handler(message_handler h); /*websocket消息回调处理函数*/
  24. void set_http_handler(http_handler h); /*http请求回调处理函数*/
  25. /*发送数据接⼝*/
  26. void send(connection_hdl hdl, std::string &payload,
  27. frame::opcode::value op);
  28. void send(connection_hdl hdl, void *payload, size_t len,
  29. frame::opcode::value op);
  30. /*关闭连接接⼝*/
  31. void close(connection_hdl hdl, close::status::value code, std::string &reason);
  32. /*获取connection_hdl 对应连接的connection_ptr*/
  33. connection_ptr get_con_from_hdl(connection_hdl hdl);
  34. /*websocketpp基于asio框架实现,init_asio⽤于初始化asio框架中的io_service调度
  35. 器*/
  36. void init_asio();
  37. /*设置是否启⽤地址重⽤*/
  38. void set_reuse_addr(bool value);
  39. /*设置endpoint的绑定监听端⼝*/
  40. void listen(uint16_t port);
  41. /*对io_service对象的run接⼝封装,⽤于启动服务器*/
  42. std::size_t run();
  43. /*websocketpp提供的定时器,以毫秒为单位*/
  44. timer_ptr set_timer(long duration, timer_handler callback);
  45. };
  46. template <typename config>
  47. class server : public endpoint<connection<config>, config>
  48. {
  49. /*初始化并启动服务端监听连接的accept事件处理*/
  50. void start_accept();
  51. };
  52. template <typename config>
  53. class connection
  54. : public config::transport_type::transport_con_type,
  55. public config::connection_base
  56. {
  57. /*发送数据接⼝*/
  58. error_code send(std::string &payload, frame::opcode::value
  59. op = frame::opcode::text);
  60. /*获取http请求头部*/
  61. std::string const &get_request_header(std::string const &key)
  62. /*获取请求正⽂*/
  63. std::string const &get_request_body();
  64. /*设置响应状态码*/
  65. void set_status(http::status_code::value code);
  66. /*设置http响应正⽂*/
  67. void set_body(std::string const &value);
  68. /*添加http响应头部字段*/
  69. void append_header(std::string const &key, std::string const &val);
  70. /*获取http请求对象*/
  71. request_type const &get_request();
  72. /*获取connection_ptr 对应的 connection_hdl */
  73. connection_hdl get_handle();
  74. };
  75. namespace http
  76. {
  77. namespace parser
  78. {
  79. class parser
  80. {
  81. std::string const &get_header(std::string const &key);
  82. };
  83. class request : public parser
  84. {
  85. /*获取请求⽅法*/
  86. std::string const &get_method();
  87. /*获取请求uri接⼝*/
  88. std::string const &get_uri();
  89. };
  90. }
  91. };
  92. namespace message_buffer
  93. {
  94. /*获取websocket请求中的payload数据类型*/
  95. frame::opcode::value get_opcode();
  96. /*获取websocket中payload数据*/
  97. std::string const &get_payload();
  98. };
  99. namespace log
  100. {
  101. struct alevel
  102. {
  103. static level const none = 0x0;
  104. static level const connect = 0x1;
  105. static level const disconnect = 0x2;
  106. static level const control = 0x4;
  107. static level const frame_header = 0x8;
  108. static level const frame_payload = 0x10;
  109. static level const message_header = 0x20;
  110. static level const message_payload = 0x40;
  111. static level const endpoint = 0x80;
  112. static level const debug_handshake = 0x100;
  113. static level const debug_close = 0x200;
  114. static level const devel = 0x400;
  115. static level const app = 0x800;
  116. static level const http = 0x1000;
  117. static level const fail = 0x2000;
  118. static level const access_core = 0x00003003;
  119. static level const all = 0xffffffff;
  120. };
  121. }
  122. namespace http
  123. {
  124. namespace status_code
  125. {
  126. enum value
  127. {
  128. uninitialized = 0,
  129. continue_code = 100,
  130. switching_protocols = 101,
  131. ok = 200,
  132. created = 201,
  133. accepted = 202,
  134. non_authoritative_information = 203,
  135. no_content = 204,
  136. reset_content = 205,
  137. partial_content = 206,
  138. multiple_choices = 300,
  139. moved_permanently = 301,
  140. found = 302,
  141. see_other = 303,
  142. not_modified = 304,
  143. use_proxy = 305,
  144. temporary_redirect = 307,
  145. bad_request = 400,
  146. unauthorized = 401,
  147. payment_required = 402,
  148. forbidden = 403,
  149. not_found = 404,
  150. method_not_allowed = 405,
  151. not_acceptable = 406,
  152. proxy_authentication_required = 407,
  153. request_timeout = 408,
  154. conflict = 409,
  155. gone = 410,
  156. length_required = 411,
  157. precondition_failed = 412,
  158. request_entity_too_large = 413,
  159. request_uri_too_long = 414,
  160. unsupported_media_type = 415,
  161. request_range_not_satisfiable = 416,
  162. expectation_failed = 417,
  163. im_a_teapot = 418,
  164. upgrade_required = 426,
  165. precondition_required = 428,
  166. too_many_requests = 429,
  167. request_header_fields_too_large = 431,
  168. internal_server_error = 500,
  169. not_implemented = 501,
  170. bad_gateway = 502,
  171. service_unavailable = 503,
  172. gateway_timeout = 504,
  173. http_version_not_supported = 505,
  174. not_extended = 510,
  175. network_authentication_required = 511
  176. };
  177. }
  178. }
  179. namespace frame
  180. {
  181. namespace opcode
  182. {
  183. enum value
  184. {
  185. continuation = 0x0,
  186. text = 0x1,
  187. binary = 0x2,
  188. rsv3 = 0x3,
  189. rsv4 = 0x4,
  190. rsv5 = 0x5,
  191. rsv6 = 0x6,
  192. rsv7 = 0x7,
  193. close = 0x8,
  194. ping = 0x9,
  195. pong = 0xA,
  196. control_rsvb = 0xB,
  197. control_rsvc = 0xC,
  198. control_rsvd = 0xD,
  199. control_rsve = 0xE,
  200. control_rsvf = 0xF,
  201. };
  202. }
  203. }
  204. }