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
|
/*
* Worldvisions Weaver Software:
* Copyright (C) 1997-2002 Net Integration Technologies, Inc.
*
* WvStream test using the new WvTask support. This is in ipstreams
* because there are more fun and stressful streams to test here.
*/
#include "wvtcplistener.h"
#include "wvistreamlist.h"
#include "wvlog.h"
#include "strutils.h"
#include "wvcont.h"
#include "wvstreamclone.h"
static void *stream_call(WvStream& s)
{
char *line;
static int sc_count = 0;
int mynum = ++sc_count, count = 0;
WvLog log(WvString("log%s", mynum), WvLog::Info);
while (s.isok())
{
if ((line = s.getline()) != NULL)
{
line = trim_string(line);
s.print("%s/%s: You said: '%s'\n", mynum, count, line);
log("#%s: You said: '%s'\n", count, line);
}
else
{
log("#%s: Tick.\n", count);
s.print("!");
}
count++;
s.continue_select(100*mynum);
}
return 0;
}
static void setupcont_call(WvIStreamList *list, IWvStream *_conn)
{
WvStreamClone *conn = new WvStreamClone(_conn);
conn->setcallback(WvCont(wv::bind(&stream_call, wv::ref(*conn))));
list->append(conn, true, "WvTCPConn");
}
int main()
{
WvLog log("conttest"), err = log.split(WvLog::Error);
WvIStreamList l;
WvTCPListener listen(WvIPPortAddr("0.0.0.0:1129"));
listen.onaccept(wv::bind(setupcont_call, &l, _1));
wvcon->setcallback(WvCont(wv::bind(&stream_call, wv::ref(*wvcon))));
log("Listening on port %s\n", *listen.src());
l.append(&listen, false, "listener");
l.append(wvcon, false, "wvcon");
while (listen.isok() && wvcon->isok())
{
log("main loop\n");
if (l.select(10000))
l.callback();
}
if (!listen.isok() && listen.geterr())
err("%s\n", listen.errstr());
return 0;
}
|