Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
tcp_echo_server.hpp
Go to the documentation of this file.
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
37
38using websocketpp::lib::placeholders::_1;
39using websocketpp::lib::placeholders::_2;
40using websocketpp::lib::bind;
41
43
44struct tcp_echo_session : websocketpp::lib::enable_shared_from_this<tcp_echo_session> {
45 typedef websocketpp::lib::shared_ptr<tcp_echo_session> ptr;
46
47 tcp_echo_session(asio::io_service & service) : m_socket(service) {}
48
49 void start() {
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));
53 }
54
55 void handle_read(const asio::error_code & ec, size_t transferred) {
56 if (!ec) {
57 asio::async_write(m_socket,
58 asio::buffer(m_buffer, transferred),
59 bind(&tcp_echo_session::handle_write, shared_from_this(), _1));
60 }
61 }
62
63 void handle_write(const asio::error_code & ec) {
64 if (!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));
67 }
68 }
69
70 asio::ip::tcp::socket m_socket;
71 char m_buffer[1024];
72};
73
75 tcp_echo_server(asio::io_service & service, short port)
76 : m_service(service)
77 , m_acceptor(service, asio::ip::tcp::endpoint(asio::ip::tcp::v6(), port))
78 {
79 this->start_accept();
80 }
81
82 void start_accept() {
84 m_acceptor.async_accept(new_session->m_socket,
85 bind(&tcp_echo_server::handle_accept, this, new_session, _1));
86 }
87
88 void handle_accept(tcp_echo_session::ptr new_session, const asio::error_code & ec) {
89 if (!ec) {
90 new_session->start();
91 }
93 }
94
95 asio::io_service & m_service;
96 asio::ip::tcp::acceptor m_acceptor;
97};
boost::asio::ip::tcp tcp
asio::ip::tcp::acceptor m_acceptor
tcp_echo_server(asio::io_service &service, short port)
void handle_accept(tcp_echo_session::ptr new_session, const asio::error_code &ec)
asio::io_service & m_service
void handle_write(const asio::error_code &ec)
asio::ip::tcp::socket m_socket
websocketpp::lib::shared_ptr< tcp_echo_session > ptr
void handle_read(const asio::error_code &ec, size_t transferred)
tcp_echo_session(asio::io_service &service)