2011-03-10 13:48:02 +03:00
|
|
|
/***************************************************************************
|
2006-09-12 15:25:00 +04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 15:51:48 +03:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2006-09-12 15:25:00 +04:00
|
|
|
*
|
2011-03-10 13:48:02 +03:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 16:02:01 +03:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2006-09-12 15:25:00 +04:00
|
|
|
*
|
2011-03-10 13:48:02 +03:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2018-03-16 18:01:01 +03:00
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 12:16:50 +03:00
|
|
|
*
|
2011-03-10 13:48:02 +03:00
|
|
|
***************************************************************************/
|
2015-06-18 12:38:54 +03:00
|
|
|
/* <DESC>
|
|
|
|
* multi socket API usage with libevent 2
|
|
|
|
* </DESC>
|
|
|
|
*/
|
2011-03-10 13:48:02 +03:00
|
|
|
/* Example application source code using the multi socket interface to
|
|
|
|
download many files at once.
|
|
|
|
|
|
|
|
Written by Jeff Pohlmeyer
|
2006-09-12 15:25:00 +04:00
|
|
|
|
2013-03-10 22:44:48 +04:00
|
|
|
Requires libevent version 2 and a (POSIX?) system that has mkfifo().
|
2006-09-12 15:25:00 +04:00
|
|
|
|
|
|
|
This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
|
|
|
|
sample programs.
|
|
|
|
|
|
|
|
When running, the program creates the named pipe "hiper.fifo"
|
|
|
|
|
|
|
|
Whenever there is input into the fifo, the program reads the input as a list
|
|
|
|
of URL's and creates some new easy handles to fetch each URL via the
|
|
|
|
curl_multi "hiper" API.
|
|
|
|
|
|
|
|
|
|
|
|
Thus, you can try a single URL:
|
|
|
|
% echo http://www.yahoo.com > hiper.fifo
|
|
|
|
|
|
|
|
Or a whole bunch of them:
|
|
|
|
% cat my-url-list > hiper.fifo
|
|
|
|
|
|
|
|
The fifo buffer is handled almost instantly, so you can even add more URL's
|
|
|
|
while the previous requests are still being downloaded.
|
|
|
|
|
|
|
|
Note:
|
|
|
|
For the sake of simplicity, URL length is limited to 1023 char's !
|
|
|
|
|
|
|
|
This is purely a demo app, all retrieved data is simply discarded by the write
|
|
|
|
callback.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <curl/curl.h>
|
2013-03-10 22:44:48 +04:00
|
|
|
#include <event2/event.h>
|
2018-03-16 18:01:01 +03:00
|
|
|
#include <event2/event_struct.h>
|
2006-09-12 15:25:00 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
2018-03-16 18:01:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2006-09-12 15:25:00 +04:00
|
|
|
|
|
|
|
#define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
|
|
|
|
|
|
|
|
|
|
|
|
/* Global information, common to all connections */
|
2013-03-10 22:44:48 +04:00
|
|
|
typedef struct _GlobalInfo
|
|
|
|
{
|
|
|
|
struct event_base *evbase;
|
2018-03-16 18:01:01 +03:00
|
|
|
struct event fifo_event;
|
|
|
|
struct event timer_event;
|
2006-09-12 15:25:00 +04:00
|
|
|
CURLM *multi;
|
|
|
|
int still_running;
|
2016-11-23 10:30:18 +03:00
|
|
|
FILE *input;
|
2018-03-16 18:01:01 +03:00
|
|
|
int stopped;
|
2006-09-12 15:25:00 +04:00
|
|
|
} GlobalInfo;
|
|
|
|
|
|
|
|
|
|
|
|
/* Information associated with a specific easy handle */
|
2013-03-10 22:44:48 +04:00
|
|
|
typedef struct _ConnInfo
|
|
|
|
{
|
2006-09-12 15:25:00 +04:00
|
|
|
CURL *easy;
|
|
|
|
char *url;
|
|
|
|
GlobalInfo *global;
|
|
|
|
char error[CURL_ERROR_SIZE];
|
|
|
|
} ConnInfo;
|
|
|
|
|
|
|
|
|
|
|
|
/* Information associated with a specific socket */
|
2013-03-10 22:44:48 +04:00
|
|
|
typedef struct _SockInfo
|
|
|
|
{
|
2006-09-12 15:25:00 +04:00
|
|
|
curl_socket_t sockfd;
|
|
|
|
CURL *easy;
|
|
|
|
int action;
|
|
|
|
long timeout;
|
2018-03-16 18:01:01 +03:00
|
|
|
struct event ev;
|
2006-09-12 15:25:00 +04:00
|
|
|
GlobalInfo *global;
|
|
|
|
} SockInfo;
|
|
|
|
|
2019-08-22 16:29:14 +03:00
|
|
|
#define mycase(code) \
|
2018-03-20 10:12:35 +03:00
|
|
|
case code: s = __STRING(code)
|
2018-03-16 18:01:01 +03:00
|
|
|
|
|
|
|
/* Die if we get a bad CURLMcode somewhere */
|
|
|
|
static void mcode_or_die(const char *where, CURLMcode code)
|
|
|
|
{
|
|
|
|
if(CURLM_OK != code) {
|
|
|
|
const char *s;
|
|
|
|
switch(code) {
|
2019-08-22 16:29:14 +03:00
|
|
|
mycase(CURLM_BAD_HANDLE); break;
|
|
|
|
mycase(CURLM_BAD_EASY_HANDLE); break;
|
|
|
|
mycase(CURLM_OUT_OF_MEMORY); break;
|
|
|
|
mycase(CURLM_INTERNAL_ERROR); break;
|
|
|
|
mycase(CURLM_UNKNOWN_OPTION); break;
|
|
|
|
mycase(CURLM_LAST); break;
|
2018-03-16 18:01:01 +03:00
|
|
|
default: s = "CURLM_unknown"; break;
|
2019-08-22 16:29:14 +03:00
|
|
|
mycase(CURLM_BAD_SOCKET);
|
2018-03-16 18:01:01 +03:00
|
|
|
fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
|
|
|
|
/* ignore this error */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
|
|
|
|
exit(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-12 15:25:00 +04:00
|
|
|
/* Update the event timer after curl_multi library calls */
|
2019-07-18 21:25:25 +03:00
|
|
|
static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
|
2006-09-12 15:25:00 +04:00
|
|
|
{
|
|
|
|
struct timeval timeout;
|
2019-07-18 21:25:25 +03:00
|
|
|
(void)multi;
|
2006-09-12 15:25:00 +04:00
|
|
|
|
|
|
|
timeout.tv_sec = timeout_ms/1000;
|
|
|
|
timeout.tv_usec = (timeout_ms%1000)*1000;
|
2007-05-02 17:52:38 +04:00
|
|
|
fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
|
2017-05-19 15:25:16 +03:00
|
|
|
|
2019-02-22 15:44:41 +03:00
|
|
|
/*
|
2017-05-19 15:25:16 +03:00
|
|
|
* if timeout_ms is -1, just delete the timer
|
|
|
|
*
|
2019-02-22 15:44:41 +03:00
|
|
|
* For all other values of timeout_ms, this should set or *update* the timer
|
|
|
|
* to the new value
|
2017-05-19 15:25:16 +03:00
|
|
|
*/
|
2019-02-22 15:44:41 +03:00
|
|
|
if(timeout_ms == -1)
|
2018-03-16 18:01:01 +03:00
|
|
|
evtimer_del(&g->timer_event);
|
2019-02-22 15:44:41 +03:00
|
|
|
else /* includes timeout zero */
|
2018-03-16 18:01:01 +03:00
|
|
|
evtimer_add(&g->timer_event, &timeout);
|
2007-05-02 17:52:38 +04:00
|
|
|
return 0;
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Check for completed transfers, and remove their easy handles */
|
2010-09-30 13:33:33 +04:00
|
|
|
static void check_multi_info(GlobalInfo *g)
|
2006-09-12 15:25:00 +04:00
|
|
|
{
|
2010-09-30 13:33:33 +04:00
|
|
|
char *eff_url;
|
|
|
|
CURLMsg *msg;
|
|
|
|
int msgs_left;
|
|
|
|
ConnInfo *conn;
|
|
|
|
CURL *easy;
|
|
|
|
CURLcode res;
|
|
|
|
|
|
|
|
fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
|
2016-02-11 11:42:38 +03:00
|
|
|
while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
|
|
|
|
if(msg->msg == CURLMSG_DONE) {
|
2010-09-30 13:33:33 +04:00
|
|
|
easy = msg->easy_handle;
|
|
|
|
res = msg->data.result;
|
|
|
|
curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
|
|
|
|
curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
|
|
|
|
fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
|
|
|
|
curl_multi_remove_handle(g->multi, easy);
|
|
|
|
free(conn->url);
|
|
|
|
curl_easy_cleanup(easy);
|
|
|
|
free(conn);
|
|
|
|
}
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
2018-03-20 10:12:35 +03:00
|
|
|
if(g->still_running == 0 && g->stopped)
|
2018-03-16 18:01:01 +03:00
|
|
|
event_base_loopbreak(g->evbase);
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Called by libevent when we get action on a multi socket */
|
|
|
|
static void event_cb(int fd, short kind, void *userp)
|
|
|
|
{
|
|
|
|
GlobalInfo *g = (GlobalInfo*) userp;
|
|
|
|
CURLMcode rc;
|
2008-11-19 18:30:41 +03:00
|
|
|
|
|
|
|
int action =
|
2019-05-21 12:25:42 +03:00
|
|
|
((kind & EV_READ) ? CURL_CSELECT_IN : 0) |
|
|
|
|
((kind & EV_WRITE) ? CURL_CSELECT_OUT : 0);
|
2008-11-19 18:30:41 +03:00
|
|
|
|
2010-09-30 13:33:33 +04:00
|
|
|
rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
|
2008-11-19 18:30:41 +03:00
|
|
|
mcode_or_die("event_cb: curl_multi_socket_action", rc);
|
|
|
|
|
2010-09-30 13:33:33 +04:00
|
|
|
check_multi_info(g);
|
2016-04-03 23:51:52 +03:00
|
|
|
if(g->still_running <= 0) {
|
2006-09-12 15:25:00 +04:00
|
|
|
fprintf(MSG_OUT, "last transfer done, kill timeout\n");
|
2018-03-16 18:01:01 +03:00
|
|
|
if(evtimer_pending(&g->timer_event, NULL)) {
|
|
|
|
evtimer_del(&g->timer_event);
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Called by libevent when our timeout expires */
|
2019-07-18 21:25:25 +03:00
|
|
|
static void timer_cb(int fd, short kind, void *userp)
|
2006-09-12 15:25:00 +04:00
|
|
|
{
|
|
|
|
GlobalInfo *g = (GlobalInfo *)userp;
|
|
|
|
CURLMcode rc;
|
2019-07-18 21:25:25 +03:00
|
|
|
(void)fd;
|
|
|
|
(void)kind;
|
2006-09-12 15:25:00 +04:00
|
|
|
|
2010-09-30 13:33:33 +04:00
|
|
|
rc = curl_multi_socket_action(g->multi,
|
2008-11-19 18:30:41 +03:00
|
|
|
CURL_SOCKET_TIMEOUT, 0, &g->still_running);
|
|
|
|
mcode_or_die("timer_cb: curl_multi_socket_action", rc);
|
2010-09-30 13:33:33 +04:00
|
|
|
check_multi_info(g);
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Clean up the SockInfo structure */
|
|
|
|
static void remsock(SockInfo *f)
|
|
|
|
{
|
2016-02-11 11:42:38 +03:00
|
|
|
if(f) {
|
2021-04-09 12:03:30 +03:00
|
|
|
if(event_initialized(&f->ev)) {
|
|
|
|
event_del(&f->ev);
|
|
|
|
}
|
2008-05-20 00:40:53 +04:00
|
|
|
free(f);
|
|
|
|
}
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Assign information to a SockInfo structure */
|
2016-11-23 10:30:18 +03:00
|
|
|
static void setsock(SockInfo *f, curl_socket_t s, CURL *e, int act,
|
|
|
|
GlobalInfo *g)
|
2006-09-12 15:25:00 +04:00
|
|
|
{
|
|
|
|
int kind =
|
2019-05-21 12:25:42 +03:00
|
|
|
((act & CURL_POLL_IN) ? EV_READ : 0) |
|
|
|
|
((act & CURL_POLL_OUT) ? EV_WRITE : 0) | EV_PERSIST;
|
2006-09-12 15:25:00 +04:00
|
|
|
|
|
|
|
f->sockfd = s;
|
|
|
|
f->action = act;
|
|
|
|
f->easy = e;
|
2021-04-09 12:03:30 +03:00
|
|
|
if(event_initialized(&f->ev)) {
|
|
|
|
event_del(&f->ev);
|
|
|
|
}
|
2018-03-16 18:01:01 +03:00
|
|
|
event_assign(&f->ev, g->evbase, f->sockfd, kind, event_cb, g);
|
|
|
|
event_add(&f->ev, NULL);
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize a new SockInfo structure */
|
2013-03-10 22:44:48 +04:00
|
|
|
static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
|
|
|
|
{
|
2021-09-06 16:51:30 +03:00
|
|
|
SockInfo *fdp = calloc(1, sizeof(SockInfo));
|
2006-09-12 15:25:00 +04:00
|
|
|
|
|
|
|
fdp->global = g;
|
|
|
|
setsock(fdp, s, easy, action, g);
|
|
|
|
curl_multi_assign(g->multi, s, fdp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CURLMOPT_SOCKETFUNCTION */
|
|
|
|
static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
|
|
|
|
{
|
|
|
|
GlobalInfo *g = (GlobalInfo*) cbp;
|
|
|
|
SockInfo *fdp = (SockInfo*) sockp;
|
2007-07-13 01:11:10 +04:00
|
|
|
const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" };
|
2006-09-12 15:25:00 +04:00
|
|
|
|
|
|
|
fprintf(MSG_OUT,
|
|
|
|
"socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
|
2016-02-11 11:42:38 +03:00
|
|
|
if(what == CURL_POLL_REMOVE) {
|
2006-09-12 15:25:00 +04:00
|
|
|
fprintf(MSG_OUT, "\n");
|
|
|
|
remsock(fdp);
|
2008-05-20 00:40:53 +04:00
|
|
|
}
|
|
|
|
else {
|
2016-02-11 11:42:38 +03:00
|
|
|
if(!fdp) {
|
2008-05-20 00:40:53 +04:00
|
|
|
fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
|
2006-09-12 15:25:00 +04:00
|
|
|
addsock(s, e, what, g);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(MSG_OUT,
|
2008-05-20 00:40:53 +04:00
|
|
|
"Changing action from %s to %s\n",
|
|
|
|
whatstr[fdp->action], whatstr[what]);
|
2006-09-12 15:25:00 +04:00
|
|
|
setsock(fdp, s, e, what, g);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* CURLOPT_WRITEFUNCTION */
|
2019-07-18 21:25:25 +03:00
|
|
|
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
|
2006-09-12 15:25:00 +04:00
|
|
|
{
|
2019-07-18 21:25:25 +03:00
|
|
|
(void)ptr;
|
2019-05-20 12:50:23 +03:00
|
|
|
(void)data;
|
2019-07-18 21:25:25 +03:00
|
|
|
return size * nmemb;
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* CURLOPT_PROGRESSFUNCTION */
|
2019-07-18 21:25:25 +03:00
|
|
|
static int prog_cb(void *p, double dltotal, double dlnow, double ult,
|
|
|
|
double uln)
|
2006-09-12 15:25:00 +04:00
|
|
|
{
|
|
|
|
ConnInfo *conn = (ConnInfo *)p;
|
2019-07-18 21:25:25 +03:00
|
|
|
(void)ult;
|
|
|
|
(void)uln;
|
2008-05-17 01:14:50 +04:00
|
|
|
|
2006-09-12 15:25:00 +04:00
|
|
|
fprintf(MSG_OUT, "Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Create a new easy handle, and add it to the global curl_multi */
|
2016-04-03 23:51:52 +03:00
|
|
|
static void new_conn(char *url, GlobalInfo *g)
|
2008-05-17 01:14:50 +04:00
|
|
|
{
|
2006-09-12 15:25:00 +04:00
|
|
|
ConnInfo *conn;
|
|
|
|
CURLMcode rc;
|
|
|
|
|
|
|
|
conn = calloc(1, sizeof(ConnInfo));
|
|
|
|
conn->error[0]='\0';
|
|
|
|
|
|
|
|
conn->easy = curl_easy_init();
|
2016-02-11 11:42:38 +03:00
|
|
|
if(!conn->easy) {
|
2006-09-12 15:25:00 +04:00
|
|
|
fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
conn->global = g;
|
|
|
|
conn->url = strdup(url);
|
|
|
|
curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url);
|
|
|
|
curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb);
|
2015-10-04 21:30:07 +03:00
|
|
|
curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, conn);
|
2008-05-23 01:20:07 +04:00
|
|
|
curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1L);
|
2006-09-12 15:25:00 +04:00
|
|
|
curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error);
|
|
|
|
curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn);
|
2008-05-23 01:20:07 +04:00
|
|
|
curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0L);
|
2006-09-12 15:25:00 +04:00
|
|
|
curl_easy_setopt(conn->easy, CURLOPT_PROGRESSFUNCTION, prog_cb);
|
|
|
|
curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn);
|
2018-03-16 18:01:01 +03:00
|
|
|
curl_easy_setopt(conn->easy, CURLOPT_FOLLOWLOCATION, 1L);
|
2006-09-12 15:25:00 +04:00
|
|
|
fprintf(MSG_OUT,
|
|
|
|
"Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url);
|
2010-09-30 13:33:33 +04:00
|
|
|
rc = curl_multi_add_handle(g->multi, conn->easy);
|
2006-09-12 15:25:00 +04:00
|
|
|
mcode_or_die("new_conn: curl_multi_add_handle", rc);
|
2008-11-19 18:30:41 +03:00
|
|
|
|
2023-09-07 20:53:49 +03:00
|
|
|
/* note that the add_handle() will set a time-out to trigger soon so that
|
|
|
|
the necessary socket_action() call will be called by this app */
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This gets called whenever data is received from the fifo */
|
2019-07-18 21:25:25 +03:00
|
|
|
static void fifo_cb(int fd, short event, void *arg)
|
2008-05-17 01:14:50 +04:00
|
|
|
{
|
2006-09-12 15:25:00 +04:00
|
|
|
char s[1024];
|
2017-09-10 00:09:06 +03:00
|
|
|
long int rv = 0;
|
|
|
|
int n = 0;
|
2006-09-12 15:25:00 +04:00
|
|
|
GlobalInfo *g = (GlobalInfo *)arg;
|
2019-07-18 21:25:25 +03:00
|
|
|
(void)fd;
|
|
|
|
(void)event;
|
2006-09-12 15:25:00 +04:00
|
|
|
|
|
|
|
do {
|
|
|
|
s[0]='\0';
|
2017-09-10 00:09:06 +03:00
|
|
|
rv = fscanf(g->input, "%1023s%n", s, &n);
|
2006-09-12 15:25:00 +04:00
|
|
|
s[n]='\0';
|
2016-04-03 23:51:52 +03:00
|
|
|
if(n && s[0]) {
|
2018-03-20 10:12:35 +03:00
|
|
|
if(!strcmp(s, "stop")) {
|
2018-03-16 18:01:01 +03:00
|
|
|
g->stopped = 1;
|
2018-03-20 10:12:35 +03:00
|
|
|
if(g->still_running == 0)
|
2018-03-16 18:01:01 +03:00
|
|
|
event_base_loopbreak(g->evbase);
|
2018-03-20 10:12:35 +03:00
|
|
|
}
|
|
|
|
else
|
2018-03-16 18:01:01 +03:00
|
|
|
new_conn(s, arg); /* if we read a URL, go get it! */
|
2016-02-11 11:42:38 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} while(rv != EOF);
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a named pipe and tell libevent to monitor it */
|
2013-03-10 22:44:48 +04:00
|
|
|
static const char *fifo = "hiper.fifo";
|
2016-12-14 01:34:59 +03:00
|
|
|
static int init_fifo(GlobalInfo *g)
|
2008-05-17 01:14:50 +04:00
|
|
|
{
|
2006-09-12 15:25:00 +04:00
|
|
|
struct stat st;
|
2010-12-18 19:12:44 +03:00
|
|
|
curl_socket_t sockfd;
|
2006-09-12 15:25:00 +04:00
|
|
|
|
|
|
|
fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
|
2016-02-11 11:42:38 +03:00
|
|
|
if(lstat (fifo, &st) == 0) {
|
|
|
|
if((st.st_mode & S_IFMT) == S_IFREG) {
|
2006-09-12 15:25:00 +04:00
|
|
|
errno = EEXIST;
|
|
|
|
perror("lstat");
|
2016-12-14 01:34:59 +03:00
|
|
|
exit(1);
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unlink(fifo);
|
2016-02-11 11:42:38 +03:00
|
|
|
if(mkfifo (fifo, 0600) == -1) {
|
2006-09-12 15:25:00 +04:00
|
|
|
perror("mkfifo");
|
2016-12-14 01:34:59 +03:00
|
|
|
exit(1);
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
2008-05-17 01:14:50 +04:00
|
|
|
sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
|
2016-02-11 11:42:38 +03:00
|
|
|
if(sockfd == -1) {
|
2008-05-17 01:14:50 +04:00
|
|
|
perror("open");
|
2016-12-14 01:34:59 +03:00
|
|
|
exit(1);
|
2006-09-12 15:25:00 +04:00
|
|
|
}
|
2008-05-17 01:14:50 +04:00
|
|
|
g->input = fdopen(sockfd, "r");
|
2006-09-12 15:25:00 +04:00
|
|
|
|
|
|
|
fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
|
2018-03-20 10:12:35 +03:00
|
|
|
event_assign(&g->fifo_event, g->evbase, sockfd, EV_READ|EV_PERSIST,
|
|
|
|
fifo_cb, g);
|
2018-03-16 18:01:01 +03:00
|
|
|
event_add(&g->fifo_event, NULL);
|
2006-09-12 15:25:00 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-03-10 22:44:48 +04:00
|
|
|
static void clean_fifo(GlobalInfo *g)
|
|
|
|
{
|
2018-03-16 18:01:01 +03:00
|
|
|
event_del(&g->fifo_event);
|
2013-03-10 22:44:48 +04:00
|
|
|
fclose(g->input);
|
|
|
|
unlink(fifo);
|
|
|
|
}
|
|
|
|
|
2019-07-18 21:25:25 +03:00
|
|
|
int main(int argc, char **argv)
|
2006-09-12 15:25:00 +04:00
|
|
|
{
|
|
|
|
GlobalInfo g;
|
2019-07-18 21:25:25 +03:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2006-09-12 15:25:00 +04:00
|
|
|
|
|
|
|
memset(&g, 0, sizeof(GlobalInfo));
|
2013-03-10 22:44:48 +04:00
|
|
|
g.evbase = event_base_new();
|
2006-09-12 15:25:00 +04:00
|
|
|
init_fifo(&g);
|
|
|
|
g.multi = curl_multi_init();
|
2018-03-16 18:01:01 +03:00
|
|
|
evtimer_assign(&g.timer_event, g.evbase, timer_cb, &g);
|
2008-11-19 18:30:41 +03:00
|
|
|
|
|
|
|
/* setup the generic multi interface options we want */
|
2006-09-12 15:25:00 +04:00
|
|
|
curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
|
|
|
|
curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
|
2007-05-02 17:52:38 +04:00
|
|
|
curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
|
|
|
|
curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
|
2008-11-19 18:30:41 +03:00
|
|
|
|
2021-10-31 18:34:44 +03:00
|
|
|
/* we do not call any curl_multi_socket*() function yet as we have no handles
|
2008-11-19 18:30:41 +03:00
|
|
|
added! */
|
|
|
|
|
2013-03-10 22:44:48 +04:00
|
|
|
event_base_dispatch(g.evbase);
|
|
|
|
|
2021-10-31 18:34:44 +03:00
|
|
|
/* this, of course, will not get called since only way to stop this program
|
|
|
|
is via ctrl-C, but it is here to show how cleanup /would/ be done. */
|
2013-03-10 22:44:48 +04:00
|
|
|
clean_fifo(&g);
|
2018-03-16 18:01:01 +03:00
|
|
|
event_del(&g.timer_event);
|
2013-03-10 22:44:48 +04:00
|
|
|
event_base_free(g.evbase);
|
2006-09-12 15:25:00 +04:00
|
|
|
curl_multi_cleanup(g.multi);
|
|
|
|
return 0;
|
|
|
|
}
|