73
73
#include < dpp/dns.h>
74
74
75
75
/* Maximum allowed time in milliseconds for socket read/write timeouts and connect() */
76
- # define SOCKET_OP_TIMEOUT 5000
76
+ constexpr uint16_t SOCKET_OP_TIMEOUT{ 5000 };
77
77
78
78
namespace dpp {
79
79
@@ -123,10 +123,10 @@ thread_local std::unordered_map<std::string, keepalive_cache_t> keepalives;
123
123
* SSL_read in non-blocking mode will only read 16k at a time. There's no point in a bigger buffer as
124
124
* it'd go unused.
125
125
*/
126
- # define DPP_BUFSIZE 16 * 1024
126
+ constexpr uint16_t DPP_BUFSIZE{ 16 * 1024 };
127
127
128
128
/* Represents a failed socket system call, e.g. connect() failure */
129
- const int ERROR_STATUS = - 1 ;
129
+ constexpr int ERROR_STATUS{- 1 } ;
130
130
131
131
bool close_socket (dpp::socket sfd)
132
132
{
@@ -176,8 +176,8 @@ bool set_nonblocking(dpp::socket sockfd, bool non_blocking)
176
176
*/
177
177
int connect_with_timeout (dpp::socket sockfd, const struct sockaddr *addr, socklen_t addrlen, unsigned int timeout_ms) {
178
178
#ifdef __APPLE__
179
- /* Unreliable on OSX right now */
180
- return (::connect (sockfd, addr, addrlen));
179
+ /* Unreliable on OSX right now */
180
+ return (::connect (sockfd, addr, addrlen));
181
181
#else
182
182
if (!set_nonblocking (sockfd, true )) {
183
183
throw dpp::connection_exception (err_nonblocking_failure, " Can't switch socket to non-blocking mode!" );
@@ -197,25 +197,26 @@ int connect_with_timeout(dpp::socket sockfd, const struct sockaddr *addr, sockle
197
197
#endif
198
198
if (rc == -1 && err != EWOULDBLOCK && err != EINPROGRESS) {
199
199
throw connection_exception (err_connect_failure, strerror (errno));
200
- } else {
201
- /* Set a deadline timestamp 'timeout' ms from now */
202
- double deadline = utility::time_f () + (timeout_ms / 1000.0 );
203
- do {
204
- rc = -1 ;
205
- if (utility::time_f () >= deadline) {
206
- throw connection_exception (err_connection_timed_out, " Connection timed out" );
207
- }
208
- pollfd pfd = {};
209
- pfd.fd = sockfd;
210
- pfd.events = POLLOUT;
211
- int r = ::poll (&pfd, 1 , 10 );
212
- if (r > 0 && pfd.revents & POLLOUT) {
213
- rc = 0 ;
214
- } else if (r != 0 || pfd.revents & POLLERR) {
215
- throw connection_exception (err_connection_timed_out, strerror (errno));
216
- }
217
- } while (rc == -1 );
218
200
}
201
+
202
+ /* Set a deadline timestamp 'timeout' ms from now */
203
+ double deadline = utility::time_f () + (timeout_ms / 1000.0 );
204
+
205
+ do {
206
+ if (utility::time_f () >= deadline) {
207
+ throw connection_exception (err_connection_timed_out, " Connection timed out" );
208
+ }
209
+ pollfd pfd = {};
210
+ pfd.fd = sockfd;
211
+ pfd.events = POLLOUT;
212
+ const int r = ::poll (&pfd, 1 , 10 );
213
+ if (r > 0 && pfd.revents & POLLOUT) {
214
+ rc = 0 ;
215
+ } else if (r != 0 || pfd.revents & POLLERR) {
216
+ throw connection_exception (err_connection_timed_out, strerror (errno));
217
+ }
218
+ } while (rc == -1 );
219
+
219
220
if (!set_nonblocking (sockfd, false )) {
220
221
throw connection_exception (err_nonblocking_failure, " Can't switch socket to blocking mode!" );
221
222
}
@@ -226,7 +227,7 @@ int connect_with_timeout(dpp::socket sockfd, const struct sockaddr *addr, sockle
226
227
#ifndef _WIN32
227
228
void set_signal_handler (int signal)
228
229
{
229
- struct sigaction sa;
230
+ struct sigaction sa{} ;
230
231
sigaction (signal , nullptr , &sa);
231
232
if (sa.sa_flags == 0 && sa.sa_handler == nullptr ) {
232
233
sa = {};
@@ -378,7 +379,7 @@ void ssl_client::connect()
378
379
}
379
380
}
380
381
381
- void ssl_client::write (const std::string & data)
382
+ void ssl_client::write (const std::string_view data)
382
383
{
383
384
/* If we are in nonblocking mode, append to the buffer,
384
385
* otherwise just use SSL_write directly. The only time we
@@ -388,16 +389,17 @@ void ssl_client::write(const std::string &data)
388
389
*/
389
390
if (nonblocking) {
390
391
obuffer += data;
392
+ return ;
393
+ }
394
+
395
+ const int data_length = static_cast <int >(data.length ());
396
+ if (plaintext) {
397
+ if (sfd == INVALID_SOCKET || ::send (sfd, data.data (), data_length, 0 ) != data_length) {
398
+ throw dpp::connection_exception (err_write, " write() failed" );
399
+ }
391
400
} else {
392
- const int data_length = (int )data.length ();
393
- if (plaintext) {
394
- if (sfd == INVALID_SOCKET || ::send (sfd, data.data (), data_length, 0 ) != data_length) {
395
- throw dpp::connection_exception (err_write, " write() failed" );
396
- }
397
- } else {
398
- if (SSL_write (ssl->ssl , data.data (), data_length) != data_length) {
399
- throw dpp::connection_exception (err_ssl_write, " SSL_write() failed" );
400
- }
401
+ if (SSL_write (ssl->ssl , data.data (), data_length) != data_length) {
402
+ throw dpp::connection_exception (err_ssl_write, " SSL_write() failed" );
401
403
}
402
404
}
403
405
}
@@ -502,16 +504,17 @@ void ssl_client::read_loop()
502
504
read_blocked_on_write = false ;
503
505
read_blocked = false ;
504
506
r = (int ) ::recv (sfd, server_to_client_buffer, DPP_BUFSIZE, 0 );
507
+
505
508
if (r <= 0 ) {
506
509
/* error or EOF */
507
510
return ;
508
- } else {
509
- buffer.append (server_to_client_buffer, r);
510
- if (!this ->handle_buffer (buffer)) {
511
- return ;
512
- }
513
- bytes_in += r;
514
511
}
512
+
513
+ buffer.append (server_to_client_buffer, r);
514
+ if (!this ->handle_buffer (buffer)) {
515
+ return ;
516
+ }
517
+ bytes_in += r;
515
518
} else {
516
519
do {
517
520
read_blocked_on_write = false ;
@@ -577,14 +580,14 @@ void ssl_client::read_loop()
577
580
if (r < 0 ) {
578
581
/* Write error */
579
582
return ;
580
- } else {
581
- client_to_server_length -= r;
582
- client_to_server_offset += r;
583
- bytes_out += r;
584
583
}
584
+
585
+ client_to_server_length -= r;
586
+ client_to_server_offset += r;
587
+ bytes_out += r;
585
588
} else {
586
589
r = SSL_write (ssl->ssl , client_to_server_buffer + client_to_server_offset, (int )client_to_server_length);
587
-
590
+
588
591
switch (SSL_get_error (ssl->ssl ,r)) {
589
592
/* We wrote something */
590
593
case SSL_ERROR_NONE:
0 commit comments