| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Distributed under the Boost Software License, Version 1.0. (See accompanying
 | 
					
						
							|  |  |  | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef BEAST_EXAMPLE_SERVER_WS_ASYNC_PORT_HPP
 | 
					
						
							|  |  |  | #define BEAST_EXAMPLE_SERVER_WS_ASYNC_PORT_HPP
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "server.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <beast/core/multi_buffer.hpp>
 | 
					
						
							|  |  |  | #include <beast/websocket/stream.hpp>
 | 
					
						
							| 
									
										
										
										
											2017-06-18 06:51:51 -07:00
										 |  |  | #include <boost/function.hpp>
 | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | #include <memory>
 | 
					
						
							|  |  |  | #include <ostream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace framework { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This object holds the state of the connection
 | 
					
						
							|  |  |  | // including, most importantly, the socket or stream.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | template<class Derived> | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  | class async_ws_con_base | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |     // This function lets us access members of the derived class
 | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     Derived& | 
					
						
							|  |  |  |     impl() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return static_cast<Derived&>(*this); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // The string used to set the Server http field
 | 
					
						
							|  |  |  |     std::string server_name_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // The stream to use for logging
 | 
					
						
							|  |  |  |     std::ostream& log_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // A small unique integer for logging
 | 
					
						
							|  |  |  |     std::size_t id_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // The remote endpoint. We cache it here because
 | 
					
						
							|  |  |  |     // calls to remote_endpoint() can fail / throw.
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     endpoint_type ep_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // This is used to hold the message data
 | 
					
						
							|  |  |  |     beast::multi_buffer buffer_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     // The strand makes sure that our data is
 | 
					
						
							|  |  |  |     // accessed from only one thread at a time.
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     strand_type strand_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     // Constructor
 | 
					
						
							|  |  |  |     template<class Callback> | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |     async_ws_con_base( | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |         beast::string_view server_name, | 
					
						
							|  |  |  |         std::ostream& log, | 
					
						
							|  |  |  |         std::size_t id, | 
					
						
							|  |  |  |         endpoint_type const& ep, | 
					
						
							|  |  |  |         Callback const& cb) | 
					
						
							|  |  |  |         : server_name_(server_name) | 
					
						
							|  |  |  |         , log_(log) | 
					
						
							|  |  |  |         , id_(id) | 
					
						
							|  |  |  |         , ep_(ep) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Limit of 1MB on messages
 | 
					
						
							|  |  |  |         , buffer_(1024 * 1024) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |         , strand_(impl().stream().get_io_service()) | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |         cb(impl().stream()); | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Run the connection
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     void | 
					
						
							|  |  |  |     run() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |         impl().do_handshake(); | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Run the connection.
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     // This overload handles the case where we
 | 
					
						
							|  |  |  |     // already have the WebSocket Upgrade request.
 | 
					
						
							|  |  |  |     //
 | 
					
						
							| 
									
										
										
										
											2017-06-19 13:01:59 -07:00
										 |  |  |     template<class Body> | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     void | 
					
						
							| 
									
										
										
										
											2017-06-19 13:01:59 -07:00
										 |  |  |     run(beast::http::request<Body> const& req) | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         // Call the overload of accept() which takes
 | 
					
						
							|  |  |  |         // the request by parameter, instead of reading
 | 
					
						
							|  |  |  |         // it from the network.
 | 
					
						
							|  |  |  |         //
 | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |         impl().stream().async_accept_ex(req, | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |             [&](beast::websocket::response_type& res) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 res.set(beast::http::field::server, server_name_); | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             strand_.wrap(std::bind( | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |                 &async_ws_con_base::on_accept, | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |                 impl().shared_from_this(), | 
					
						
							|  |  |  |                 std::placeholders::_1))); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  | protected: | 
					
						
							|  |  |  |     // Performs the WebSocket handshake
 | 
					
						
							|  |  |  |     void | 
					
						
							|  |  |  |     do_accept() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Read the WebSocket upgrade request and attempt
 | 
					
						
							|  |  |  |         // to send back the response.
 | 
					
						
							|  |  |  |         //
 | 
					
						
							|  |  |  |         impl().stream().async_accept_ex( | 
					
						
							|  |  |  |             [&](beast::websocket::response_type& res) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 res.set(beast::http::field::server, server_name_); | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             strand_.wrap(std::bind( | 
					
						
							|  |  |  |                 &async_ws_con_base::on_accept, | 
					
						
							|  |  |  |                 impl().shared_from_this(), | 
					
						
							|  |  |  |                 std::placeholders::_1))); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // This helper reports failures
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     void | 
					
						
							|  |  |  |     fail(std::string what, error_code ec) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if(ec != beast::websocket::error::closed) | 
					
						
							|  |  |  |             log_ << | 
					
						
							|  |  |  |                 "[#" << id_ << " " << ep_ << "] " << | 
					
						
							|  |  |  |             what << ": " << ec.message() << std::endl; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | private: | 
					
						
							|  |  |  |     // Called when accept_ex completes
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     void | 
					
						
							|  |  |  |     on_accept(error_code ec) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if(ec) | 
					
						
							|  |  |  |             return fail("async_accept", ec); | 
					
						
							|  |  |  |         do_read(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Read the next WebSocket message
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     void | 
					
						
							|  |  |  |     do_read() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |         impl().stream().async_read( | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |             buffer_, | 
					
						
							|  |  |  |             strand_.wrap(std::bind( | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |                 &async_ws_con_base::on_read, | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |                 impl().shared_from_this(), | 
					
						
							|  |  |  |                 std::placeholders::_1))); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |     // Called when the message read completes
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     void | 
					
						
							|  |  |  |     on_read(error_code ec) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if(ec) | 
					
						
							|  |  |  |             return fail("on_read", ec); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Set the outgoing message type. We will use
 | 
					
						
							|  |  |  |         // the same setting as the message we just read.
 | 
					
						
							|  |  |  |         //
 | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |         impl().stream().binary(impl().stream().got_binary()); | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Now echo back the message
 | 
					
						
							|  |  |  |         //
 | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |         impl().stream().async_write( | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |             buffer_.data(), | 
					
						
							|  |  |  |             strand_.wrap(std::bind( | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |                 &async_ws_con_base::on_write, | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |                 impl().shared_from_this(), | 
					
						
							|  |  |  |                 std::placeholders::_1))); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Called when the message write completes
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     void | 
					
						
							|  |  |  |     on_write(error_code ec) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if(ec) | 
					
						
							|  |  |  |             return fail("on_write", ec); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Empty out the contents of the message buffer
 | 
					
						
							|  |  |  |         // to prepare it for the next call to read.
 | 
					
						
							|  |  |  |         //
 | 
					
						
							|  |  |  |         buffer_.consume(buffer_.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Now read another message
 | 
					
						
							|  |  |  |         //
 | 
					
						
							|  |  |  |         do_read(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  | // This class represents an asynchronous WebSocket connection
 | 
					
						
							|  |  |  | // which uses a plain TCP/IP socket (no encryption) as the stream.
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  | class async_ws_con | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 06:51:51 -07:00
										 |  |  |     // Give this object the enable_shared_from_this, and have
 | 
					
						
							|  |  |  |     // the base class call impl().shared_from_this(). The reason
 | 
					
						
							|  |  |  |     // is so that the shared_ptr has the correct type. This lets
 | 
					
						
							|  |  |  |     // the derived class (this class) use its members in calls to
 | 
					
						
							|  |  |  |     // `std::bind`, without an ugly call to `dynamic_downcast` or
 | 
					
						
							|  |  |  |     // other nonsense.
 | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |     //
 | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |     : public std::enable_shared_from_this<async_ws_con> | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 06:51:51 -07:00
										 |  |  |     // The stream should be created before the base class so
 | 
					
						
							|  |  |  |     // use the "base from member" idiom.
 | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |     //
 | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     , public base_from_member<beast::websocket::stream<socket_type>> | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 06:51:51 -07:00
										 |  |  |     // Constructs last, destroys first
 | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |     //
 | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |     , public async_ws_con_base<async_ws_con> | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2017-06-18 06:51:51 -07:00
										 |  |  |     // Constructor
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     // Additional arguments are forwarded to the base class
 | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |     //
 | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     template<class... Args> | 
					
						
							|  |  |  |     explicit | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |     async_ws_con( | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |         socket_type&& sock, | 
					
						
							|  |  |  |         Args&&... args) | 
					
						
							|  |  |  |         : base_from_member<beast::websocket::stream<socket_type>>(std::move(sock)) | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |         , async_ws_con_base<async_ws_con>(std::forward<Args>(args)...) | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |     // Returns the stream.
 | 
					
						
							|  |  |  |     //
 | 
					
						
							| 
									
										
										
										
											2017-06-18 06:51:51 -07:00
										 |  |  |     // The base class calls this to obtain the object to use for
 | 
					
						
							|  |  |  |     // reading and writing HTTP messages. This allows the same base
 | 
					
						
							|  |  |  |     // class to work with different return types for `stream()` such
 | 
					
						
							|  |  |  |     // as a `boost::asio::ip::tcp::socket&` or a `boost::asio::ssl::stream&`
 | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |     //
 | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     beast::websocket::stream<socket_type>& | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |     stream() | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         return this->member; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     // Base class needs to be a friend to call our private members
 | 
					
						
							|  |  |  |     friend async_ws_con_base<async_ws_con>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void | 
					
						
							|  |  |  |     do_handshake() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         do_accept(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //------------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  | /** An asynchronous WebSocket @b PortHandler which implements echo.
 | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     This is a port handler which accepts WebSocket upgrade HTTP | 
					
						
							|  |  |  |     requests and implements the echo protocol. All received | 
					
						
							|  |  |  |     WebSocket messages will be echoed back to the remote host. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | class ws_async_port | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-06-18 06:51:51 -07:00
										 |  |  |     // The type of the on_new_stream callback
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     using on_new_stream_cb = | 
					
						
							|  |  |  |         boost::function<void(beast::websocket::stream<socket_type>&)>; | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     server& instance_; | 
					
						
							|  |  |  |     std::ostream& log_; | 
					
						
							|  |  |  |     on_new_stream_cb cb_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     /** Constructor
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @param instance The server instance which owns this port | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @param log The stream to use for logging | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @param cb A callback which will be invoked for every new | 
					
						
							|  |  |  |         WebSocket connection. This provides an opportunity to change | 
					
						
							|  |  |  |         the settings on the stream before it is used. The callback | 
					
						
							|  |  |  |         should have this equivalent signature: | 
					
						
							|  |  |  |         @code | 
					
						
							|  |  |  |         template<class NextLayer> | 
					
						
							|  |  |  |         void callback(beast::websocket::stream<NextLayer>&); | 
					
						
							|  |  |  |         @endcode | 
					
						
							|  |  |  |         In C++14 this can be accomplished with a generic lambda. In | 
					
						
							|  |  |  |         C++11 it will be necessary to write out a lambda manually, | 
					
						
							|  |  |  |         with a templated operator(). | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     template<class Callback> | 
					
						
							|  |  |  |     ws_async_port( | 
					
						
							|  |  |  |         server& instance, | 
					
						
							|  |  |  |         std::ostream& log, | 
					
						
							|  |  |  |         Callback const& cb) | 
					
						
							|  |  |  |         : instance_(instance) | 
					
						
							|  |  |  |         , log_(log) | 
					
						
							|  |  |  |         , cb_(cb) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |     /** Accept a TCP/IP connection.
 | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         This function is called when the server has accepted an | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |         incoming connection. | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         @param sock The connected socket. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @param ep The endpoint of the remote host. | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     void | 
					
						
							|  |  |  |     on_accept( | 
					
						
							|  |  |  |         socket_type&& sock, | 
					
						
							|  |  |  |         endpoint_type ep) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |         std::make_shared<async_ws_con>( | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |             std::move(sock), | 
					
						
							|  |  |  |             "ws_async_port", | 
					
						
							|  |  |  |             log_, | 
					
						
							|  |  |  |             instance_.next_id(), | 
					
						
							|  |  |  |             ep, | 
					
						
							| 
									
										
										
										
											2017-06-18 06:51:51 -07:00
										 |  |  |             cb_)->run(); | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** Accept a WebSocket upgrade request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |         This is used to accept a connection that has already | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |         delivered the handshake. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |         @param stream The stream corresponding to the connection. | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         @param ep The remote endpoint. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @param req The upgrade request. | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2017-06-19 13:01:59 -07:00
										 |  |  |     template<class Body> | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     void | 
					
						
							| 
									
										
										
										
											2017-06-18 06:51:51 -07:00
										 |  |  |     on_upgrade( | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |         socket_type&& sock, | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |         endpoint_type ep, | 
					
						
							| 
									
										
										
										
											2017-06-19 13:01:59 -07:00
										 |  |  |         beast::http::request<Body>&& req) | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-06-17 13:42:13 -07:00
										 |  |  |         std::make_shared<async_ws_con>( | 
					
						
							| 
									
										
										
										
											2017-06-17 10:20:45 -07:00
										 |  |  |             std::move(sock), | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |             "ws_async_port", | 
					
						
							|  |  |  |             log_, | 
					
						
							|  |  |  |             instance_.next_id(), | 
					
						
							|  |  |  |             ep, | 
					
						
							| 
									
										
										
										
											2017-06-18 06:51:51 -07:00
										 |  |  |             cb_)->run(std::move(req)); | 
					
						
							| 
									
										
										
										
											2017-06-12 19:16:39 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // framework
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 |