Skip to content

Commit b2150f8

Browse files
david-cermakespressif-bot
authored andcommitted
asio: make the example code conform to Espressif C++ standards
1 parent 0f72c05 commit b2150f8

File tree

1 file changed

+74
-98
lines changed

1 file changed

+74
-98
lines changed

examples/protocols/asio/ssl_client_server/main/asio_ssl_main.cpp

Lines changed: 74 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ extern const unsigned char cacert_pem_end[] asm("_binary_ca_crt_end");
2727
extern const unsigned char prvtkey_pem_start[] asm("_binary_server_key_start");
2828
extern const unsigned char prvtkey_pem_end[] asm("_binary_server_key_end");
2929

30-
const asio::const_buffer cert_chain(cacert_pem_start, cacert_pem_end - cacert_pem_start);
31-
const asio::const_buffer privkey(prvtkey_pem_start, prvtkey_pem_end - prvtkey_pem_start);
32-
const asio::const_buffer server_cert(server_pem_start, server_pem_end - server_pem_start);
30+
static const asio::const_buffer cert_chain(cacert_pem_start, cacert_pem_end - cacert_pem_start);
31+
static const asio::const_buffer privkey(prvtkey_pem_start, prvtkey_pem_end - prvtkey_pem_start);
32+
static const asio::const_buffer server_cert(server_pem_start, server_pem_end - server_pem_start);
3333

3434
using asio::ip::tcp;
3535

36-
enum { max_length = 1024 };
36+
static const std::size_t max_length = 1024;
3737

3838
class Client {
3939
public:
40-
Client(asio::io_context& io_context,
41-
asio::ssl::context& context,
42-
const tcp::resolver::results_type& endpoints)
43-
: socket_(io_context, context)
40+
Client(asio::io_context &io_context,
41+
asio::ssl::context &context,
42+
const tcp::resolver::results_type &endpoints)
43+
: socket_(io_context, context)
4444
{
4545

4646
#if CONFIG_EXAMPLE_CLIENT_VERIFY_PEER
@@ -53,37 +53,29 @@ class Client {
5353
}
5454

5555
private:
56-
void connect(const tcp::resolver::results_type& endpoints)
56+
void connect(const tcp::resolver::results_type &endpoints)
5757
{
5858
asio::async_connect(socket_.lowest_layer(), endpoints,
59-
[this](const std::error_code& error,
60-
const tcp::endpoint& /*endpoint*/)
61-
{
62-
if (!error)
63-
{
64-
handshake();
65-
}
66-
else
67-
{
68-
std::cout << "Connect failed: " << error.message() << "\n";
69-
}
70-
});
59+
[this](const std::error_code & error,
60+
const tcp::endpoint & /*endpoint*/) {
61+
if (!error) {
62+
handshake();
63+
} else {
64+
std::cout << "Connect failed: " << error.message() << "\n";
65+
}
66+
});
7167
}
7268

7369
void handshake()
7470
{
7571
socket_.async_handshake(asio::ssl::stream_base::client,
76-
[this](const std::error_code& error)
77-
{
78-
if (!error)
79-
{
80-
send_request();
81-
}
82-
else
83-
{
84-
std::cout << "Handshake failed: " << error.message() << "\n";
85-
}
86-
});
72+
[this](const std::error_code & error) {
73+
if (!error) {
74+
send_request();
75+
} else {
76+
std::cout << "Handshake failed: " << error.message() << "\n";
77+
}
78+
});
8779
}
8880

8981
void send_request()
@@ -92,37 +84,28 @@ class Client {
9284

9385
asio::async_write(socket_,
9486
asio::buffer(request_, request_length),
95-
[this](const std::error_code& error, std::size_t length)
96-
{
97-
if (!error)
98-
{
99-
receive_response(length);
100-
}
101-
else
102-
{
103-
std::cout << "Write failed: " << error.message() << "\n";
104-
}
105-
});
87+
[this](const std::error_code & error, std::size_t length) {
88+
if (!error) {
89+
receive_response(length);
90+
} else {
91+
std::cout << "Write failed: " << error.message() << "\n";
92+
}
93+
});
10694
}
10795

10896
void receive_response(std::size_t length)
10997
{
11098
asio::async_read(socket_,
11199
asio::buffer(reply_, length),
112-
[this](const std::error_code& error, std::size_t length)
113-
{
114-
if (!error)
115-
{
116-
std::cout << "Reply: ";
117-
std::cout.write(reply_, length);
118-
std::cout << "\n";
119-
}
120-
else
121-
{
122-
std::cout << "Read failed: " << error.message() << "\n";
123-
}
124-
});
125-
100+
[this](const std::error_code & error, std::size_t length) {
101+
if (!error) {
102+
std::cout << "Reply: ";
103+
std::cout.write(reply_, length);
104+
std::cout << "\n";
105+
} else {
106+
std::cout << "Read failed: " << error.message() << "\n";
107+
}
108+
});
126109
}
127110

128111
asio::ssl::stream<tcp::socket> socket_;
@@ -132,8 +115,8 @@ class Client {
132115

133116
class Session : public std::enable_shared_from_this<Session> {
134117
public:
135-
Session(tcp::socket socket, asio::ssl::context& context)
136-
: socket_(std::move(socket), context)
118+
Session(tcp::socket socket, asio::ssl::context &context)
119+
: socket_(std::move(socket), context)
137120
{
138121
}
139122

@@ -147,42 +130,37 @@ class Session : public std::enable_shared_from_this<Session> {
147130
{
148131
auto self(shared_from_this());
149132
socket_.async_handshake(asio::ssl::stream_base::server,
150-
[this, self](const std::error_code& error)
151-
{
152-
if (!error)
153-
{
154-
do_read();
155-
}
156-
});
133+
[this, self](const std::error_code & error) {
134+
if (!error) {
135+
do_read();
136+
}
137+
});
157138
}
158139

159140
void do_read()
160141
{
161142
auto self(shared_from_this());
162143
socket_.async_read_some(asio::buffer(data_),
163-
[this, self](const std::error_code& ec, std::size_t length)
164-
{
165-
if (!ec)
166-
{
167-
data_[length] = 0;
168-
std::cout << "Server received: " << data_ << std::endl;
169-
do_write(length);
170-
}
171-
});
144+
[this, self](const std::error_code & ec, std::size_t length) {
145+
if (!ec) {
146+
std::cout << "Server received: ";
147+
std::cout.write(data_, length);
148+
std::cout << std::endl;
149+
do_write(length);
150+
}
151+
});
172152
}
173153

174154
void do_write(std::size_t length)
175155
{
176156
auto self(shared_from_this());
177157
asio::async_write(socket_, asio::buffer(data_, length),
178-
[this, self](const std::error_code& ec,
179-
std::size_t /*length*/)
180-
{
181-
if (!ec)
182-
{
183-
do_read();
184-
}
185-
});
158+
[this, self](const std::error_code & ec,
159+
std::size_t /*length*/) {
160+
if (!ec) {
161+
do_read();
162+
}
163+
});
186164
}
187165

188166
asio::ssl::stream<tcp::socket> socket_;
@@ -191,13 +169,13 @@ class Session : public std::enable_shared_from_this<Session> {
191169

192170
class Server {
193171
public:
194-
Server(asio::io_context& io_context, unsigned short port)
195-
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port)),
196-
context_(asio::ssl::context::tls_server)
172+
Server(asio::io_context &io_context, unsigned short port)
173+
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port)),
174+
context_(asio::ssl::context::tls_server)
197175
{
198176
context_.set_options(
199-
asio::ssl::context::default_workarounds
200-
| asio::ssl::context::no_sslv2);
177+
asio::ssl::context::default_workarounds
178+
| asio::ssl::context::no_sslv2);
201179
context_.use_certificate_chain(server_cert);
202180
context_.use_private_key(privkey, asio::ssl::context::pem);
203181

@@ -208,15 +186,13 @@ class Server {
208186
void do_accept()
209187
{
210188
acceptor_.async_accept(
211-
[this](const std::error_code& error, tcp::socket socket)
212-
{
213-
if (!error)
214-
{
215-
std::make_shared<Session>(std::move(socket), context_)->start();
216-
}
217-
218-
do_accept();
219-
});
189+
[this](const std::error_code & error, tcp::socket socket) {
190+
if (!error) {
191+
std::make_shared<Session>(std::move(socket), context_)->start();
192+
}
193+
194+
do_accept();
195+
});
220196
}
221197

222198
tcp::acceptor acceptor_;
@@ -289,7 +265,7 @@ extern "C" void app_main(void)
289265
work_threads.emplace_back(ssl_client_thread);
290266
#endif // CONFIG_EXAMPLE_CLIENT
291267

292-
for (auto & t : work_threads) {
268+
for (auto &t : work_threads) {
293269
t.join();
294270
}
295271

0 commit comments

Comments
 (0)