Refactor to remove duplicate code.

pull/2255/head
Roger A. Light 4 years ago
parent 625e2a5060
commit 5851713f3e

@ -106,36 +106,44 @@ int mux_poll__init(struct mosquitto__listener_sock *listensock, int listensock_c
} }
int mux_poll__add_out(struct mosquitto *context) static mux_poll__add(struct mosquitto* context, int evt)
{ {
size_t i; size_t i;
if(!(context->events & POLLOUT)) { if(context->events == evt){
if(context->pollfd_index != -1){ return MOSQ_ERR_SUCCESS;
pollfds[context->pollfd_index].fd = context->sock; }
pollfds[context->pollfd_index].events = POLLIN | POLLOUT;
pollfds[context->pollfd_index].revents = 0; if(context->pollfd_index != -1){
}else{ pollfds[context->pollfd_index].fd = context->sock;
for(i=0; i<pollfd_max; i++){ pollfds[context->pollfd_index].events = evt;
if(pollfds[i].fd == INVALID_SOCKET){ pollfds[context->pollfd_index].revents = 0;
pollfds[i].fd = context->sock; }else{
pollfds[i].events = POLLIN | POLLOUT; for(i=0; i<pollfd_max; i++) {
pollfds[i].revents = 0; if(pollfds[i].fd == INVALID_SOCKET){
context->pollfd_index = (int )i; pollfds[i].fd = context->sock;
if(i > pollfd_current_max){ pollfds[i].events = POLLIN;
pollfd_current_max = i; pollfds[i].revents = 0;
} context->pollfd_index = (int)i;
break; if(i > pollfd_current_max){
pollfd_current_max = i;
} }
break;
} }
} }
context->events = POLLIN | POLLOUT;
} }
context->events = evt;
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;
} }
int mux_poll__add_out(struct mosquitto *context)
{
return mux_poll__add(context, POLLIN | POLLOUT);
}
int mux_poll__remove_out(struct mosquitto *context) int mux_poll__remove_out(struct mosquitto *context)
{ {
if(context->events & POLLOUT) { if(context->events & POLLOUT) {
@ -148,29 +156,7 @@ int mux_poll__remove_out(struct mosquitto *context)
int mux_poll__add_in(struct mosquitto *context) int mux_poll__add_in(struct mosquitto *context)
{ {
size_t i; return mux_poll__add(context, POLLIN);
if(context->pollfd_index != -1){
pollfds[context->pollfd_index].fd = context->sock;
pollfds[context->pollfd_index].events = POLLIN;
pollfds[context->pollfd_index].revents = 0;
}else{
for(i=0; i<pollfd_max; i++){
if(pollfds[i].fd == INVALID_SOCKET){
pollfds[i].fd = context->sock;
pollfds[i].events = POLLIN;
pollfds[i].revents = 0;
context->pollfd_index = (int )i;
if(i > pollfd_current_max){
pollfd_current_max = i;
}
break;
}
}
}
context->events = POLLIN;
return MOSQ_ERR_SUCCESS;
} }
int mux_poll__delete(struct mosquitto *context) int mux_poll__delete(struct mosquitto *context)

Loading…
Cancel
Save