| 143 | //------------------------------------------------------------------------------ |
| 144 | |
| 145 | int main(int argc, char** argv) |
| 146 | { |
| 147 | // Check command line arguments. |
| 148 | if(argc != 4) |
| 149 | { |
| 150 | std::cerr << |
| 151 | "Usage: websocket-client-coro-ssl <host> <port> <text>\n" << |
| 152 | "Example:\n" << |
| 153 | " websocket-client-coro-ssl echo.websocket.org 443 \"Hello, world!\"\n"; |
| 154 | return EXIT_FAILURE; |
| 155 | } |
| 156 | auto const host = argv[1]; |
| 157 | auto const port = argv[2]; |
| 158 | auto const text = argv[3]; |
| 159 | |
| 160 | // The io_context is required for all I/O |
| 161 | net::io_context ioc; |
| 162 | |
| 163 | // The SSL context is required, and holds certificates |
| 164 | ssl::context ctx{ssl::context::tlsv12_client}; |
| 165 | |
| 166 | // Verify the remote server's certificate |
| 167 | ctx.set_verify_mode(ssl::verify_peer); |
| 168 | |
| 169 | // This holds the root certificate used for verification |
| 170 | load_root_certificates(ctx); |
| 171 | |
| 172 | // Launch the asynchronous operation |
| 173 | boost::asio::spawn(ioc, std::bind( |
| 174 | &do_session, |
| 175 | std::string(host), |
| 176 | std::string(port), |
| 177 | std::string(text), |
| 178 | std::ref(ioc), |
| 179 | std::ref(ctx), |
| 180 | std::placeholders::_1), |
| 181 | // on completion, spawn will call this function |
| 182 | [](std::exception_ptr ex) |
| 183 | { |
| 184 | // if an exception occurred in the coroutine, |
| 185 | // it's something critical, e.g. out of memory |
| 186 | // we capture normal errors in the ec |
| 187 | // so we just rethrow the exception here, |
| 188 | // which will cause `ioc.run()` to throw |
| 189 | if (ex) |
| 190 | std::rethrow_exception(ex); |
| 191 | }); |
| 192 | |
| 193 | // Run the I/O service. The call will return when |
| 194 | // the socket is closed. |
| 195 | ioc.run(); |
| 196 | |
| 197 | return EXIT_SUCCESS; |
| 198 | } |
nothing calls this directly
no test coverage detected