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
|
/*
* Worldvisions Weaver Software:
* Copyright (C) 1997-2002 Net Integration Technologies, Inc.
*/
#include "wvunixsocket.h"
#include "wvistreamlist.h"
#include "wvlog.h"
static void stream_bounce_to_list(WvStream &s, void *userdata)
{
WvIStreamList &l = *(WvIStreamList *)userdata;
WvIStreamList::Iter out(l);
char *line;
while ((line = s.getline(0)) != NULL)
{
if (!strncmp(line, "quit", 4))
{
s.close();
continue;
}
for (out.rewind(); out.next(); )
{
if (&out() != &s && out->iswritable())
{
static_cast<WvStream*>(&out())->print("%s> %s\n",
s.src() ? (WvString)*s.src() : WvString("stdin"),
line);
if (s.src())
wvcon->print("Local address of source was %s\n",
((WvUnixConn *)&s)->localaddr());
}
}
}
}
int main(int argc, char **argv)
{
{
WvLog log("testlisten"), err = log.split(WvLog::Error);
WvIStreamList l;
WvUnixListener sock(argc==2 ? argv[1] : "/tmp/fuzzy",
0777);
wvin->setcallback(stream_bounce_to_list, &l);
sock.auto_accept(&l, stream_bounce_to_list, &l);
log("Listening on port %s\n", *sock.src());
l.append(&sock, false);
l.append(wvin, false);
while (sock.isok() && wvin->isok())
{
if (l.select(-1))
l.callback();
}
if (!sock.isok() && sock.geterr())
err("%s\n", strerror(sock.geterr()));
}
// all variables should now be freed
#if DEBUG
fprintf(stderr, "File descriptors still open: ");
bool found = false;
for (int count = 0; count < 255; count++)
{
int dupfd = dup(count);
if (dupfd >= 0)
{
fprintf(stderr, "#%d ", count);
found = true;
close(dupfd);
}
}
if (!found)
fprintf(stderr, "none.\n");
else
fprintf(stderr, "\n");
#endif
return 0;
}
|