@@ -27,20 +27,20 @@ extern const unsigned char cacert_pem_end[] asm("_binary_ca_crt_end");
27
27
extern const unsigned char prvtkey_pem_start[] asm (" _binary_server_key_start" );
28
28
extern const unsigned char prvtkey_pem_end[] asm (" _binary_server_key_end" );
29
29
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);
33
33
34
34
using asio::ip::tcp;
35
35
36
- enum { max_length = 1024 } ;
36
+ static const std:: size_t max_length = 1024 ;
37
37
38
38
class Client {
39
39
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)
44
44
{
45
45
46
46
#if CONFIG_EXAMPLE_CLIENT_VERIFY_PEER
@@ -53,37 +53,29 @@ class Client {
53
53
}
54
54
55
55
private:
56
- void connect (const tcp::resolver::results_type& endpoints)
56
+ void connect (const tcp::resolver::results_type & endpoints)
57
57
{
58
58
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
+ });
71
67
}
72
68
73
69
void handshake ()
74
70
{
75
71
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
+ });
87
79
}
88
80
89
81
void send_request ()
@@ -92,37 +84,28 @@ class Client {
92
84
93
85
asio::async_write (socket_,
94
86
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
+ });
106
94
}
107
95
108
96
void receive_response (std::size_t length)
109
97
{
110
98
asio::async_read (socket_,
111
99
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
+ });
126
109
}
127
110
128
111
asio::ssl::stream<tcp::socket> socket_;
@@ -132,8 +115,8 @@ class Client {
132
115
133
116
class Session : public std ::enable_shared_from_this<Session> {
134
117
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)
137
120
{
138
121
}
139
122
@@ -147,42 +130,37 @@ class Session : public std::enable_shared_from_this<Session> {
147
130
{
148
131
auto self (shared_from_this ());
149
132
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
+ });
157
138
}
158
139
159
140
void do_read ()
160
141
{
161
142
auto self (shared_from_this ());
162
143
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
+ });
172
152
}
173
153
174
154
void do_write (std::size_t length)
175
155
{
176
156
auto self (shared_from_this ());
177
157
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
+ });
186
164
}
187
165
188
166
asio::ssl::stream<tcp::socket> socket_;
@@ -191,13 +169,13 @@ class Session : public std::enable_shared_from_this<Session> {
191
169
192
170
class Server {
193
171
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)
197
175
{
198
176
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);
201
179
context_.use_certificate_chain (server_cert);
202
180
context_.use_private_key (privkey, asio::ssl::context::pem);
203
181
@@ -208,15 +186,13 @@ class Server {
208
186
void do_accept ()
209
187
{
210
188
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
+ });
220
196
}
221
197
222
198
tcp::acceptor acceptor_;
@@ -289,7 +265,7 @@ extern "C" void app_main(void)
289
265
work_threads.emplace_back (ssl_client_thread);
290
266
#endif // CONFIG_EXAMPLE_CLIENT
291
267
292
- for (auto & t : work_threads) {
268
+ for (auto &t : work_threads) {
293
269
t.join ();
294
270
}
295
271
0 commit comments