Blame view

3rdparty/websocketpp-0.8.2/examples/echo_server_both/echo_server_both.cpp 3.21 KB
73ef4ff3   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  #include <websocketpp/config/asio.hpp>
  #include <websocketpp/server.hpp>
  
  #include <iostream>
  
  // define types for two different server endpoints, one for each config we are
  // using
  typedef websocketpp::server<websocketpp::config::asio> server_plain;
  typedef websocketpp::server<websocketpp::config::asio_tls> server_tls;
  
  // alias some of the bind related functions as they are a bit long
  using websocketpp::lib::placeholders::_1;
  using websocketpp::lib::placeholders::_2;
  using websocketpp::lib::bind;
  
  // type of the ssl context pointer is long so alias it
  typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
  
  // The shared on_message handler takes a template parameter so the function can
  // resolve any endpoint dependent types like message_ptr or connection_ptr
  template <typename EndpointType>
  void on_message(EndpointType* s, websocketpp::connection_hdl hdl,
      typename EndpointType::message_ptr msg)
  {
      std::cout << "on_message called with hdl: " << hdl.lock().get()
                << " and message: " << msg->get_payload()
                << std::endl;
  
      try {
          s->send(hdl, msg->get_payload(), msg->get_opcode());
      } catch (websocketpp::exception const & e) {
          std::cout << "Echo failed because: "
                    << "(" << e.what() << ")" << std::endl;
      }
  }
  
  // No change to TLS init methods from echo_server_tls
  std::string get_password() {
      return "test";
  }
  
  context_ptr on_tls_init(websocketpp::connection_hdl hdl) {
      std::cout << "on_tls_init called with hdl: " << hdl.lock().get() << std::endl;
      context_ptr ctx(new boost::asio::ssl::context(boost::asio::ssl::context::tlsv1));
  
      try {
          ctx->set_options(boost::asio::ssl::context::default_workarounds |
                           boost::asio::ssl::context::no_sslv2 |
                           boost::asio::ssl::context::no_sslv3 |
                           boost::asio::ssl::context::single_dh_use);
          ctx->set_password_callback(bind(&get_password));
          ctx->use_certificate_chain_file("server.pem");
          ctx->use_private_key_file("server.pem", boost::asio::ssl::context::pem);
      } catch (std::exception& e) {
          std::cout << e.what() << std::endl;
      }
      return ctx;
  }
  
  int main() {
      // set up an external io_service to run both endpoints on. This is not
      // strictly necessary, but simplifies thread management a bit.
      boost::asio::io_service ios;
  
      // set up plain endpoint
      server_plain endpoint_plain;
      // initialize asio with our external io_service rather than an internal one
      endpoint_plain.init_asio(&ios);
      endpoint_plain.set_message_handler(
          bind(&on_message<server_plain>,&endpoint_plain,::_1,::_2));
      endpoint_plain.listen(80);
      endpoint_plain.start_accept();
  
      // set up tls endpoint
      server_tls endpoint_tls;
      endpoint_tls.init_asio(&ios);
      endpoint_tls.set_message_handler(
          bind(&on_message<server_tls>,&endpoint_tls,::_1,::_2));
      // TLS endpoint has an extra handler for the tls init
      endpoint_tls.set_tls_init_handler(bind(&on_tls_init,::_1));
      // tls endpoint listens on a different port
      endpoint_tls.listen(443);
      endpoint_tls.start_accept();
  
      // Start the ASIO io_service run loop running both endpoints
      ios.run();
  }