uhub-admin: don't busy loop before a connection has been established.

This happened due to the network polling mechanism had nothing to poll
for, so it returned immediately only to be called again (during DNS lookup).

This fix introduces a control pipe that is polled for reading, althoug nothing
is ever sent to that pipe. But, it can be used instead of the signal
handler approach which is currently used for terminating the program.
This commit is contained in:
Jan Vidar Krey 2014-07-29 17:35:58 +02:00
parent 9f78a2e85f
commit b5bedfe9e4
2 changed files with 44 additions and 15 deletions

View File

@ -600,6 +600,17 @@ int ADC_client_connect(struct ADC_client* client, const char* address)
return 1; return 1;
} }
static void ADC_client_send_handshake(struct ADC_client* client)
{
ADC_TRACE;
struct adc_message* handshake = adc_msg_create(ADC_HANDSHAKE);
client->callback(client, ADC_CLIENT_CONNECTED, 0);
net_con_update(client->con, NET_EVENT_READ);
ADC_client_send(client, handshake);
ADC_client_set_state(client, ps_protocol);
adc_msg_free(handshake);
}
static void ADC_client_on_connected(struct ADC_client* client) static void ADC_client_on_connected(struct ADC_client* client)
{ {
@ -610,32 +621,20 @@ static void ADC_client_on_connected(struct ADC_client* client)
net_con_update(client->con, NET_EVENT_READ | NET_EVENT_WRITE); net_con_update(client->con, NET_EVENT_READ | NET_EVENT_WRITE);
client->callback(client, ADC_CLIENT_SSL_HANDSHAKE, 0); client->callback(client, ADC_CLIENT_SSL_HANDSHAKE, 0);
ADC_client_set_state(client, ps_conn_ssl); ADC_client_set_state(client, ps_conn_ssl);
net_con_ssl_handshake(client->con, net_con_ssl_mode_client, NULL); net_con_ssl_handshake(client->con, net_con_ssl_mode_client, NULL);
} }
else else
#endif #endif
{ ADC_client_send_handshake(client);
struct adc_message* handshake = adc_msg_create(ADC_HANDSHAKE);
net_con_update(client->con, NET_EVENT_READ);
client->callback(client, ADC_CLIENT_CONNECTED, 0);
ADC_client_send(client, handshake);
ADC_client_set_state(client, ps_protocol);
adc_msg_free(handshake);
}
} }
#ifdef SSL_SUPPORT #ifdef SSL_SUPPORT
static void ADC_client_on_connected_ssl(struct ADC_client* client) static void ADC_client_on_connected_ssl(struct ADC_client* client)
{ {
ADC_TRACE; ADC_TRACE;
struct adc_message* handshake = adc_msg_create(ADC_HANDSHAKE);
client->callback(client, ADC_CLIENT_SSL_OK, 0); client->callback(client, ADC_CLIENT_SSL_OK, 0);
client->callback(client, ADC_CLIENT_CONNECTED, 0); ADC_client_send_handshake(client);
net_con_update(client->con, NET_EVENT_READ);
ADC_client_send(client, handshake);
ADC_client_set_state(client, ps_protocol);
adc_msg_free(handshake);
} }
#endif #endif

View File

@ -159,6 +159,9 @@ static int handle(struct ADC_client* client, enum ADC_client_callback_type type,
static int running = 1; static int running = 1;
#if !defined(WIN32) #if !defined(WIN32)
static int adm_pipes[2] = { -1, -1 };
static struct net_connection* adm_con = 0;
void adm_handle_signal(int sig) void adm_handle_signal(int sig)
{ {
switch (sig) switch (sig)
@ -195,6 +198,10 @@ static int signals[] =
0 0
}; };
void adm_callback(struct net_connection* con, int event, void* ptr)
{
}
void adm_setup_signal_handlers() void adm_setup_signal_handlers()
{ {
sigset_t sig_set; sigset_t sig_set;
@ -215,6 +222,26 @@ void adm_setup_signal_handlers()
} }
} }
void adm_setup_control_pipe()
{
int ret = pipe(adm_pipes);
if (ret == -1)
{
LOG_ERROR("Unable to setup control pipes.");
}
adm_con = net_con_create();
net_con_initialize(adm_con, adm_pipes[0], adm_callback, 0, NET_EVENT_READ);
}
void adm_shutdown_control_pipe()
{
net_con_destroy(adm_con);
close(adm_pipes[0]);
close(adm_pipes[1]);
adm_pipes[0] = -1;
adm_pipes[0] = -1;
}
void adm_shutdown_signal_handlers() void adm_shutdown_signal_handlers()
{ {
} }
@ -233,6 +260,7 @@ int main(int argc, char** argv)
struct ADC_client* client; struct ADC_client* client;
net_initialize(); net_initialize();
adm_setup_control_pipe();
memset(g_usermap, 0, sizeof(g_usermap)); memset(g_usermap, 0, sizeof(g_usermap));
@ -242,6 +270,8 @@ int main(int argc, char** argv)
while (running && net_backend_process()) { } while (running && net_backend_process()) { }
adm_shutdown_control_pipe();
ADC_client_destroy(client); ADC_client_destroy(client);
net_destroy(); net_destroy();
adm_shutdown_signal_handlers(); adm_shutdown_signal_handlers();