Skip to content

Commit

Permalink
Reduce repetitive code
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Mar 4, 2021
1 parent 08ea20c commit b1f3569
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
3 changes: 3 additions & 0 deletions include/swoole_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class Client {
int socks5_handshake(const char *recv_data, size_t length);
#ifdef SW_USE_OPENSSL
int enable_ssl_encrypt();
#ifdef SW_SUPPORT_DTLS
void enable_dtls();
#endif
int ssl_handshake();
int ssl_verify(int allow_self_signed);
#endif
Expand Down
22 changes: 12 additions & 10 deletions src/network/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ int Client::socks5_handshake(const char *recv_data, size_t length) {
}

#ifdef SW_USE_OPENSSL
#ifdef SW_SUPPORT_DTLS
void Client::enable_dtls() {
ssl_context->protocols = SW_SSL_DTLS;
socket->dtls = 1;
socket->chunk_size = SW_SSL_BUFFER_SIZE;
send = Client_tcp_send_sync;
recv = Client_tcp_recv_no_buffer;
}
#endif

int Client::enable_ssl_encrypt() {
if (ssl_context) {
return SW_ERR;
Expand All @@ -269,11 +279,7 @@ int Client::enable_ssl_encrypt() {
open_ssl = true;
#ifdef SW_SUPPORT_DTLS
if (socket->is_dgram()) {
ssl_context->protocols = SW_SSL_DTLS;
socket->dtls = 1;
socket->chunk_size = SW_SSL_BUFFER_SIZE;
send = Client_tcp_send_sync;
recv = Client_tcp_recv_no_buffer;
enable_dtls();
}
#else
{
Expand Down Expand Up @@ -820,11 +826,7 @@ static int Client_udp_connect(Client *cli, const char *host, int port, double ti
#ifdef SW_SUPPORT_DTLS
{
udp_connect = 1;
cli->ssl_context->protocols = SW_SSL_DTLS;
cli->socket->dtls = 1;
cli->socket->chunk_size = SW_SSL_BUFFER_SIZE;
cli->send = Client_tcp_send_sync;
cli->recv = Client_tcp_recv_no_buffer;
cli->enable_dtls();
}
#else
{
Expand Down

0 comments on commit b1f3569

Please sign in to comment.