Skip to content

Commit b1f3569

Browse files
committed
Reduce repetitive code
1 parent 08ea20c commit b1f3569

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

include/swoole_client.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ class Client {
134134
int socks5_handshake(const char *recv_data, size_t length);
135135
#ifdef SW_USE_OPENSSL
136136
int enable_ssl_encrypt();
137+
#ifdef SW_SUPPORT_DTLS
138+
void enable_dtls();
139+
#endif
137140
int ssl_handshake();
138141
int ssl_verify(int allow_self_signed);
139142
#endif

src/network/client.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,16 @@ int Client::socks5_handshake(const char *recv_data, size_t length) {
261261
}
262262

263263
#ifdef SW_USE_OPENSSL
264+
#ifdef SW_SUPPORT_DTLS
265+
void Client::enable_dtls() {
266+
ssl_context->protocols = SW_SSL_DTLS;
267+
socket->dtls = 1;
268+
socket->chunk_size = SW_SSL_BUFFER_SIZE;
269+
send = Client_tcp_send_sync;
270+
recv = Client_tcp_recv_no_buffer;
271+
}
272+
#endif
273+
264274
int Client::enable_ssl_encrypt() {
265275
if (ssl_context) {
266276
return SW_ERR;
@@ -269,11 +279,7 @@ int Client::enable_ssl_encrypt() {
269279
open_ssl = true;
270280
#ifdef SW_SUPPORT_DTLS
271281
if (socket->is_dgram()) {
272-
ssl_context->protocols = SW_SSL_DTLS;
273-
socket->dtls = 1;
274-
socket->chunk_size = SW_SSL_BUFFER_SIZE;
275-
send = Client_tcp_send_sync;
276-
recv = Client_tcp_recv_no_buffer;
282+
enable_dtls();
277283
}
278284
#else
279285
{
@@ -820,11 +826,7 @@ static int Client_udp_connect(Client *cli, const char *host, int port, double ti
820826
#ifdef SW_SUPPORT_DTLS
821827
{
822828
udp_connect = 1;
823-
cli->ssl_context->protocols = SW_SSL_DTLS;
824-
cli->socket->dtls = 1;
825-
cli->socket->chunk_size = SW_SSL_BUFFER_SIZE;
826-
cli->send = Client_tcp_send_sync;
827-
cli->recv = Client_tcp_recv_no_buffer;
829+
cli->enable_dtls();
828830
}
829831
#else
830832
{

0 commit comments

Comments
 (0)