WvStreams
wvlistener.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * A base implementation for "listeners", streams that spawn other streams
6  * from (presumably) incoming connections.
7  */
8 #ifndef __WVLISTENER_H
9 #define __WVLISTENER_H
10 
11 #include "iwvlistener.h"
12 #include "wvstreamclone.h" // FIXME needed *only* for CompatCallback
13 #include "wvattrs.h"
14 
15 class WvListener : public IWvListener
16 {
17  IMPLEMENT_IOBJECT(WvListener);
18  WvAttrs attrs;
19 public:
20  IWvStream *cloned;
21  IWvListenerCallback acceptor;
22  IWvListenerWrapper wrapper;
23 
24  WvListener(IWvStream *_cloned);
25  virtual ~WvListener();
26 
27  virtual void addwrap(IWvListenerWrapper _wrapper);
28 
29  virtual IWvListenerCallback onaccept(IWvListenerCallback _cb);
30  IWvStream *wrap(IWvStream *s);
31  void runonce(time_t msec_delay);
32 
33  //
34  // IWvStream default implementation.
35  //
36  virtual void close()
37  { if (cloned) cloned->close(); }
38  virtual bool isok() const
39  { return WvErrorBase::isok() && cloned && cloned->isok(); }
40 
41  virtual void callback();
42 
43  int getfd() const
44  { return getrfd(); }
45  virtual int getrfd() const
46  { return cloned ? cloned->getrfd() : -1; }
47  virtual int getwfd() const
48  { return cloned ? cloned->getwfd() : -1; }
49 
50  virtual const WvAddr *src() const
51  { return cloned ? cloned->src() : NULL; }
52 
53  virtual void pre_select(SelectInfo &si)
54  { if (cloned) cloned->pre_select(si); }
55  virtual bool post_select(SelectInfo &si)
56  { return cloned ? cloned->post_select(si) : false; }
57 
58  virtual size_t read(void *buf, size_t count)
59  { return 0; }
60  virtual size_t write(const void *buf, size_t count)
61  { return 0; }
62  virtual size_t read(WvBuf &outbuf, size_t count)
63  { return 0; }
64  virtual size_t write(WvBuf &inbuf, size_t count = INT_MAX)
65  { return 0; }
66  virtual void noread()
67  { }
68  virtual void nowrite()
69  { }
70  virtual void maybe_autoclose()
71  { }
72  virtual bool isreadable()
73  { return false; }
74  virtual bool iswritable()
75  { return false; }
76  virtual bool flush(time_t msec_timeout)
77  { return false; }
78  virtual bool should_flush()
79  { return false; }
80  virtual IWvStreamCallback setreadcallback(IWvStreamCallback _cb)
81  { return 0; }
82  virtual IWvStreamCallback setwritecallback(IWvStreamCallback _cb)
83  { return 0; }
84  virtual IWvStreamCallback setexceptcallback(IWvStreamCallback _cb)
85  { return 0; }
86  virtual IWvStreamCallback setclosecallback(IWvStreamCallback _cb)
87  { return 0; }
88  virtual const char *wsname() const
89  { return "Listener"; }
90  virtual void set_wsname(WvStringParm name)
91  { }
92  void set_wsname(WVSTRING_FORMAT_DECL)
93  { set_wsname(WvString(WVSTRING_FORMAT_CALL)); }
94  virtual const char *wstype() const
95  { return "Listener"; }
96  virtual WSID wsid() const
97  { return 0; }
98  virtual void outbuf_limit(size_t size)
99  { }
100  virtual WvString getattr(WvStringParm name) const;
101 };
102 
108 {
109 public:
110  WvNullListener() : WvListener(NULL)
111  {
112  // nothing to do
113  }
114 
115  virtual IWvStream *accept()
116  { return NULL; }
117 
118  virtual bool isok() const
119  { return false; }
120 
121  virtual const WvAddr *src() const;
122 };
123 
124 #endif // __WVLISTENER_H
WvListener
Definition: wvlistener.h:15
WvListener::setclosecallback
virtual IWvStreamCallback setclosecallback(IWvStreamCallback _cb)
Sets a callback to be invoked on close().
Definition: wvlistener.h:86
WvListener::isreadable
virtual bool isreadable()
Returns true if the stream is readable.
Definition: wvlistener.h:72
WvErrorBase::isok
virtual bool isok() const
By default, returns true if geterr() == 0.
Definition: wverror.h:39
IWvStream
Definition: iwvstream.h:24
WvListener::setexceptcallback
virtual IWvStreamCallback setexceptcallback(IWvStreamCallback _cb)
Sets a callback to be invoked when the stream is in exception state.
Definition: wvlistener.h:84
IWvListener
Definition: iwvlistener.h:16
WvListener::maybe_autoclose
virtual void maybe_autoclose()
Auto-close the stream if the time is right.
Definition: wvlistener.h:70
WvListener::onaccept
virtual IWvListenerCallback onaccept(IWvListenerCallback _cb)
Set a user-defined function to be called when a new connection is available.
Definition: wvlistener.cc:87
WvNullListener::accept
virtual IWvStream * accept()
Accept a connection from this stream.
Definition: wvlistener.h:115
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvListener::setreadcallback
virtual IWvStreamCallback setreadcallback(IWvStreamCallback _cb)
Sets a callback to be invoked when the stream is readable.
Definition: wvlistener.h:80
WvListener::flush
virtual bool flush(time_t msec_timeout)
flush the output buffer, if we can do it without delaying more than msec_timeout milliseconds at a ti...
Definition: wvlistener.h:76
WvAttrs
Definition: wvattrs.h:6
WvListener::setwritecallback
virtual IWvStreamCallback setwritecallback(IWvStreamCallback _cb)
Sets a callback to be invoked when the stream is writable.
Definition: wvlistener.h:82
WvAddr
Base class for different address types, each of which will have the ability to convert itself to/from...
Definition: wvaddr.h:118
WvBufBase< unsigned char >
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
Definition: wvbuf.h:22
WvNullListener
This is a listener that doesn't work.
Definition: wvlistener.h:107
WvListener::iswritable
virtual bool iswritable()
Returns true if the stream is writable (without using the outbuf).
Definition: wvlistener.h:74
IWvStream::isok
virtual bool isok() const =0
By default, returns true if geterr() == 0.
WvListener::should_flush
virtual bool should_flush()
Returns true if we want to flush the output buffer right now.
Definition: wvlistener.h:78
WvListener::noread
virtual void noread()
Shuts down the reading side of the stream.
Definition: wvlistener.h:66
WvListener::nowrite
virtual void nowrite()
Shuts down the writing side of the stream.
Definition: wvlistener.h:68
WvNullListener::isok
virtual bool isok() const
By default, returns true if geterr() == 0.
Definition: wvlistener.h:118
WvListener::addwrap
virtual void addwrap(IWvListenerWrapper _wrapper)
Add a wrapper function for this stream: something that accept() will call to possibly wrap the stream...
Definition: wvlistener.cc:54
WvListener::outbuf_limit
virtual void outbuf_limit(size_t size)
set the maximum size of outbuf, beyond which a call to write() will return 0.
Definition: wvlistener.h:98
WvListener::isok
virtual bool isok() const
By default, returns true if geterr() == 0.
Definition: wvlistener.h:38