11#include "wvtimeutils.h"
12#include "wvstreamsdebugger.h"
76 void seterr(WVSTRING_FORMAT_DECL)
83 virtual size_t read(
void *buf,
size_t count);
94 virtual size_t read(WvBuf &outbuf,
size_t count);
101 virtual void unread(WvBuf &outbuf,
size_t count);
109 virtual size_t write(
const void *buf,
size_t count);
118 virtual size_t write(WvBuf &inbuf,
size_t count = INT_MAX);
130 { max_outbuf_size = size; }
146 virtual size_t uread(
void *buf,
size_t count)
156 virtual size_t uwrite(
const void *buf,
size_t count)
176 char separator =
'\n',
int readahead = 1024)
183 char separator =
'\n',
int readahead = 1024)
185 return getline(time_t(wait_msec), separator, readahead);
190 char separator =
'\n',
int readahead = 1024)
192 return getline(time_t(wait_msec), separator, readahead);
200 char *
getline(
char,
int i = 0);
201 char *
getline(
bool,
int i = 0);
216 int readahead = 1024);
223 int readahead = 1024);
233 { queue_min = count; }
248 outbuf_delayed_flush = is_delayed;
249 want_to_flush = !is_delayed;
259 { is_auto_flush = is_automatic; }
267 virtual bool flush(time_t msec_timeout);
377 {
return _select(msec_timeout,
false,
false,
false,
true); }
416 bool readable,
bool writable,
bool isex =
false)
417 {
return _select(msec_timeout, readable, writable, isex,
false); }
434 void force_select(
bool readable,
bool writable,
bool isexception =
false);
441 bool isexception =
false);
548 size_t operator() (WVSTRING_FORMAT_DECL)
551 const char *wsname()
const
552 {
return my_wsname; }
554 { my_wsname = wsname; }
555 void set_wsname(WVSTRING_FORMAT_DECL)
556 { set_wsname(
WvString(WVSTRING_FORMAT_CALL)); }
558 const char *wstype()
const {
return "WvStream"; }
560 WSID wsid()
const {
return my_wsid; }
561 static IWvStream *find_by_wsid(WSID wsid);
564 {
return attrs.get(name); }
569#ifdef __WVSTREAM_UNIT_TEST
572 {
return outbuf.
used(); }
574 {
return inbuf.
used(); }
581 { attrs.set(name, value); }
587 void _build_selectinfo(SelectInfo &si, time_t msec_timeout,
588 bool readable,
bool writable,
bool isexcept,
594 int _do_select(SelectInfo &si);
598 bool _process_selectinfo(SelectInfo &si,
bool forceable);
604 bool flush_outbuf(time_t msec_timeout);
608 virtual bool flush_internal(time_t msec_timeout);
613 virtual int getrfd()
const;
614 virtual int getwfd()
const;
618 friend class WvHTTPClientProxyStream;
622 IWvStreamCallback callfunc;
623 wv::function<
void*(
void*)> call_ctx;
625 IWvStreamCallback readcb, writecb, exceptcb, closecb;
627 size_t max_outbuf_size;
628 bool outbuf_delayed_flush;
638 time_t autoclose_time;
658 static void debugger_streams_display_header(
WvStringParm cmd,
659 WvStreamsDebugger::ResultCallback result_cb);
660 static void debugger_streams_display_one_stream(
WvStream *s,
662 WvStreamsDebugger::ResultCallback result_cb);
663 static void debugger_streams_maybe_display_one_stream(
WvStream *s,
666 WvStreamsDebugger::ResultCallback result_cb);
670 bool _select(time_t msec_timeout,
671 bool readable,
bool writable,
bool isexcept,
674 void legacy_callback();
679 static void add_debugger_commands();
683 WvStreamsDebugger::ResultCallback result_cb,
void *);
686 WvStreamsDebugger::ResultCallback result_cb,
void *);
Base class for different address types, each of which will have the ability to convert itself to/from...
size_t used() const
Returns the number of elements in the buffer currently available for reading.
virtual void seterr(int _errnum)
Set the errnum variable – we have an error.
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
const char * cstr() const
return a (const char *) for this string.
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
char * getline(double wait_msec, char separator='\n', int readahead=1024)
Auto-convert double to time_t.
bool xpost_select(SelectInfo &si, const SelectRequest &r)
Like post_select(), but still exists even if you override the other post_select() in a subclass.
virtual size_t read(WvBuf &outbuf, size_t count)
Reads up to 'count' bytes of data from the stream into the buffer.
size_t print(WVSTRING_FORMAT_DECL)
preformat and write() a string.
virtual const WvAddr * src() const
get the remote address from which the last data block was received.
void flush_then_close(int msec_timeout)
flush the output buffer automatically as select() is called.
virtual void execute()
The callback() function calls execute(), and then calls the user- specified callback if one is define...
IWvStreamCallback setclosecallback(IWvStreamCallback _callback)
Sets a callback to be invoked on close().
void queuemin(size_t count)
force read() to not return any bytes unless 'count' bytes can be read at once.
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...
IWvStream::SelectRequest get_select_request()
Use get_select_request() to save the current state of the selection state of this stream.
bool post_select(SelectInfo &si, const SelectRequest &r)
A more convenient version of post_select() usable for overriding the 'want' value temporarily.
virtual size_t write(const void *buf, size_t count)
Write data to the stream.
virtual void close()
Close the stream if it is open; isok() becomes false from now on.
virtual bool post_select(SelectInfo &si)
post_select() is called after select(), and returns true if this object is now ready.
virtual bool should_flush()
Returns true if we want to flush the output buffer right now.
virtual void seterr(int _errnum)
Override seterr() from WvError so that it auto-closes the stream.
virtual bool post_select(SelectInfo &si)
post_select() is called after select(), and returns true if this object is now ready.
void delay_output(bool is_delayed)
force write() to always buffer output.
size_t write(WvStringParm s)
print a preformatted WvString to the stream.
char * blocking_getline(time_t wait_msec, int separator='\n', int readahead=1024)
This is a version of getline() that allows you to block for more data to arrive.
virtual bool isok() const
return true if the stream is actually usable right now
virtual void callback()
if the stream has a callback function defined, call it now.
void auto_flush(bool is_automatic)
if true, force write() to call flush() each time, the default behavour.
char * continue_getline(time_t wait_msec, int separator='\n', int readahead=1024)
This is a version of blocking_getline() that uses continue_select to avoid blocking other streams.
void undo_force_select(bool readable, bool writable, bool isexception=false)
Undo a previous force_select() - ie.
void autoforward(WvStream &s)
set the callback function for this stream to an internal routine that auto-forwards all incoming stre...
virtual size_t uread(void *buf, size_t count)
unbuffered I/O functions; these ignore the buffer, which is handled by read().
void alarm(time_t msec_timeout)
set an alarm, ie.
void setcallback(IWvStreamCallback _callfunc)
define the callback function for this stream, called whenever the callback() member is run,...
void force_select(bool readable, bool writable, bool isexception=false)
Use force_select() to force one or more particular modes (readable, writable, or isexception) to true...
WvStream()
Basic constructor for just a do-nothing WvStream.
virtual size_t uwrite(const void *buf, size_t count)
unbuffered I/O functions; these ignore the buffer, which is handled by write().
char * getline(int wait_msec, char separator='\n', int readahead=1024)
Auto-convert int to time_t.
virtual void noread()
Shuts down the reading side of the stream.
bool uses_continue_select
If this is set, enables the use of continue_select().
bool stop_read
True if noread()/nowrite()/close() have been called, respectively.
virtual size_t write(const void *buf, size_t count)
Write data to the stream.
void * _callwrap(void *)
A wrapper that's compatible with WvCont, but calls the "real" callback.
virtual void pre_select(SelectInfo &si)
pre_select() sets up for eventually calling select().
bool select(time_t msec_timeout)
Return true if any of the requested features are true on the stream.
void runonce(time_t msec_timeout=-1)
Exactly the same as: if (select(timeout)) callback();.
WvStream * read_requires_writable
If this is set, select() doesn't return true for read unless the given stream also returns true for w...
char * getline(time_t wait_msec=0, char separator='\n', int readahead=1024)
Read up to one line of data from the stream and return a pointer to the internal buffer containing th...
virtual bool iswritable()
Returns true if the stream is writable (without using the outbuf).
void pre_select(SelectInfo &si, const SelectRequest &r)
A more convenient version of pre_select() usable for overriding the 'want' value temporarily.
void xpre_select(SelectInfo &si, const SelectRequest &r)
Like pre_select(), but still exists even if you override the other pre_select() in a subclass.
IWvStreamCallback setexceptcallback(IWvStreamCallback _callback)
Sets a callback to be invoked when the stream is in exception state.
void terminate_continue_select()
you MUST run this from your destructor if you use continue_select(), or very weird things will happen...
void noautoforward()
Stops autoforwarding.
void _callback()
Actually call the registered callfunc and execute().
time_t alarm_remaining()
return the number of milliseconds remaining before the alarm will go off; -1 means no alarm is set (i...
bool select(time_t msec_timeout, bool readable, bool writable, bool isex=false)
This version of select() sets forceable==false, so we use the exact readable/writable/isexception opt...
IWvStreamCallback setreadcallback(IWvStreamCallback _callback)
Sets a callback to be invoked when the stream is readable.
size_t personal_stack_size
Specifies the stack size to reserve for continue_select().
void drain()
drain the input buffer (read and discard data until select(0) returns false)
virtual void pre_select(SelectInfo &si)
pre_select() sets up for eventually calling select().
virtual void nowrite()
Shuts down the writing side of the stream.
virtual size_t read(void *buf, size_t count)
read a data block on the stream.
bool continue_select(time_t msec_timeout)
return to the caller from execute(), but don't really return exactly; this uses WvCont::yield() to re...
virtual size_t write(WvBuf &inbuf, size_t count=INT_MAX)
Writes data to the stream from the given buffer.
virtual void seterr(int _errnum)
Override seterr() from WvError so that it auto-closes the stream.
IWvStreamCallback setwritecallback(IWvStreamCallback _callback)
Sets a callback to be invoked when the stream is writable.
void outbuf_limit(size_t size)
set the maximum size of outbuf, beyond which a call to write() will return 0.
virtual bool isreadable()
Returns true if the stream is readable.
virtual void maybe_autoclose()
Auto-close the stream if the time is right.
virtual void unread(WvBuf &outbuf, size_t count)
Puts data back into the stream's internal buffer.
WvStream * write_requires_readable
If this is set, select() doesn't return true for write unless the given stream also returns true for ...
virtual void callback()
if the stream has a callback function defined, call it now.
bool alarm_was_ticking
This will be true during callback execution if the callback was triggered by the alarm going off.
This is a WvList of WvStrings, and is a really handy way to parse strings.
WvString is an implementation of a simple and efficient printable-string class.
Based on (and interchangeable with) struct timeval.
#define IMPLEMENT_IOBJECT(component)
Helper macro to implement the IObject methods automatically.
the data structure used by pre_select()/post_select() and internally by select().
A SelectRequest is a convenient way to remember what we want to do to a particular stream: read from ...