|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, 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 | +/** |
| 28 | + * TCP Echo Server |
| 29 | + * |
| 30 | + * This file defines a simple TCP Echo Server. It is adapted from the Asio |
| 31 | + * example: cpp03/echo/async_tcp_echo_server.cpp |
| 32 | + */ |
| 33 | + |
| 34 | +#include <websocketpp/common/asio.hpp> |
| 35 | +#include <websocketpp/common/memory.hpp> |
| 36 | +#include <websocketpp/common/functional.hpp> |
| 37 | + |
| 38 | +using websocketpp::lib::placeholders::_1; |
| 39 | +using websocketpp::lib::placeholders::_2; |
| 40 | +using websocketpp::lib::bind; |
| 41 | + |
| 42 | +namespace asio = websocketpp::lib::asio; |
| 43 | +using tcp = asio::ip::tcp; |
| 44 | + |
| 45 | +struct tcp_echo_session : websocketpp::lib::enable_shared_from_this<tcp_echo_session> { |
| 46 | + typedef websocketpp::lib::shared_ptr<tcp_echo_session> ptr; |
| 47 | + |
| 48 | + tcp_echo_session(asio::io_service & service) : m_socket(service) {} |
| 49 | + |
| 50 | + void start() { |
| 51 | + m_socket.async_read_some(asio::buffer(m_buffer, sizeof(m_buffer)), |
| 52 | + websocketpp::lib::bind( |
| 53 | + &tcp_echo_session::handle_read, shared_from_this(), _1, _2)); |
| 54 | + } |
| 55 | + |
| 56 | + void handle_read(const asio::error_code & ec, size_t transferred) { |
| 57 | + if (!ec) { |
| 58 | + asio::async_write(m_socket, |
| 59 | + asio::buffer(m_buffer, transferred), |
| 60 | + bind(&tcp_echo_session::handle_write, shared_from_this(), _1)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + void handle_write(const asio::error_code & ec) { |
| 65 | + if (!ec) { |
| 66 | + m_socket.async_read_some(asio::buffer(m_buffer, sizeof(m_buffer)), |
| 67 | + bind(&tcp_echo_session::handle_read, shared_from_this(), _1, _2)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + tcp::socket m_socket; |
| 72 | + char m_buffer[1024]; |
| 73 | +}; |
| 74 | + |
| 75 | +struct tcp_echo_server { |
| 76 | + tcp_echo_server(asio::io_service & service, short port) |
| 77 | + : m_service(service) |
| 78 | + , m_acceptor(service, tcp::endpoint(tcp::v6(), port)) |
| 79 | + { |
| 80 | + this->start_accept(); |
| 81 | + } |
| 82 | + |
| 83 | + void start_accept() { |
| 84 | + tcp_echo_session::ptr new_session(new tcp_echo_session(m_service)); |
| 85 | + m_acceptor.async_accept(new_session->m_socket, |
| 86 | + bind(&tcp_echo_server::handle_accept, this, new_session, _1)); |
| 87 | + } |
| 88 | + |
| 89 | + void handle_accept(tcp_echo_session::ptr new_session, const asio::error_code & ec) { |
| 90 | + if (!ec) { |
| 91 | + new_session->start(); |
| 92 | + } |
| 93 | + start_accept(); |
| 94 | + } |
| 95 | + |
| 96 | + asio::io_service & m_service; |
| 97 | + tcp::acceptor m_acceptor; |
| 98 | +}; |
0 commit comments