2 * Copyright (c) 2015, Peter Thorson. All rights reserved.
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.
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.
30 * This file defines a simple TCP Echo Server. It is adapted from the Asio
31 * example: cpp03/echo/async_tcp_echo_server.cpp
34 #include <websocketpp/common/asio.hpp>
35 #include <websocketpp/common/memory.hpp>
36 #include <websocketpp/common/functional.hpp>
38 using websocketpp::lib::placeholders::_1;
39 using websocketpp::lib::placeholders::_2;
40 using websocketpp::lib::bind;
42 namespace asio = websocketpp::lib::asio;
44 struct tcp_echo_session : websocketpp::lib::enable_shared_from_this<tcp_echo_session> {
45 typedef websocketpp::lib::shared_ptr<tcp_echo_session> ptr;
47 tcp_echo_session(asio::io_service & service) : m_socket(service) {}
50 m_socket.async_read_some(asio::buffer(m_buffer, sizeof(m_buffer)),
51 websocketpp::lib::bind(
52 &tcp_echo_session::handle_read, shared_from_this(), _1, _2));
55 void handle_read(const asio::error_code & ec, size_t transferred) {
57 asio::async_write(m_socket,
58 asio::buffer(m_buffer, transferred),
59 bind(&tcp_echo_session::handle_write, shared_from_this(), _1));
63 void handle_write(const asio::error_code & ec) {
65 m_socket.async_read_some(asio::buffer(m_buffer, sizeof(m_buffer)),
66 bind(&tcp_echo_session::handle_read, shared_from_this(), _1, _2));
70 asio::ip::tcp::socket m_socket;
74 struct tcp_echo_server {
75 tcp_echo_server(asio::io_service & service, short port)
77 , m_acceptor(service, asio::ip::tcp::endpoint(asio::ip::tcp::v6(), port))
83 tcp_echo_session::ptr new_session(new tcp_echo_session(m_service));
84 m_acceptor.async_accept(new_session->m_socket,
85 bind(&tcp_echo_server::handle_accept, this, new_session, _1));
88 void handle_accept(tcp_echo_session::ptr new_session, const asio::error_code & ec) {
95 asio::io_service & m_service;
96 asio::ip::tcp::acceptor m_acceptor;