mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-30 18:57:28 +02:00
asio: make the example code conform to Espressif C++ standards
* Original commit: espressif/esp-idf@b2150f86a5
This commit is contained in:
@ -27,13 +27,13 @@ extern const unsigned char cacert_pem_end[] asm("_binary_ca_crt_end");
|
|||||||
extern const unsigned char prvtkey_pem_start[] asm("_binary_server_key_start");
|
extern const unsigned char prvtkey_pem_start[] asm("_binary_server_key_start");
|
||||||
extern const unsigned char prvtkey_pem_end[] asm("_binary_server_key_end");
|
extern const unsigned char prvtkey_pem_end[] asm("_binary_server_key_end");
|
||||||
|
|
||||||
const asio::const_buffer cert_chain(cacert_pem_start, cacert_pem_end - cacert_pem_start);
|
static const asio::const_buffer cert_chain(cacert_pem_start, cacert_pem_end - cacert_pem_start);
|
||||||
const asio::const_buffer privkey(prvtkey_pem_start, prvtkey_pem_end - prvtkey_pem_start);
|
static const asio::const_buffer privkey(prvtkey_pem_start, prvtkey_pem_end - prvtkey_pem_start);
|
||||||
const asio::const_buffer server_cert(server_pem_start, server_pem_end - server_pem_start);
|
static const asio::const_buffer server_cert(server_pem_start, server_pem_end - server_pem_start);
|
||||||
|
|
||||||
using asio::ip::tcp;
|
using asio::ip::tcp;
|
||||||
|
|
||||||
enum { max_length = 1024 };
|
static const std::size_t max_length = 1024;
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
public:
|
public:
|
||||||
@ -57,14 +57,10 @@ private:
|
|||||||
{
|
{
|
||||||
asio::async_connect(socket_.lowest_layer(), endpoints,
|
asio::async_connect(socket_.lowest_layer(), endpoints,
|
||||||
[this](const std::error_code & error,
|
[this](const std::error_code & error,
|
||||||
const tcp::endpoint& /*endpoint*/)
|
const tcp::endpoint & /*endpoint*/) {
|
||||||
{
|
if (!error) {
|
||||||
if (!error)
|
|
||||||
{
|
|
||||||
handshake();
|
handshake();
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout << "Connect failed: " << error.message() << "\n";
|
std::cout << "Connect failed: " << error.message() << "\n";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -73,14 +69,10 @@ private:
|
|||||||
void handshake()
|
void handshake()
|
||||||
{
|
{
|
||||||
socket_.async_handshake(asio::ssl::stream_base::client,
|
socket_.async_handshake(asio::ssl::stream_base::client,
|
||||||
[this](const std::error_code& error)
|
[this](const std::error_code & error) {
|
||||||
{
|
if (!error) {
|
||||||
if (!error)
|
|
||||||
{
|
|
||||||
send_request();
|
send_request();
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout << "Handshake failed: " << error.message() << "\n";
|
std::cout << "Handshake failed: " << error.message() << "\n";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -92,14 +84,10 @@ private:
|
|||||||
|
|
||||||
asio::async_write(socket_,
|
asio::async_write(socket_,
|
||||||
asio::buffer(request_, request_length),
|
asio::buffer(request_, request_length),
|
||||||
[this](const std::error_code& error, std::size_t length)
|
[this](const std::error_code & error, std::size_t length) {
|
||||||
{
|
if (!error) {
|
||||||
if (!error)
|
|
||||||
{
|
|
||||||
receive_response(length);
|
receive_response(length);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout << "Write failed: " << error.message() << "\n";
|
std::cout << "Write failed: " << error.message() << "\n";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -109,20 +97,15 @@ private:
|
|||||||
{
|
{
|
||||||
asio::async_read(socket_,
|
asio::async_read(socket_,
|
||||||
asio::buffer(reply_, length),
|
asio::buffer(reply_, length),
|
||||||
[this](const std::error_code& error, std::size_t length)
|
[this](const std::error_code & error, std::size_t length) {
|
||||||
{
|
if (!error) {
|
||||||
if (!error)
|
|
||||||
{
|
|
||||||
std::cout << "Reply: ";
|
std::cout << "Reply: ";
|
||||||
std::cout.write(reply_, length);
|
std::cout.write(reply_, length);
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout << "Read failed: " << error.message() << "\n";
|
std::cout << "Read failed: " << error.message() << "\n";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
asio::ssl::stream<tcp::socket> socket_;
|
asio::ssl::stream<tcp::socket> socket_;
|
||||||
@ -147,10 +130,8 @@ private:
|
|||||||
{
|
{
|
||||||
auto self(shared_from_this());
|
auto self(shared_from_this());
|
||||||
socket_.async_handshake(asio::ssl::stream_base::server,
|
socket_.async_handshake(asio::ssl::stream_base::server,
|
||||||
[this, self](const std::error_code& error)
|
[this, self](const std::error_code & error) {
|
||||||
{
|
if (!error) {
|
||||||
if (!error)
|
|
||||||
{
|
|
||||||
do_read();
|
do_read();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -160,12 +141,11 @@ private:
|
|||||||
{
|
{
|
||||||
auto self(shared_from_this());
|
auto self(shared_from_this());
|
||||||
socket_.async_read_some(asio::buffer(data_),
|
socket_.async_read_some(asio::buffer(data_),
|
||||||
[this, self](const std::error_code& ec, std::size_t length)
|
[this, self](const std::error_code & ec, std::size_t length) {
|
||||||
{
|
if (!ec) {
|
||||||
if (!ec)
|
std::cout << "Server received: ";
|
||||||
{
|
std::cout.write(data_, length);
|
||||||
data_[length] = 0;
|
std::cout << std::endl;
|
||||||
std::cout << "Server received: " << data_ << std::endl;
|
|
||||||
do_write(length);
|
do_write(length);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -176,10 +156,8 @@ private:
|
|||||||
auto self(shared_from_this());
|
auto self(shared_from_this());
|
||||||
asio::async_write(socket_, asio::buffer(data_, length),
|
asio::async_write(socket_, asio::buffer(data_, length),
|
||||||
[this, self](const std::error_code & ec,
|
[this, self](const std::error_code & ec,
|
||||||
std::size_t /*length*/)
|
std::size_t /*length*/) {
|
||||||
{
|
if (!ec) {
|
||||||
if (!ec)
|
|
||||||
{
|
|
||||||
do_read();
|
do_read();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -208,10 +186,8 @@ private:
|
|||||||
void do_accept()
|
void do_accept()
|
||||||
{
|
{
|
||||||
acceptor_.async_accept(
|
acceptor_.async_accept(
|
||||||
[this](const std::error_code& error, tcp::socket socket)
|
[this](const std::error_code & error, tcp::socket socket) {
|
||||||
{
|
if (!error) {
|
||||||
if (!error)
|
|
||||||
{
|
|
||||||
std::make_shared<Session>(std::move(socket), context_)->start();
|
std::make_shared<Session>(std::move(socket), context_)->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user