Skip to content

Commit

Permalink
Update examples to remove use of now deprecated functionality referen…
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Thorson authored and TwentyPast4 committed Aug 18, 2022
1 parent 75e8337 commit ec9d3bc
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 25 deletions.
8 changes: 7 additions & 1 deletion examples/associative_storage/associative_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::error_code;

struct connection_data {
int sessionid;
Expand Down Expand Up @@ -44,6 +45,11 @@ class print_server {
m_connections.erase(hdl);
}

void on_end_accept(error_code lib_ec, error_code trans_ec) {
std::cout << "Accept loop ended "
<< lib_ec.message() << "/" << trans_ec.message() << std::endl;
}

void on_message(connection_hdl hdl, server::message_ptr msg) {
connection_data& data = get_data_from_hdl(hdl);

Expand Down Expand Up @@ -71,7 +77,7 @@ class print_server {

void run(uint16_t port) {
m_server.listen(port);
m_server.start_accept();
m_server.start_accept(bind(&print_server::on_end_accept,this,::_1,::_2));
m_server.run();
}
private:
Expand Down
40 changes: 28 additions & 12 deletions examples/broadcast_server/broadcast_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ struct action {
class broadcast_server {
public:
broadcast_server() {
websocketpp::lib::error_code ec;
// Initialize Asio Transport
m_server.init_asio();
m_server.init_asio(ec);
if (!ec) {
return;
}

// Register handler callbacks
m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
Expand All @@ -57,18 +61,26 @@ class broadcast_server {
}

void run(uint16_t port) {
websocketpp::lib::error_code ec;

// listen on specified port
m_server.listen(port);
m_server.listen(port,ec);
if (!ec) {
return;
}

// Start the server accept loop
m_server.start_accept();
m_server.start_accept(ec);
if (!ec) {
return;
}

// Start the ASIO io_service run loop
try {
//try {
m_server.run();
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
}
//} catch (const std::exception & e) {
// std::cout << e.what() << std::endl;
//}
}

void on_open(connection_hdl hdl) {
Expand Down Expand Up @@ -100,6 +112,7 @@ class broadcast_server {
}

void process_messages() {
websocketpp::lib::error_code ec;
while(1) {
unique_lock<mutex> lock(m_action_lock);

Expand All @@ -123,7 +136,10 @@ class broadcast_server {

con_list::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); ++it) {
m_server.send(*it,a.msg);
m_server.send(*it,a.msg,ec);
if (ec) {
return;
}
}
} else {
// undefined.
Expand All @@ -143,7 +159,7 @@ class broadcast_server {
};

int main() {
try {
//try {
broadcast_server server_instance;

// Start a thread to run the processing loop
Expand All @@ -154,7 +170,7 @@ int main() {

t.join();

} catch (websocketpp::exception const & e) {
std::cout << e.what() << std::endl;
}
//} catch (websocketpp::exception const & e) {
// std::cout << e.what() << std::endl;
//}
}
5 changes: 2 additions & 3 deletions examples/debug_client/debug_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
*/

#include <websocketpp/config/asio_client.hpp>

#include <websocketpp/client.hpp>

#include <iostream>
#include <chrono>

typedef websocketpp::client<websocketpp::config::asio_client> client;
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;

using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
Expand All @@ -63,7 +62,7 @@ class perftest {

// Register our handlers
m_endpoint.set_socket_init_handler(bind(&type::on_socket_init,this,::_1));
//m_endpoint.set_tls_init_handler(bind(&type::on_tls_init,this,::_1));
m_endpoint.set_tls_init_handler(bind(&type::on_tls_init,this,::_1));
m_endpoint.set_message_handler(bind(&type::on_message,this,::_1,::_2));
m_endpoint.set_open_handler(bind(&type::on_open,this,::_1));
m_endpoint.set_close_handler(bind(&type::on_close,this,::_1));
Expand Down
9 changes: 8 additions & 1 deletion examples/echo_server/echo_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::error_code;

// pull out the type of messages sent by our config
typedef server::message_ptr message_ptr;
Expand All @@ -34,6 +35,12 @@ void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
}
}

// Define a callback to handle failures accepting connections
void on_end_accept(error_code lib_ec, error_code trans_ec) {
std::cout << "Accept loop ended "
<< lib_ec.message() << "/" << trans_ec.message() << std::endl;
}

int main() {
// Create a server endpoint
server echo_server;
Expand All @@ -53,7 +60,7 @@ int main() {
echo_server.listen(9002);

// Start the server accept loop
echo_server.start_accept();
echo_server.start_accept(&on_end_accept);

// Start the ASIO io_service run loop
echo_server.run();
Expand Down
11 changes: 9 additions & 2 deletions examples/echo_server_both/echo_server_both.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef websocketpp::server<websocketpp::config::asio_tls> server_tls;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::error_code;

// type of the ssl context pointer is long so alias it
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
Expand All @@ -34,6 +35,12 @@ void on_message(EndpointType* s, websocketpp::connection_hdl hdl,
}
}

// Define a callback to handle failures accepting connections
void on_end_accept(error_code lib_ec, error_code trans_ec) {
std::cout << "Accept loop ended "
<< lib_ec.message() << "/" << trans_ec.message() << std::endl;
}

// No change to TLS init methods from echo_server_tls
std::string get_password() {
return "test";
Expand Down Expand Up @@ -69,7 +76,7 @@ int main() {
endpoint_plain.set_message_handler(
bind(&on_message<server_plain>,&endpoint_plain,::_1,::_2));
endpoint_plain.listen(80);
endpoint_plain.start_accept();
endpoint_plain.start_accept(&on_end_accept);

// set up tls endpoint
server_tls endpoint_tls;
Expand All @@ -80,7 +87,7 @@ int main() {
endpoint_tls.set_tls_init_handler(bind(&on_tls_init,::_1));
// tls endpoint listens on a different port
endpoint_tls.listen(443);
endpoint_tls.start_accept();
endpoint_tls.start_accept(&on_end_accept);

// Start the ASIO io_service run loop running both endpoints
ios.run();
Expand Down
9 changes: 8 additions & 1 deletion examples/echo_server_tls/echo_server_tls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef websocketpp::server<websocketpp::config::asio_tls> server;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::error_code;

// pull out the type of messages sent by our config
typedef websocketpp::config::asio::message_type::ptr message_ptr;
Expand All @@ -63,6 +64,12 @@ void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
}
}

// Define a callback to handle failures accepting connections
void on_end_accept(error_code lib_ec, error_code trans_ec) {
std::cout << "Accept loop ended "
<< lib_ec.message() << "/" << trans_ec.message() << std::endl;
}

void on_http(server* s, websocketpp::connection_hdl hdl) {
server::connection_ptr con = s->get_con_from_hdl(hdl);

Expand Down Expand Up @@ -146,7 +153,7 @@ int main() {
echo_server.listen(9002);

// Start the server accept loop
echo_server.start_accept();
echo_server.start_accept(&on_end_accept);

// Start the ASIO io_service run loop
echo_server.run();
Expand Down
8 changes: 7 additions & 1 deletion examples/enriched_storage/enriched_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::error_code;

class print_server {
public:
Expand All @@ -57,6 +58,11 @@ class print_server {
std::cout << "Closing connection " << con->name
<< " with sessionid " << con->sessionid << std::endl;
}

void on_end_accept(error_code lib_ec, error_code trans_ec) {
std::cout << "Accept loop ended "
<< lib_ec.message() << "/" << trans_ec.message() << std::endl;
}

void on_message(connection_hdl hdl, server::message_ptr msg) {
connection_ptr con = m_server.get_con_from_hdl(hdl);
Expand All @@ -73,7 +79,7 @@ class print_server {

void run(uint16_t port) {
m_server.listen(port);
m_server.start_accept();
m_server.start_accept(bind(&print_server::on_end_accept,this,::_1,::_2));
m_server.run();
}
private:
Expand Down
9 changes: 8 additions & 1 deletion examples/external_io_service/external_io_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::error_code;

typedef websocketpp::server<websocketpp::config::asio> ws_echo_server;

Expand All @@ -58,6 +59,12 @@ void on_message(ws_echo_server* s, websocketpp::connection_hdl hdl, ws_echo_serv
}
}

// Define a callback to handle failures accepting connections
void on_end_accept(error_code lib_ec, error_code trans_ec) {
std::cout << "Accept loop ended "
<< lib_ec.message() << "/" << trans_ec.message() << std::endl;
}

int main() {
asio::io_service service;

Expand All @@ -76,7 +83,7 @@ int main() {
// Register our message handler
ws_server.set_message_handler(bind(&on_message,&ws_server,::_1,::_2));
ws_server.listen(9002);
ws_server.start_accept();
ws_server.start_accept(&on_end_accept);

// TODO: add a timer?

Expand Down
2 changes: 1 addition & 1 deletion examples/handler_switch/handler_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main() {

s.init_asio();
s.listen(9002);
s.start_accept();
s.start_accept(NULL); // ignore errors to keep example code consise

s.run();
}
8 changes: 7 additions & 1 deletion examples/iostream_server/iostream_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef websocketpp::server<websocketpp::config::core> server;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::error_code;

// pull out the type of messages sent by our config
typedef server::message_ptr message_ptr;
Expand Down Expand Up @@ -55,7 +56,12 @@ int main() {
// Register our message handler
s.set_message_handler(bind(&on_message,&s,::_1,::_2));

server::connection_ptr con = s.get_connection();
error_code ec;
server::connection_ptr con = s.get_connection(ec);
if (ec) {
std::cout << "Failed to generate new connection because " << ec.message() << std::endl;
return 1;
}

con->start();

Expand Down
2 changes: 1 addition & 1 deletion examples/print_server/print_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main() {

print_server.init_asio();
print_server.listen(9002);
print_server.start_accept();
print_server.start_accept(NULL); // omit error handling to keep example consise

print_server.run();
}

0 comments on commit ec9d3bc

Please sign in to comment.