mirror of
https://gitlab.xiph.org/xiph/icecast-server.git
synced 2024-12-04 14:46:30 -05:00
Feature: Added basic ping request support
This commit is contained in:
parent
91ea5d0f79
commit
3c352746ab
102
src/ping.c
102
src/ping.c
@ -12,6 +12,7 @@
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "thread/thread.h"
|
||||
|
||||
@ -21,16 +22,72 @@
|
||||
|
||||
#define CATMODULE "ping"
|
||||
|
||||
static bool ping_running = false;
|
||||
static thread_type *_ping_thread_id;
|
||||
static mutex_t _ping_mutex;
|
||||
typedef struct ping_queue_tag ping_queue_t;
|
||||
|
||||
static void *_ping_thread(void *arg)
|
||||
struct ping_queue_tag {
|
||||
CURL *curl;
|
||||
ping_queue_t *next;
|
||||
};
|
||||
|
||||
static bool ping_running = false;
|
||||
static thread_type *ping_thread_id;
|
||||
static mutex_t ping_mutex;
|
||||
static ping_queue_t *ping_queue;
|
||||
|
||||
static void on_done(ping_queue_t *entry)
|
||||
{
|
||||
icecast_curl_free(entry->curl);
|
||||
free(entry);
|
||||
}
|
||||
|
||||
static void *ping_thread(void *arg)
|
||||
{
|
||||
CURLM *curl_multi = curl_multi_init();
|
||||
|
||||
while (ping_running) {
|
||||
break;
|
||||
ping_queue_t *take;
|
||||
int status;
|
||||
|
||||
thread_mutex_lock(&ping_mutex);
|
||||
take = ping_queue;
|
||||
ping_queue = NULL;
|
||||
thread_mutex_unlock(&ping_mutex);
|
||||
|
||||
while (take) {
|
||||
ping_queue_t *entry = take;
|
||||
|
||||
take = take->next;
|
||||
entry->next = NULL;
|
||||
|
||||
curl_easy_setopt(entry->curl, CURLOPT_PRIVATE, entry);
|
||||
curl_multi_add_handle(curl_multi, entry->curl);
|
||||
}
|
||||
|
||||
if (curl_multi_perform(curl_multi, &status) != CURLM_OK)
|
||||
break;
|
||||
|
||||
if (!status) {
|
||||
thread_sleep(100000);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (curl_multi_wait(curl_multi, NULL, 0, 1000, &status) != CURLM_OK)
|
||||
break;
|
||||
|
||||
while (true) {
|
||||
struct CURLMsg *m = curl_multi_info_read(curl_multi, &status);
|
||||
|
||||
if (!m)
|
||||
break;
|
||||
|
||||
if (m->msg == CURLMSG_DONE) {
|
||||
ping_queue_t *entry = NULL;
|
||||
CURL *e = m->easy_handle;
|
||||
curl_multi_remove_handle(curl_multi, e);
|
||||
curl_easy_getinfo(e, CURLINFO_PRIVATE, &entry);
|
||||
on_done(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
curl_multi_cleanup(curl_multi);
|
||||
@ -38,15 +95,39 @@ static void *_ping_thread(void *arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void ping_add_to_queue(ping_queue_t *entry)
|
||||
{
|
||||
thread_mutex_lock(&ping_mutex);
|
||||
entry->next = ping_queue;
|
||||
ping_queue = entry;
|
||||
thread_mutex_unlock(&ping_mutex);
|
||||
}
|
||||
|
||||
void ping_simple(const char *url)
|
||||
{
|
||||
ping_queue_t *entry = calloc(1, sizeof(*entry));
|
||||
|
||||
if (!entry)
|
||||
return;
|
||||
|
||||
entry->curl = icecast_curl_new(url, NULL);
|
||||
if (!entry->curl) {
|
||||
free(entry);
|
||||
return;
|
||||
}
|
||||
|
||||
ping_add_to_queue(entry);
|
||||
}
|
||||
|
||||
void ping_initialize(void)
|
||||
{
|
||||
if (ping_running)
|
||||
return;
|
||||
|
||||
thread_mutex_create(&_ping_mutex);
|
||||
thread_mutex_create(&ping_mutex);
|
||||
|
||||
ping_running = true;
|
||||
_ping_thread_id = thread_create("Ping Thread", _ping_thread, NULL, THREAD_ATTACHED);
|
||||
ping_thread_id = thread_create("Ping Thread", ping_thread, NULL, THREAD_ATTACHED);
|
||||
}
|
||||
|
||||
void ping_shutdown(void)
|
||||
@ -55,7 +136,8 @@ void ping_shutdown(void)
|
||||
return;
|
||||
|
||||
ping_running = false;
|
||||
ICECAST_LOG_DEBUG("waiting for ping thread");
|
||||
thread_join(_ping_thread_id);
|
||||
thread_mutex_destroy(&_ping_mutex);
|
||||
ICECAST_LOG_DEBUG("Waiting for ping thread");
|
||||
thread_join(ping_thread_id);
|
||||
thread_mutex_destroy(&ping_mutex);
|
||||
ICECAST_LOG_DEBUG("Joined ping thread, good job!");
|
||||
}
|
||||
|
@ -12,4 +12,6 @@
|
||||
void ping_initialize(void);
|
||||
void ping_shutdown(void);
|
||||
|
||||
void ping_simple(const char *url);
|
||||
|
||||
#endif /* __PING_H__ */
|
||||
|
Loading…
Reference in New Issue
Block a user