1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include "wvistreamlist.h"
#include "wvlog.h"
#include "pwvstream.h"
#include "wvstreamclone.h"
#include "wvlinkerhack.h"
#include <signal.h>
WV_LINK_TO(WvConStream);
WV_LINK_TO(WvTCPConn);
volatile bool want_to_die = false;
static void signalhandler(int sig)
{
fprintf(stderr, "Caught signal %d. Exiting...\n", sig);
want_to_die = true;
signal(sig, SIG_DFL);
}
static void bounce_to_list(IWvStream *in, WvIStreamList *list)
{
char buf[4096];
size_t len;
len = in->read(buf, sizeof(buf));
WvIStreamList::Iter i(*list);
for (i.rewind(); i.next(); )
{
if (in != i.ptr())
{
// you might think this assumes IWvStream has a buffer; but in
// fact, we already know that everything in the list is a
// WvStreamClone, and WvStreamClone *does* have an output
// buffer, so this is safe.
i->write(buf, len);
}
}
}
static void died(WvLog &log, WvStringParm name, IWvStream *s)
{
if (s->geterr())
log("%s: %s\n", name, s->errstr());
}
static void add(WvLog &log, WvIStreamList &list, const char *_mon)
{
WvString mon(_mon);
if (mon == "-")
mon = "stdio";
log("Creating stream: '%s'\n", mon);
PWvStream s(mon);
if (!s->isok())
died(log, _mon, s.addRef());
else
{
s->setcallback(wv::bind(bounce_to_list, s.get(), &list));
s->setclosecallback(wv::bind(died, log, _mon, s.addRef()));
}
list.append(s.addRef(), true, _mon);
}
int main(int argc, char **argv)
{
WvIStreamList list;
WvLog log(argv[0], WvLog::Debug);
signal(SIGTERM, signalhandler);
signal(SIGINT, signalhandler);
if (argc <= 1)
{
fprintf(stderr, "Usage: %s <stream1> [stream2 [stream3...]]\n",
argv[0]);
return 1;
}
if (argc == 2) // talking to just one stream means send it to stdio
add(log, list, "-");
for (int count = 1; count < argc; count++)
add(log, list, argv[count]);
while (!want_to_die && list.count() >= 2)
list.runonce();
}
|