NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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
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
44
struct
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
74
struct
tcp_echo_server
{
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
() {
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));
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
}
92
start_accept();
93
}
94
95
asio::io_service &
m_service
;
96
asio::ip::tcp::acceptor
m_acceptor
;
97
};
tcp_echo_session
Definition:
tcp_echo_server.hpp:44
tcp_echo_session::m_socket
asio::ip::tcp::socket m_socket
Definition:
tcp_echo_server.hpp:70
tcp_echo_server::start_accept
void start_accept()
Definition:
tcp_echo_server.hpp:82
tcp_echo_session::m_buffer
char m_buffer[1024]
Definition:
tcp_echo_server.hpp:71
tcp_echo_session::handle_read
void handle_read(const asio::error_code &ec, size_t transferred)
Definition:
tcp_echo_server.hpp:55
tcp_echo_session::start
void start()
Definition:
tcp_echo_server.hpp:49
tcp_echo_server
Definition:
tcp_echo_server.hpp:74
tcp_echo_server::tcp_echo_server
tcp_echo_server(asio::io_service &service, short port)
Definition:
tcp_echo_server.hpp:75
asio.hpp
websocketpp::lib::asio
Definition:
asio.hpp:94
websocketpp::transport::asio::socket::error::socket
Catch-all error for socket component errors that don't fit in other categories.
Definition:
base.hpp:83
tcp_echo_server::m_service
asio::io_service & m_service
Definition:
tcp_echo_server.hpp:95
tcp_echo_session::tcp_echo_session
tcp_echo_session(asio::io_service &service)
Definition:
tcp_echo_server.hpp:47
tcp_echo_server::m_acceptor
asio::ip::tcp::acceptor m_acceptor
Definition:
tcp_echo_server.hpp:96
memory.hpp
tcp_echo_server::handle_accept
void handle_accept(tcp_echo_session::ptr new_session, const asio::error_code &ec)
Definition:
tcp_echo_server.hpp:88
tcp_echo_session::handle_write
void handle_write(const asio::error_code &ec)
Definition:
tcp_echo_server.hpp:63
tcp_echo_session::ptr
websocketpp::lib::shared_ptr< tcp_echo_session > ptr
Definition:
tcp_echo_server.hpp:45
functional.hpp
ndnSIM
NFD
websocketpp
examples
external_io_service
tcp_echo_server.hpp
Generated on Fri May 6 2022 12:34:14 for ndnSIM by
1.8.13