Refactor to remove duplicate code.

pull/2255/head
Roger A. Light 4 years ago
parent 0e1388a615
commit 625e2a5060

@ -943,27 +943,26 @@ int net__socket_connect(struct mosquitto *mosq, const char *host, uint16_t port,
} }
ssize_t net__read(struct mosquitto *mosq, void *buf, size_t count)
{
#ifdef WITH_TLS #ifdef WITH_TLS
int ret; static net__handle_ssl(struct mosquitto* mosq, int ret)
{
int err; int err;
#endif
assert(mosq);
errno = 0;
#ifdef WITH_TLS
if(mosq->ssl){
ret = SSL_read(mosq->ssl, buf, (int)count);
if(ret <= 0){
err = SSL_get_error(mosq->ssl, ret); err = SSL_get_error(mosq->ssl, ret);
if (err == SSL_ERROR_WANT_READ) { if (err == SSL_ERROR_WANT_READ) {
ret = -1; ret = -1;
errno = EAGAIN; errno = EAGAIN;
}else if(err == SSL_ERROR_WANT_WRITE){ }
else if (err == SSL_ERROR_WANT_WRITE) {
ret = -1; ret = -1;
#ifdef WITH_BROKER
mux__add_out(mosq);
#else
mosq->want_write = true; mosq->want_write = true;
#endif
errno = EAGAIN; errno = EAGAIN;
}else{ }
else {
net__print_ssl_error(mosq); net__print_ssl_error(mosq);
errno = EPROTO; errno = EPROTO;
} }
@ -971,6 +970,23 @@ ssize_t net__read(struct mosquitto *mosq, void *buf, size_t count)
#ifdef WIN32 #ifdef WIN32
WSASetLastError(errno); WSASetLastError(errno);
#endif #endif
return ret;
}
#endif
ssize_t net__read(struct mosquitto *mosq, void *buf, size_t count)
{
#ifdef WITH_TLS
int ret;
#endif
assert(mosq);
errno = 0;
#ifdef WITH_TLS
if(mosq->ssl){
ret = SSL_read(mosq->ssl, buf, (int)count);
if(ret <= 0){
ret = net__handle_ssl(mosq, ret);
} }
return (ssize_t )ret; return (ssize_t )ret;
}else{ }else{
@ -993,7 +1009,6 @@ ssize_t net__write(struct mosquitto *mosq, const void *buf, size_t count)
{ {
#ifdef WITH_TLS #ifdef WITH_TLS
int ret; int ret;
int err;
#endif #endif
assert(mosq); assert(mosq);
@ -1003,22 +1018,7 @@ ssize_t net__write(struct mosquitto *mosq, const void *buf, size_t count)
mosq->want_write = false; mosq->want_write = false;
ret = SSL_write(mosq->ssl, buf, (int)count); ret = SSL_write(mosq->ssl, buf, (int)count);
if(ret < 0){ if(ret < 0){
err = SSL_get_error(mosq->ssl, ret); ret = net__handle_ssl(mosq, ret);
if(err == SSL_ERROR_WANT_READ){
ret = -1;
errno = EAGAIN;
}else if(err == SSL_ERROR_WANT_WRITE){
ret = -1;
mosq->want_write = true;
errno = EAGAIN;
}else{
net__print_ssl_error(mosq);
errno = EPROTO;
}
ERR_clear_error();
#ifdef WIN32
WSASetLastError(errno);
#endif
} }
return (ssize_t )ret; return (ssize_t )ret;
}else{ }else{

Loading…
Cancel
Save