Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added exception handling code. #254

Merged
merged 2 commits into from
May 13, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 73 additions & 8 deletions include/mqtt/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,25 @@ class server {
void listen() {
close_request_ = false;
renew_socket();

auto post_error =
[&](boost::system::error_code const& ec) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the lambda? It's only used in one place (inside the try-catch).

It would be simpler to call ios_accept_.post() directly, wouldn't it?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonesmz I will fix it as you pointed out.

I tried open, set_option, bind, listen, and then accept approach before, but didn't work as I expected. The lambda was used multiple times at that approach. I forgot to update it.

ios_accept_.post(
[this, ec] {
if (h_error_) h_error_(ec);
}
);
};

if (!acceptor_) {
acceptor_.emplace(ios_accept_, ep_);
config_(acceptor_.value());
try {
acceptor_.emplace(ios_accept_, ep_);
config_(acceptor_.value());
}
catch (boost::system::system_error const& e) {
post_error(e.code());
return;
}
}
do_accept();
}
Expand Down Expand Up @@ -208,9 +224,25 @@ class server_tls {
void listen() {
close_request_ = false;
renew_socket();

auto post_error =
[&](boost::system::error_code const& ec) {
ios_accept_.post(
[this, ec] {
if (h_error_) h_error_(ec);
}
);
};

if (!acceptor_) {
acceptor_.emplace(ios_accept_, ep_);
config_(acceptor_.value());
try {
acceptor_.emplace(ios_accept_, ep_);
config_(acceptor_.value());
}
catch (boost::system::system_error const& e) {
post_error(e.code());
return;
}
}
do_accept();
}
Expand Down Expand Up @@ -255,6 +287,7 @@ class server_tls {
[this]
(boost::system::error_code ec) {
if (ec) {
acceptor_.reset();
if (h_error_) h_error_(ec);
return;
}
Expand Down Expand Up @@ -355,9 +388,25 @@ class server_ws {
void listen() {
close_request_ = false;
renew_socket();

auto post_error =
[&](boost::system::error_code const& ec) {
ios_accept_.post(
[this, ec] {
if (h_error_) h_error_(ec);
}
);
};

if (!acceptor_) {
acceptor_.emplace(ios_accept_, ep_);
config_(acceptor_.value());
try {
acceptor_.emplace(ios_accept_, ep_);
config_(acceptor_.value());
}
catch (boost::system::system_error const& e) {
post_error(e.code());
return;
}
}
do_accept();
}
Expand Down Expand Up @@ -519,9 +568,25 @@ class server_tls_ws {
void listen() {
close_request_ = false;
renew_socket();

auto post_error =
[&](boost::system::error_code const& ec) {
ios_accept_.post(
[this, ec] {
if (h_error_) h_error_(ec);
}
);
};

if (!acceptor_) {
acceptor_.emplace(ios_accept_, ep_);
config_(acceptor_.value());
try {
acceptor_.emplace(ios_accept_, ep_);
config_(acceptor_.value());
}
catch (boost::system::system_error const& e) {
post_error(e.code());
return;
}
}
do_accept();
}
Expand Down