WvStreams
iwvstream.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * The basic streaming I/O interface.
6  */
7 #ifndef __IWVSTREAM_H
8 #define __IWVSTREAM_H
9 
10 #include "wvbuf.h"
11 #include "wverror.h"
12 #include "wvtr1.h"
13 #include "wvxplc.h"
14 
15 
16 class WvAddr;
17 class WvStream;
18 
19 
20 /* The stream gets passed back as a parameter. */
21 typedef wv::function<void()> IWvStreamCallback;
22 typedef unsigned int WSID;
23 
24 class IWvStream : public WvErrorBase, public IObject
25 {
26 public:
27  static IWvStream *create(WvStringParm moniker, IObject *obj = NULL);
28 
34  struct SelectRequest {
35  bool readable, writable, isexception;
36 
37  SelectRequest() { }
38  SelectRequest(bool r, bool w, bool x = false)
39  { readable = r; writable = w; isexception = x; }
40 
41  SelectRequest &operator |= (const SelectRequest &r)
42  { readable |= r.readable; writable |= r.writable;
43  isexception |= r.isexception; return *this; }
44  };
45 
50  struct SelectInfo {
51  fd_set read, write, except; // set by pre_select, read by post_select
52  SelectRequest wants; // what is the user looking for?
53  int max_fd; // largest fd in read, write, or except
54  time_t msec_timeout; // max time to wait, or -1 for forever
55  bool inherit_request; // 'wants' values passed to child streams
56  bool global_sure; // should we run the globalstream callback
57  };
58 
59  IWvStream();
60  virtual ~IWvStream();
61  virtual void close() = 0;
62  virtual bool isok() const = 0;
63  virtual void callback() = 0;
64 
65  // FIXME: these really have no place in the interface...
66  virtual int getrfd() const = 0;
67  virtual int getwfd() const = 0;
68 
69  // FIXME: evil, should go away (or be changed to localaddr/remoteaddr)
70  virtual const WvAddr *src() const = 0;
71 
72  // needed for select().
73  // Some say that pre_select() should go away.
74  virtual void pre_select(SelectInfo &si) = 0;
75  virtual bool post_select(SelectInfo &si) = 0;
76 
77  // these are now the official way to get/put data to your stream.
78  // The old uread() and uwrite() are just implementation details!
79  virtual size_t read(void *buf, size_t count) = 0;
80  virtual size_t write(const void *buf, size_t count) = 0;
81 
82  // FIXME: these are the new fancy versions, but WvBuf needs to have
83  // a safely abstracted interface class (IWvBuf) before IWvStream will
84  // really be safe, if we include these.
85  virtual size_t read(WvBuf &outbuf, size_t count) = 0;
86  virtual size_t write(WvBuf &inbuf, size_t count = INT_MAX) = 0;
87 
100  virtual void noread() = 0;
101 
110  virtual void nowrite() = 0;
111 
116  virtual void maybe_autoclose() = 0;
117 
119  virtual bool isreadable() = 0;
120 
122  virtual bool iswritable() = 0;
123 
133  virtual bool flush(time_t msec_timeout) = 0;
134 
141  virtual bool should_flush() = 0;
142 
143  /*
144  * WARNING: these don't work as expected!
145  */
147  virtual IWvStreamCallback setreadcallback(IWvStreamCallback _callfunc) = 0;
148 
150  virtual IWvStreamCallback setwritecallback(IWvStreamCallback _callfunc) = 0;
151 
154  virtual IWvStreamCallback setexceptcallback(IWvStreamCallback _callfunc) = 0;
155  /*
156  * END WARNING
157  */
158 
160  virtual IWvStreamCallback setclosecallback(IWvStreamCallback _callfunc) = 0;
161 
162  /* Stream identification/debugging */
163  virtual const char *wsname() const = 0;
164  virtual void set_wsname(WvStringParm name) = 0;
165  virtual const char *wstype() const = 0; // This is not static due to, eg, WvStreamClone
166  virtual WSID wsid() const = 0;
167 
173  virtual void outbuf_limit(size_t size) = 0;
174  virtual WvString getattr(WvStringParm name) const = 0;
175 };
176 
177 DEFINE_IID(IWvStream, {0x7ca76e98, 0xb653, 0x43d7,
178  {0xb0, 0x56, 0x8b, 0x9d, 0xde, 0x9a, 0xbe, 0x9d}});
179 
180 
181 #endif /* __IWVSTREAM_H */
IWvStream::isreadable
virtual bool isreadable()=0
Returns true if the stream is readable.
IWvStream::nowrite
virtual void nowrite()=0
Shuts down the writing side of the stream.
IWvStream::iswritable
virtual bool iswritable()=0
Returns true if the stream is writable (without using the outbuf).
IWvStream::outbuf_limit
virtual void outbuf_limit(size_t size)=0
set the maximum size of outbuf, beyond which a call to write() will return 0.
IWvStream::setreadcallback
virtual IWvStreamCallback setreadcallback(IWvStreamCallback _callfunc)=0
Sets a callback to be invoked when the stream is readable.
IWvStream
Definition: iwvstream.h:24
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvErrorBase
A class for managing error numbers and strings.
Definition: wverror.h:23
IWvStream::SelectInfo
the data structure used by pre_select()/post_select() and internally by select().
Definition: iwvstream.h:50
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
IWvStream::maybe_autoclose
virtual void maybe_autoclose()=0
Auto-close the stream if the time is right.
IObject
Definition: IObject.h:65
IWvStream::isok
virtual bool isok() const =0
By default, returns true if geterr() == 0.
WvStream
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
Definition: wvstream.h:24
IWvStream::setexceptcallback
virtual IWvStreamCallback setexceptcallback(IWvStreamCallback _callfunc)=0
Sets a callback to be invoked when the stream is in exception state.
IWvStream::SelectRequest
A SelectRequest is a convenient way to remember what we want to do to a particular stream: read from ...
Definition: iwvstream.h:34
IWvStream::should_flush
virtual bool should_flush()=0
Returns true if we want to flush the output buffer right now.
IWvStream::setclosecallback
virtual IWvStreamCallback setclosecallback(IWvStreamCallback _callfunc)=0
Sets a callback to be invoked on close().
IWvStream::setwritecallback
virtual IWvStreamCallback setwritecallback(IWvStreamCallback _callfunc)=0
Sets a callback to be invoked when the stream is writable.
IWvStream::flush
virtual bool flush(time_t msec_timeout)=0
flush the output buffer, if we can do it without delaying more than msec_timeout milliseconds at a ti...
IWvStream::noread
virtual void noread()=0
Shuts down the reading side of the stream.
DEFINE_IID
#define DEFINE_IID(iface, u1, u2, u3, u4, u5, u6, u7, u8, u9, u10, u11)
Used to define the IID of an interface.
Definition: uuid.h:134